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