Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.selection.Selection");
12
dojo.require("dojo.lang.array");
13
dojo.require("dojo.lang.func");
14
dojo.require("dojo.lang.common");
15
dojo.require("dojo.math");
16
dojo.declare("dojo.selection.Selection", null, {initializer:function (items, isCollection) {
17
	this.items = [];
18
	this.selection = [];
19
	this._pivotItems = [];
20
	this.clearItems();
21
	if (items) {
22
		if (isCollection) {
23
			this.setItemsCollection(items);
24
		} else {
25
			this.setItems(items);
26
		}
27
	}
28
}, items:null, selection:null, lastSelected:null, allowImplicit:true, length:0, isGrowable:true, _pivotItems:null, _pivotItem:null, onSelect:function (item) {
29
}, onDeselect:function (item) {
30
}, onSelectChange:function (item, selected) {
31
}, _find:function (item, inSelection) {
32
	if (inSelection) {
33
		return dojo.lang.find(this.selection, item);
34
	} else {
35
		return dojo.lang.find(this.items, item);
36
	}
37
}, isSelectable:function (item) {
38
	return true;
39
}, setItems:function () {
40
	this.clearItems();
41
	this.addItems.call(this, arguments);
42
}, setItemsCollection:function (collection) {
43
	this.items = collection;
44
}, addItems:function () {
45
	var args = dojo.lang.unnest(arguments);
46
	for (var i = 0; i < args.length; i++) {
47
		this.items.push(args[i]);
48
	}
49
}, addItemsAt:function (item, before) {
50
	if (this.items.length == 0) {
51
		return this.addItems(dojo.lang.toArray(arguments, 2));
52
	}
53
	if (!this.isItem(item)) {
54
		item = this.items[item];
55
	}
56
	if (!item) {
57
		throw new Error("addItemsAt: item doesn't exist");
58
	}
59
	var idx = this._find(item);
60
	if (idx > 0 && before) {
61
		idx--;
62
	}
63
	for (var i = 2; i < arguments.length; i++) {
64
		if (!this.isItem(arguments[i])) {
65
			this.items.splice(idx++, 0, arguments[i]);
66
		}
67
	}
68
}, removeItem:function (item) {
69
	var idx = this._find(item);
70
	if (idx > -1) {
71
		this.items.splice(idx, 1);
72
	}
73
	idx = this._find(item, true);
74
	if (idx > -1) {
75
		this.selection.splice(idx, 1);
76
	}
77
}, clearItems:function () {
78
	this.items = [];
79
	this.deselectAll();
80
}, isItem:function (item) {
81
	return this._find(item) > -1;
82
}, isSelected:function (item) {
83
	return this._find(item, true) > -1;
84
}, selectFilter:function (item, selection, add, grow) {
85
	return true;
86
}, update:function (item, add, grow, noToggle) {
87
	if (!this.isItem(item)) {
88
		return false;
89
	}
90
	if (this.isGrowable && grow) {
91
		if ((!this.isSelected(item)) && this.selectFilter(item, this.selection, false, true)) {
92
			this.grow(item);
93
			this.lastSelected = item;
94
		}
95
	} else {
96
		if (add) {
97
			if (this.selectFilter(item, this.selection, true, false)) {
98
				if (noToggle) {
99
					if (this.select(item)) {
100
						this.lastSelected = item;
101
					}
102
				} else {
103
					if (this.toggleSelected(item)) {
104
						this.lastSelected = item;
105
					}
106
				}
107
			}
108
		} else {
109
			this.deselectAll();
110
			this.select(item);
111
		}
112
	}
113
	this.length = this.selection.length;
114
	return true;
115
}, grow:function (toItem, fromItem) {
116
	if (!this.isGrowable) {
117
		return;
118
	}
119
	if (arguments.length == 1) {
120
		fromItem = this._pivotItem;
121
		if (!fromItem && this.allowImplicit) {
122
			fromItem = this.items[0];
123
		}
124
	}
125
	if (!toItem || !fromItem) {
126
		return false;
127
	}
128
	var fromIdx = this._find(fromItem);
129
	var toDeselect = {};
130
	var lastIdx = -1;
131
	if (this.lastSelected) {
132
		lastIdx = this._find(this.lastSelected);
133
		var step = fromIdx < lastIdx ? -1 : 1;
134
		var range = dojo.math.range(lastIdx, fromIdx, step);
135
		for (var i = 0; i < range.length; i++) {
136
			toDeselect[range[i]] = true;
137
		}
138
	}
139
	var toIdx = this._find(toItem);
140
	var step = fromIdx < toIdx ? -1 : 1;
141
	var shrink = lastIdx >= 0 && step == 1 ? lastIdx < toIdx : lastIdx > toIdx;
142
	var range = dojo.math.range(toIdx, fromIdx, step);
143
	if (range.length) {
144
		for (var i = range.length - 1; i >= 0; i--) {
145
			var item = this.items[range[i]];
146
			if (this.selectFilter(item, this.selection, false, true)) {
147
				if (this.select(item, true) || shrink) {
148
					this.lastSelected = item;
149
				}
150
				if (range[i] in toDeselect) {
151
					delete toDeselect[range[i]];
152
				}
153
			}
154
		}
155
	} else {
156
		this.lastSelected = fromItem;
157
	}
158
	for (var i in toDeselect) {
159
		if (this.items[i] == this.lastSelected) {
160
		}
161
		this.deselect(this.items[i]);
162
	}
163
	this._updatePivot();
164
}, growUp:function () {
165
	if (!this.isGrowable) {
166
		return;
167
	}
168
	var idx = this._find(this.lastSelected) - 1;
169
	while (idx >= 0) {
170
		if (this.selectFilter(this.items[idx], this.selection, false, true)) {
171
			this.grow(this.items[idx]);
172
			break;
173
		}
174
		idx--;
175
	}
176
}, growDown:function () {
177
	if (!this.isGrowable) {
178
		return;
179
	}
180
	var idx = this._find(this.lastSelected);
181
	if (idx < 0 && this.allowImplicit) {
182
		this.select(this.items[0]);
183
		idx = 0;
184
	}
185
	idx++;
186
	while (idx > 0 && idx < this.items.length) {
187
		if (this.selectFilter(this.items[idx], this.selection, false, true)) {
188
			this.grow(this.items[idx]);
189
			break;
190
		}
191
		idx++;
192
	}
193
}, toggleSelected:function (item, noPivot) {
194
	if (this.isItem(item)) {
195
		if (this.select(item, noPivot)) {
196
			return 1;
197
		}
198
		if (this.deselect(item)) {
199
			return -1;
200
		}
201
	}
202
	return 0;
203
}, select:function (item, noPivot) {
204
	if (this.isItem(item) && !this.isSelected(item) && this.isSelectable(item)) {
205
		this.selection.push(item);
206
		this.lastSelected = item;
207
		this.onSelect(item);
208
		this.onSelectChange(item, true);
209
		if (!noPivot) {
210
			this._addPivot(item);
211
		}
212
		this.length = this.selection.length;
213
		return true;
214
	}
215
	return false;
216
}, deselect:function (item) {
217
	var idx = this._find(item, true);
218
	if (idx > -1) {
219
		this.selection.splice(idx, 1);
220
		this.onDeselect(item);
221
		this.onSelectChange(item, false);
222
		if (item == this.lastSelected) {
223
			this.lastSelected = null;
224
		}
225
		this._removePivot(item);
226
		this.length = this.selection.length;
227
		return true;
228
	}
229
	return false;
230
}, selectAll:function () {
231
	for (var i = 0; i < this.items.length; i++) {
232
		this.select(this.items[i]);
233
	}
234
}, deselectAll:function () {
235
	while (this.selection && this.selection.length) {
236
		this.deselect(this.selection[0]);
237
	}
238
}, selectNext:function () {
239
	var idx = this._find(this.lastSelected);
240
	while (idx > -1 && ++idx < this.items.length) {
241
		if (this.isSelectable(this.items[idx])) {
242
			this.deselectAll();
243
			this.select(this.items[idx]);
244
			return true;
245
		}
246
	}
247
	return false;
248
}, selectPrevious:function () {
249
	var idx = this._find(this.lastSelected);
250
	while (idx-- > 0) {
251
		if (this.isSelectable(this.items[idx])) {
252
			this.deselectAll();
253
			this.select(this.items[idx]);
254
			return true;
255
		}
256
	}
257
	return false;
258
}, selectFirst:function () {
259
	this.deselectAll();
260
	var idx = 0;
261
	while (this.items[idx] && !this.select(this.items[idx])) {
262
		idx++;
263
	}
264
	return this.items[idx] ? true : false;
265
}, selectLast:function () {
266
	this.deselectAll();
267
	var idx = this.items.length - 1;
268
	while (this.items[idx] && !this.select(this.items[idx])) {
269
		idx--;
270
	}
271
	return this.items[idx] ? true : false;
272
}, _addPivot:function (item, andClear) {
273
	this._pivotItem = item;
274
	if (andClear) {
275
		this._pivotItems = [item];
276
	} else {
277
		this._pivotItems.push(item);
278
	}
279
}, _removePivot:function (item) {
280
	var i = dojo.lang.find(this._pivotItems, item);
281
	if (i > -1) {
282
		this._pivotItems.splice(i, 1);
283
		this._pivotItem = this._pivotItems[this._pivotItems.length - 1];
284
	}
285
	this._updatePivot();
286
}, _updatePivot:function () {
287
	if (this._pivotItems.length == 0) {
288
		if (this.lastSelected) {
289
			this._addPivot(this.lastSelected);
290
		}
291
	}
292
}, sorted:function () {
293
	return dojo.lang.toArray(this.selection).sort(dojo.lang.hitch(this, function (a, b) {
294
		var A = this._find(a), B = this._find(b);
295
		if (A > B) {
296
			return 1;
297
		} else {
298
			if (A < B) {
299
				return -1;
300
			} else {
301
				return 0;
302
			}
303
		}
304
	}));
305
}, updateSelected:function () {
306
	for (var i = 0; i < this.selection.length; i++) {
307
		if (this._find(this.selection[i]) < 0) {
308
			var removed = this.selection.splice(i, 1);
309
			this._removePivot(removed[0]);
310
		}
311
	}
312
	this.length = this.selection.length;
313
}});
314