forked from NeXTs/Clusterize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterize.js
255 lines (241 loc) · 8.92 KB
/
clusterize.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
/*! Clusterize.js - v0.6.0 - 2015-05-10
* http://NeXTs.github.com/Clusterize.js/
* Copyright (c) 2015 Denis Lukov; Licensed MIT */
;(function(name, definition) {
if (typeof module != 'undefined') module.exports = definition();
else if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
else this[name] = definition();
}('Clusterize', function() {
"use strict"
// detect ie9 and lower
// https://gist.github.com/padolsey/527683#comment-786682
var ie = (function(){
for( var v = 3,
el = document.createElement('b'),
all = el.all || [];
el.innerHTML = '<!--[if gt IE ' + (++v) + ']><i><![endif]-->',
all[0];
){}
return v > 4 ? v : document.documentMode;
}());
var Clusterize = function(data) {
if( ! (this instanceof Clusterize))
return new Clusterize(data);
var self = this;
var defaults = {
item_height: 0,
block_height: 0,
rows_in_block: 50,
rows_in_cluster: 0,
cluster_height: 0,
blocks_in_cluster: 4,
tag: null,
content_tag: null,
show_no_data_row: true,
no_data_class: 'clusterize-no-data',
no_data_text: 'No data',
keep_parity: true,
verify_change: false
}
// public parameters
self.options = {};
var options = ['rows_in_block', 'blocks_in_cluster', 'verify_change', 'show_no_data_row', 'no_data_class', 'no_data_text', 'keep_parity', 'tag'];
for(var i = 0, option; option = options[i]; i++) {
self.options[option] = typeof data[option] != 'undefined' && data[option] != null
? data[option]
: defaults[option];
}
var elems = ['scroll', 'content'];
for(var i = 0, elem; elem = elems[i]; i++) {
self[elem + '_elem'] = data[elem + 'Id']
? document.getElementById(data[elem + 'Id'])
: data[elem + 'Elem'];
if( ! self[elem + '_elem'])
throw new Error("Error! Could not find " + elem + " element");
}
// private parameters
var rows = isArray(data.rows)
? data.rows
: self.fetchMarkup(),
cache = {data: ''},
scroll_top = self.scroll_elem.scrollTop;
// get row height
self.exploreEnvironment(rows);
// append initial data
self.insertToDOM(rows, cache);
// restore the scroll position
self.scroll_elem.scrollTop = scroll_top;
// adding scroll handler
var last_cluster = false,
scrollEv = function () {
if (last_cluster != (last_cluster = self.getClusterNum()))
self.insertToDOM(rows, cache);
}
on('scroll', self.scroll_elem, scrollEv);
// public methods
self.destroy = function(clean) {
off('scroll', self.scroll_elem, scrollEv);
self.html((clean ? self.generateEmptyRow() : rows).join(''));
}
self.update = function(new_rows) {
rows = isArray(new_rows)
? new_rows
: [];
var scroll_top = self.scroll_elem.scrollTop;
self.insertToDOM(rows, cache);
self.scroll_elem.scrollTop = scroll_top;
}
self.append = function(_new_rows) {
var new_rows = isArray(_new_rows)
? _new_rows
: [];
if( ! new_rows.length) return;
rows = rows.concat(new_rows);
self.insertToDOM(rows, cache);
}
self.clear = function() {
self.update([]);
}
self.getRowsAmount = function() {
return rows.length;
}
}
Clusterize.prototype = {
constructor: Clusterize,
// fetch existing markup
fetchMarkup: function() {
var rows = [], rows_nodes = this.getChildNodes(this.content_elem);
while (rows_nodes.length) {
rows.push(rows_nodes.shift().outerHTML);
}
return rows;
},
// get tag name, content tag name, tag height, calc cluster height
exploreEnvironment: function(rows) {
var opts = this.options;
opts.content_tag = this.content_elem.tagName.toLowerCase();
if( ! opts.item_height || ! opts.tag) {
if( ! rows.length) return;
if(ie && ie <= 9) opts.tag = rows[0].split('<')[1].split(' ')[0].split('>')[0];
this.html(rows[0] + rows[0] + rows[0]);
var node = this.content_elem.children[1];
if( ! opts.tag) opts.tag = node.tagName.toLowerCase();
opts.item_height = node.offsetHeight;
}
opts.block_height = opts.item_height * opts.rows_in_block;
opts.rows_in_cluster = opts.blocks_in_cluster * opts.rows_in_block;
opts.cluster_height = opts.blocks_in_cluster * opts.block_height;
},
// get current cluster number
getClusterNum: function () {
var opts = this.options;
return Math.floor(this.scroll_elem.scrollTop / (opts.cluster_height - opts.block_height));
},
// generate empty row if no data provided
generateEmptyRow: function() {
var opts = this.options;
if( ! opts.tag || ! opts.show_no_data_row) return [];
var empty_row = document.createElement(opts.tag),
no_data_content = document.createTextNode(opts.no_data_text), td;
empty_row.className = opts.no_data_class;
if(opts.tag == 'tr') {
td = document.createElement('td');
td.appendChild(no_data_content);
}
empty_row.appendChild(td || no_data_content);
return [empty_row.outerHTML];
},
// generate cluster for current scroll position
generate: function (rows, cluster_num) {
var opts = this.options,
rows_len = rows.length;
if (rows_len < opts.rows_in_block) {
return {
rows_above: 0,
rows: rows_len ? rows : this.generateEmptyRow()
}
}
if( ! opts.cluster_height) {
this.exploreEnvironment(rows);
}
var items_start = cluster_num * opts.rows_in_cluster - opts.rows_in_block * cluster_num,
items_start = items_start > 0 ? items_start : 0,
items_end = items_start + opts.rows_in_cluster,
top_space = items_start * opts.item_height,
bottom_space = (rows_len - items_end) * opts.item_height,
this_cluster_rows = [],
rows_above = items_start;
if(top_space > 0) {
opts.keep_parity && this_cluster_rows.push(this.renderExtraTag('keep-parity'));
this_cluster_rows.push(this.renderExtraTag('top-space', top_space));
} else {
rows_above++;
}
for (var i = items_start; i < items_end; i++) {
rows[i] && this_cluster_rows.push(rows[i]);
}
bottom_space > 0 && this_cluster_rows.push(this.renderExtraTag('bottom-space', bottom_space));
return {
rows_above: rows_above,
rows: this_cluster_rows
}
},
renderExtraTag: function(class_name, height) {
var tag = document.createElement(this.options.tag),
clusterize_prefix = 'clusterize-';
tag.className = [clusterize_prefix + 'extra-row', clusterize_prefix + class_name].join(' ');
height && (tag.style.height = height + 'px');
return tag.outerHTML;
},
// if necessary verify data changed and insert to DOM
insertToDOM: function(rows, cache) {
var data = this.generate(rows, this.getClusterNum()),
outer_data = data.rows.join('');
if( ! this.options.verify_change || this.options.verify_change && this.dataChanged(outer_data, cache)) {
this.html(outer_data);
this.options.content_tag == 'ol' && this.content_elem.setAttribute('start', data.rows_above);
}
},
// unfortunately ie <= 9 does not allow to use innerHTML for table elements, so make a workaround
html: function(data) {
var content_elem = this.content_elem;
if(ie && ie <= 9 && this.options.tag == 'tr') {
var div = document.createElement('div'), last;
div.innerHTML = '<table><tbody>' + data + '</tbody></table>'
while((last = content_elem.lastChild)) {
content_elem.removeChild(last)
}
var rows_nodes = this.getChildNodes(div.firstChild.firstChild);
while (rows_nodes.length) {
content_elem.appendChild(rows_nodes.shift());
}
} else {
content_elem.innerHTML = data;
}
},
getChildNodes: function(tag) {
var child_nodes = tag.children,
ie8_child_nodes_helper = [];
for (var i = 0, ii = child_nodes.length; i < ii; i++) {
ie8_child_nodes_helper.push(child_nodes[i]);
}
return Array.prototype.slice.call(ie8_child_nodes_helper);
},
dataChanged: function(data, cache) {
var current_data = JSON.stringify(data),
changed = current_data !== cache.data;
return changed && (cache.data = current_data);
}
}
// support functions
function on(evt, element, fnc) {
return element.addEventListener ? element.addEventListener(evt, fnc, false) : element.attachEvent("on" + evt, fnc);
}
function off(evt, element, fnc) {
return element.removeEventListener ? element.removeEventListener(evt, fnc, false) : element.detachEvent("on" + evt, fnc);
}
function isArray(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
return Clusterize;
}));