-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-item-selection.html
More file actions
322 lines (269 loc) · 8.18 KB
/
basic-item-selection.html
File metadata and controls
322 lines (269 loc) · 8.18 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!--
Aspect which manages selection semantics for items in a list.
@element basic-item-selection
-->
<link rel="import" href="../basic-aspect/basic-aspect.html">
<script>
/**
* Fires when the selectedItem property changes.
*
* @event selected-item-changed
* @param detail.selectedItem The new selected item.
* @param detail.previousItem The previously selected item.
*/
/**
* Fires when the selectedIndex property changes.
*
* @event selected-item-changed
* @param detail.selectedIndex The new selected index.
*/
window.Basic = window.Basic || {};
window.Basic.ItemSelection = {
contribute: {
// Default implementations. These will typically be handled by other aspects
// in the collective.
applySelection: function(item, selected) {},
get canSelectNext() {
return this._canSelectNext;
},
set canSelectNext(canSelectNext) {
this._canSelectNext = canSelectNext;
},
get canSelectPrevious() {
return this._canSelectPrevious;
},
set canSelectPrevious(canSelectPrevious) {
this._canSelectPrevious = canSelectPrevious;
},
itemAdded: function(item) {
this.collective.applySelection(item, item === this.collective.selectedItem);
},
itemsChanged: function() {
var index = this.collective.items.indexOf(this.collective.selectedItem);
if (index < 0) {
// Selected item is no longer in the current set of items.
this.collective.selectedItem = null;
if (this.collective.selectionRequired) {
// Ensure selection, but do this in the next tick to give other
// aspects a chance to do their own itemsChanged work.
setTimeout(function() {
this._ensureSelection();
}.bind(this));
}
}
},
get selectedItem() {
return this._selectedItem;
},
/**
* The currently selected item, or null if there is no selection.
*
* @property selectedItem
* @type Object
*/
// TODO: Confirm item is in items before selecting.
set selectedItem(item) {
var previousItem = this._selectedItem;
if (previousItem) {
// Remove previous selection.
this.collective.applySelection(previousItem, false);
}
this._selectedItem = item;
if (item) {
this.collective.applySelection(item, true);
}
// TODO: Rationalize with selectedIndex so we're not recalculating item
// or index in each setter.
var index = this.collective.indexOfItem(item);
this._updatePossibleNavigations(index);
var outermost = this.collective.outermostAttached;
if (outermost) {
var event = new CustomEvent('selected-item-changed', {
bubbles: true,
detail: {
selectedItem: item,
previousItem: previousItem,
value: item // for Polymer binding
}
});
outermost.dispatchEvent(event);
}
},
/**
* The index of the item which is currently selected, or -1 if there is no
* selection.
*
* @property selectedIndex
* @type Number
*/
get selectedIndex() {
var selectedItem = this.collective.selectedItem;
if (selectedItem == null) {
return -1;
}
// TODO: Memoize
var index = this.collective.indexOfItem(selectedItem);
// If index = -1, selection wasn't found. Most likely cause is that the
// DOM was manipulated from underneath us.
// TODO: Once we track content changes, turn this into an exception.
return index;
},
set selectedIndex(index) {
var items = this.collective.items;
var item;
if (index < 0 || items.length === 0) {
item = null;
} else {
item = items[index];
}
this.collective.selectedItem = item;
var outermost = this.collective.outermostAttached;
if (outermost) {
var event = new CustomEvent('selected-index-changed', {
bubbles: true,
detail: {
selectedIndex: index,
value: index // for Polymer binding
}
});
outermost.dispatchEvent(event);
}
},
/**
* Select the first item in the list.
*
* @method selectFirst
*/
selectFirst: function() {
return this._selectIndex(0);
},
/**
* True if the list should always have a selection (if it has items).
*
* @property selectionRequired
* @type Boolean
*/
get selectionRequired() {
return this._selectionRequired;
},
set selectionRequired(selectionRequired) {
this._selectionRequired = selectionRequired;
this._ensureSelection();
},
/**
* Select the last item in the list.
*
* @method selectLast
*/
selectLast: function() {
return this._selectIndex(this.collective.items.length - 1);
},
/**
* Select the next item in the list.
*
* @method selectNext
*/
selectNext: function() {
return this._selectIndex(this.collective.selectedIndex + 1);
},
/**
* Select the previous item in the list.
*
* @method selectPrevious
*/
selectPrevious: function() {
return this._selectIndex(this.collective.selectedIndex - 1);
}
},
// If no item is selected, select a default item.
// TODO: If the previously-selected item has been deleted, try to select an
// item adjacent to the position it held.
_ensureSelection: function() {
if (!this.collective.selectedItem && this.collective.items && this.collective.items.length > 0) {
this.collective.selectedIndex = 0;
}
},
name: 'ItemSelection',
// Ensure the given index is within bounds, and select it if it's not already
// selected.
_selectIndex: function(index) {
var boundedIndex = Math.max(Math.min(index, this.collective.items.length - 1), 0);
var previousIndex = this.collective.selectedIndex;
if (previousIndex !== boundedIndex) {
this.collective.selectedIndex = boundedIndex;
return true;
} else {
return false;
}
},
// Following a change in selection, report whether it's now possible to
// go next/previous from the given index.
_updatePossibleNavigations: function(index) {
var canSelectNext;
var canSelectPrevious;
var items = this.collective.items;
if (items == null || items.length === 0) {
canSelectNext = false;
canSelectPrevious = false;
} else if (items.length === 1) {
// Special case. If there's no selection, we declare that it's always
// possible to go next/previous to create a selection.
canSelectNext = true;
canSelectPrevious = true;
} else {
// Normal case: we have an index in a list that has items.
canSelectPrevious = (index > 0);
canSelectNext = (index < items.length - 1);
}
this.collective.canSelectNext = canSelectNext;
this.collective.canSelectPrevious = canSelectPrevious;
},
_selectedItem: null
};
Polymer({
aspects: [Basic.ItemSelection],
behaviors: [Basic.Aspect],
is: 'basic-item-selection',
properties: {
selectedIndex: {
type: Number
},
selectedItem: {
type: Object
},
selectionRequired: {
type: Boolean,
observer: 'selectionRequiredChanged',
value: false
}
},
selectFirst: function() {
this.collective.selectFirst();
},
selectionRequiredChanged: function(selectionRequired) {
this.collective.selectionRequired = selectionRequired;
},
selectLast: function() {
this.collective.selectLast();
},
selectNext: function() {
this.collective.selectNext();
},
selectPrevious: function() {
this.collective.selectPrevious();
},
get selectedIndex() {
// HACK: Proxied getter/setter properties like this one can't be set via
// attributes. See https://github.com/Polymer/polymer/issues/2454. We
// currently hack around this by only returning a value for this property if
// the element is ready. A negative side effect is that inspecting this
// property before the element is ready will always return undefined.
if (this._readied) {
return this.collective.selectedIndex;
}
},
get selectedItem() {
return this.collective.selectedItem;
}
});
</script>