Debian Codegraph @

dijit/tree/_dndSelector.js

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