forked from ibm-js/delite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection.js
286 lines (260 loc) · 9.28 KB
/
Selection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/** @module delite/Selection */
define(["dcl/dcl", "decor/sniff", "./Widget"], function (dcl, has, Widget) {
/**
* Selection change event. Dispatched after the selection has
* been modified through user interaction.
* @example
* widget.on("selection-change", function (evt) {
* console.log("old value: " + evt.oldValue);
* console.log("new value: " + evt.newValue);
* }
* @event module:delite/Selection#selection-change
* @property {number} oldValue - The previously selected item.
* @property {number} newValue- The new selected item.
* @property {Object} renderer - The visual renderer of the selected/deselected item.
* @property {Event} triggerEvent - The event that lead to the selection of the item.
*/
/**
* Mixin for widgets that manage a list of selected data items.
* @mixin module:delite/Selection
* @augments module:delite/Widget
*/
return dcl(Widget, /** @lends module:delite/Selection# */{
preRender: function () {
this._set("selectedItems", []);
},
/**
* The chosen selection mode.
*
* Valid values are:
*
* 1. "none": No selection can be done.
* 2. "single": Only one or zero items can be selected at a time. Interactively selecting a new item deselects
* the previously selected one.
* 3. "radio": Initially only one or zero items can be selected. Once an item has been selected, interactively
* selecting another item deselects the previously selected item, and the user cannot deselect the currently
* selected item.
* 4. "multiple": Multiple items can be selected. By default ctrl key must be used to select additional items.
* However that behavior might be specialized by subclasses.
*
* Changing this value impacts the current selected items to adapt the selection to the new mode. However
* whatever the selection mode is you can always set several selected items using the selectItem(s) API.
* The mode will be enforced only when using setSelected and/or selectFromEvent APIs.
*
* @member {string}
* @default "single"
*/
selectionMode: "single",
_setSelectionModeAttr: function (value) {
if (value !== "none" && value !== "single" && value !== "multiple" && value !== "radio") {
throw new TypeError("selectionMode invalid value");
}
if (value !== this.selectionMode) {
this._set("selectionMode", value);
if (value === "none") {
this.selectedItems = null;
} else if ((value === "single" || value === "radio") && this.selectedItem) {
this.selectedItems = [this.selectedItem];
}
}
},
/**
* In single selection mode, the selected item or in multiple selection mode the last selected item.
* @member {Object}
* @default null
*/
selectedItem: null,
_setSelectedItemAttr: function (value) {
if (this.selectedItem !== value) {
this.selectedItems = (value == null ? null : [value]);
}
},
/**
* The list of selected items.
* @member {Object[]}
* @default null
*/
selectedItems: null,
_setSelectedItemsAttr: function (value) {
var oldSelectedItems = this.selectedItems;
this._set("selectedItems", value);
if (oldSelectedItems != null && oldSelectedItems.length > 0) {
this.updateRenderers(oldSelectedItems);
}
if (this.selectedItems && this.selectedItems.length > 0) {
this._set("selectedItem", this.selectedItems[0]);
this.updateRenderers(this.selectedItems);
} else {
this._set("selectedItem", null);
}
},
_getSelectedItemsAttr: function () {
return this._get("selectedItems") == null ? [] : this._get("selectedItems").concat();
},
/**
* Tests if an event has a selection modifier.
*
* If it has a selection modifier, that means that:
*
* * if selectionMode is "single", the event will be able to deselect a selected item
* * if selectionMode is "multiple", the event will trigger the selection state of the item
*
* The default implementation of this method returns true if the event.ctrlKey attribute is
* true, which means that:
*
* * if selectionMode is "single", the Ctrl (or Command on MacOS) key must be pressed for the
* * event to deselect the currently selected item
* * if selectionMode is "multiple", the Ctrl (or Command on MacOS) key must be pressed for the
* event to toggle the selection status of the item.
*
* @param {Event} event - The event that led to the selection .
* @returns {boolean} Whether the event has selection modifier.
* @protected
*/
hasSelectionModifier: function (event) {
return !has("mac") ? event.ctrlKey : event.metaKey;
},
/**
* Returns whether an item is selected or not.
* @param {item} object The item to test.
* @returns {Object} The item to test the selection for.
*/
isSelected: function (item) {
if (this.selectedItems == null || this.selectedItems.length === 0) {
return false;
}
var identity = this.getIdentity(item);
return this.selectedItems.some(function (sitem) {
return this.getIdentity(sitem) === identity;
}, this);
},
/**
* This function must be implemented to return the id of a item.
* @param {item} object - The item the identity of must be returned.
* @returns {string} The identity of the item.
*/
getIdentity: function (/*jshint unused: vars */item) {
},
/**
* This function must be implemented to update the rendering of the items based on whether they are
* selected or not. The implementation must check for their new selection state and update
* accordingly.
* @param {Object[]} items - The array of items changing their selection state.
* @protected
*/
updateRenderers: function (/*jshint unused: vars */items) {
},
/**
* Change the selection state of an item.
* @param {Object} item - The item to change the selection state for.
* @param {boolean} value - True to select the item, false to deselect it.
*/
setSelected: function (item, value) {
if (this.selectionMode === "none" || item == null) {
return;
}
this._setSelected(item, value);
},
/* jshint maxcomplexity: 11*/
_setSelected: function (item, value) {
// copy is returned
var sel = this.selectedItems, res, identity;
if (this.selectionMode === "single" || this.selectionMode === "radio") {
if (value) {
this.selectedItem = item;
} else if (this.selectionMode === "single" && this.isSelected(item)) {
this.selectedItems = null;
}
} else { // multiple
if (value) {
if (this.isSelected(item)) {
return; // already selected
}
if (sel == null) {
sel = [item];
} else {
sel.unshift(item);
}
this.selectedItems = sel;
} else {
identity = this.getIdentity(item);
res = sel ? sel.filter(function (sitem) {
return this.getIdentity(sitem) !== identity;
}, this) : [];
if (res == null || res.length === sel.length) {
return; // already not selected
}
this.selectedItems = res;
}
}
},
/* jshint maxcomplexity: 10*/
/**
* Applies selection triggered by an user interaction.
* @param {Event} event - The source event of the user interaction.
* @param {Object} item - The item that has been selected/deselected.
* @param {Object} renderer - The visual renderer of the selected/deselected item.
* @param {boolean} dispatch - Whether an event must be dispatched or not.
* @returns {boolean} True if the selection has changed and false otherwise.
* @protected
*/
selectFromEvent: function (event, item, renderer, dispatch) {
if (this.selectionMode === "none") {
return false;
}
return this._selectFromEvent(event, item, renderer, dispatch);
},
_selectFromEvent: function (event, item, renderer, dispatch) {
var changed;
var oldSelectedItem = this.selectedItem;
var selected = item == null ? false : this.isSelected(item);
if (item == null) {
if ((this.selectionMode === "multiple" && !this.hasSelectionModifier(event))
&& this.selectedItem != null) {
this.selectedItem = null;
changed = true;
}
} else if (this.selectionMode === "multiple") {
if (this.hasSelectionModifier(event)) {
this.setSelected(item, !selected);
changed = true;
} else {
this.selectedItem = item;
changed = true;
}
} else { // single
if (this.selectionMode === "single" && this.hasSelectionModifier(event)) {
//if the object is selected deselects it.
this.selectedItem = (selected ? null : item);
changed = true;
} else {
if (!selected) {
this.selectedItem = item;
changed = true;
}
}
}
if (dispatch && changed) {
this.dispatchSelectionChange(oldSelectedItem, this.selectedItem, renderer, event);
}
return changed;
},
/**
* Dispatch a selection change event.
* @param {Object} oldSelectedItem - The previously selected item.
* @param {Object} newSelectedItem - The new selected item.
* @param {Object} renderer - The visual renderer of the selected/deselected item.
* @param {Event} triggerEvent - The event that lead to the selection of the item.
* @protected
* @fires module:delite/Selection#selection-change
*/
dispatchSelectionChange: function (oldSelectedItem, newSelectedItem, renderer, triggerEvent) {
this.emit("selection-change", {
oldValue: oldSelectedItem,
newValue: newSelectedItem,
renderer: renderer,
triggerEvent: triggerEvent
});
}
});
});