_dndSelector.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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["dijit.tree._dndSelector"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.tree._dndSelector"] = true;
  8. dojo.provide("dijit.tree._dndSelector");
  9. dojo.require("dojo.dnd.common");
  10. dojo.require("dijit.tree._dndContainer");
  11. dojo.declare("dijit.tree._dndSelector",
  12. dijit.tree._dndContainer,
  13. {
  14. // summary:
  15. // This is a base class for `dijit.tree.dndSource` , and isn't meant to be used directly.
  16. // It's based on `dojo.dnd.Selector`.
  17. // tags:
  18. // protected
  19. /*=====
  20. // selection: Hash<String, DomNode>
  21. // (id, DomNode) map for every TreeNode that's currently selected.
  22. // The DOMNode is the TreeNode.rowNode.
  23. selection: {},
  24. =====*/
  25. constructor: function(tree, params){
  26. // summary:
  27. // Initialization
  28. // tags:
  29. // private
  30. this.selection={};
  31. this.anchor = null;
  32. dijit.setWaiState(this.tree.domNode, "multiselect", !this.singular);
  33. this.events.push(
  34. dojo.connect(this.tree.domNode, "onmousedown", this,"onMouseDown"),
  35. dojo.connect(this.tree.domNode, "onmouseup", this,"onMouseUp"),
  36. dojo.connect(this.tree.domNode, "onmousemove", this,"onMouseMove")
  37. );
  38. },
  39. // singular: Boolean
  40. // Allows selection of only one element, if true.
  41. // Tree hasn't been tested in singular=true mode, unclear if it works.
  42. singular: false,
  43. // methods
  44. getSelectedTreeNodes: function(){
  45. // summary:
  46. // Returns a list of selected node(s).
  47. // Used by dndSource on the start of a drag.
  48. // tags:
  49. // protected
  50. var nodes=[], sel = this.selection;
  51. for(var i in sel){
  52. nodes.push(sel[i]);
  53. }
  54. return nodes;
  55. },
  56. selectNone: function(){
  57. // summary:
  58. // Unselects all items
  59. // tags:
  60. // private
  61. this.setSelection([]);
  62. return this; // self
  63. },
  64. destroy: function(){
  65. // summary:
  66. // Prepares the object to be garbage-collected
  67. this.inherited(arguments);
  68. this.selection = this.anchor = null;
  69. },
  70. addTreeNode: function(/*dijit._TreeNode*/node, /*Boolean?*/isAnchor){
  71. // summary
  72. // add node to current selection
  73. // node: Node
  74. // node to add
  75. // isAnchor: Boolean
  76. // Whether the node should become anchor.
  77. this.setSelection(this.getSelectedTreeNodes().concat( [node] ));
  78. if(isAnchor){ this.anchor = node; }
  79. return node;
  80. },
  81. removeTreeNode: function(/*dijit._TreeNode*/node){
  82. // summary
  83. // remove node from current selection
  84. // node: Node
  85. // node to remove
  86. this.setSelection(this._setDifference(this.getSelectedTreeNodes(), [node]))
  87. return node;
  88. },
  89. isTreeNodeSelected: function(/*dijit._TreeNode*/node){
  90. // summary
  91. // return true if node is currently selected
  92. // node: Node
  93. // the node to check whether it's in the current selection
  94. return node.id && !!this.selection[node.id];
  95. },
  96. setSelection: function(/*dijit._treeNode[]*/ newSelection){
  97. // summary
  98. // set the list of selected nodes to be exactly newSelection. All changes to the
  99. // selection should be passed through this function, which ensures that derived
  100. // attributes are kept up to date. Anchor will be deleted if it has been removed
  101. // from the selection, but no new anchor will be added by this function.
  102. // newSelection: Node[]
  103. // list of tree nodes to make selected
  104. var oldSelection = this.getSelectedTreeNodes();
  105. dojo.forEach(this._setDifference(oldSelection, newSelection), dojo.hitch(this, function(node){
  106. node.setSelected(false);
  107. if(this.anchor == node){
  108. delete this.anchor;
  109. }
  110. delete this.selection[node.id];
  111. }));
  112. dojo.forEach(this._setDifference(newSelection, oldSelection), dojo.hitch(this, function(node){
  113. node.setSelected(true);
  114. this.selection[node.id] = node;
  115. }));
  116. this._updateSelectionProperties();
  117. },
  118. _setDifference: function(xs,ys){
  119. // summary
  120. // Returns a copy of xs which lacks any objects
  121. // occurring in ys. Checks for membership by
  122. // modifying and then reading the object, so it will
  123. // not properly handle sets of numbers or strings.
  124. dojo.forEach(ys, function(y){ y.__exclude__ = true; });
  125. var ret = dojo.filter(xs, function(x){ return !x.__exclude__; });
  126. // clean up after ourselves.
  127. dojo.forEach(ys, function(y){ delete y['__exclude__'] });
  128. return ret;
  129. },
  130. _updateSelectionProperties: function() {
  131. // summary
  132. // Update the following tree properties from the current selection:
  133. // path[s], selectedItem[s], selectedNode[s]
  134. var selected = this.getSelectedTreeNodes();
  135. var paths = [], nodes = [];
  136. dojo.forEach(selected, function(node) {
  137. nodes.push(node);
  138. paths.push(node.getTreePath());
  139. });
  140. var items = dojo.map(nodes,function(node) { return node.item; });
  141. this.tree._set("paths", paths);
  142. this.tree._set("path", paths[0] || []);
  143. this.tree._set("selectedNodes", nodes);
  144. this.tree._set("selectedNode", nodes[0] || null);
  145. this.tree._set("selectedItems", items);
  146. this.tree._set("selectedItem", items[0] || null);
  147. },
  148. // mouse events
  149. onMouseDown: function(e){
  150. // summary:
  151. // Event processor for onmousedown
  152. // e: Event
  153. // mouse event
  154. // tags:
  155. // protected
  156. // ignore click on expando node
  157. if(!this.current || this.tree.isExpandoNode( e.target, this.current)){ return; }
  158. if(e.button == dojo.mouseButtons.RIGHT){ return; } // ignore right-click
  159. dojo.stopEvent(e);
  160. var treeNode = this.current,
  161. copy = dojo.isCopyKey(e), id = treeNode.id;
  162. // if shift key is not pressed, and the node is already in the selection,
  163. // delay deselection until onmouseup so in the case of DND, deselection
  164. // will be canceled by onmousemove.
  165. if(!this.singular && !e.shiftKey && this.selection[id]){
  166. this._doDeselect = true;
  167. return;
  168. }else{
  169. this._doDeselect = false;
  170. }
  171. this.userSelect(treeNode, copy, e.shiftKey);
  172. },
  173. onMouseUp: function(e){
  174. // summary:
  175. // Event processor for onmouseup
  176. // e: Event
  177. // mouse event
  178. // tags:
  179. // protected
  180. // _doDeselect is the flag to indicate that the user wants to either ctrl+click on
  181. // a already selected item (to deselect the item), or click on a not-yet selected item
  182. // (which should remove all current selection, and add the clicked item). This can not
  183. // be done in onMouseDown, because the user may start a drag after mousedown. By moving
  184. // the deselection logic here, the user can drags an already selected item.
  185. if(!this._doDeselect){ return; }
  186. this._doDeselect = false;
  187. this.userSelect(this.current, dojo.isCopyKey( e ), e.shiftKey);
  188. },
  189. onMouseMove: function(e){
  190. // summary
  191. // event processor for onmousemove
  192. // e: Event
  193. // mouse event
  194. this._doDeselect = false;
  195. },
  196. userSelect: function(node, multi, range){
  197. // summary:
  198. // Add or remove the given node from selection, responding
  199. // to a user action such as a click or keypress.
  200. // multi: Boolean
  201. // Indicates whether this is meant to be a multi-select action (e.g. ctrl-click)
  202. // range: Boolean
  203. // Indicates whether this is meant to be a ranged action (e.g. shift-click)
  204. // tags:
  205. // protected
  206. if(this.singular){
  207. if(this.anchor == node && multi){
  208. this.selectNone();
  209. }else{
  210. this.setSelection([node]);
  211. this.anchor = node;
  212. }
  213. }else{
  214. if(range && this.anchor){
  215. var cr = dijit.tree._compareNodes(this.anchor.rowNode, node.rowNode),
  216. begin, end, anchor = this.anchor;
  217. if(cr < 0){ //current is after anchor
  218. begin = anchor;
  219. end = node;
  220. }else{ //current is before anchor
  221. begin = node;
  222. end = anchor;
  223. }
  224. nodes = [];
  225. //add everything betweeen begin and end inclusively
  226. while(begin != end) {
  227. nodes.push(begin)
  228. begin = this.tree._getNextNode(begin);
  229. }
  230. nodes.push(end)
  231. this.setSelection(nodes);
  232. }else{
  233. if( this.selection[ node.id ] && multi ) {
  234. this.removeTreeNode( node );
  235. } else if(multi) {
  236. this.addTreeNode(node, true);
  237. } else {
  238. this.setSelection([node]);
  239. this.anchor = node;
  240. }
  241. }
  242. }
  243. },
  244. forInSelectedItems: function(/*Function*/ f, /*Object?*/ o){
  245. // summary:
  246. // Iterates over selected items;
  247. // see `dojo.dnd.Container.forInItems()` for details
  248. o = o || dojo.global;
  249. for(var id in this.selection){
  250. // console.log("selected item id: " + id);
  251. f.call(o, this.getItem(id), id, this);
  252. }
  253. }
  254. });
  255. }