]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/dnd/Selector.js.uncompressed.js
1 define("dojo/dnd/Selector", ["../main", "./common", "./Container"], function(dojo
) {
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
16 dojo.declare("dojo.dnd.__SelectorArgs", [dojo.dnd.__ContainerArgs], {
18 // allows selection of only one element, if true
22 // autosynchronizes the source with its list of DnD nodes,
27 dojo
.declare("dojo.dnd.Selector", dojo
.dnd
.Container
, {
29 // a Selector object, which knows how to select its children
32 // selection: Set<String>
33 // The set of id's that are currently selected, such that this.selection[id] == 1
34 // if the node w/that id is selected. Can iterate over selected node's id's like:
35 // | for(var id in this.selection)
39 constructor: function(node
, params
){
41 // constructor of the Selector
43 // node or node's id to build the selector on
44 // params: dojo.dnd.__SelectorArgs?
45 // a dictionary of parameters
46 if(!params
){ params
= {}; }
47 this.singular
= params
.singular
;
48 this.autoSync
= params
.autoSync
;
49 // class-specific variables
52 this.simpleSelection
= false;
55 dojo
.connect(this.node
, "onmousedown", this, "onMouseDown"),
56 dojo
.connect(this.node
, "onmouseup", this, "onMouseUp"));
59 // object attributes (for markup)
60 singular
: false, // is singular property
63 getSelectedNodes: function(){
65 // returns a list (an array) of selected nodes
66 var t
= new dojo
.NodeList();
67 var e
= dojo
.dnd
._empty
;
68 for(var i
in this.selection
){
69 if(i
in e
){ continue; }
74 selectNone: function(){
76 // unselects all items
77 return this._removeSelection()._removeAnchor(); // self
79 selectAll: function(){
82 this.forInItems(function(data
, id
){
83 this._addItemClass(dojo
.byId(id
), "Selected");
84 this.selection
[id
] = 1;
86 return this._removeAnchor(); // self
88 deleteSelectedNodes: function(){
90 // deletes all selected items
91 var e
= dojo
.dnd
._empty
;
92 for(var i
in this.selection
){
93 if(i
in e
){ continue; }
102 forInSelectedItems: function(/*Function*/ f
, /*Object?*/ o
){
104 // iterates over selected items;
105 // see `dojo.dnd.Container.forInItems()` for details
106 o
= o
|| dojo
.global
;
107 var s
= this.selection
, e
= dojo
.dnd
._empty
;
109 if(i
in e
){ continue; }
110 f
.call(o
, this.getItem(i
), i
, this);
115 // sync up the node list with the data map
117 dojo
.dnd
.Selector
.superclass
.sync
.call(this);
121 if(!this.getItem(this.anchor
.id
)){
127 var t
= [], e
= dojo
.dnd
._empty
;
128 for(var i
in this.selection
){
129 if(i
in e
){ continue; }
130 if(!this.getItem(i
)){
134 dojo
.forEach(t
, function(i
){
135 delete this.selection
[i
];
140 insertNodes: function(addSelected
, data
, before
, anchor
){
142 // inserts new data items (see `dojo.dnd.Container.insertNodes()` method for details)
143 // addSelected: Boolean
144 // all new nodes will be added to selected items, if true, no selection change otherwise
146 // a list of data items, which should be processed by the creator function
148 // insert before the anchor, if true, and after the anchor otherwise
150 // the anchor node to be used as a point of insertion
151 var oldCreator
= this._normalizedCreator
;
152 this._normalizedCreator = function(item
, hint
){
153 var t
= oldCreator
.call(this, item
, hint
);
156 this.anchor
= t
.node
;
157 this._removeItemClass(t
.node
, "Selected");
158 this._addItemClass(this.anchor
, "Anchor");
159 }else if(this.anchor
!= t
.node
){
160 this._removeItemClass(t
.node
, "Anchor");
161 this._addItemClass(t
.node
, "Selected");
163 this.selection
[t
.node
.id
] = 1;
165 this._removeItemClass(t
.node
, "Selected");
166 this._removeItemClass(t
.node
, "Anchor");
170 dojo
.dnd
.Selector
.superclass
.insertNodes
.call(this, data
, before
, anchor
);
171 this._normalizedCreator
= oldCreator
;
176 // prepares the object to be garbage-collected
177 dojo
.dnd
.Selector
.superclass
.destroy
.call(this);
178 this.selection
= this.anchor
= null;
182 onMouseDown: function(e
){
184 // event processor for onmousedown
187 if(this.autoSync
){ this.sync(); }
188 if(!this.current
){ return; }
189 if(!this.singular
&& !dojo
.isCopyKey(e
) && !e
.shiftKey
&& (this.current
.id
in this.selection
)){
190 this.simpleSelection
= true;
191 if(e
.button
=== dojo
.mouseButtons
.LEFT
){
192 // accept the left button and stop the event
193 // for IE we don't stop event when multiple buttons are pressed
198 if(!this.singular
&& e
.shiftKey
){
199 if(!dojo
.isCopyKey(e
)){
200 this._removeSelection();
202 var c
= this.getAllNodes();
206 this._addItemClass(this.anchor
, "Anchor");
208 this.selection
[this.anchor
.id
] = 1;
209 if(this.anchor
!= this.current
){
211 for(; i
< c
.length
; ++i
){
213 if(node
== this.anchor
|| node
== this.current
){ break; }
215 for(++i
; i
< c
.length
; ++i
){
217 if(node
== this.anchor
|| node
== this.current
){ break; }
218 this._addItemClass(node
, "Selected");
219 this.selection
[node
.id
] = 1;
221 this._addItemClass(this.current
, "Selected");
222 this.selection
[this.current
.id
] = 1;
227 if(this.anchor
== this.current
){
228 if(dojo
.isCopyKey(e
)){
233 this.anchor
= this.current
;
234 this._addItemClass(this.anchor
, "Anchor");
235 this.selection
[this.current
.id
] = 1;
238 if(dojo
.isCopyKey(e
)){
239 if(this.anchor
== this.current
){
240 delete this.selection
[this.anchor
.id
];
241 this._removeAnchor();
243 if(this.current
.id
in this.selection
){
244 this._removeItemClass(this.current
, "Selected");
245 delete this.selection
[this.current
.id
];
248 this._removeItemClass(this.anchor
, "Anchor");
249 this._addItemClass(this.anchor
, "Selected");
251 this.anchor
= this.current
;
252 this._addItemClass(this.current
, "Anchor");
253 this.selection
[this.current
.id
] = 1;
257 if(!(this.current
.id
in this.selection
)){
259 this.anchor
= this.current
;
260 this._addItemClass(this.current
, "Anchor");
261 this.selection
[this.current
.id
] = 1;
268 onMouseUp: function(e
){
270 // event processor for onmouseup
273 if(!this.simpleSelection
){ return; }
274 this.simpleSelection
= false;
277 this.anchor
= this.current
;
278 this._addItemClass(this.anchor
, "Anchor");
279 this.selection
[this.current
.id
] = 1;
282 onMouseMove: function(e
){
284 // event processor for onmousemove
287 this.simpleSelection
= false;
291 onOverEvent: function(){
293 // this function is called once, when mouse is over our container
294 this.onmousemoveEvent
= dojo
.connect(this.node
, "onmousemove", this, "onMouseMove");
296 onOutEvent: function(){
298 // this function is called once, when mouse is out of our container
299 dojo
.disconnect(this.onmousemoveEvent
);
300 delete this.onmousemoveEvent
;
302 _removeSelection: function(){
304 // unselects all items
305 var e
= dojo
.dnd
._empty
;
306 for(var i
in this.selection
){
307 if(i
in e
){ continue; }
308 var node
= dojo
.byId(i
);
309 if(node
){ this._removeItemClass(node
, "Selected"); }
314 _removeAnchor: function(){
316 this._removeItemClass(this.anchor
, "Anchor");
323 return dojo
.dnd
.Selector
;