forked from komitywa/plantingjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a part of komitywa#208. Previously we were using jquery-ui draggable in order to move objects. In future we should refactor this mixin to use css transform property instead of top/left. This is why: https://csstriggers.com/left vs https://csstriggers.com/transform
- Loading branch information
Showing
2 changed files
with
49 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export const MOVE_END = 'moveend'; | ||
export const MOVE_START = 'movestart'; | ||
const PREVENT_SELECT_CLASS = 'noselect'; | ||
|
||
function moveableMixin(viewInstance) { | ||
let isDragging = false; | ||
const offset = {}; | ||
const position = { x: 0, y: 0 }; | ||
const element = viewInstance.el; | ||
|
||
element.classList.add(PREVENT_SELECT_CLASS); | ||
|
||
viewInstance.onMousedown = ({ clientX, clientY }) => { | ||
offset.x = clientX - element.offsetLeft; | ||
offset.y = clientY - element.offsetTop; | ||
isDragging = true; | ||
viewInstance.trigger(MOVE_START); | ||
}; | ||
|
||
viewInstance.onMouseup = () => { | ||
isDragging = false; | ||
viewInstance.trigger(MOVE_END, position); | ||
}; | ||
|
||
viewInstance.onMousemove = ({ clientX, clientY }) => { | ||
if (isDragging) { | ||
position.x = clientX - offset.x; | ||
position.y = clientY - offset.y; | ||
element.style.top = `${position.y}px`; | ||
element.style.left = `${position.x}px`; | ||
} | ||
}; | ||
|
||
viewInstance.events = { | ||
...viewInstance.events, | ||
mousedown: 'onMousedown', | ||
mouseup: 'onMouseup', | ||
mousemove: 'onMousemove', | ||
}; | ||
viewInstance.delegateEvents(); | ||
} | ||
|
||
export default moveableMixin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters