define('application/widgets/pointerdrag', [
'rofl/lib/utils'
], function (
Utils
) {
'use strict';
return {
/**
* Initialises the decorator.
*/
initialize: function () {
this._detectElement = this;
this._onMouseDownBound = Utils.bind(this._onMouseDown, this);
this._onMouseUpBound = Utils.bind(this._onMouseUp, this);
this._onMouseMoveBound = Utils.bind(this._onMouseMove, this);
},
/**
* Sets the pointer listeners.
*
* @param {Object} [widget] - The widget to set the listener to. Optional.
*/
setPointerListeners: function (widget) {
var outputElement;
if (widget) {
this._detectElement = widget;
}
outputElement = this._detectElement.outputElement;
outputElement.addEventListener('mousedown', this._onMouseDownBound);
},
/**
* Gets executed when the mouse gets pressed.
*
* @param {Object} e - The mouse event.
* @private
*/
_onMouseDown: function (e) {
e.preventDefault();
if (Utils.isFunction(this._isMouseDownEnabled) && !this._isMouseDownEnabled()) {
return;
}
this._elementStyle = this._detectElement.outputElement.getBoundingClientRect();
if (!this._mouseDown) {
this._mouseDown = true;
this._setMouseListener();
}
},
/**
* Gets executed when the mouse goes up.
*
* @param {Object} e - The mouse event data.
* @private
*/
_onMouseUp: function (e) {
var elementStyle = this._elementStyle;
if (this._mouseDown) {
this._mouseDown = false;
this._removeMouseListener();
}
if (Utils.isFunction(this._onMouseFinished)) {
this._onMouseFinished({
mouseClientX: e.clientX,
mouseClientY: e.clientY,
elementLeft: elementStyle.left,
elementRight: elementStyle.right
});
}
},
/**
* Gets executed when the mouse moves.
*
* @param {Object} e - The mouse event.
* @private
*/
_onMouseMove: function (e) {
var elementStyle = this._elementStyle;
e.preventDefault();
if (Utils.isFunction(this._onMousePosition)) {
this._onMousePosition({
mouseClientX: e.clientX,
mouseClientY: e.clientY,
elementLeft: elementStyle.left,
elementRight: elementStyle.right
});
}
},
/**
* Sets the mouse event listeners.
*
* @private
*/
_setMouseListener: function () {
document.addEventListener('mousemove', this._onMouseMoveBound);
document.addEventListener('mouseup', this._onMouseUpBound);
},
/**
* Removes the mouse event listeners.
*
* @private
*/
_removeMouseListener: function () {
document.removeEventListener('mousemove', this._onMouseMoveBound);
document.removeEventListener('mouseup', this._onMouseUpBound);
},
/**
* Returns true if the mouse is active.
*
* @returns {boolean} - True if the mouse is active.
* @private
*/
_isMouseActive: function () {
return this._mouseDown;
}
};
});