Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dd-plugin.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('dd-plugin', function(Y) {
  9. /**
  10. * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
  11. * @module dd
  12. * @submodule dd-plugin
  13. */
  14. /**
  15. * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
  16. * @class Drag
  17. * @extends DD.Drag
  18. * @constructor
  19. * @namespace Plugin
  20. */
  21. var Drag = function(config) {
  22. if (Y.Widget && config.host instanceof Y.Widget) {
  23. config.node = config.host.get('boundingBox');
  24. config.widget = config.host;
  25. }
  26. else {
  27. config.node = config.host;
  28. config.widget = false;
  29. }
  30. Drag.superclass.constructor.call(this, config);
  31. },
  32. EV_DRAG = 'drag:drag',
  33. EV_DRAG_END = 'drag:end';
  34. /**
  35. * @property NAME
  36. * @description dd-plugin
  37. * @type {String}
  38. */
  39. Drag.NAME = "dd-plugin";
  40. /**
  41. * @property NS
  42. * @description The Drag instance will be placed on the Node instance under the dd namespace. It can be accessed via Node.dd;
  43. * @type {String}
  44. */
  45. Drag.NS = "dd";
  46. Y.extend(Drag, Y.DD.Drag, {
  47. /**
  48. * refers to a Y.Widget if its the host, otherwise = false.
  49. *
  50. * @attribute _widget
  51. * @private
  52. */
  53. _widget: undefined,
  54. /**
  55. * refers to the [x,y] coordinate where the drag was stopped last
  56. *
  57. * @attribute _stoppedPosition
  58. * @private
  59. */
  60. _stoppedPosition: undefined,
  61. /**
  62. * Returns true if widget uses widgetPosition, otherwise returns false
  63. *
  64. * @method _usesWidgetPosition
  65. * @private
  66. */
  67. _usesWidgetPosition: function(widget) {
  68. var r = false;
  69. if (widget) {
  70. r = (widget.hasImpl && widget.hasImpl(Y.WidgetPosition)) ? true : false;
  71. }
  72. return r;
  73. },
  74. /**
  75. * Sets up event listeners on drag events if interacting with a widget
  76. *
  77. * @method initializer
  78. * @protected
  79. */
  80. initializer: function(config) {
  81. this._widget = config.widget;
  82. //if this thing is a widget, and it uses widgetposition...
  83. if (this._usesWidgetPosition(this._widget)) {
  84. //set the x,y on the widget's ATTRS
  85. this.on(EV_DRAG, this._setWidgetCoords);
  86. //store the new position that the widget ends up on
  87. this.on(EV_DRAG_END, this._updateStopPosition);
  88. }
  89. },
  90. /**
  91. * Updates x,y or xy attributes on widget based on where the widget is dragged
  92. *
  93. * @method initializer
  94. * @param {EventFacade} e Event Facade
  95. * @private
  96. */
  97. _setWidgetCoords: function(e) {
  98. //get the last position where the widget was, or get the starting point
  99. var nodeXY = this._stoppedPosition || e.target.nodeXY,
  100. realXY = e.target.realXY,
  101. //amount moved = [(x2 - x1) , (y2 - y1)]
  102. movedXY = [realXY[0] - nodeXY[0], realXY[1] - nodeXY[0]];
  103. //if both have changed..
  104. if (movedXY[0] !== 0 && movedXY[1] !== 0) {
  105. this._widget.set('xy', realXY);
  106. }
  107. //if only x is 0, set the Y
  108. else if (movedXY[0] === 0) {
  109. this._widget.set('y',realXY[1]);
  110. }
  111. //otherwise, y is 0, so set X
  112. else if (movedXY[1] === 0){
  113. this._widget.set('x', realXY[0]);
  114. }
  115. },
  116. /**
  117. * Updates the last position where the widget was stopped.
  118. *
  119. * @method updateStopPosition
  120. * @param {EventFacade} e Event Facade
  121. * @private
  122. */
  123. updateStopPosition: function(e) {
  124. this._stoppedPosition = e.target.realXY;
  125. }
  126. });
  127. Y.namespace('Plugin');
  128. Y.Plugin.Drag = Drag;
  129. }, '3.4.0' ,{skinnable:false, optional:['dd-constrain', 'dd-proxy'], requires:['dd-drag']});