Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 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");
4
 
5
dojo.require("dojo.dnd.common");
6
dojo.require("dojo.dnd.Container");
7
 
8
/*
9
	Container item states:
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
13
*/
14
 
15
dojo.declare("dojo.dnd.Selector", dojo.dnd.Container, {
16
	// summary: a Selector object, which knows how to select its children
17
 
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
27
		this.selection = {};
28
		this.anchor = null;
29
		this.simpleSelection = false;
30
		// set up events
31
		this.events.push(
32
			dojo.connect(this.node, "onmousedown", this, "onMouseDown"),
33
			dojo.connect(this.node, "onmouseup",   this, "onMouseUp"));
34
	},
35
 
36
	// object attributes (for markup)
37
	singular: false,	// is singular property
38
 
39
	// methods
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; }
46
			t.push(dojo.byId(i));
47
		}
48
		return t;	// Array
49
	},
50
	selectNone: function(){
51
		// summary: unselects all items
52
		return this._removeSelection()._removeAnchor();	// self
53
	},
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;
59
		}, this);
60
		return this._removeAnchor();	// self
61
	},
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; }
67
			var n = dojo.byId(i);
68
			this.delItem(i);
69
			dojo._destroyElement(n);
70
		}
71
		this.anchor = null;
72
		this.selection = {};
73
		return this;	// self
74
	},
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);
84
			if(addSelected){
85
				if(!this.anchor){
86
					this.anchor = t.node;
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");
92
				}
93
				this.selection[t.node.id] = 1;
94
			}else{
95
				this._removeItemClass(t.node, "Selected");
96
				this._removeItemClass(t.node, "Anchor");
97
			}
98
			return t;
99
		};
100
		dojo.dnd.Selector.superclass.insertNodes.call(this, data, before, anchor);
101
		this._normalizedCreator = oldCreator;
102
		return this;	// self
103
	},
104
	destroy: function(){
105
		// summary: prepares the object to be garbage-collected
106
		dojo.dnd.Selector.superclass.destroy.call(this);
107
		this.selection = this.anchor = null;
108
	},
109
 
110
	// markup methods
111
	markupFactory: function(params, node){
112
		params._skipStartup = true;
113
		return new dojo.dnd.Selector(node, params);
114
	},
115
 
116
	// mouse events
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;
123
			dojo.stopEvent(e);
124
			return;
125
		}
126
		if(!this.singular && e.shiftKey){
127
			if(!dojo.dnd.getCopyKeyState(e)){
128
				this._removeSelection();
129
			}
130
			var c = dojo.query("> .dojoDndItem", this.parent);
131
			if(c.length){
132
				if(!this.anchor){
133
					this.anchor = c[0];
134
					this._addItemClass(this.anchor, "Anchor");
135
				}
136
				this.selection[this.anchor.id] = 1;
137
				if(this.anchor != this.current){
138
					var i = 0;
139
					for(; i < c.length; ++i){
140
						var node = c[i];
141
						if(node == this.anchor || node == this.current){ break; }
142
					}
143
					for(++i; i < c.length; ++i){
144
						var node = c[i];
145
						if(node == this.anchor || node == this.current){ break; }
146
						this._addItemClass(node, "Selected");
147
						this.selection[node.id] = 1;
148
					}
149
					this._addItemClass(this.current, "Selected");
150
					this.selection[this.current.id] = 1;
151
				}
152
			}
153
		}else{
154
			if(this.singular){
155
				if(this.anchor == this.current){
156
					if(dojo.dnd.getCopyKeyState(e)){
157
						this.selectNone();
158
					}
159
				}else{
160
					this.selectNone();
161
					this.anchor = this.current;
162
					this._addItemClass(this.anchor, "Anchor");
163
					this.selection[this.current.id] = 1;
164
				}
165
			}else{
166
				if(dojo.dnd.getCopyKeyState(e)){
167
					if(this.anchor == this.current){
168
						delete this.selection[this.anchor.id];
169
						this._removeAnchor();
170
					}else{
171
						if(this.current.id in this.selection){
172
							this._removeItemClass(this.current, "Selected");
173
							delete this.selection[this.current.id];
174
						}else{
175
							if(this.anchor){
176
								this._removeItemClass(this.anchor, "Anchor");
177
								this._addItemClass(this.anchor, "Selected");
178
							}
179
							this.anchor = this.current;
180
							this._addItemClass(this.current, "Anchor");
181
							this.selection[this.current.id] = 1;
182
						}
183
					}
184
				}else{
185
					if(!(this.current.id in this.selection)){
186
						this.selectNone();
187
						this.anchor = this.current;
188
						this._addItemClass(this.current, "Anchor");
189
						this.selection[this.current.id] = 1;
190
					}
191
				}
192
			}
193
		}
194
		dojo.stopEvent(e);
195
	},
196
	onMouseUp: function(e){
197
		// summary: event processor for onmouseup
198
		// e: Event: mouse event
199
		if(!this.simpleSelection){ return; }
200
		this.simpleSelection = false;
201
		this.selectNone();
202
		if(this.current){
203
			this.anchor = this.current;
204
			this._addItemClass(this.anchor, "Anchor");
205
			this.selection[this.current.id] = 1;
206
		}
207
	},
208
	onMouseMove: function(e){
209
		// summary: event processor for onmousemove
210
		// e: Event: mouse event
211
		this.simpleSelection = false;
212
	},
213
 
214
	// utilities
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");
218
	},
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;
223
	},
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"); }
231
		}
232
		this.selection = {};
233
		return this;	// self
234
	},
235
	_removeAnchor: function(){
236
		if(this.anchor){
237
			this._removeItemClass(this.anchor, "Anchor");
238
			this.anchor = null;
239
		}
240
		return this;	// self
241
	}
242
});
243
 
244
}