| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/kernel", |
| 5 |
"dojo/_base/lang", |
| 6 |
"dojo/dnd/common", |
| 7 |
"dojo/dom", |
| 8 |
"dojo/mouse", |
| 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 |
|
| 16 |
|
| 17 |
|
| 18 |
return declare("dijit.tree._dndSelector", _dndContainer, { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
constructor: function(){ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
this.selection={}; |
| 39 |
this.anchor = null; |
| 40 |
|
| 41 |
this.events.push( |
| 42 |
|
| 43 |
on(this.tree.domNode, touch.press, lang.hitch(this,"onMouseDown")), |
| 44 |
on(this.tree.domNode, touch.release, lang.hitch(this,"onMouseUp")), |
| 45 |
|
| 46 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
singular: false, |
| 57 |
|
| 58 |
|
| 59 |
getSelectedTreeNodes: function(){ |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
this.setSelection([]); |
| 79 |
return this; |
| 80 |
}, |
| 81 |
|
| 82 |
destroy: function(){ |
| 83 |
|
| 84 |
|
| 85 |
this.inherited(arguments); |
| 86 |
this.selection = this.anchor = null; |
| 87 |
}, |
| 88 |
addTreeNode: function( node, isAnchor){ |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
this.setSelection(this.getSelectedTreeNodes().concat( [node] )); |
| 97 |
if(isAnchor){ this.anchor = node; } |
| 98 |
return node; |
| 99 |
}, |
| 100 |
removeTreeNode: function( node){ |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
var newSelection = array.filter(this.getSelectedTreeNodes(), function(selectedNode){ |
| 106 |
return !dom.isDescendant(selectedNode.domNode, node.domNode); |
| 107 |
}); |
| 108 |
this.setSelection(newSelection); |
| 109 |
return node; |
| 110 |
}, |
| 111 |
isTreeNodeSelected: function( node){ |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
return node.id && !!this.selection[node.id]; |
| 118 |
}, |
| 119 |
setSelection: function( newSelection){ |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
array.forEach(ys, function(y){ y.__exclude__ = true; }); |
| 149 |
var ret = array.filter(xs, function(x){ return !x.__exclude__; }); |
| 150 |
|
| 151 |
|
| 152 |
array.forEach(ys, function(y){ delete y['__exclude__'] }); |
| 153 |
return ret; |
| 154 |
}, |
| 155 |
_updateSelectionProperties: function(){ |
| 156 |
|
| 157 |
|
| 158 |
|
| 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 |
|
| 177 |
onClickPress: function(e){ |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
e.preventDefault(); |
| 197 |
} |
| 198 |
|
| 199 |
var treeNode = e.type == "keydown" ? this.tree.focusedChild : this.current; |
| 200 |
|
| 201 |
if(!treeNode){ |
| 202 |
|
| 203 |
|
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
var copy = dndCommon.getCopyKeyState(e), id = treeNode.id; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 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 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 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(){ |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
this._doDeselect = false; |
| 244 |
}, |
| 245 |
|
| 246 |
|
| 247 |
onMouseDown: function(){ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
}, |
| 255 |
onMouseUp: function(){ |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
}, |
| 263 |
|
| 264 |
_compareNodes: function(n1, n2){ |
| 265 |
if(n1 === n2){ |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
if('sourceIndex' in document.documentElement){ |
| 270 |
|
| 271 |
return n1.sourceIndex - n2.sourceIndex; |
| 272 |
}else if('compareDocumentPosition' in document.documentElement){ |
| 273 |
return n1.compareDocumentPosition(n2) & 2 ? 1: -1; |
| 274 |
}else if(document.createRange){ |
| 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 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 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){ |
| 311 |
begin = anchor; |
| 312 |
end = node; |
| 313 |
}else{ |
| 314 |
begin = node; |
| 315 |
end = anchor; |
| 316 |
} |
| 317 |
var nodes = []; |
| 318 |
|
| 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( key){ |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
var widget = this.selection[key]; |
| 347 |
return { |
| 348 |
data: widget, |
| 349 |
type: ["treeNode"] |
| 350 |
}; |
| 351 |
}, |
| 352 |
|
| 353 |
forInSelectedItems: function( f, o){ |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
o = o || kernel.global; |
| 358 |
for(var id in this.selection){ |
| 359 |
|
| 360 |
f.call(o, this.getItem(id), id, this); |
| 361 |
} |
| 362 |
} |
| 363 |
}); |
| 364 |
}); |