Subversion Repositories Applications.papyrus

Rev

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