[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/dd-plugin/ -> dd-plugin.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('dd-plugin', function (Y, NAME) {
   9  
  10  
  11  
  12         /**
  13          * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
  14          * @module dd
  15          * @submodule dd-plugin
  16          */
  17         /**
  18          * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
  19          * @class Drag
  20          * @extends DD.Drag
  21          * @constructor
  22          * @namespace Plugin
  23          */
  24          var Drag = function(config) {
  25                  if (Y.Widget && config.host instanceof Y.Widget) {
  26                          config.node = config.host.get('boundingBox');
  27                          config.widget = config.host;
  28                  } else {
  29                          config.node = config.host;
  30                          config.widget = false;
  31                  }
  32                  Drag.superclass.constructor.call(this, config);
  33          },
  34  
  35          EV_START = 'drag:start',
  36          EV_DRAG = 'drag:drag',
  37          EV_DRAG_END = 'drag:end';
  38  
  39          /**
  40          * dd-plugin
  41          * @property NAME
  42          * @type {String}
  43          */
  44          Drag.NAME = "dd-plugin";
  45  
  46          /**
  47          * The Drag instance will be placed on the Node instance under the dd namespace. It can be accessed via Node.dd;
  48          * @property NS
  49          * @type {String}
  50          */
  51          Drag.NS = "dd";
  52  
  53          Y.extend(Drag, Y.DD.Drag, {
  54  
  55                  _widgetHandles: null,
  56  
  57                  /**
  58                  * refers to a Y.Widget if its the host, otherwise = false.
  59                  *
  60                  * @attribute _widget
  61                  * @private
  62                  */
  63                  _widget: undefined,
  64  
  65  
  66                  /**
  67                  * refers to the [x,y] coordinate where the drag was stopped last
  68                  *
  69                  * @attribute _stoppedPosition
  70                  * @private
  71                  */
  72                  _stoppedPosition: undefined,
  73  
  74  
  75                  /**
  76                  * Returns true if widget uses widgetPosition, otherwise returns false
  77                  *
  78                  * @method _usesWidgetPosition
  79                  * @private
  80                  */
  81                  _usesWidgetPosition: function(widget) {
  82                          var r = false;
  83                          if (widget) {
  84                                  r = (widget.hasImpl && widget.hasImpl(Y.WidgetPosition)) ? true : false;
  85                          }
  86                          return r;
  87                  },
  88                  /**
  89                  * Attached to the `drag:start` event, it checks if this plugin needs
  90                  * to attach or detach listeners for widgets. If `dd-proxy` is plugged
  91                  * the default widget positioning should be ignored.
  92                  * @method _checkEvents
  93                  * @private
  94                  */
  95                  _checkEvents: function() {
  96                      if (this._widget) {
  97                          //It's a widget
  98                          if (this.proxy) {
  99                              //It's a proxy
 100                              if (this._widgetHandles.length > 0) {
 101                                  //Remove Listeners
 102                                  this._removeWidgetListeners();
 103                              }
 104                          } else {
 105                              if (this._widgetHandles.length === 0) {
 106                                  this._attachWidgetListeners();
 107                              }
 108                          }
 109                      }
 110                  },
 111                  /**
 112                  * Remove the attached widget listeners
 113                  * @method _removeWidgetListeners
 114                  * @private
 115                  */
 116                  _removeWidgetListeners: function() {
 117                      Y.Array.each(this._widgetHandles, function(handle) {
 118                          handle.detach();
 119                      });
 120                      this._widgetHandles = [];
 121                  },
 122                  /**
 123                  * If this is a Widget, then attach the positioning listeners
 124                  * @method _attachWidgetListeners
 125                  * @private
 126                  */
 127                  _attachWidgetListeners: function() {
 128                          //if this thing is a widget, and it uses widgetposition...
 129                          if (this._usesWidgetPosition(this._widget)) {
 130  
 131                                 //set the x,y on the widget's ATTRS
 132                                 this._widgetHandles.push(this.on(EV_DRAG, this._setWidgetCoords));
 133  
 134                                 //store the new position that the widget ends up on
 135                                 this._widgetHandles.push(this.on(EV_DRAG_END, this._updateStopPosition));
 136                          }
 137                  },
 138                  /**
 139                  * Sets up event listeners on drag events if interacting with a widget
 140                  *
 141                  * @method initializer
 142                  * @protected
 143                  */
 144                  initializer: function(config) {
 145  
 146                          this._widgetHandles = [];
 147  
 148                          this._widget = config.widget;
 149  
 150                          this.on(EV_START, this._checkEvents); //Always run, don't check
 151  
 152                          this._attachWidgetListeners();
 153  
 154                  },
 155  
 156                  /**
 157                  * Updates x,y or xy attributes on widget based on where the widget is dragged
 158                  *
 159                  * @method initializer
 160                  * @param {EventFacade} e Event Facade
 161                  * @private
 162                  */
 163                  _setWidgetCoords: function(e) {
 164  
 165                          //get the last position where the widget was, or get the starting point
 166                          var nodeXY = this._stoppedPosition || e.target.nodeXY,
 167                           realXY = e.target.realXY,
 168  
 169                           //amount moved = [(x2 - x1) , (y2 - y1)]
 170                           movedXY = [realXY[0] - nodeXY[0], realXY[1] - nodeXY[1]];
 171  
 172                           //if both have changed..
 173                           if (movedXY[0] !== 0 && movedXY[1] !== 0) {
 174                                   this._widget.set('xy', realXY);
 175                           }
 176  
 177                           //if only x is 0, set the Y
 178                           else if (movedXY[0] === 0) {
 179                                   this._widget.set('y',realXY[1]);
 180                           }
 181  
 182                           //otherwise, y is 0, so set X
 183                           else if (movedXY[1] === 0){
 184                                   this._widget.set('x', realXY[0]);
 185                           }
 186                  },
 187  
 188                  /**
 189                  * Updates the last position where the widget was stopped.
 190                  *
 191                  * @method _updateStopPosition
 192                  * @param {EventFacade} e Event Facade
 193                  * @private
 194                  */
 195                  _updateStopPosition: function(e) {
 196                          this._stoppedPosition = e.target.realXY;
 197                  }
 198          });
 199  
 200          Y.namespace('Plugin');
 201          Y.Plugin.Drag = Drag;
 202  
 203  
 204  
 205  
 206  
 207  }, '3.17.2', {"optional": ["dd-constrain", "dd-proxy"], "requires": ["dd-drag"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1