forked from javve/list.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.min.js
1 lines (1 loc) · 8.76 KB
/
list.min.js
1
(function(d,e){var a=d.document,c;var b=function(g,s,o){var r=this,n,q,m,j,f,h,p={updated:[]};this.listContainer=(typeof(g)=="string")?a.getElementById(g):g;this.items=[];this.visibleItems=[];this.matchingItems=[];this.searched=false;this.filtered=false;this.list=null;this.templateEngines={};this.page=s.page||200;this.i=s.i||1;q={start:function(t,u){u.plugins=u.plugins||{};this.classes(u);n=new f(r,u);this.callbacks(u);this.items.start(t,u);r.update();this.plugins(u.plugins)},classes:function(t){t.listClass=t.listClass||"list";t.searchClass=t.searchClass||"search";t.sortClass=t.sortClass||"sort"},callbacks:function(t){r.list=c.getByClass(t.listClass,r.listContainer,true);c.addEvent(c.getByClass(t.searchClass,r.listContainer),"keyup",r.search);h=c.getByClass(t.sortClass,r.listContainer);c.addEvent(h,"click",r.sort)},items:{start:function(t,v){if(v.valueNames){var w=this.get(),u=v.valueNames;if(v.indexAsync){this.indexAsync(w,u)}else{this.index(w,u)}}if(t!==e){r.add(t)}},get:function(){var v=r.list.childNodes,u=[];for(var w=0,t=v.length;w<t;w++){if(v[w].data===e){u.push(v[w])}}return u},index:function(t,v){for(var w=0,u=t.length;w<u;w++){r.items.push(new j(v,t[w]))}},indexAsync:function(t,u){var v=t.splice(0,100);this.index(v,u);if(t.length>0){setTimeout(function(){q.items.indexAsync(t,u)},10)}else{r.update()}}},plugins:function(t){for(var u=0;u<t.length;u++){var v=t[u][1].name||t[u][0];r[v]=new r.plugins[t[u][0]](r,t[u][1])}}};this.add=function(u,z){if(z){l(u,z)}var x=[],w=false;if(u[0]===e){u=[u]}for(var v=0,t=u.length;v<t;v++){var y=null;if(u[v] instanceof j){y=u[v];y.reload()}else{w=(r.items.length>r.page)?true:false;y=new j(u[v],e,w)}r.items.push(y);x.push(y)}r.update();return x};var l=function(u,w,t){var v=u.splice(0,100);t=t||[];t=t.concat(r.add(v));if(u.length>0){setTimeout(function(){l(u,w,t)},10)}else{r.update();w(t)}};this.show=function(t,u){this.i=t;this.page=u;r.update()};this.remove=function(y,x,u){var w=0;for(var v=0,t=r.items.length;v<t;v++){if(r.items[v].values()[y]==x){n.remove(r.items[v],u);r.items.splice(v,1);t--;w++}}r.update();return w};this.get=function(y,w){var x=[];for(var u=0,t=r.items.length;u<t;u++){var v=r.items[u];if(v.values()[y]==w){x.push(v)}}if(x.length==0){return null}else{if(x.length==1){return x[0]}else{return x}}};this.sort=function(z,D){var t=r.items.length,C=null,x=z.target||z.srcElement,v="",y=false,B="asc",w="desc",D=D||{};if(x===e){C=z;y=D.asc||false}else{C=c.getAttribute(x,"data-sort");y=c.hasClass(x,B)?false:true}for(var u=0,A=h.length;u<A;u++){c.removeClass(h[u],B);c.removeClass(h[u],w)}if(y){if(x!==e){c.addClass(x,B)}y=true}else{if(x!==e){c.addClass(x,w)}y=false}if(D.sortFunction){D.sortFunction=D.sortFunction}else{D.sortFunction=function(F,E){return c.sorter.alphanum(F.values()[C],E.values()[C],y)}}r.items.sort(D.sortFunction);r.update()};this.search=function(E,v){r.i=1;var u=[],D,C,B,A,y,v=(v===e)?r.items[0].values():v,E=(E===e)?"":E,z=E.target||E.srcElement;E=(z===e)?(""+E).toLowerCase():""+z.value.toLowerCase();y=r.items;E=E.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&");n.clear();if(E===""){k.search();r.searched=false;r.update()}else{r.searched=true;for(var w=0,t=y.length;w<t;w++){D=false;C=y[w];A=C.values();for(var x in v){if(A.hasOwnProperty(x)&&v[x]!==null){B=(A[x]!=null)?A[x].toString().toLowerCase():"";if((E!=="")&&(B.search(E)>-1)){D=true}}}if(D){C.found=true;u.push(C)}else{C.found=false}}r.update()}return r.visibleItems};this.filter=function(y){r.i=1;var x=e;k.filter();if(y===e){r.filtered=false}else{x=[];r.filtered=true;var w=r.items;for(var u=0,t=w.length;u<t;u++){var v=w[u];if(y(v.values())){v.filtered=true;x.push(v)}else{v.filtered=false}}}r.update();return r.visibleItems};this.size=function(){return r.items.length};this.clear=function(){n.clear();r.items=[]};this.on=function(t,u){p[t].push(u)};var i=function(u){var t=p[u].length;while(t--){p[u][t]()}};var k={filter:function(){var u=r.items,t=u.length;while(t--){u[t].filtered=false}},search:function(){var u=r.items,t=u.length;while(t--){u[t].found=false}}};this.update=function(){var v=r.items,t=v.length;r.visibleItems=[];r.matchingItems=[];n.clear();for(var u=0;u<t;u++){if(v[u].matching()&&((u+1)>=r.i&&r.visibleItems.length<r.page)){v[u].show();r.visibleItems.push(v[u]);r.matchingItems.push(v[u])}else{if(v[u].matching()){r.matchingItems.push(v[u]);v[u].hide()}else{v[u].hide()}}}i("updated")};j=function(u,w,v){var x=this,t={};this.found=false;this.filtered=false;var y=function(A,C,B){if(C===e){if(B){x.values(A,B)}else{x.values(A)}}else{x.elm=C;var z=n.get(x,A);x.values(z)}};this.values=function(A,B){if(A!==e){for(var z in A){t[z]=A[z]}if(B!==true){n.set(x,x.values())}}else{return t}};this.show=function(){n.show(x)};this.hide=function(){n.hide(x)};this.matching=function(){return((r.filtered&&r.searched&&x.found&&x.filtered)||(r.filtered&&!r.searched&&x.filtered)||(!r.filtered&&r.searched&&x.found)||(!r.filtered&&!r.searched))};this.visible=function(){return(x.elm.parentNode)?true:false};y(u,w,v)};f=function(u,t){if(t.engine===e){t.engine="standard"}else{t.engine=t.engine.toLowerCase()}return new r.constructor.prototype.templateEngines[t.engine](u,t)};q.start(o,s)};b.prototype.templateEngines={};b.prototype.plugins={};b.prototype.templateEngines.standard=function(k,h){var l=c.getByClass(h.listClass,k.listContainer)[0],j=f(h.item),g=this;function f(q){if(q===e){var o=l.childNodes,n=[];for(var p=0,m=o.length;p<m;p++){if(o[p].data===e){return o[p]}}return null}else{if(q.indexOf("<")!==-1){var r=a.createElement("div");r.innerHTML=q;return r.firstChild}else{return a.getElementById(h.item)}}}var i={created:function(m){if(m.elm===e){g.create(m)}}};this.get=function(q,o){i.created(q);var n={};for(var p=0,m=o.length;p<m;p++){n[o[p]]=c.getByClass(o[p],q.elm)[0].innerHTML}return n};this.set=function(o,n){i.created(o);for(var m in n){if(n.hasOwnProperty(m)){var p=c.getByClass(m,o.elm,true);if(p){p.innerHTML=n[m]}}}};this.create=function(n){if(n.elm!==e){return}var m=j.cloneNode(true);m.id="";n.elm=m;g.set(n,n.values())};this.remove=function(m){l.removeChild(m.elm)};this.show=function(m){i.created(m);l.appendChild(m.elm)};this.hide=function(m){if(m.elm!==e&&m.elm.parentNode===l){l.removeChild(m.elm)}};this.clear=function(){if(l.hasChildNodes()){while(l.childNodes.length>=1){l.removeChild(l.firstChild)}}}};c={getByClass:(function(){if(a.getElementsByClassName){return function(g,f,h){if(h){return f.getElementsByClassName(g)[0]}else{return f.getElementsByClassName(g)}}}else{return function(m,g,o){var p=[],q="*";if(g==null){g=a}var k=g.getElementsByTagName(q);var f=k.length;var n=new RegExp("(^|\\s)"+m+"(\\s|$)");for(var l=0,h=0;l<f;l++){if(n.test(k[l].className)){if(o){return k[l]}else{p[h]=k[l];h++}}}return p}}})(),addEvent:(function(g,f){if(f.addEventListener){return function(m,l,j){if((m&&!(m instanceof Array)&&!m.length&&!c.isNodeList(m)&&(m.length!==0))||m===g){m.addEventListener(l,j,false)}else{if(m&&m[0]!==e){var h=m.length;for(var k=0;k<h;k++){c.addEvent(m[k],l,j)}}}}}else{if(f.attachEvent){return function(m,l,j){if((m&&!(m instanceof Array)&&!m.length&&!c.isNodeList(m)&&(m.length!==0))||m===g){m.attachEvent("on"+l,function(){return j.call(m,g.event)})}else{if(m&&m[0]!==e){var h=m.length;for(var k=0;k<h;k++){c.addEvent(m[k],l,j)}}}}}}})(this,a),getAttribute:function(l,g){var f=(l.getAttribute&&l.getAttribute(g))||null;if(!f){var h=l.attributes;var k=h.length;for(var j=0;j<k;j++){if(g[j]!==e){if(g[j].nodeName===g){f=g[j].nodeValue}}}}return f},isNodeList:function(g){var f=Object.prototype.toString.call(g);if(typeof g==="object"&&/^\[object (HTMLCollection|NodeList|Object)\]$/.test(f)&&(g.length==0||(typeof node==="object"&&g[0].nodeType>0))){return true}return false},hasClass:function(h,g){var f=this.getAttribute(h,"class")||this.getAttribute(h,"className");return(f.search(g)>-1)},addClass:function(h,g){if(!this.hasClass(h,g)){var f=this.getAttribute(h,"class")||this.getAttribute(h,"className");f=f+" "+g+" ";f=f.replace(/\s{2,}/g," ");h.setAttribute("class",f)}},removeClass:function(h,g){if(this.hasClass(h,g)){var f=this.getAttribute(h,"class")||this.getAttribute(h,"className");f=f.replace(g,"");h.setAttribute("class",f)}},sorter:{alphanum:function(h,g,i){if(h===e||h===null){h=""}if(g===e||g===null){g=""}h=h.toString().replace(/&(lt|gt);/g,function(n,o){return(o=="lt")?"<":">"});h=h.replace(/<\/?[^>]+(>|$)/g,"");g=g.toString().replace(/&(lt|gt);/g,function(n,o){return(o=="lt")?"<":">"});g=g.replace(/<\/?[^>]+(>|$)/g,"");var j=this.chunkify(h);var l=this.chunkify(g);for(var f=0;j[f]&&l[f];f++){if(j[f]!==l[f]){var m=Number(j[f]),k=Number(l[f]);if(i){if(m==j[f]&&k==l[f]){return m-k}else{return(j[f]>l[f])?1:-1}}else{if(m==j[f]&&k==l[f]){return k-m}else{return(j[f]>l[f])?-1:1}}}}return j.length-l.length},chunkify:function(l){var q=[],g=0,p=-1,o=0,k,h;while(k=(h=l.charAt(g++)).charCodeAt(0)){var f=(k==45||k==46||(k>=48&&k<=57));if(f!==o){q[++p]="";o=f}q[p]+=h}return q}}};d.List=b;d.ListJsHelpers=c})(window);