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