PureSource.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.mdnd.PureSource"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mdnd.PureSource"] = true;
  8. dojo.provide("dojox.mdnd.PureSource");
  9. dojo.require("dojo.dnd.Selector");
  10. dojo.require("dojo.dnd.Manager");
  11. dojo.declare(
  12. "dojox.mdnd.PureSource",
  13. dojo.dnd.Selector,
  14. {
  15. // summary:
  16. // A Source Object, which can be used only as a DnD source.
  17. // A Source can contained several dnd items.
  18. // A dnd item is not a source.
  19. horizontal: false,
  20. copyOnly: true,
  21. skipForm: false,
  22. withHandles: false,
  23. isSource: true,
  24. targetState: "Disabled",
  25. generateText: true,
  26. constructor: function(/*DOMNode|String*/node, /*dojo.dnd.__SourceArgs?*/params){
  27. // summary:
  28. // Initialize a new PureSource.
  29. // node:
  30. // Node or node's id to build the source on.
  31. // params:
  32. // Any property of this class may be configured via the params
  33. // object which is mixed-in to the 'dojo.dnd.Source' instance.
  34. //console.log('dojox.mdnd.PureSource ::: constructor');
  35. dojo.mixin(this, dojo.mixin({}, params));
  36. var type = this.accept;
  37. // class-specific variables
  38. this.isDragging = false;
  39. this.mouseDown = false;
  40. // states
  41. this.sourceState = "";
  42. dojo.addClass(this.node, "dojoDndSource");
  43. if(this.horizontal){
  44. dojo.addClass(this.node, "dojoDndHorizontal");
  45. }
  46. // set up events
  47. this.topics = [
  48. dojo.subscribe("/dnd/cancel", this, "onDndCancel"),
  49. dojo.subscribe("/dnd/drop", this, "onDndCancel")
  50. ];
  51. },
  52. onDndCancel: function(){
  53. // summary:
  54. // Topic event processor for /dnd/cancel, called to cancel the Dnd
  55. // operation.
  56. // tags:
  57. // callback
  58. //console.log('dojox.mdnd.PureSource ::: onDndCancel');
  59. this.isDragging = false;
  60. this.mouseDown = false;
  61. delete this.mouseButton;
  62. },
  63. copyState: function(/*Boolean*/keyPressed){
  64. // summary:
  65. // Returns true, if we need to copy items, false to move.
  66. // It is separated to be overwritten dynamically, if needed.
  67. // keyPressed:
  68. // The "copy" was pressed.
  69. // returns:
  70. // True, if we need to copy items, false to move.
  71. //console.log('dojox.mdnd.PureSource ::: copyState');
  72. return this.copyOnly || keyPressed; // Boolean
  73. },
  74. destroy: function(){
  75. // summary:
  76. // Prepares the object to be garbage-collected.
  77. //console.log('dojox.mdnd.PureSource ::: destroy');
  78. dojox.mdnd.PureSource.superclass.destroy.call(this);
  79. dojo.forEach(this.topics, dojo.unsubscribe);
  80. this.targetAnchor = null;
  81. },
  82. markupFactory: function(/*Object*/params, /*DomNode*/node){
  83. // summary:
  84. // Markup methods.
  85. // params:
  86. // ???
  87. // node:
  88. // ???
  89. // returns:
  90. // New dojox.mdnd.PureSource instance.
  91. //console.log('dojox.mdnd.PureSource ::: markupFactory');
  92. params._skipStartup = true;
  93. return new dojox.mdnd.PureSource(node, params);
  94. },
  95. onMouseMove: function(/*Event*/e){
  96. // summary:
  97. // Event processor for onmousemove.
  98. // e:
  99. // Mouse event.
  100. //console.log('dojox.mdnd.PureSource ::: onMouseMove');
  101. if(this.isDragging){
  102. return;
  103. }
  104. dojox.mdnd.PureSource.superclass.onMouseMove.call(this, e);
  105. var m = dojo.dnd.manager();
  106. if(this.mouseDown && !this.isDragging && this.isSource){
  107. var nodes = this.getSelectedNodes();
  108. if(nodes.length){
  109. m.startDrag(this, nodes, this.copyState(dojo.isCopyKey(e)));
  110. this.isDragging = true;
  111. }
  112. }
  113. },
  114. onMouseDown: function(/*Event*/e){
  115. // summary:
  116. // Event processor for onmousedown.
  117. // e:
  118. // Mouse event.
  119. // tags:
  120. // callback
  121. //console.log('dojox.mdnd.PureSource ::: onMouseDown');
  122. if(this._legalMouseDown(e) && (!this.skipForm || !dojo.dnd.isFormElement(e))){
  123. this.mouseDown = true;
  124. this.mouseButton = e.button;
  125. dojox.mdnd.PureSource.superclass.onMouseDown.call(this, e);
  126. }
  127. },
  128. onMouseUp: function(/*Event*/e){
  129. // summary:
  130. // Event processor for onmouseup.
  131. // e:
  132. // Mouse event
  133. // tags:
  134. // callback
  135. //console.log('.dnd.PureSource ::: onMouseUp');
  136. if(this.mouseDown){
  137. this.mouseDown = false;
  138. dojox.mdnd.PureSource.superclass.onMouseUp.call(this, e);
  139. }
  140. },
  141. onOverEvent: function(){
  142. // summary:
  143. // Called once, when mouse is over our container.
  144. // tags:
  145. // callback
  146. //console.log('dojox.mdnd.PureSource ::: onOverEvent');
  147. dojox.mdnd.PureSource.superclass.onOverEvent.call(this);
  148. dojo.dnd.manager().overSource(this);
  149. },
  150. onOutEvent: function(){
  151. // summary:
  152. // Called once, when mouse is out our container.
  153. // tags:
  154. // callback
  155. //console.log('dojox.mdnd.PureSource ::: onOutEvent');
  156. dojox.mdnd.PureSource.superclass.onOutEvent.call(this);
  157. dojo.dnd.manager().outSource(this);
  158. },
  159. _markDndStatus: function(/*Boolean*/copy){
  160. // summary:
  161. // Changes source's state based on "copy" status.
  162. // copy:
  163. // Copy status.
  164. // tags:
  165. // protected
  166. //console.log('dojox.mdnd.PureSource ::: _markDndStatus');
  167. this._changeState("Source", copy ? "Copied" : "Moved");
  168. },
  169. _legalMouseDown: function(/*Event*/e){
  170. // summary:
  171. // Checks if user clicked on "approved" items.
  172. // e:
  173. // Mouse event.
  174. // returns:
  175. // True if user clicked on "approved" items.
  176. // tags:
  177. // protected
  178. //console.log('dojox.mdnd.PureSource ::: _legalMouseDown');
  179. if(!this.withHandles){ return true; }
  180. for(var node = e.target; node && !dojo.hasClass(node, "dojoDndItem"); node = node.parentNode){
  181. if(dojo.hasClass(node, "dojoDndHandle")){ return true; }
  182. }
  183. return false; // Boolean
  184. }
  185. });
  186. }