forked from NeXTs/Clusterize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterize.min.js
12 lines (11 loc) · 3.91 KB
/
clusterize.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*! Clusterize.js - v0.2.0 - 2015-04-29
* http://NeXTs.github.com/Clusterize.js/
* Copyright (c) 2015 Denis Lukov; Licensed MIT */
;(function(g,f){"undefined"!=typeof module?module.exports=f():"function"==typeof define&&"object"==typeof define.amd?define(f):this[g]=f()})("Clusterize",function(){var g=function(){for(var b=3,a=document.createElement("b"),c=a.all||[];a.innerHTML="\x3c!--[if gt IE "+ ++b+"]><i><![endif]--\x3e",c[0];);return 4<b?b:document.documentMode}(),f=function(b){if(!(this instanceof f))return new f(b);var a=this,c={item_height:0,block_height:0,rows_in_block:50,rows_in_cluster:0,cluster_height:0,blocks_in_cluster:4,
tag:null,show_no_data_row:!0,no_data_class:"clusterize-no-data",no_data_text:"No data",keep_parity:!0,verify_change:!1};a.options={};"rows_in_block blocks_in_cluster verify_change show_no_data_row no_data_class no_data_text keep_parity tag".split(" ").forEach(function(k){a.options[k]="undefined"!=typeof b[k]&&null!=b[k]?b[k]:c[k]});a.scrollElem=b.scrollId?document.getElementById(b.scrollId):b.scrollElem;a.contentElem=b.contentId?document.getElementById(b.contentId):b.contentElem;if(!a.contentElem)throw Error("Error! Could not find content element");
if(!a.scrollElem)throw Error("Error! Could not find scroll element");var d=b.rows||[],e={data:""},g=a.scrollElem.scrollTop;a.calcClusterHeight(d);a.insertToDOM(d,e);a.scrollElem.scrollTop=g;var l=!1,h=function(){l!=(l=a.getClusterNum())&&a.insertToDOM(d,e)};a.scrollElem.addEventListener("scroll",h);a.destroy=function(b){a.scrollElem.removeEventListener("scroll",h);a.html(b?a.generateEmptyRow().join(""):d.join(""))};a.update=function(b){d=a.isArray(b)?b:[];b=a.scrollElem.scrollTop;a.insertToDOM(d,
e);a.scrollElem.scrollTop=b};a.append=function(b){b=a.isArray(b)?b:[];b.length&&(d=d.concat(b),a.insertToDOM(d,e))};a.clear=function(){a.update([])};a.getRowsAmount=function(){return d.length}};f.prototype={constructor:f,calcClusterHeight:function(b){var a=this.options;if(!a.item_height||!a.tag){if(!b.length)return;this.html(b[0]+b[0]+b[0]);b=this.contentElem.children[1];a.tag||(a.tag=b.tagName.toLowerCase());a.item_height=b.offsetHeight}a.block_height=a.item_height*a.rows_in_block;a.rows_in_cluster=
a.blocks_in_cluster*a.rows_in_block;a.cluster_height=a.blocks_in_cluster*a.block_height},getClusterNum:function(){var b=this.options;return Math.floor(this.scrollElem.scrollTop/(b.cluster_height-b.block_height))},generateEmptyRow:function(){var b=this.options;if(!b.tag||!b.show_no_data_row)return[];var a="<"+b.tag+' class="'+b.no_data_class+'">';switch(b.tag){case "tr":a+="<td>"+b.no_data_text+"</td>";break;default:a+=b.no_data_text}a+="</"+b.tag+">";return[a]},generate:function(b,a){var c=this.options,
d=b.length;if(!d)return this.generateEmptyRow();if(d<c.rows_in_block)return b;c.cluster_height||this.calcClusterHeight(b);var e=a*c.rows_in_cluster-c.rows_in_block*a,e=0<e?e:0,f=e+c.rows_in_cluster,g=e*c.item_height,d=(d-f)*c.item_height,h=[];0<g&&(c.keep_parity&&h.push("<"+c.tag+' class="clusterize-extra-row clusterize-keep-parity"></'+c.tag+">"),h.push("<"+c.tag+' class="clusterize-extra-row clusterize-top-space" style="height:'+g+'px;"></'+c.tag+">"));for(;e<f;e++)b[e]&&h.push(b[e]);0<d&&h.push("<"+
c.tag+' class="clusterize-extra-row clusterize-bottom-space" style="height:'+d+'px;"></'+c.tag+">");return h},insertToDOM:function(b,a){var c=this.generate(b,this.getClusterNum()).join("");(!this.options.verify_change||this.options.verify_change&&this.dataChanged(c,a))&&this.html(c)},html:function(b){var a=this.contentElem;if(g&&9>=g&&"tr"==this.options.tag){var c=document.createElement("div");for(c.innerHTML="<table><tbody>"+b+"</tbody></table>";b=a.lastChild;)a.removeChild(b);for(c=Array.prototype.slice.call(c.firstChild.firstChild.childNodes);c.length;)a.appendChild(c.shift())}else a.innerHTML=
b},dataChanged:function(b,a){var c=JSON.stringify(b);return c!==a.data&&(a.data=c)},isArray:function(b){return"[object Array]"===Object.prototype.toString.call(b)}};return f});