1 if(!dojo._hasResource["dojo.dnd.Selector"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.dnd.Selector"] = true;
3 dojo.provide("dojo.dnd.Selector");
5 dojo.require("dojo.dnd.common");
6 dojo.require("dojo.dnd.Container");
10 "" - an item is not selected
11 "Selected" - an item is selected
12 "Anchor" - an item is selected, and is an anchor for a "shift" selection
15 dojo.declare("dojo.dnd.Selector", dojo.dnd.Container, {
16 // summary: a Selector object, which knows how to select its children
18 constructor: function(node, params){
19 // summary: a constructor of the Selector
20 // node: Node: node or node's id to build the selector on
21 // params: Object: a dict of parameters, recognized parameters are:
22 // singular: Boolean: allows selection of only one element, if true
23 // the rest of parameters are passed to the container
24 if(!params){ params = {}; }
25 this.singular = params.singular;
26 // class-specific variables
29 this.simpleSelection = false;
32 dojo.connect(this.node, "onmousedown", this, "onMouseDown"),
33 dojo.connect(this.node, "onmouseup", this, "onMouseUp"));
36 // object attributes (for markup)
37 singular: false, // is singular property
40 getSelectedNodes: function(){
41 // summary: returns a list (an array) of selected nodes
42 var t = new dojo.NodeList();
43 var e = dojo.dnd._empty;
44 for(var i in this.selection){
45 if(i in e){ continue; }
50 selectNone: function(){
51 // summary: unselects all items
52 return this._removeSelection()._removeAnchor(); // self
54 selectAll: function(){
55 // summary: selects all items
56 this.forInItems(function(data, id){
57 this._addItemClass(dojo.byId(id), "Selected");
58 this.selection[id] = 1;
60 return this._removeAnchor(); // self
62 deleteSelectedNodes: function(){
63 // summary: deletes all selected items
64 var e = dojo.dnd._empty;
65 for(var i in this.selection){
66 if(i in e){ continue; }
69 dojo._destroyElement(n);
75 insertNodes: function(addSelected, data, before, anchor){
76 // summary: inserts new data items (see Container's insertNodes method for details)
77 // addSelected: Boolean: all new nodes will be added to selected items, if true, no selection change otherwise
78 // data: Array: a list of data items, which should be processed by the creator function
79 // before: Boolean: insert before the anchor, if true, and after the anchor otherwise
80 // anchor: Node: the anchor node to be used as a point of insertion
81 var oldCreator = this._normalizedCreator;
82 this._normalizedCreator = function(item, hint){
83 var t = oldCreator.call(this, item, hint);
87 this._removeItemClass(t.node, "Selected");
88 this._addItemClass(this.anchor, "Anchor");
89 }else if(this.anchor != t.node){
90 this._removeItemClass(t.node, "Anchor");
91 this._addItemClass(t.node, "Selected");
93 this.selection[t.node.id] = 1;
95 this._removeItemClass(t.node, "Selected");
96 this._removeItemClass(t.node, "Anchor");
100 dojo.dnd.Selector.superclass.insertNodes.call(this, data, before, anchor);
101 this._normalizedCreator = oldCreator;
105 // summary: prepares the object to be garbage-collected
106 dojo.dnd.Selector.superclass.destroy.call(this);
107 this.selection = this.anchor = null;
111 markupFactory: function(params, node){
112 params._skipStartup = true;
113 return new dojo.dnd.Selector(node, params);
117 onMouseDown: function(e){
118 // summary: event processor for onmousedown
119 // e: Event: mouse event
120 if(!this.current){ return; }
121 if(!this.singular && !dojo.dnd.getCopyKeyState(e) && !e.shiftKey && (this.current.id in this.selection)){
122 this.simpleSelection = true;
126 if(!this.singular && e.shiftKey){
127 if(!dojo.dnd.getCopyKeyState(e)){
128 this._removeSelection();
130 var c = this.getAllNodes();
134 this._addItemClass(this.anchor, "Anchor");
136 this.selection[this.anchor.id] = 1;
137 if(this.anchor != this.current){
139 for(; i < c.length; ++i){
141 if(node == this.anchor || node == this.current){ break; }
143 for(++i; i < c.length; ++i){
145 if(node == this.anchor || node == this.current){ break; }
146 this._addItemClass(node, "Selected");
147 this.selection[node.id] = 1;
149 this._addItemClass(this.current, "Selected");
150 this.selection[this.current.id] = 1;
155 if(this.anchor == this.current){
156 if(dojo.dnd.getCopyKeyState(e)){
161 this.anchor = this.current;
162 this._addItemClass(this.anchor, "Anchor");
163 this.selection[this.current.id] = 1;
166 if(dojo.dnd.getCopyKeyState(e)){
167 if(this.anchor == this.current){
168 delete this.selection[this.anchor.id];
169 this._removeAnchor();
171 if(this.current.id in this.selection){
172 this._removeItemClass(this.current, "Selected");
173 delete this.selection[this.current.id];
176 this._removeItemClass(this.anchor, "Anchor");
177 this._addItemClass(this.anchor, "Selected");
179 this.anchor = this.current;
180 this._addItemClass(this.current, "Anchor");
181 this.selection[this.current.id] = 1;
185 if(!(this.current.id in this.selection)){
187 this.anchor = this.current;
188 this._addItemClass(this.current, "Anchor");
189 this.selection[this.current.id] = 1;
196 onMouseUp: function(e){
197 // summary: event processor for onmouseup
198 // e: Event: mouse event
199 if(!this.simpleSelection){ return; }
200 this.simpleSelection = false;
203 this.anchor = this.current;
204 this._addItemClass(this.anchor, "Anchor");
205 this.selection[this.current.id] = 1;
208 onMouseMove: function(e){
209 // summary: event processor for onmousemove
210 // e: Event: mouse event
211 this.simpleSelection = false;
215 onOverEvent: function(){
216 // summary: this function is called once, when mouse is over our container
217 this.onmousemoveEvent = dojo.connect(this.node, "onmousemove", this, "onMouseMove");
219 onOutEvent: function(){
220 // summary: this function is called once, when mouse is out of our container
221 dojo.disconnect(this.onmousemoveEvent);
222 delete this.onmousemoveEvent;
224 _removeSelection: function(){
225 // summary: unselects all items
226 var e = dojo.dnd._empty;
227 for(var i in this.selection){
228 if(i in e){ continue; }
229 var node = dojo.byId(i);
230 if(node){ this._removeItemClass(node, "Selected"); }
235 _removeAnchor: function(){
237 this._removeItemClass(this.anchor, "Anchor");