forked from iljamaas/vanilla-js-smartscroll
/
scroller.src.js
393 lines (336 loc) · 12.6 KB
/
scroller.src.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
/**
*
* Smartscroll
*
* 2015 Dreamsolution B.V., Delft, The Netherlands
*
*/
var smartscroll = function () {
'use strict';
function createStyler(window) {
var handle, // timeout handle
stylers, // style create functions
listeners; // resize listeners
(function () {
listeners = [];
stylers = [];
window.addEventListener('resize', onResize, false);
window.addEventListener('unload', onUnload, false);
})();
function create_styler(styleFn) {
return function (width, height) {
var s = '\n';
if (styleFn.hasOwnProperty('name')) {
s += '/* ' + styleFn.name + ' */\n';
}
s += styleFn(width, height);
return s;
};
}
// Create a new style element with the given CSS and append, and remove
// any earlier style element _after_ that.
function replace_style_elm(text) {
var cstyler = document.querySelector('head .viewport-styler');
document.querySelector('head').insertAdjacentHTML( 'beforeend',
'<style type="text/css" class="viewport-styler">' +
text + '</style>');
if (cstyler) {
cstyler.parentNode.removeChild(cstyler);
}
}
function eval_stylers() {
var i,
exprs = [],
width = window.innerWidth,
height = window.innerHeight;
handle = undefined;
for (i = 0; i < stylers.length; i++) {
exprs.push(stylers[i](width, height));
}
replace_style_elm(exprs.join('\n'));
for (i = listeners.length; i--;) {
listeners[i]();
}
}
function onResize() {
if (handle) {
window.clearTimeout(handle);
}
handle = window.setTimeout(eval_stylers, 120);
}
function onUnload() {
window.clearTimeout(handle);
listeners = handle = stylers = undefined;
}
return {
resize: function resize(callbackFn) {
listeners.push(callbackFn);
},
addStyleCreator: function addStyleCreator(styleFn) {
var styler = create_styler(styleFn),
width = window.innerWidth,
height = window.innerHeight;
stylers[stylers.length] = styler;
// Immediately append the style:
var elm = document.querySelector('head .viewport-styler');
var text = elm ? elm.innerHTML : '';
replace_style_elm(text + styler(width, height));
},
redraw: function redraw() {
return onResize();
}
};
}
function smartScroll(conf) {
var wrapper,
pane,
actualcontent,
attic,
heightforcer,
smartlist;
var rowHeight = 120,
listlength = 0,
curfirst = 1,
drawnum = 0;
(function() {
if (conf.hasOwnProperty('height')) {
rowHeight = conf.height;
}
smartlist = conf.list;
wrapper = conf.container;
var structure = '<div class="smsc_sp">' +
'<div class="smsc_faker"></div>' +
'<div class="smsc_attic"></div>' +
'<div class="smsc_ac"></div>' +
'</div>';
wrapper.insertAdjacentHTML('afterbegin', structure);
wrapper.classList.add('smsc_wrap');
heightforcer = wrapper.querySelector('.smsc_faker');
pane = wrapper.querySelector('.smsc_sp');
actualcontent = wrapper.querySelector('.smsc_ac');
attic = wrapper.querySelector('.smsc_attic');
pane.style.height = getHeight(wrapper);
pane.addEventListener('scroll', adjustView, false);
styler.resize(onStylerResize);
layout();
})();
function transform(d) {
actualcontent.style.transform = 'translate(0px, ' + d + 'px)';
actualcontent.style.webkitTransform = 'translate(0px, ' + d + 'px)';
}
function getHeight(el){
return window.getComputedStyle(el).height;
}
function layout() {
listlength = smartlist.getSize();
drawnum = Math.ceil(parseInt(getHeight(pane),10) / rowHeight) + 1;
heightforcer.style.height = (listlength * rowHeight) + 'px';
if(pane.scrollTop > listlength * rowHeight){
pane.scrollTop = listlength * rowHeight - parseInt(getHeight(pane),10);
}
adjustView(null, true);
}
function adjustView(event, listchanged) {
var dx = 0;
var o = pane.scrollTop;
o = o > 0 ? o - 1 : 0;
var firstElement = Math.floor((o) / rowHeight) + 1;
if (drawnum < listlength && firstElement > listlength-drawnum + 1) {
firstElement = listlength - drawnum + 1;
dx = rowHeight;
}
if (curfirst !== firstElement || listchanged) {
drawContent(firstElement, listchanged);
}
transform(Math.min(o - ((o % rowHeight))) - dx);
curfirst = firstElement;
}
function moveToAttic(tbr){
attic.appendChild(tbr);
window.setTimeout(function(){
attic.removeChild(tbr);
}, 1000);
}
function drawContent(startWith, listchanged) {
/** if moving by one, just remove first one
* and append one or vice-verse to keep performance. prevent unneeded redraws.
*/
var t;
if (!listchanged && startWith === curfirst + 1) {
moveToAttic(actualcontent.firstChild);
t = smartlist.renderItemAtPosition(curfirst + drawnum);
if (typeof t === 'string') {
actualcontent.insertAdjacentHTML('beforeend', t);
} else {
actualcontent.appendChild(t);
}
}
else if (!listchanged && startWith === curfirst) {
moveToAttic(actualcontent.lastChild);
t = smartlist.renderItemAtPosition(curfirst - 1);
if (typeof t === 'string') {
actualcontent.insertAdjacentHTML('afterbegin', t);
} else {
actualcontent.insertBefore(t, actualcontent.firstChild);
}
}
else {
// too much of a hassle.. just redraw!
while (actualcontent.firstChild) {
moveToAttic(actualcontent.firstChild);
}
for (var i = startWith; i < startWith + drawnum &&
i - startWith < listlength; i++) {
t = smartlist.renderItemAtPosition(i);
if (t && typeof t === 'string') {
actualcontent.insertAdjacentHTML('beforeend', t);
} else if (t) {
actualcontent.appendChild(t);
}
}
}
}
function onStylerResize() {
pane.style.height = window.getComputedStyle(wrapper).height;
layout();
}
return {
handleEvent: function handleEvent(type, msg) {
if (type === 'listchanged') {
layout();
var event = new CustomEvent('my-event', { detail: msg});
wrapper.dispatchEvent(event);
}
},
setRowHeight: function setRowHeight(h) {
// must be called if the row-detail-level is changed!
rowHeight = h;
layout();
}
};
}
function smartList(conf) {
var list = [],
shadowlist = [], // contains filtered or ordered list
listeners = [],
filterfn = conf.filterfn,
renderfn = conf.renderfn,
order = "id",
filter = "";
// We're using an object-based event handling, and not callback so that
// we can easily hook up a smartscroll object to the accompanying
// smartlist object.
function fireEvent(type, evt) {
for (var i = listeners.length; i--; ) {
listeners[i].handleEvent.apply(listeners[i], [type, evt]);
}
}
// filter callback implementation for browsers that
// support js 1.6 array.filter:
function js_filter_callback(item) {
return filterfn(item, filter);
}
function maintainShadowList() {
if ('filter' in list && typeof list.filter === 'function') {
shadowlist = list.filter(js_filter_callback);
} else {
shadowlist = [];
for (var i = 0; i < list.length; i++) {
if (filterfn(list[i], filter)) {
shadowlist[shadowlist.length] = list[i];
}
}
}
shadowlist.sort(dynamicSort());
fireEvent('listchanged', {
size_total: list.length,
size_filtered: shadowlist.length
});
}
function dynamicSort() {
if (typeof order === 'function') {
return order;
} else {
var ordering = order[0] === "-" ? -1 : 1,
prop = ordering === -1 ? order.substr(1) : order;
return function sorter(a, b) {
/** XXX Force the null values to be on the end,
* always...(regardless of sort order):
*/
var prop_a = a[prop],
prop_b = b[prop],
missing_in_a = prop_a === undefined || prop_a === null,
missing_in_b = prop_b === undefined || prop_b === null;
if (missing_in_a && missing_in_b) {
return 0;
} else if (missing_in_a) {
return 1;
} else if (missing_in_b) {
return -1;
} else {
return ordering * ((prop_a < prop_b) ?
-1 : (prop_a > prop_b) ? 1 : 0);
}
};
}
}
return {
loadData: function loadData(data, redraw) {
list = data;
if (redraw) {
maintainShadowList();
}
},
set: function set(settings) {
var mt = false;
if (settings.hasOwnProperty('filter') &&
settings.filter !== filter) {
filter = settings.filter;
mt = true;
}
if (settings.hasOwnProperty('order') &&
settings.order !== order) {
order = settings.order;
mt = true;
}
if (mt) {
maintainShadowList();
}
},
getSize: function getSize() {
return shadowlist.length;
},
renderItemAtPosition: function renderItemAtPosition(offset) {
if (offset >= 0 && offset <= shadowlist.length){
return renderfn(shadowlist[offset - 1]);
} else {
return '';
}
},
addListener: function addListener(listener) {
listeners.push(listener);
},
/**
* Sort function for use in the "order" config option.
*/
lowerCompare: function lowerCompare(a, b) {
a = ("" + a).toLowerCase();
b = ("" + b).toLowerCase();
return a.localeCompare(b);
},
debug: function debug() {
var t = [];
for (var i = 0; i< shadowlist.length; i++) {
t.push(shadowlist[i].id);
}
}
};
}
// There should be one instance of the styler per window.
var styler = createStyler(window);
return {
viewport_styler: styler,
Scroll: smartScroll,
List: smartList
};
}();