Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/ui/selectionmodel.js
4503 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Single-selection model implemenation.
9
*
10
* TODO(attila): Add keyboard & mouse event hooks?
11
* TODO(attila): Add multiple selection?
12
*/
13
14
15
goog.provide('goog.ui.SelectionModel');
16
17
goog.require('goog.array');
18
goog.require('goog.events.EventTarget');
19
goog.require('goog.events.EventType');
20
21
22
23
/**
24
* Single-selection model. Dispatches a {@link goog.events.EventType.SELECT}
25
* event when a selection is made.
26
* @param {Array<Object>=} opt_items Array of items; defaults to empty.
27
* @extends {goog.events.EventTarget}
28
* @constructor
29
*/
30
goog.ui.SelectionModel = function(opt_items) {
31
'use strict';
32
goog.events.EventTarget.call(this);
33
34
/**
35
* Array of items controlled by the selection model. If the items support
36
* the `setSelected(Boolean)` interface, they will be (de)selected
37
* as needed.
38
* @type {!Array<Object>}
39
* @private
40
*/
41
this.items_ = [];
42
this.addItems(opt_items);
43
};
44
goog.inherits(goog.ui.SelectionModel, goog.events.EventTarget);
45
46
47
/**
48
* The currently selected item (null if none).
49
* @type {?Object}
50
* @private
51
*/
52
goog.ui.SelectionModel.prototype.selectedItem_ = null;
53
54
55
/**
56
* Selection handler function. Called with two arguments (the item to be
57
* selected or deselected, and a Boolean indicating whether the item is to
58
* be selected or deselected).
59
* @type {?Function}
60
* @private
61
*/
62
goog.ui.SelectionModel.prototype.selectionHandler_ = null;
63
64
65
/**
66
* Returns the selection handler function used by the selection model to change
67
* the internal selection state of items under its control.
68
* @return {Function} Selection handler function (null if none).
69
*/
70
goog.ui.SelectionModel.prototype.getSelectionHandler = function() {
71
'use strict';
72
return this.selectionHandler_;
73
};
74
75
76
/**
77
* Sets the selection handler function to be used by the selection model to
78
* change the internal selection state of items under its control. The
79
* function must take two arguments: an item and a Boolean to indicate whether
80
* the item is to be selected or deselected. Selection handler functions are
81
* only needed if the items in the selection model don't natively support the
82
* `setSelected(Boolean)` interface.
83
* @param {Function} handler Selection handler function.
84
*/
85
goog.ui.SelectionModel.prototype.setSelectionHandler = function(handler) {
86
'use strict';
87
this.selectionHandler_ = handler;
88
};
89
90
91
/**
92
* Returns the number of items controlled by the selection model.
93
* @return {number} Number of items.
94
*/
95
goog.ui.SelectionModel.prototype.getItemCount = function() {
96
'use strict';
97
return this.items_.length;
98
};
99
100
101
/**
102
* Returns the 0-based index of the given item within the selection model, or
103
* -1 if no such item is found.
104
* @param {Object|undefined} item Item to look for.
105
* @return {number} Index of the given item (-1 if none).
106
*/
107
goog.ui.SelectionModel.prototype.indexOfItem = function(item) {
108
'use strict';
109
return item ? this.items_.indexOf(item) : -1;
110
};
111
112
113
/**
114
* @return {Object|undefined} The first item, or undefined if there are no items
115
* in the model.
116
*/
117
goog.ui.SelectionModel.prototype.getFirst = function() {
118
'use strict';
119
return this.items_[0];
120
};
121
122
123
/**
124
* @return {Object|undefined} The last item, or undefined if there are no items
125
* in the model.
126
*/
127
goog.ui.SelectionModel.prototype.getLast = function() {
128
'use strict';
129
return this.items_[this.items_.length - 1];
130
};
131
132
133
/**
134
* Returns the item at the given 0-based index.
135
* @param {number} index Index of the item to return.
136
* @return {Object} Item at the given index (null if none).
137
*/
138
goog.ui.SelectionModel.prototype.getItemAt = function(index) {
139
'use strict';
140
return this.items_[index] || null;
141
};
142
143
144
/**
145
* Bulk-adds items to the selection model. This is more efficient than calling
146
* {@link #addItem} for each new item.
147
* @param {Array<Object>|undefined} items New items to add.
148
*/
149
goog.ui.SelectionModel.prototype.addItems = function(items) {
150
'use strict';
151
if (items) {
152
// New items shouldn't be selected.
153
items.forEach(function(item) {
154
'use strict';
155
this.selectItem_(item, false);
156
}, this);
157
goog.array.extend(this.items_, items);
158
}
159
};
160
161
162
/**
163
* Adds an item at the end of the list.
164
* @param {Object} item Item to add.
165
*/
166
goog.ui.SelectionModel.prototype.addItem = function(item) {
167
'use strict';
168
this.addItemAt(item, this.getItemCount());
169
};
170
171
172
/**
173
* Adds an item at the given index.
174
* @param {Object} item Item to add.
175
* @param {number} index Index at which to add the new item.
176
*/
177
goog.ui.SelectionModel.prototype.addItemAt = function(item, index) {
178
'use strict';
179
if (item) {
180
// New items must not be selected.
181
this.selectItem_(item, false);
182
goog.array.insertAt(this.items_, item, index);
183
}
184
};
185
186
187
/**
188
* Removes the given item (if it exists). Dispatches a `SELECT` event if
189
* the removed item was the currently selected item.
190
* @param {Object} item Item to remove.
191
*/
192
goog.ui.SelectionModel.prototype.removeItem = function(item) {
193
'use strict';
194
if (item && goog.array.remove(this.items_, item)) {
195
if (item == this.selectedItem_) {
196
this.selectedItem_ = null;
197
this.dispatchEvent(goog.events.EventType.SELECT);
198
}
199
}
200
};
201
202
203
/**
204
* Removes the item at the given index.
205
* @param {number} index Index of the item to remove.
206
*/
207
goog.ui.SelectionModel.prototype.removeItemAt = function(index) {
208
'use strict';
209
this.removeItem(this.getItemAt(index));
210
};
211
212
213
/**
214
* @return {Object} The currently selected item, or null if none.
215
*/
216
goog.ui.SelectionModel.prototype.getSelectedItem = function() {
217
'use strict';
218
return this.selectedItem_;
219
};
220
221
222
/**
223
* @return {!Array<Object>} All items in the selection model.
224
*/
225
goog.ui.SelectionModel.prototype.getItems = function() {
226
'use strict';
227
return goog.array.clone(this.items_);
228
};
229
230
231
/**
232
* Selects the given item, deselecting any previously selected item, and
233
* dispatches a `SELECT` event.
234
* @param {Object} item Item to select (null to clear the selection).
235
*/
236
goog.ui.SelectionModel.prototype.setSelectedItem = function(item) {
237
'use strict';
238
if (item != this.selectedItem_) {
239
this.selectItem_(this.selectedItem_, false);
240
this.selectedItem_ = item;
241
this.selectItem_(item, true);
242
}
243
244
// Always dispatch a SELECT event; let listeners decide what to do if the
245
// selected item hasn't changed.
246
this.dispatchEvent(goog.events.EventType.SELECT);
247
};
248
249
250
/**
251
* @return {number} The 0-based index of the currently selected item, or -1
252
* if none.
253
*/
254
goog.ui.SelectionModel.prototype.getSelectedIndex = function() {
255
'use strict';
256
return this.indexOfItem(this.selectedItem_);
257
};
258
259
260
/**
261
* Selects the item at the given index, deselecting any previously selected
262
* item, and dispatches a `SELECT` event.
263
* @param {number} index Index to select (-1 to clear the selection).
264
*/
265
goog.ui.SelectionModel.prototype.setSelectedIndex = function(index) {
266
'use strict';
267
this.setSelectedItem(this.getItemAt(index));
268
};
269
270
271
/**
272
* Clears the selection model by removing all items from the selection.
273
*/
274
goog.ui.SelectionModel.prototype.clear = function() {
275
'use strict';
276
goog.array.clear(this.items_);
277
this.selectedItem_ = null;
278
};
279
280
281
/** @override */
282
goog.ui.SelectionModel.prototype.disposeInternal = function() {
283
'use strict';
284
goog.ui.SelectionModel.superClass_.disposeInternal.call(this);
285
delete this.items_;
286
this.selectedItem_ = null;
287
};
288
289
290
/**
291
* Private helper; selects or deselects the given item based on the value of
292
* the `select` argument. If a selection handler has been registered
293
* (via {@link #setSelectionHandler}, calls it to update the internal selection
294
* state of the item. Otherwise, attempts to call `setSelected(Boolean)`
295
* on the item itself, provided the object supports that interface.
296
* @param {Object} item Item to select or deselect.
297
* @param {boolean} select If true, the object will be selected; if false, it
298
* will be deselected.
299
* @private
300
*/
301
goog.ui.SelectionModel.prototype.selectItem_ = function(item, select) {
302
'use strict';
303
if (item) {
304
if (typeof this.selectionHandler_ == 'function') {
305
// Use the registered selection handler function.
306
this.selectionHandler_(item, select);
307
} else if (typeof item.setSelected == 'function') {
308
// Call setSelected() on the item, if it supports it.
309
item.setSelected(select);
310
}
311
}
312
};
313
314