dd-plugin.js revision 063bc75c8da327a18bc88759919aac5a9466881c
157N/AYUI.add('dd-plugin', function(Y) {
157N/A
157N/A
157N/A /**
157N/A * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
157N/A * @module dd
157N/A * @submodule dd-plugin
157N/A */
157N/A /**
157N/A * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
157N/A * @class Drag
157N/A * @extends DD.Drag
157N/A * @constructor
157N/A * @namespace Plugin
157N/A */
157N/A var Drag = function(config) {
157N/A if (Y.Widget && config.host instanceof Y.Widget) {
157N/A config.node = config.host.get('boundingBox');
157N/A config.widget = config.host;
157N/A }
2899N/A else {
157N/A config.node = config.host;
260N/A config.widget = false;
157N/A }
157N/A Drag.superclass.constructor.call(this, config);
1413N/A },
157N/A
157N/A EV_DRAG = 'drag:drag',
844N/A EV_DRAG_END = 'drag:end';
1413N/A
260N/A /**
260N/A * @property NAME
1289N/A * @description dd-plugin
157N/A * @type {String}
2899N/A */
2899N/A Drag.NAME = "dd-plugin";
260N/A
260N/A /**
260N/A * @property NS
157N/A * @description The Drag instance will be placed on the Node instance under the dd namespace. It can be accessed via Node.dd;
1413N/A * @type {String}
1413N/A */
1938N/A Drag.NS = "dd";
1938N/A
1413N/A Y.extend(Drag, Y.DD.Drag, {
260N/A
1413N/A
157N/A /**
1413N/A * refers to a Y.Widget if its the host, otherwise = false.
260N/A *
260N/A * @attribute _widget
260N/A * @private
260N/A */
_widget: undefined,
/**
* refers to the [x,y] coordinate where the drag was stopped last
*
* @attribute _stoppedPosition
* @private
*/
_stoppedPosition: undefined,
/**
* Returns true if widget uses widgetPosition, otherwise returns false
*
* @method _usesWidgetPosition
* @private
*/
_usesWidgetPosition: function(widget) {
var r = false;
if (widget) {
r = (widget.hasImpl && widget.hasImpl(Y.WidgetPosition)) ? true : false;
}
return r;
},
/**
* Sets up event listeners on drag events if interacting with a widget
*
* @method initializer
* @protected
*/
initializer: function(config) {
this._widget = config.widget;
//if this thing is a widget, and it uses widgetposition...
if (this._usesWidgetPosition(this._widget)) {
//set the x,y on the widget's ATTRS
this.on(EV_DRAG, this._setWidgetCoords);
//store the new position that the widget ends up on
this.on(EV_DRAG_END, this._updateStopPosition);
}
},
/**
* Updates x,y or xy attributes on widget based on where the widget is dragged
*
* @method initializer
* @param {EventFacade} e Event Facade
* @private
*/
_setWidgetCoords: function(e) {
//get the last position where the widget was, or get the starting point
var nodeXY = this._stoppedPosition || e.target.nodeXY,
realXY = e.target.realXY,
//amount moved = [(x2 - x1) , (y2 - y1)]
movedXY = [realXY[0] - nodeXY[0], realXY[1] - nodeXY[0]];
//if both have changed..
if (movedXY[0] !== 0 && movedXY[1] !== 0) {
this._widget.set('xy', realXY);
}
//if only x is 0, set the Y
else if (movedXY[0] === 0) {
this._widget.set('y',realXY[1]);
}
//otherwise, y is 0, so set X
else if (movedXY[1] === 0){
this._widget.set('x', realXY[0]);
}
},
/**
* Updates the last position where the widget was stopped.
*
* @method updateStopPosition
* @param {EventFacade} e Event Facade
* @private
*/
updateStopPosition: function(e) {
this._stoppedPosition = e.target.realXY;
}
});
Y.namespace('Plugin');
Y.Plugin.Drag = Drag;
}, '@VERSION@' ,{optional:['dd-constrain', 'dd-proxy'], skinnable:false, requires:['dd-drag']});