-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathFlyweightRepeater.js
498 lines (456 loc) · 12.2 KB
/
FlyweightRepeater.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
* Contains the declaration for the {@link module:layout/FlyweightRepeater~FlyweightRepeater} kind.
* @module layout/FlyweightRepeater
*/
var
kind = require('enyo/kind'),
dom = require('enyo/dom'),
utils = require('enyo/utils'),
Control = require('enyo/Control'),
HTMLStringDelegate = require('enyo/HTMLStringDelegate'),
Selection = require('enyo/Selection');
var FlyweightRepeaterDelegate = Object.create(HTMLStringDelegate);
FlyweightRepeaterDelegate.generateInnerHtml = function (control) {
var h = '';
control.index = null;
// note: can supply a rowOffset
// and indicate if rows should be rendered top down or bottomUp
for (var i=0, r=0; i<control.count; i++) {
r = control.rowOffset + (this.bottomUp ? control.count - i-1 : i);
control.setupItem(r);
control.$.client.setAttribute('data-enyo-index', r);
if (control.orient == 'h') {
control.$.client.setStyle('display:inline-block;');
}
h += HTMLStringDelegate.generateChildHtml(control);
control.$.client.teardownRender();
}
return h;
};
/**
* Fires once per row at render time.
*
* @event module:layout/FlyweightRepeater~FlyweightRepeater#onSetupItem
* @type {Object}
* @property {Number} index - The index of the row being rendered.
* @property {Boolean} selected - `true` if the row is selected; otherwise, `false`.
* @public
*/
/**
* Fires after an individual row has been rendered.
*
* @event module:layout/FlyweightRepeater~FlyweightRepeater#onRenderRow
* @type {Object}
* @property {Number} rowIndex - The index of the row that was rendered.
* @public
*/
/**
* {@link module:layout/FlyweightRepeater~FlyweightRepeater} is a control that displays a repeating list of
* rows, suitable for displaying medium-sized lists (up to ~100 items). A
* flyweight strategy is employed to render one set of row controls, as needed,
* for as many rows as are contained in the repeater.
*
* The FlyweightRepeater's `components` block contains the controls to be used
* for a single row. This set of controls will be rendered for each row. You
* may customize row rendering by handling the
* [onSetupItem]{@link module:layout/FlyweightRepeater~FlyweightRepeater#onSetupItem} event.
*
* The controls inside a FlyweightRepeater are non-interactive. This means that
* calling methods that would normally cause rendering to occur (e.g.,
* `set('content', <value>)`) will not do so. However, you may force a row to
* render by calling [renderRow()]{@link module:layout/FlyweightRepeater~FlyweightRepeater#renderRow}.
*
* In addition, you may force a row to be temporarily interactive by calling
* [prepareRow()]{@link module:layout/FlyweightRepeater~FlyweightRepeater#prepareRow}. Call
* [lockRow()]{@link module:layout/FlyweightRepeater~FlyweightRepeater#lockRow} when the interaction
* is complete.
*
* For more information, see the documentation on
* [Lists]{@linkplain $dev-guide/building-apps/layout/lists.html} in the
* Enyo Developer Guide.
*
* @class FlyweightRepeater
* @extends module:enyo/Control~Control
* @ui
* @public
*/
var FlyweightRepeater = module.exports = kind(
/** @lends module:layout/FlyweightRepeater~FlyweightRepeater.prototype */ {
/**
* @private
*/
name: 'enyo.FlyweightRepeater',
/**
* @private
*/
kind: Control,
/**
* @lends module:layout/FlyweightRepeater~FlyweightRepeater.prototype
* @private
*/
published: {
/**
* The number of rows to render.
*
* @type {Number}
* @default 0
* @public
*/
count: 0,
/**
* If `true`, the selection mechanism is disabled. Tap events are still
* sent, but items won't be automatically re-rendered when tapped.
*
* @type {Boolean}
* @default false
* @public
*/
noSelect: false,
/**
* If `true`, multiple selection is allowed.
*
* @type {Boolean}
* @default false
* @public
*/
multiSelect: false,
/**
* If `true`, the selected item will toggle.
*
* @type {Boolean}
* @default false
* @public
*/
toggleSelected: false,
/**
* Used to specify CSS classes for the repeater's wrapper component (client).
* Input is identical to that of {@link module:enyo/Control~Control#setClasses}.
*
* @type {String}
* @default ''
* @public
*/
clientClasses: '',
/**
* Used to specify custom styling for the repeater's wrapper component
* (client). Input is identical to that of {@link module:enyo/Control~Control#setStyle}.
*
* @type {String}
* @default ''
* @public
*/
clientStyle: '',
/**
* Numerical offset applied to row number during row generation. Allows items
* to have natural indices instead of `0`-based ones. This value must be
* positive, as row number `-1` is used to represent undefined rows in the
* event system.
*
* @type {Number}
* @default 0
* @public
*/
rowOffset: 0,
/**
* Direction in which items will be laid out. Valid values are `'v'` for
* vertical or `'h'` for horizontal.
*
* @type {String}
* @default 'h'
* @public
*/
orient: 'v'
},
/**
* @private
*/
events: {
onSetupItem: '',
onRenderRow: ''
},
/**
* Setting cachePoint: true ensures that events from the repeater's subtree will
* always bubble up through the repeater, allowing the events to be decorated with repeater-
* related metadata and references.
*
* @type {Boolean}
* @default true
* @private
*/
cachePoint: true,
/**
* Design-time attribute indicating whether row indices run
* from `0` to [`count`]{@link module:layout/FlyweightRepeater~FlyweightRepeater#count}`-1` `(false)` or
* from [`count`]{@link module:layout/FlyweightRepeater~FlyweightRepeater#count}`-1` to `0` `(true)`.
*
* @type {Boolean}
* @default false
* @public
*/
bottomUp: false,
/**
* @private
*/
renderDelegate: FlyweightRepeaterDelegate,
/**
* @private
*/
components: [
{kind: Selection, onSelect: 'selectDeselect', onDeselect: 'selectDeselect'},
{name: 'client'}
],
/**
* @method
* @private
*/
create: function () {
Control.prototype.create.apply(this, arguments);
this.noSelectChanged();
this.multiSelectChanged();
this.clientClassesChanged();
this.clientStyleChanged();
},
/**
* @private
*/
noSelectChanged: function () {
if (this.noSelect) {
this.$.selection.clear();
}
},
/**
* @private
*/
multiSelectChanged: function () {
this.$.selection.setMulti(this.multiSelect);
},
/**
* @private
*/
clientClassesChanged: function () {
this.$.client.setClasses(this.clientClasses);
},
/**
* @private
*/
clientStyleChanged: function () {
this.$.client.setStyle(this.clientStyle);
},
/**
* @fires module:layout/FlyweightRepeater~FlyweightRepeater#onSetupItem
* @private
*/
setupItem: function (index) {
this.doSetupItem({index: index, selected: this.isSelected(index)});
},
/**
* Renders the list.
*
* @private
*/
generateChildHtml: function () {
return this.renderDelegate.generateInnerHtml(this);
},
/**
* @todo add link to preview.js
* @private
*/
previewDomEvent: function (event) {
var i = this.index = this.rowForEvent(event);
event.rowIndex = event.index = i;
event.flyweight = this;
},
/**
* @method
* @private
*/
decorateEvent: function (eventName, event, sender) {
// decorate event with index found via dom iff event does not already contain an index.
var i = (event && event.index != null) ? event.index : this.index;
if (event && i != null) {
event.index = i;
event.flyweight = this;
}
Control.prototype.decorateEvent.apply(this, arguments);
},
/**
* @private
*/
tap: function (sender, event) {
// ignore taps if selecting is disabled or if they don't target a row
if (this.noSelect || event.index === -1) {
return;
}
if (this.toggleSelected) {
this.$.selection.toggle(event.index);
} else {
this.$.selection.select(event.index);
}
},
/**
* Handler for selection and deselection.
*
* @private
*/
selectDeselect: function (sender, event) {
this.renderRow(event.key);
},
/**
* Returns the repeater's [selection]{@link module:enyo/Selection~Selection} component.
*
* @return {module:enyo/Selection~Selection} The repeater's selection component.
* @public
*/
getSelection: function () {
return this.$.selection;
},
/**
* Gets the selection state for the given row index.
*
* @return {Boolean} `true` if the row is currently selected; otherwise, `false`.
* @public
*/
isSelected: function (index) {
return this.getSelection().isSelected(index);
},
/**
* Renders the row with the specified index.
*
* @param {Number} index - The index of the row to render.
* @fires module:layout/FlyweightRepeater~FlyweightRepeater#onRenderRow
* @public
*/
renderRow: function (index) {
// do nothing if index is out-of-range
if (index < this.rowOffset || index >= this.count + this.rowOffset) {
return;
}
//this.index = null;
// always call the setupItem callback, as we may rely on the post-render state
this.setupItem(index);
var node = this.fetchRowNode(index);
if (node) {
// hack to keep this working...
var delegate = HTMLStringDelegate;
dom.setInnerHtml(node, delegate.generateChildHtml(this.$.client));
this.$.client.teardownChildren();
this.doRenderRow({rowIndex: index});
}
},
/**
* Fetches the DOM node for the given row index.
*
* @param {Number} index - The index of the row whose DOM node is to be fetched.
* @return {Node} The DOM node for the specified row.
* @public
*/
fetchRowNode: function (index) {
if (this.hasNode()) {
return this.node.querySelector('[data-enyo-index="' + index + '"]');
}
},
/**
* Fetches the row number corresponding to the target of a given event.
*
* @param {Object} event - Event object.
* @return {Number} The index of the row corresponding to the event's target.
* @public
*/
rowForEvent: function (event) {
if (!this.hasNode()) {
return -1;
}
var n = event.target;
while (n && n !== this.node) {
var i = n.getAttribute && n.getAttribute('data-enyo-index');
if (i !== null) {
return Number(i);
}
n = n.parentNode;
}
return -1;
},
/**
* Prepares the specified row such that changes made to the controls inside
* the repeater will be rendered for the row.
*
* @param {Number} index - The index of the row to be prepared.
* @public
*/
prepareRow: function (index) {
// do nothing if index is out-of-range
if (index < this.rowOffset || index >= this.count + this.rowOffset) {
return;
}
// update row internals to match model
this.setupItem(index);
var n = this.fetchRowNode(index);
FlyweightRepeater.claimNode(this.$.client, n);
},
/**
* Prevents rendering of changes made to controls inside the repeater.
*
* @public
*/
lockRow: function () {
this.$.client.teardownChildren();
},
/**
* Prepares the specified row such that changes made to the controls in the
* repeater will be rendered in the row; then performs the function `func`
* and, finally, locks the row.
*
* @param {Number} index - The index of the row to be acted upon.
* @param {Function} func - The function to perform.
* @param {Object} context - The context to which `func` is bound.
* @private
*/
performOnRow: function (index, func, context) {
// do nothing if index is out-of-range
if (index < this.rowOffset || index >= this.count + this.rowOffset) {
return;
}
if (func) {
this.prepareRow(index);
utils.call(context || null, func);
this.lockRow();
}
},
/**
* @lends module:layout/FlyweightRepeater~FlyweightRepeater
* @private
*/
statics: {
/**
* Associates a flyweight rendered control (`control`) with a
* rendering context specified by `node`.
*
* @param {module:enyo/Control~Control} control - A flyweight-rendered control.
* @param {Node} node - The DOM node to be associated with `control`.
* @public
*/
claimNode: function (control, node) {
var n;
if (node) {
if (node.id !== control.id) {
n = node.querySelector('#' + control.id);
} else {
// node is already the right node, so just use it
n = node;
}
}
// FIXME: consider controls generated if we found a node or tag: null, the later so can teardown render
control.generated = Boolean(n || !control.tag);
control.node = n;
if (control.node) {
control.rendered();
} else {
//enyo.log('Failed to find node for', control.id, control.generated);
}
// update control's class cache based on the node contents
for (var i=0, c$=control.children, c; (c=c$[i]); i++) {
this.claimNode(c, node);
}
}
}
});