|
|
|
@ -1,48 +1,53 @@ |
|
|
|
import Backbone from 'backbone'; |
|
|
|
const $ = Backbone.$; |
|
|
|
|
|
|
|
var getBoundingRect = (el, win) => { |
|
|
|
var w = win || window; |
|
|
|
var rect = el.getBoundingClientRect(); |
|
|
|
return { |
|
|
|
left: rect.left + w.pageXOffset, |
|
|
|
top: rect.top + w.pageYOffset, |
|
|
|
width: rect.width, |
|
|
|
height: rect.height |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
// TODO move to opts
|
|
|
|
setKey(keys, command) { |
|
|
|
//key(keys, command);
|
|
|
|
}, |
|
|
|
import { bindAll, isFunction } from 'underscore'; |
|
|
|
import { on, off } from 'utils/mixins'; |
|
|
|
|
|
|
|
const options = { |
|
|
|
/** |
|
|
|
* Return element position |
|
|
|
* @param {HTMLElement} el |
|
|
|
* @return {Object} |
|
|
|
* Callback on start |
|
|
|
* onStart(ev, dragger) { |
|
|
|
* console.log('pointer start', dragger.startPointer, 'position start', dragger.startPosition); |
|
|
|
* }, |
|
|
|
*/ |
|
|
|
getElementRect(el) { |
|
|
|
var posFetcher = this.opts.posFetcher || ''; |
|
|
|
return posFetcher |
|
|
|
? posFetcher(el, { |
|
|
|
avoidFrameOffset: 1 |
|
|
|
}) |
|
|
|
: getBoundingRect(el); |
|
|
|
}, |
|
|
|
onStart: null, |
|
|
|
/** |
|
|
|
* Callback on drag |
|
|
|
* onDrag(ev, dragger) { |
|
|
|
* console.log('pointer', dragger.currentPointer, 'position', dragger.position, 'delta', dragger.delta); |
|
|
|
* }, |
|
|
|
*/ |
|
|
|
onDrag: null, |
|
|
|
/** |
|
|
|
* Callback on drag |
|
|
|
* onEnd(ev, dragger) { |
|
|
|
* console.log('pointer', dragger.currentPointer, 'position', dragger.position, 'delta', dragger.delta); |
|
|
|
* }, |
|
|
|
*/ |
|
|
|
onEnd: null, |
|
|
|
/** |
|
|
|
* Indicate a callback where to pass an object with new coordinates |
|
|
|
*/ |
|
|
|
setPosition: null, |
|
|
|
/** |
|
|
|
* Indicate a callback where to get initial coordinates. |
|
|
|
* getPosition: () => { |
|
|
|
* ... |
|
|
|
* return { x: 10, y: 100 } |
|
|
|
* } |
|
|
|
*/ |
|
|
|
getPosition: null |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
/** |
|
|
|
* Init the resizer |
|
|
|
* @param {Object} opts |
|
|
|
*/ |
|
|
|
init(opts) { |
|
|
|
bindAll(this, 'drag', 'stop'); |
|
|
|
this.opts = options; |
|
|
|
this.setOptions(opts); |
|
|
|
this.handleMouseDown = this.handleMouseDown.bind(this); |
|
|
|
this.drag = this.drag.bind(this); |
|
|
|
this.move = this.move.bind(this); |
|
|
|
this.stop = this.stop.bind(this); |
|
|
|
this.setKey('up, right, down, left', this.handleKey); |
|
|
|
this.delta = { x: 0, y: 0 }; |
|
|
|
this.startPosition = this.getStartPosition(); |
|
|
|
return this; |
|
|
|
}, |
|
|
|
|
|
|
|
@ -50,135 +55,105 @@ module.exports = { |
|
|
|
* Update options |
|
|
|
* @param {Object} options |
|
|
|
*/ |
|
|
|
setOptions(opts) { |
|
|
|
this.opts = opts || {}; |
|
|
|
setOptions(opts = {}) { |
|
|
|
this.opts = { |
|
|
|
...this.opts, |
|
|
|
...opts |
|
|
|
}; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Focus dragger on the element |
|
|
|
* @param {HTMLElement} el |
|
|
|
*/ |
|
|
|
focus(el) { |
|
|
|
// Avoid focusing on already focused element
|
|
|
|
if (el && el === this.el) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.getDocumentEl(el); |
|
|
|
this.blur(); |
|
|
|
this.el = el; |
|
|
|
this.handlers = this.opts.dragHandlers || [el]; |
|
|
|
|
|
|
|
var elRect = this.getElementRect(el); //<-- TODO have wrong top:left
|
|
|
|
this.elRect = elRect; |
|
|
|
this.startTop = elRect.top; |
|
|
|
this.startLeft = elRect.left; |
|
|
|
|
|
|
|
// TODO init snapper
|
|
|
|
|
|
|
|
this.getDocumentEl().on('mousedown', this.handleMouseDown); |
|
|
|
toggleDrag(enable) { |
|
|
|
const docs = this.getDocumentEl(); |
|
|
|
const method = enable ? 'on' : 'off'; |
|
|
|
const methods = { on, off }; |
|
|
|
methods[method](docs, 'mousemove', this.drag); |
|
|
|
methods[method](docs, 'mouseup', this.stop); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Blur from the focused element |
|
|
|
* Start dragging |
|
|
|
* @param {Event} e |
|
|
|
*/ |
|
|
|
blur() { |
|
|
|
this.getDocumentEl().off('mousedown', this.handleMouseDown); |
|
|
|
this.el = null; |
|
|
|
start(ev) { |
|
|
|
const { onStart } = this.opts; |
|
|
|
this.toggleDrag(1); |
|
|
|
this.startPointer = this.getPointerPos(ev); |
|
|
|
this.startPosition = this.getStartPosition(); |
|
|
|
isFunction(onStart) && onStart(ev, this); |
|
|
|
this.drag(ev); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Start dragging |
|
|
|
* @param {Event} e |
|
|
|
* Drag event |
|
|
|
* @param {Event} event |
|
|
|
*/ |
|
|
|
start(e) { |
|
|
|
this.startPos = this.getMousePos(e); |
|
|
|
var docs = this.getDocumentEl(); |
|
|
|
docs.on('mousemove', this.drag); |
|
|
|
docs.on('mouseup', this.stop); |
|
|
|
drag(ev) { |
|
|
|
const { startPointer } = this; |
|
|
|
const currentPos = this.getPointerPos(ev); |
|
|
|
const delta = { |
|
|
|
x: currentPos.x - startPointer.x, |
|
|
|
y: currentPos.y - startPointer.y |
|
|
|
}; |
|
|
|
let { lockedAxis } = this; |
|
|
|
|
|
|
|
// Start callback
|
|
|
|
var onStart = this.opts.onStart; |
|
|
|
if (typeof onStart === 'function') { |
|
|
|
onStart(e, { |
|
|
|
docs, |
|
|
|
el: this.el, |
|
|
|
start: this.startPos, |
|
|
|
elRect: this.elRect |
|
|
|
}); |
|
|
|
// Lock one axis
|
|
|
|
if (ev.shiftKey) { |
|
|
|
lockedAxis = !lockedAxis && this.detectAxisLock(delta.x, delta.y); |
|
|
|
} else { |
|
|
|
lockedAxis = null; |
|
|
|
} |
|
|
|
|
|
|
|
this.drag(e); |
|
|
|
}, |
|
|
|
if (lockedAxis === 'x') { |
|
|
|
delta.x = startPointer.x; |
|
|
|
} else if (lockedAxis === 'y') { |
|
|
|
delta.y = startPointer.y; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Stop dragging |
|
|
|
*/ |
|
|
|
stop(e) { |
|
|
|
var docs = this.getDocumentEl(); |
|
|
|
docs.off('mousemove', this.drag); |
|
|
|
docs.off('mouseup', this.stop); |
|
|
|
this.lockedAxis = null; |
|
|
|
this.lockedAxis = lockedAxis; |
|
|
|
this.delta = delta; |
|
|
|
this.move(delta.x, delta.y); |
|
|
|
this.currentPointer = currentPos; |
|
|
|
const { onDrag } = this.opts; |
|
|
|
isFunction(onDrag) && onDrag(ev, this); |
|
|
|
|
|
|
|
// Stop callback
|
|
|
|
var onEnd = this.opts.onEnd; |
|
|
|
if (typeof onEnd === 'function') { |
|
|
|
onEnd(e, { |
|
|
|
docs, |
|
|
|
delta: this.delta, |
|
|
|
end: { |
|
|
|
x: this.startLeft + this.delta.x, |
|
|
|
y: this.startTop + this.delta.y |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
// In case the mouse button was released outside of the window
|
|
|
|
ev.which === 0 && this.stop(ev); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Handle mousedown to check if it's possible to drag |
|
|
|
* @param {Event} e |
|
|
|
* Stop dragging |
|
|
|
*/ |
|
|
|
handleMouseDown(e) { |
|
|
|
var el = e.target; |
|
|
|
if (this.isHandler(el)) { |
|
|
|
this.start(e); |
|
|
|
} |
|
|
|
stop(ev) { |
|
|
|
const { delta } = this; |
|
|
|
this.toggleDrag(); |
|
|
|
this.lockedAxis = null; |
|
|
|
this.move(delta.x, delta.y, 1); |
|
|
|
const { onEnd } = this.opts; |
|
|
|
isFunction(onEnd) && onEnd(ev, this); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Detects if the clicked element is a valid handler |
|
|
|
* @param {HTMLElement} el |
|
|
|
* @return {Boolean} |
|
|
|
* Move the element |
|
|
|
* @param {integer} x |
|
|
|
* @param {integer} y |
|
|
|
*/ |
|
|
|
isHandler(el) { |
|
|
|
var handlers = this.handlers; |
|
|
|
|
|
|
|
for (var n in handlers) { |
|
|
|
if (handlers[n] === el) return true; |
|
|
|
} |
|
|
|
move: function(x, y, end) { |
|
|
|
const { el, opts } = this; |
|
|
|
const pos = this.startPosition; |
|
|
|
const { setPosition } = opts; |
|
|
|
const xPos = pos.x + x; |
|
|
|
const yPos = pos.y + y; |
|
|
|
this.position = { |
|
|
|
x: xPos, |
|
|
|
y: yPos, |
|
|
|
end |
|
|
|
}; |
|
|
|
|
|
|
|
return false; |
|
|
|
}, |
|
|
|
isFunction(setPosition) && setPosition(this.position); |
|
|
|
|
|
|
|
/** |
|
|
|
* Handle key press |
|
|
|
* @param {Event} e |
|
|
|
* @param {Object} handler |
|
|
|
*/ |
|
|
|
handleKey(e, handler) { |
|
|
|
switch (handler.shortcut) { |
|
|
|
case 'up': |
|
|
|
this.move(0, -1); |
|
|
|
break; |
|
|
|
case 'right': |
|
|
|
this.move(1, 0); |
|
|
|
break; |
|
|
|
case 'down': |
|
|
|
this.move(0, 1); |
|
|
|
break; |
|
|
|
case 'left': |
|
|
|
this.move(-1, 0); |
|
|
|
break; |
|
|
|
if (el) { |
|
|
|
el.style.left = `${xPos}px`; |
|
|
|
el.style.top = `${yPos}px`; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
@ -186,15 +161,15 @@ module.exports = { |
|
|
|
* Returns documents |
|
|
|
*/ |
|
|
|
getDocumentEl(el) { |
|
|
|
var el = el || this.el; |
|
|
|
if (!this.$doc) { |
|
|
|
var docs = [document]; |
|
|
|
if (el) { |
|
|
|
docs.push(el.ownerDocument); |
|
|
|
} |
|
|
|
this.$doc = $(docs); |
|
|
|
el = el || this.el; |
|
|
|
|
|
|
|
if (!this.docs) { |
|
|
|
const docs = [document]; |
|
|
|
el && docs.push(el.ownerDocument); |
|
|
|
this.docs = docs; |
|
|
|
} |
|
|
|
return this.$doc; |
|
|
|
|
|
|
|
return this.docs; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
@ -202,126 +177,44 @@ module.exports = { |
|
|
|
* @param {Event} event |
|
|
|
* @return {Object} |
|
|
|
*/ |
|
|
|
getMousePos(e) { |
|
|
|
var mouseFetch = this.opts.mousePosFetcher; |
|
|
|
return mouseFetch |
|
|
|
? mouseFetch(e) |
|
|
|
getPointerPos(ev) { |
|
|
|
const getPos = this.opts.getPointerPosition; |
|
|
|
return getPos |
|
|
|
? getPos(ev) |
|
|
|
: { |
|
|
|
x: e.clientX, |
|
|
|
y: e.clientY |
|
|
|
x: ev.clientX, |
|
|
|
y: ev.clientY |
|
|
|
}; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Drag event |
|
|
|
* @param {Event} event |
|
|
|
*/ |
|
|
|
drag(e) { |
|
|
|
var lockedAxis = this.lockedAxis; |
|
|
|
var currentPos = this.getMousePos(e); |
|
|
|
var delta = { |
|
|
|
x: currentPos.x - this.startPos.x, |
|
|
|
y: currentPos.y - this.startPos.y |
|
|
|
}; |
|
|
|
// Lock one axis
|
|
|
|
if (e.shiftKey) { |
|
|
|
if (!lockedAxis) { |
|
|
|
var relX = delta.x; |
|
|
|
var relY = delta.y; |
|
|
|
var absX = Math.abs(relX); |
|
|
|
var absY = Math.abs(relY); |
|
|
|
|
|
|
|
// Vertical or Horizontal lock
|
|
|
|
if (relY >= absX || relY <= -absX) { |
|
|
|
lockedAxis = 'x'; |
|
|
|
} else if (relX > absY || relX < -absY) { |
|
|
|
lockedAxis = 'y'; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
lockedAxis = null; |
|
|
|
} |
|
|
|
|
|
|
|
if (lockedAxis === 'x') { |
|
|
|
delta.x = this.startPos.x; |
|
|
|
} |
|
|
|
getStartPosition() { |
|
|
|
const { el, opts } = this; |
|
|
|
const getPos = opts.getPosition; |
|
|
|
let result = { x: 0, y: 0 }; |
|
|
|
|
|
|
|
if (lockedAxis === 'y') { |
|
|
|
delta.y = this.startPos.y; |
|
|
|
if (isFunction(getPos)) { |
|
|
|
result = getPos(); |
|
|
|
} else if (el) { |
|
|
|
result = { |
|
|
|
x: parseFloat(el.style.left), |
|
|
|
y: parseFloat(el.style.top) |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
this.lockedAxis = lockedAxis; |
|
|
|
this.delta = delta; |
|
|
|
this.move(delta.x, delta.y); |
|
|
|
|
|
|
|
// Drag callback
|
|
|
|
const onDrag = this.opts.onDrag; |
|
|
|
if (typeof onDrag === 'function') { |
|
|
|
onDrag(e, { |
|
|
|
delta, |
|
|
|
current: { |
|
|
|
x: this.startLeft + delta.x, |
|
|
|
y: this.startTop + delta.y |
|
|
|
}, |
|
|
|
lockedAxis |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// In case the mouse button was released outside of the window
|
|
|
|
if (e.which === 0) { |
|
|
|
this.stop(e); |
|
|
|
} |
|
|
|
return result; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Move the element |
|
|
|
* @param {integer} x |
|
|
|
* @param {integer} y |
|
|
|
*/ |
|
|
|
move: function(x, y) { |
|
|
|
this.moveX(x); |
|
|
|
this.moveY(y); |
|
|
|
}, |
|
|
|
detectAxisLock(x, y) { |
|
|
|
const relX = x; |
|
|
|
const relY = y; |
|
|
|
const absX = Math.abs(relX); |
|
|
|
const absY = Math.abs(relY); |
|
|
|
|
|
|
|
/** |
|
|
|
* Move in x direction |
|
|
|
* @param {integer} x |
|
|
|
*/ |
|
|
|
moveX(x) { |
|
|
|
var el = this.el; |
|
|
|
var opts = this.opts; |
|
|
|
var xPos = this.startLeft + x; |
|
|
|
const setX = this.opts.setX; |
|
|
|
|
|
|
|
if (typeof setX === 'function') { |
|
|
|
setX(xPos, { |
|
|
|
el, |
|
|
|
start: this.startLeft, |
|
|
|
delta: x |
|
|
|
}); |
|
|
|
} else { |
|
|
|
el.style.left = xPos + 'px'; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Move in y direction |
|
|
|
* @param {integer} y |
|
|
|
*/ |
|
|
|
moveY(y) { |
|
|
|
var el = this.el; |
|
|
|
var opts = this.opts; |
|
|
|
var yPos = this.startTop + y; |
|
|
|
const setY = this.opts.setY; |
|
|
|
|
|
|
|
if (typeof setY === 'function') { |
|
|
|
setY(yPos, { |
|
|
|
el, |
|
|
|
start: this.startTop, |
|
|
|
delta: y |
|
|
|
}); |
|
|
|
} else { |
|
|
|
el.style.top = yPos + 'px'; |
|
|
|
// Vertical or Horizontal lock
|
|
|
|
if (relY >= absX || relY <= -absX) { |
|
|
|
return 'x'; |
|
|
|
} else if (relX > absY || relX < -absY) { |
|
|
|
return 'y'; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|