forked from NeXTs/Clusterize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterize.min.js
13 lines (12 loc) · 4.33 KB
/
clusterize.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*! Clusterize.js - v0.5.0 - 2015-05-05
* http://NeXTs.github.com/Clusterize.js/
* Copyright (c) 2015 Denis Lukov; Licensed MIT */
;(function(m,f){"undefined"!=typeof module?module.exports=f():"function"==typeof define&&"object"==typeof define.amd?define(f):this[m]=f()})("Clusterize",function(){function m(a,b,c){return b.addEventListener?b.addEventListener(a,c,!1):b.attachEvent("on"+a,c)}var f=function(){for(var a=3,b=document.createElement("b"),c=b.all||[];b.innerHTML="\x3c!--[if gt IE "+ ++a+"]><i><![endif]--\x3e",c[0];);return 4<a?a:document.documentMode}(),k=function(a){if(!(this instanceof k))return new k(a);var b=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,content_tag:null,show_no_data_row:!0,no_data_class:"clusterize-no-data",no_data_text:"No data",keep_parity:!0,verify_change:!1};b.options={};for(var d="rows_in_block blocks_in_cluster verify_change show_no_data_row no_data_class no_data_text keep_parity tag".split(" "),e=0,l;l=d[e];e++)b.options[l]="undefined"!=typeof a[l]&&null!=a[l]?a[l]:c[l];c=["scroll","content"];for(e=0;d=c[e];e++)if(b[d+
"_elem"]=a[d+"Id"]?document.getElementById(a[d+"Id"]):a[d+"Elem"],!b[d+"_elem"])throw Error("Error! Could not find "+d+" element");var g=a.rows||[],h={data:""};a=b.scroll_elem.scrollTop;b.exploreEnvironment(g);b.insertToDOM(g,h);b.scroll_elem.scrollTop=a;var f=!1,n=function(){f!=(f=b.getClusterNum())&&b.insertToDOM(g,h)};m("scroll",b.scroll_elem,n);b.destroy=function(a){var c=b.scroll_elem;c.removeEventListener?c.removeEventListener("scroll",n,!1):c.detachEvent("onscroll",n);b.html(a?b.generateEmptyRow().join(""):
g.join(""))};b.update=function(a){g="[object Array]"===Object.prototype.toString.call(a)?a:[];a=b.scroll_elem.scrollTop;b.insertToDOM(g,h);b.scroll_elem.scrollTop=a};b.append=function(a){a="[object Array]"===Object.prototype.toString.call(a)?a:[];a.length&&(g=g.concat(a),b.insertToDOM(g,h))};b.clear=function(){b.update([])};b.getRowsAmount=function(){return g.length}};k.prototype={constructor:k,exploreEnvironment:function(a){var b=this.options;b.content_tag=this.content_elem.tagName.toLowerCase();
if(!b.item_height||!b.tag){if(!a.length)return;f&&9>=f&&(b.tag=a[0].split("<")[1].split(" ")[0].split(">")[0]);this.html(a[0]+a[0]+a[0]);a=this.content_elem.children[1];b.tag||(b.tag=a.tagName.toLowerCase());b.item_height=a.offsetHeight}b.block_height=b.item_height*b.rows_in_block;b.rows_in_cluster=b.blocks_in_cluster*b.rows_in_block;b.cluster_height=b.blocks_in_cluster*b.block_height},getClusterNum:function(){var a=this.options;return Math.floor(this.scroll_elem.scrollTop/(a.cluster_height-a.block_height))},
generateEmptyRow:function(){var a=this.options;if(!a.tag||!a.show_no_data_row)return[];var b=document.createElement(a.tag),c=document.createTextNode(a.no_data_text),d;b.className=a.no_data_class;"tr"==a.tag&&(d=document.createElement("td"),d.appendChild(c));b.appendChild(d||c);return[b.outerHTML]},generate:function(a,b){var c=this.options,d=a.length;if(d<c.rows_in_block)return{rows_above:0,rows:d?a:this.generateEmptyRow()};c.cluster_height||this.exploreEnvironment(a);var e=b*c.rows_in_cluster-c.rows_in_block*
b,e=0<e?e:0,f=e+c.rows_in_cluster,g=e*c.item_height,d=(d-f)*c.item_height,h=[],k=e;0<g?(c.keep_parity&&h.push(this.renderExtraTag("keep-parity")),h.push(this.renderExtraTag("top-space",g))):k++;for(c=e;c<f;c++)a[c]&&h.push(a[c]);0<d&&h.push(this.renderExtraTag("bottom-space",d));return{rows_above:k,rows:h}},renderExtraTag:function(a,b){var c=document.createElement(this.options.tag);c.className=["clusterize-extra-row","clusterize-"+a].join(" ");b&&(c.style.height=b+"px");return c.outerHTML},insertToDOM:function(a,
b){var c=this.generate(a,this.getClusterNum()),d=c.rows.join("");if(!this.options.verify_change||this.options.verify_change&&this.dataChanged(d,b))this.html(d),"ol"==this.options.content_tag&&this.content_elem.setAttribute("start",c.rows_above)},html:function(a){var b=this.content_elem;if(f&&9>=f&&"tr"==this.options.tag){var c=document.createElement("div");for(c.innerHTML="<table><tbody>"+a+"</tbody></table>";a=b.lastChild;)b.removeChild(a);c=c.firstChild.firstChild.childNodes;a=[];for(var d=0,e=
c.length;d<e;d++)a.push(c[d]);for(c=Array.prototype.slice.call(a);c.length;)b.appendChild(c.shift())}else b.innerHTML=a},dataChanged:function(a,b){var c=JSON.stringify(a);return c!==b.data&&(b.data=c)}};return k});