_dndSelector.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. define("dijit/tree/_dndSelector", [
  2. "dojo/_base/array", // array.filter array.forEach array.map
  3. "dojo/_base/connect", // connect.isCopyKey
  4. "dojo/_base/declare", // declare
  5. "dojo/_base/lang", // lang.hitch
  6. "dojo/mouse", // mouse.isLeft
  7. "dojo/on",
  8. "dojo/touch",
  9. "dojo/_base/window", // win.global
  10. "./_dndContainer"
  11. ], function(array, connect, declare, lang, mouse, on, touch, win, _dndContainer){
  12. // module:
  13. // dijit/tree/_dndSelector
  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. return declare("dijit.tree._dndSelector", _dndContainer, {
  18. // summary:
  19. // This is a base class for `dijit.tree.dndSource` , and isn't meant to be used directly.
  20. // It's based on `dojo.dnd.Selector`.
  21. // tags:
  22. // protected
  23. /*=====
  24. // selection: Hash<String, DomNode>
  25. // (id, DomNode) map for every TreeNode that's currently selected.
  26. // The DOMNode is the TreeNode.rowNode.
  27. selection: {},
  28. =====*/
  29. constructor: function(){
  30. // summary:
  31. // Initialization
  32. // tags:
  33. // private
  34. this.selection={};
  35. this.anchor = null;
  36. this.tree.domNode.setAttribute("aria-multiselect", !this.singular);
  37. this.events.push(
  38. on(this.tree.domNode, touch.press, lang.hitch(this,"onMouseDown")),
  39. on(this.tree.domNode, touch.release, lang.hitch(this,"onMouseUp")),
  40. on(this.tree.domNode, touch.move, lang.hitch(this,"onMouseMove"))
  41. );
  42. },
  43. // singular: Boolean
  44. // Allows selection of only one element, if true.
  45. // Tree hasn't been tested in singular=true mode, unclear if it works.
  46. singular: false,
  47. // methods
  48. getSelectedTreeNodes: function(){
  49. // summary:
  50. // Returns a list of selected node(s).
  51. // Used by dndSource on the start of a drag.
  52. // tags:
  53. // protected
  54. var nodes=[], sel = this.selection;
  55. for(var i in sel){
  56. nodes.push(sel[i]);
  57. }
  58. return nodes;
  59. },
  60. selectNone: function(){
  61. // summary:
  62. // Unselects all items
  63. // tags:
  64. // private
  65. this.setSelection([]);
  66. return this; // self
  67. },
  68. destroy: function(){
  69. // summary:
  70. // Prepares the object to be garbage-collected
  71. this.inherited(arguments);
  72. this.selection = this.anchor = null;
  73. },
  74. addTreeNode: function(/*dijit._TreeNode*/node, /*Boolean?*/isAnchor){
  75. // summary:
  76. // add node to current selection
  77. // node: Node
  78. // node to add
  79. // isAnchor: Boolean
  80. // Whether the node should become anchor.
  81. this.setSelection(this.getSelectedTreeNodes().concat( [node] ));
  82. if(isAnchor){ this.anchor = node; }
  83. return node;
  84. },
  85. removeTreeNode: function(/*dijit._TreeNode*/node){
  86. // summary:
  87. // remove node from current selection
  88. // node: Node
  89. // node to remove
  90. this.setSelection(this._setDifference(this.getSelectedTreeNodes(), [node]));
  91. return node;
  92. },
  93. isTreeNodeSelected: function(/*dijit._TreeNode*/node){
  94. // summary:
  95. // return true if node is currently selected
  96. // node: Node
  97. // the node to check whether it's in the current selection
  98. return node.id && !!this.selection[node.id];
  99. },
  100. setSelection: function(/*dijit._treeNode[]*/ newSelection){
  101. // summary:
  102. // set the list of selected nodes to be exactly newSelection. All changes to the
  103. // selection should be passed through this function, which ensures that derived
  104. // attributes are kept up to date. Anchor will be deleted if it has been removed
  105. // from the selection, but no new anchor will be added by this function.
  106. // newSelection: Node[]
  107. // list of tree nodes to make selected
  108. var oldSelection = this.getSelectedTreeNodes();
  109. array.forEach(this._setDifference(oldSelection, newSelection), lang.hitch(this, function(node){
  110. node.setSelected(false);
  111. if(this.anchor == node){
  112. delete this.anchor;
  113. }
  114. delete this.selection[node.id];
  115. }));
  116. array.forEach(this._setDifference(newSelection, oldSelection), lang.hitch(this, function(node){
  117. node.setSelected(true);
  118. this.selection[node.id] = node;
  119. }));
  120. this._updateSelectionProperties();
  121. },
  122. _setDifference: function(xs,ys){
  123. // summary:
  124. // Returns a copy of xs which lacks any objects
  125. // occurring in ys. Checks for membership by
  126. // modifying and then reading the object, so it will
  127. // not properly handle sets of numbers or strings.
  128. array.forEach(ys, function(y){ y.__exclude__ = true; });
  129. var ret = array.filter(xs, function(x){ return !x.__exclude__; });
  130. // clean up after ourselves.
  131. array.forEach(ys, function(y){ delete y['__exclude__'] });
  132. return ret;
  133. },
  134. _updateSelectionProperties: function(){
  135. // summary:
  136. // Update the following tree properties from the current selection:
  137. // path[s], selectedItem[s], selectedNode[s]
  138. var selected = this.getSelectedTreeNodes();
  139. var paths = [], nodes = [];
  140. array.forEach(selected, function(node){
  141. nodes.push(node);
  142. paths.push(node.getTreePath());
  143. });
  144. var items = array.map(nodes,function(node){ return node.item; });
  145. this.tree._set("paths", paths);
  146. this.tree._set("path", paths[0] || []);
  147. this.tree._set("selectedNodes", nodes);
  148. this.tree._set("selectedNode", nodes[0] || null);
  149. this.tree._set("selectedItems", items);
  150. this.tree._set("selectedItem", items[0] || null);
  151. },
  152. // mouse events
  153. onMouseDown: function(e){
  154. // summary:
  155. // Event processor for onmousedown/ontouchstart
  156. // e: Event
  157. // onmousedown/ontouchstart event
  158. // tags:
  159. // protected
  160. // ignore click on expando node
  161. if(!this.current || this.tree.isExpandoNode(e.target, this.current)){ return; }
  162. if(e.type == "mousedown" && mouse.isLeft(e)){
  163. // Prevent text selection while dragging on desktop, see #16328. But don't call preventDefault()
  164. // for mobile because it will break things completely, see #15838. Also, don't preventDefault() on
  165. // MSPointerDown or pointerdown events, because that stops the mousedown event from being generated,
  166. // see #17709.
  167. // TODO: remove this completely in 2.0. It shouldn't be needed since dojo/dnd/Manager already
  168. // calls preventDefault() for the "selectstart" event. It can also be achieved via CSS:
  169. // http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting
  170. e.preventDefault();
  171. }else if(e.type != "touchstart"){
  172. // Ignore right click
  173. return;
  174. }
  175. var treeNode = this.current,
  176. copy = connect.isCopyKey(e), id = treeNode.id;
  177. // if shift key is not pressed, and the node is already in the selection,
  178. // delay deselection until onmouseup so in the case of DND, deselection
  179. // will be canceled by onmousemove.
  180. if(!this.singular && !e.shiftKey && this.selection[id]){
  181. this._doDeselect = true;
  182. return;
  183. }else{
  184. this._doDeselect = false;
  185. }
  186. this.userSelect(treeNode, copy, e.shiftKey);
  187. },
  188. onMouseUp: function(e){
  189. // summary:
  190. // Event processor for onmouseup/ontouchend
  191. // e: Event
  192. // onmouseup/ontouchend event
  193. // tags:
  194. // protected
  195. // _doDeselect is the flag to indicate that the user wants to either ctrl+click on
  196. // a already selected item (to deselect the item), or click on a not-yet selected item
  197. // (which should remove all current selection, and add the clicked item). This can not
  198. // be done in onMouseDown, because the user may start a drag after mousedown. By moving
  199. // the deselection logic here, the user can drags an already selected item.
  200. if(!this._doDeselect){ return; }
  201. this._doDeselect = false;
  202. this.userSelect(this.current, connect.isCopyKey(e), e.shiftKey);
  203. },
  204. onMouseMove: function(/*===== e =====*/){
  205. // summary:
  206. // event processor for onmousemove/ontouchmove
  207. // e: Event
  208. // onmousemove/ontouchmove event
  209. this._doDeselect = false;
  210. },
  211. _compareNodes: function(n1, n2){
  212. if(n1 === n2){
  213. return 0;
  214. }
  215. if('sourceIndex' in document.documentElement){ //IE
  216. //TODO: does not yet work if n1 and/or n2 is a text node
  217. return n1.sourceIndex - n2.sourceIndex;
  218. }else if('compareDocumentPosition' in document.documentElement){ //FF, Opera
  219. return n1.compareDocumentPosition(n2) & 2 ? 1: -1;
  220. }else if(document.createRange){ //Webkit
  221. var r1 = doc.createRange();
  222. r1.setStartBefore(n1);
  223. var r2 = doc.createRange();
  224. r2.setStartBefore(n2);
  225. return r1.compareBoundaryPoints(r1.END_TO_END, r2);
  226. }else{
  227. throw Error("dijit.tree._compareNodes don't know how to compare two different nodes in this browser");
  228. }
  229. },
  230. userSelect: function(node, multi, range){
  231. // summary:
  232. // Add or remove the given node from selection, responding
  233. // to a user action such as a click or keypress.
  234. // multi: Boolean
  235. // Indicates whether this is meant to be a multi-select action (e.g. ctrl-click)
  236. // range: Boolean
  237. // Indicates whether this is meant to be a ranged action (e.g. shift-click)
  238. // tags:
  239. // protected
  240. if(this.singular){
  241. if(this.anchor == node && multi){
  242. this.selectNone();
  243. }else{
  244. this.setSelection([node]);
  245. this.anchor = node;
  246. }
  247. }else{
  248. if(range && this.anchor){
  249. var cr = this._compareNodes(this.anchor.rowNode, node.rowNode),
  250. begin, end, anchor = this.anchor;
  251. if(cr < 0){ //current is after anchor
  252. begin = anchor;
  253. end = node;
  254. }else{ //current is before anchor
  255. begin = node;
  256. end = anchor;
  257. }
  258. var nodes = [];
  259. //add everything betweeen begin and end inclusively
  260. while(begin != end){
  261. nodes.push(begin);
  262. begin = this.tree._getNextNode(begin);
  263. }
  264. nodes.push(end);
  265. this.setSelection(nodes);
  266. }else{
  267. if( this.selection[ node.id ] && multi ){
  268. this.removeTreeNode( node );
  269. }else if(multi){
  270. this.addTreeNode(node, true);
  271. }else{
  272. this.setSelection([node]);
  273. this.anchor = node;
  274. }
  275. }
  276. }
  277. },
  278. getItem: function(/*String*/ key){
  279. // summary:
  280. // Returns the dojo.dnd.Item (representing a dragged node) by it's key (id).
  281. // Called by dojo.dnd.Source.checkAcceptance().
  282. // tags:
  283. // protected
  284. var widget = this.selection[key];
  285. return {
  286. data: widget,
  287. type: ["treeNode"]
  288. }; // dojo.dnd.Item
  289. },
  290. forInSelectedItems: function(/*Function*/ f, /*Object?*/ o){
  291. // summary:
  292. // Iterates over selected items;
  293. // see `dojo.dnd.Container.forInItems()` for details
  294. o = o || win.global;
  295. for(var id in this.selection){
  296. // console.log("selected item id: " + id);
  297. f.call(o, this.getItem(id), id, this);
  298. }
  299. }
  300. });
  301. });