Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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