-
Notifications
You must be signed in to change notification settings - Fork 1
/
table-view.min.js
1 lines (1 loc) · 10.6 KB
/
table-view.min.js
1
!function(t){"use strict";var e,i,s,r,n,a,o,l,h,f,u,c,d,p,g,m=1,y=2,v=["Change","Update"],b=["key","value"],C="data-column-key",S="@",k="#",x="?",A=">",w="<",I="function"===typeof Object.observe,T=/&/g,H=/</g,L=/>/g,P=/"/g,D=/'/g;e=function(t){var e=t;if(!e)s("invalidSettings");if(e.columns)this.columns=e.columns;if(e.columnAttributes)this.columnAttributes=e.columnAttributes;if(e.rowAttributes)this.rowAttributes=e.rowAttributes;if(e.linkSettings)this.linkSettings=e.linkSettings;if(e.table)this.setTable(e.table,e.body||0);if(e.exclude)this.exclude=e.exclude;if(e.defaultText)this.defaultText=e.defaultText;this.sortClass=e.sortClass||"sorted";this.sortClassAscending=e.sortClassAscending||"sorted-asc";this.sortClassDescending=e.sortClassDescending||"sorted-desc";this.isRecursive="undefined"===typeof e.isRecursive?true:e.isRecursive;if(e.formatter)if("function"===typeof e.formatter)this.cellFormatter=e.formatter;else this.cellFormatter=this.formatCellHtml;if(e.compareItems)if("function"===typeof e.compareItems)this.compareItems=e.compareItems;this.setData(e.data);if(e.triggers)this.watchTriggers(e.triggers);if(e.onProcess)this.onProcess=e.onProcess;if(e.onCheckChange)this.onCheckChange=e.onCheckChange;n(this,this.updateEvents,e);if(e.pageSize){this.pageSize=e.pageSize;this.pageIndex=e.pageIndex||0}if(e.sortColumn){this.sort(e.sortColumn);this.update(null)}};e.prototype.updateEvents=v;e.prototype.generateBody=function(){var t=this.dataIsArray,e=t?this.dataSort||this.data:this.keys,i=this.isReversed,s=this.pageSize||0,r,n,a,o,l,h="";if(i){n=e.length-1;a=0;o=-1;if(s){n=Math.max(n-this.pageIndex*s,a);a=Math.max(n-s+1,0)}}else{a=s?Math.min((this.pageIndex+1)*s,e.length):!e?0:e.length;n=s?this.pageIndex*s:0;o=1}for(r=n;i?r>=a:r<a;r+=o){l=e[r];if(t)h+=this.generateRowHtml(l,r);else h+=this.generateRowHtml(this.data,r,l)}return h};e.prototype.generateRowHtml=function(t,e,i){var s,r,n,a,o,l=this.sortColumn,h,f=this.columnAttributes,u=this.rowAttributes,d,v,C,I,T=this.columns,H=T?T.length:0,L=this.cellFormatter,P=this.linkSettings;if(i&&u){if(u[i])u=u?u[i]:null;d=u&&u.cellAttributes}r="<tr"+this.generateAttributeHtml(u)+">";for(s=0;s<H;s++){n=T[s];v=false;I=n;if(n===y){a=t[i];I=i}else if(n===m){a=i;I=null}else if(n===S){a=e.toString();v=true}else if(n===k){a=(e+1).toString();v=true}else if(n===x){a=this.generateCheckHtml(t);v=true}else if(n===A)a=c(t);else if(n===w)a=c(t,"LOW");else{a=t[n];if(n===l)h=true}if(d)C=u.cellAttributes[b[s]];else C=f?f[n]:null;if(h){o=this.sortClass+" "+(this.isReversed?this.sortClassDescending:this.sortClassAscending);if(!C)C={};if(!C["class"])C["class"]=o;else C["class"]+=" "+o;h=false}if(P){g=P[n];if(!!g){L=p;v=false}}r+="<td"+this.generateAttributeHtml(C)+">";r+=(L&&!v?L.call(this,a,I,t,e):a)+"</td>";if(g){L=this.cellFormatter;g=null}}return r+"</tr>"};e.prototype.generateAttributeHtml=function(t,e){var i,s,r="";if(!!t&&"object"===typeof t)for(i in t)if(t.hasOwnProperty(i)&&"cellAttributes"!==i){s=this.formatCellHtml(t[i]);r+=" "+(e||"")+i+'="'+s+'"'}return r};e.prototype.generateCheckHtml=function(t){return'<input type="checkbox" '+this.generateAttributeHtml(t,"data-")+"/>"};e.prototype.formatCellJson=function(t){var e=JSON.stringify(t);return e};e.prototype.formatCellHtml=function(t){var e,i,s=typeof t;if("undefined"===s||null===t)i=this.defaultText||"";else i=t.toString();e=i.replace(T,"&").replace(H,"<").replace(L,">").replace(P,""").replace(D,"'");return e};e.prototype.update=function(t){var e,i,s,r,n=null!==t?["Update"]:t;e=this.generateBody();this.tableBody.innerHTML=e;if(this.onCheckChange)this.processChecks();if(n&&n.length)for(i=0;i<n.length;i++){s="on"+n[i];r=this[s];if("function"===typeof r)r.call(this)}};e.prototype.processChanges=function(t){if("function"===typeof this.onProcess)if(false===this.onProcess(t))return;if(this.dataSort)delete this.dataSort;if(!this.dataIsArray)this.keys=r(this.data,this.exclude||[]);this.sort(this.sortColumn);this.update(this.updateEvents)};e.prototype.sort=function(t,e){var i,s,r,n,a,o,l=!!e;if(!t)return;if(this.sortColumn!=t){if(l)this.isReversed=!this.sortColumn&&t==S;this.sortColumn=t}else if(l)this.isReversed=!this.isReversed;if(t==S)return;if(this.dataIsArray)this.dataSort=this.data.slice(0);else{r=t===b[0];if(r)this.dataSort=this.keys.slice(0);else{s=true;this.dataSort=[];a=this.keys||[];for(o=0;o<a.length;o++){n=a[o];this.dataSort.push({key:n,value:this.data[n]})}}}if(this.compareItems){i=this;this.dataSort.sort(function(t,e){if(s)return i.compareItems(t.value,e.value);else return i.compareItems(t,e)})}else this.dataSort.sort(function(e,i){if(s)return u(e.value,i.value);else return u(e,i,r?null:t)});if(s){this.keys=[];for(o=0;o<this.dataSort.length;o++){n=this.dataSort[o].key;this.keys.push(n)}}else if(r)this.keys=this.dataSort;this.updateHeader(t)};e.prototype.processChecks=function(t){var e,i,s,r,n,a,o,l,h,f=this,u=this.tableBody,c=null;if(u&&u.rows){r=u.rows;for(e=0;e<r.length;e++){s=r[e];for(i=0;i<s.children.length;i++){n=s.children[i];l=n.children;h=-1;for(o=0;o<l.length;o++){a=l[o];if(!(a instanceof HTMLInputElement))continue;if(!a.type||"checkbox"!==a.type.toLowerCase())continue;h++;if(!a.getAttribute("data-check-index")){a.setAttribute("data-check-index",h.toString());a.addEventListener("change",function(t){var e=parseInt(a.getAttribute("data-check-index"),10);if("function"===typeof f.onCheckChange)f.onCheckChange.call(this,this.checked,e)})}if(a.checked||t){if(!c)c=[];c.push(a)}}}}}return c};e.prototype.getChecks=function(){return this.processChecks()||[]};e.prototype.releaseData=function(){var t=this.data;if(t){delete this.data;delete this.dataIsArray;if(this.observeChanges){Object.unobserve(t,this.observeChanges);delete this.observeChanges}}if(this.dataSort)delete this.dataSort};e.prototype.setData=function(t){var e=this;this.releaseData();if("undefined"===typeof t||"string"===typeof t)return this.setData(this.getDataFromTable(t));else if(t){this.data=t;this.dataIsArray=t instanceof Array;if(!this.dataIsArray){this.keys=r(t,this.exclude||[]);this.columns=[m,y]}if(I){this.observeChanges=function(t){e.processChanges(t)};h(function(t){Object.observe(t,e.observeChanges)},!this.isRecursive)(t)}}this.sort(this.sortColumn)};e.prototype.getDataFromTable=function(t){var e=[],i,s,r,n,a,o,l,h=this.tableBody,f=this.columns;if(t){h=document.getElementById(t);if(h)h=h.tBodies[0]}if(h&&f){l=h.rows;if(l)for(i=0;i<l.length;i++){r=l[i];n={};for(s=0;s<r.children.length;s++){o=r.children[s].getAttribute("data-column-type");a=r.children[s].innerText;if(o){o=o.toLowerCase();if("date"===o)a=new Date(a);else if("number"===o)a=parseFloat(a)}if(f.length>0&&s<f.length)n[f[s]]=a}e.push(n)}}return e};e.prototype.setTable=function(t,e){if("string"===typeof t&&"undefined"!==typeof document)this.table=document.getElementById(t);else this.table=t;if(null===this.table||!(this.table instanceof HTMLTableElement))s("invalidTable");else{if(e instanceof HTMLTableSectionElement)this.tableBody=e;else this.tableBody=this.table.tBodies[e];this.setTableHeaderEvents()}};e.prototype.iterateHeaderCells=function(t){var e,i,s,r,n,a;if(!this.table||!this.table.tHead)return;e=this.table.tHead.rows;if(e&&e.length>0){i=e[0].children;if(!i||!i.length)return;n=this;for(r=0;r<i.length;r++){s=i[r];a=t.call(this,s,r);if(void 0!==a)return a}}};e.prototype.setTableHeaderEvents=function(){var t=this,e=t.columns,i="ontouchstart"in window,s=i?"touchstart":"mousedown";if(!e)if(!t.dataIsArray)e=b;else return;this.iterateHeaderCells(function(i,r){i.setAttribute(C,e[r]);i.addEventListener(s,function(e){return d.call(this,e,t)})})};e.prototype.updateHeader=function(t){var e=this;this.iterateHeaderCells(function(i,s){var r=i.getAttribute(C),n=e.sortClass,a=e.sortClassDescending,o=e.sortClassAscending;if(!i.classList)return;if(t&&r===t){i.classList.add(n);i.classList.remove(!e.isReversed?a:o);i.classList.add(e.isReversed?a:o)}else{i.classList.remove(n);i.classList.remove(o);i.classList.remove(a)}})};h=function(t,e){var i,s=arguments.length>1&&!!arguments[1];i=function(e){var r,n;if(!e||"object"!==typeof e)return;t(e);if(!s)for(r in e)if(e.hasOwnProperty(r)){n=e[r];i(n)}};return i};e.prototype.watchTriggers=function(t){var e,i=this;if(!t||!I)return;l=function(){i.update()};o=h(function(t){Object.observe(t,l)},!this.isRecursive);for(e=0;e<t.length;e++){o(t[e]);if(!this.triggers)this.triggers=[];this.triggers.push(t[e])}};e.prototype.unwatchTriggers=function(){var t,e=this.triggers;if(!l||!e||!I)return;f=h(function(t){Object.unobserve(t,l)});for(t=0;t<e.length;t++)f(e[t]);l=null};e.prototype.movePage=function(t){var e=t||1,i=this.getPageTotal();if(!this.pageSize)return;this.viewPage(this.pageIndex+e)};e.prototype.viewPage=function(t){var e;if(!this.pageSize)return;e=this.getPageTotal();this.pageIndex=t;if(this.pageIndex<0)this.pageIndex=0;else if(this.pageIndex>e-1)this.pageIndex=Math.max(e-1,0);this.update()};e.prototype.getPageTotal=function(t){var e=0,i=this.dataIsArray,s=i?this.dataSort||this.data:this.keys,r=this.pageSize;if(r&&s&&s.length>0)e=Math.ceil(s.length/r);return e};d=function(t,e){var i,s,r=-1,n=this.parentNode.children;for(i=0;i<n.length;i++)if(n[i]===this){r=i;break}if(r>=0){s=this.getAttribute(C);if(e.dataSort)delete e.dataSort;e.sort(s,true);e.update(["Sort","Update"])}return a(t)};p=function(t,e,i,s){var n,a,o,l,h,f,u,c=this,d=g.attributes,p,m,y,v,b,C,S=g.url||"#",k=r(i,null,true);u=function(t){for(a=0;a<k.length;a++){l=k[a];h="{"+l+"}";f=c.formatCellHtml(i[l]);t=t.replace(h,f)}return t};p=u(g.text||t.toString());S=u(S);if(d){C=r(d,null,true);b={};for(o=0;o<C.length;o++){m=C[o];y=d[m];b[m]=u(y)}}n='<a href="'+S+'"';if(d)n+=this.generateAttributeHtml(b);return n+">"+p+"</a>"};e.prototype.formatLink=function(t,e,i){var s,r=g;g=i;s=p.call(this,t,null,e);g=r;return s};u=function(t,e,i){var s,r;if(i){s=t[i];r=e[i]}else{s=t;r=e}if(s!==r){if(s<r)return-1;if(s>r)return 1}return 0};a=function(t){if(t.stopPropagation)t.stopPropagation();if(t.preventDefault)t.preventDefault();t.cancelBubble=true;t.returnValue=false;return false};n=function(t,e,i){var s,r,n;for(s=0;s<e.length;s++){r="on"+e[s];n=i[r];if("function"===typeof n)t[r]=n}};r=function(t,e,i){var s,r=[];for(s in t)if(t.hasOwnProperty(s)&&(!e||e.indexOf(s)<0))r.push(s);if(!i)r.sort();return r};c=function(t,e){var i,s,r,n=e||"TOP";for(i in t)if(t.hasOwnProperty(i)){s=t[i];if("undefined"===typeof r)r=s;else if(!(s instanceof Date)||null===s||isNaN(s));else if("LOW"===n){if(s<r)r=s}else if(s>r)r=s}return r};s=function(t){var e=i[t],s=e.type||Error,r="Table Error: "+e.text;throw s(r)};i={invalidTable:{text:"Table is not valid."},invalidSettings:{text:"Settings are not valid."}};t.exports.TableView=e}(function(){if("undefined"===typeof module)return{exports:"undefined"===typeof document?{}:window};else return module}());