]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/dnd/Selector.js.uncompressed.js
1 define("dojo/dnd/Selector", [
2 "../_base/array", "../_base/declare", "../_base/event", "../_base/kernel", "../_base/lang",
3 "../dom", "../dom-construct", "../mouse", "../_base/NodeList", "../on", "../touch", "./common", "./Container"
4 ], function(array
, declare
, event
, kernel
, lang
, dom
, domConstruct
, mouse
, NodeList
, on
, touch
, dnd
, Container
){
10 Container item states:
11 "" - an item is not selected
12 "Selected" - an item is selected
13 "Anchor" - an item is selected, and is an anchor for a "shift" selection
17 var __SelectorArgs = declare([Container.__ContainerArgs], {
19 // allows selection of only one element, if true
23 // autosynchronizes the source with its list of DnD nodes,
28 var Selector
= declare("dojo.dnd.Selector", Container
, {
30 // a Selector object, which knows how to select its children
33 // selection: Set<String>
34 // The set of id's that are currently selected, such that this.selection[id] == 1
35 // if the node w/that id is selected. Can iterate over selected node's id's like:
36 // | for(var id in this.selection)
40 constructor: function(node
, params
){
42 // constructor of the Selector
44 // node or node's id to build the selector on
45 // params: __SelectorArgs?
46 // a dictionary of parameters
47 if(!params
){ params
= {}; }
48 this.singular
= params
.singular
;
49 this.autoSync
= params
.autoSync
;
50 // class-specific variables
53 this.simpleSelection
= false;
56 on(this.node
, touch
.press
, lang
.hitch(this, "onMouseDown")),
57 on(this.node
, touch
.release
, lang
.hitch(this, "onMouseUp"))
61 // object attributes (for markup)
62 singular
: false, // is singular property
65 getSelectedNodes: function(){
67 // returns a list (an array) of selected nodes
68 var t
= new NodeList();
70 for(var i
in this.selection
){
71 if(i
in e
){ continue; }
76 selectNone: function(){
78 // unselects all items
79 return this._removeSelection()._removeAnchor(); // self
81 selectAll: function(){
84 this.forInItems(function(data
, id
){
85 this._addItemClass(dom
.byId(id
), "Selected");
86 this.selection
[id
] = 1;
88 return this._removeAnchor(); // self
90 deleteSelectedNodes: function(){
92 // deletes all selected items
94 for(var i
in this.selection
){
95 if(i
in e
){ continue; }
98 domConstruct
.destroy(n
);
104 forInSelectedItems: function(/*Function*/ f
, /*Object?*/ o
){
106 // iterates over selected items;
107 // see `dojo/dnd/Container.forInItems()` for details
108 o
= o
|| kernel
.global
;
109 var s
= this.selection
, e
= dnd
._empty
;
111 if(i
in e
){ continue; }
112 f
.call(o
, this.getItem(i
), i
, this);
117 // sync up the node list with the data map
119 Selector
.superclass
.sync
.call(this);
123 if(!this.getItem(this.anchor
.id
)){
129 var t
= [], e
= dnd
._empty
;
130 for(var i
in this.selection
){
131 if(i
in e
){ continue; }
132 if(!this.getItem(i
)){
136 array
.forEach(t
, function(i
){
137 delete this.selection
[i
];
142 insertNodes: function(addSelected
, data
, before
, anchor
){
144 // inserts new data items (see `dojo/dnd/Container.insertNodes()` method for details)
145 // addSelected: Boolean
146 // all new nodes will be added to selected items, if true, no selection change otherwise
148 // a list of data items, which should be processed by the creator function
150 // insert before the anchor, if true, and after the anchor otherwise
152 // the anchor node to be used as a point of insertion
153 var oldCreator
= this._normalizedCreator
;
154 this._normalizedCreator = function(item
, hint
){
155 var t
= oldCreator
.call(this, item
, hint
);
158 this.anchor
= t
.node
;
159 this._removeItemClass(t
.node
, "Selected");
160 this._addItemClass(this.anchor
, "Anchor");
161 }else if(this.anchor
!= t
.node
){
162 this._removeItemClass(t
.node
, "Anchor");
163 this._addItemClass(t
.node
, "Selected");
165 this.selection
[t
.node
.id
] = 1;
167 this._removeItemClass(t
.node
, "Selected");
168 this._removeItemClass(t
.node
, "Anchor");
172 Selector
.superclass
.insertNodes
.call(this, data
, before
, anchor
);
173 this._normalizedCreator
= oldCreator
;
178 // prepares the object to be garbage-collected
179 Selector
.superclass
.destroy
.call(this);
180 this.selection
= this.anchor
= null;
184 onMouseDown: function(e
){
186 // event processor for onmousedown
189 if(this.autoSync
){ this.sync(); }
190 if(!this.current
){ return; }
191 if(!this.singular
&& !dnd
.getCopyKeyState(e
) && !e
.shiftKey
&& (this.current
.id
in this.selection
)){
192 this.simpleSelection
= true;
194 // Accept the left button and stop the event. Stopping the event prevents text selection while
195 // dragging. However, don't stop the event on mobile because that prevents a click event,
196 // and also prevents scroll (see #15838).
197 // For IE we don't stop event when multiple buttons are pressed.
202 if(!this.singular
&& e
.shiftKey
){
203 if(!dnd
.getCopyKeyState(e
)){
204 this._removeSelection();
206 var c
= this.getAllNodes();
210 this._addItemClass(this.anchor
, "Anchor");
212 this.selection
[this.anchor
.id
] = 1;
213 if(this.anchor
!= this.current
){
215 for(; i
< c
.length
; ++i
){
217 if(node
== this.anchor
|| node
== this.current
){ break; }
219 for(++i
; i
< c
.length
; ++i
){
221 if(node
== this.anchor
|| node
== this.current
){ break; }
222 this._addItemClass(node
, "Selected");
223 this.selection
[node
.id
] = 1;
225 this._addItemClass(this.current
, "Selected");
226 this.selection
[this.current
.id
] = 1;
231 if(this.anchor
== this.current
){
232 if(dnd
.getCopyKeyState(e
)){
237 this.anchor
= this.current
;
238 this._addItemClass(this.anchor
, "Anchor");
239 this.selection
[this.current
.id
] = 1;
242 if(dnd
.getCopyKeyState(e
)){
243 if(this.anchor
== this.current
){
244 delete this.selection
[this.anchor
.id
];
245 this._removeAnchor();
247 if(this.current
.id
in this.selection
){
248 this._removeItemClass(this.current
, "Selected");
249 delete this.selection
[this.current
.id
];
252 this._removeItemClass(this.anchor
, "Anchor");
253 this._addItemClass(this.anchor
, "Selected");
255 this.anchor
= this.current
;
256 this._addItemClass(this.current
, "Anchor");
257 this.selection
[this.current
.id
] = 1;
261 if(!(this.current
.id
in this.selection
)){
263 this.anchor
= this.current
;
264 this._addItemClass(this.current
, "Anchor");
265 this.selection
[this.current
.id
] = 1;
272 onMouseUp: function(/*===== e =====*/){
274 // event processor for onmouseup
277 if(!this.simpleSelection
){ return; }
278 this.simpleSelection
= false;
281 this.anchor
= this.current
;
282 this._addItemClass(this.anchor
, "Anchor");
283 this.selection
[this.current
.id
] = 1;
286 onMouseMove: function(/*===== e =====*/){
288 // event processor for onmousemove
291 this.simpleSelection
= false;
295 onOverEvent: function(){
297 // this function is called once, when mouse is over our container
298 this.onmousemoveEvent
= on(this.node
, touch
.move, lang
.hitch(this, "onMouseMove"));
300 onOutEvent: function(){
302 // this function is called once, when mouse is out of our container
303 if(this.onmousemoveEvent
){
304 this.onmousemoveEvent
.remove();
305 delete this.onmousemoveEvent
;
308 _removeSelection: function(){
310 // unselects all items
312 for(var i
in this.selection
){
313 if(i
in e
){ continue; }
314 var node
= dom
.byId(i
);
315 if(node
){ this._removeItemClass(node
, "Selected"); }
320 _removeAnchor: function(){
322 this._removeItemClass(this.anchor
, "Anchor");