-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtablebuilder.js
More file actions
1 lines (1 loc) · 5.38 KB
/
tablebuilder.js
File metadata and controls
1 lines (1 loc) · 5.38 KB
1
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.TableBuilder=r():t.TableBuilder=r()}(this,function(){return function(t){function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}var e={};return r.m=t,r.c=e,r.i=function(t){return t},r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},r.p="",r(r.s=6)}([function(t,r,e){function n(t,r){return"<"+r+" "+a(t,function(t,r){return r+'="'+o(t)+'"'}).join(" ")+">"}function i(t){return"</"+t+">"}function o(t){return(t||"").replace(/"/g,""").replace(/'/g,"'")}var a=e(2);t.exports=function(t,r,e){return n(r,t)+e+i(t)}},function(t,r){t.exports=function(t){return"[object Object]"===Object.prototype.toString.call(t)}},function(t,r){t.exports=function(t,r){var e=[];for(var n in t)t.hasOwnProperty(n)&&e.push(r(t[n],n));return e}},function(t,r){t.exports=function(t){var r=[];for(var e in t)t.hasOwnProperty(e)&&r.push([e,t[e]]);return r}},function(t,r,e){var n=e(1),i=function(t){return t},o=e(3),a=e(0),u=e(5),s=t.exports={isDataCorrect:function(t){return t instanceof Array&&(0===t.length||n(t[0]))},isCellValCorrect:function(t){return"string"!=typeof t&&"number"!=typeof t},prismData:function(t,r,e){return t.map(function(t){return r.map(function(r){var o=(r[1],r[0]),a=(e[o]||i)(t[o],t);return[o,n(a)?a:{presentation:a,raw:t[o]}]})})},buildBody:function(t,r,e){return a("tbody",{},t.map(function(t){var n=t[1],i=t[0];return n?s.buildGroupTitle(n,i,r,e)+s.buildRows(n):""}).join("\n"))},buildGroupTitle:function(t,r,e,n){if(!r)return"";var i=e(r,t.length,s.getTotals(t[0],t,n).value()),o=a("td",{class:"group-name-td",colspan:String(t[0].length)},i);return a("tr",{},o)},buildRows:function(t){return t.map(function(t){return t.map(function(t){var r=t[1],e=t[0],n=u(e)+"-td "+(isNaN(+r.raw)?"td_text":"td_num");return a("td",{class:n},r.presentation)}).join("")}).map(function(r,e){var n="__vert_header__"===t[e][0][0]&&t[e][0][1],i=n?{class:u(n.raw)+"-tr","data-id":u(n.raw)}:{};return a("tr",i,r)}).join("\n")},buildHeaders:function(t){return"<thead><tr>"+t.map(function(t){var r=t[1],e=t[0];return a("th",{class:u(e)+"-th"},r)}).join("")+"</tr></thead>"},buildFooter:function(t,r,e){var n=s.getTotals(t,r,e).map(function(t){return a("td",{},t)});return Object.keys(e)<1?"":"<tfoot><tr>"+n.join("")+"</tr></tfoot>"},getTotals:function(t,r,e){return t.map(function(t){var n=t[0],i=r.map(function(t){return t.reduce(function(t,r){return r[0]===n?r[1].raw:t})}),o=function(){return""};return e["*"]&&"__vert_header__"!==n?o=e["*"]:e[n]&&(o=e[n]),o(i,r)})},groupData:function(t,r,e){var n={};return r?t.forEach(function(t){var e=n[t[r].presentation]||[];e.push(t),n[t[r].presentation]=e,delete t[r]}):n[e||""]=t,o(n)},joinVertHeaders:function(t,r){if(!r)return t;var e=function(t,e){return[["__vert_header__",{raw:r[e],presentation:r[e]}]].concat(t.slice(1))};return t.map(e)},deletedByKey:function(t,r){return t.filter(function(t){return t[0]!==r})}}},function(t,r){t.exports=function(){var t={"ё":"yo","й":"i","ц":"ts","у":"u","к":"k","е":"e","н":"n","г":"g","ш":"sh","щ":"sch","з":"z","х":"h","ъ":"","ф":"f","ы":"i","в":"v","а":"a","п":"p","р":"r","о":"o","л":"l","д":"d","ж":"zh","э":"e","я":"ya","ч":"ch","с":"s","м":"m","и":"i","т":"t","ь":"","б":"b","ю":"yu"};return function(r){return r.split("").map(function(r){var e=r.toLowerCase();return e in t?t[e]:e>="a"&&e<="z"||e>="0"&&e<="9"?e:"-"}).join("").replace(/-+/g,"-")}}()},function(t,r,e){var n=function(){var t,r=(e(2),e(3)),n=e(0),i=e(1),o=e(4),a=function(t){return t};return t=function(t,r){this.attributes=t,this.headers=null,this.data=[],this.tableHtml=null,this.prisms={},this.totals={},this.group={field:"",fn:a},this.caption=r},t.prototype.setPrism=function(t,r){var e=function(t){this.prisms[t]=r}.bind(this);return[].concat(t).forEach(e),this},t.prototype.setTotal=function(t,r){var e=this;return(Array.isArray(t)?t:[t]).forEach(function(t){e.totals[t]=r}),this},t.prototype.setGroup=function(t,r){return this.group.field=t,this.group.fn=r||a,this},t.prototype.setHeaders=function(t){return this.headers=i(t)?r(t):t,this},t.prototype.setVertHeaders=function(t,r){return this.vertHeaders=r,this.headers=[["__vert_header__",t]].concat(this.headers),this},t.prototype.setData=function(t){if(!t||!t.length)return this.data=[],this.tbody="",this;if(!o.isDataCorrect(t))throw"invalid format - data expected to be empty, or an array of arrays.";if(!this.headers)throw"invalid format - headers expected to be not empty.";return this.data=o.prismData(t,this.headers,this.prisms),this.data=o.joinVertHeaders(this.data,this.vertHeaders),t=o.groupData(this.data,this.group.field,this.caption),this.group.field&&(this.headers=o.deletedByKey(this.headers,this.group.field)),this.tbody=o.buildBody(t,this.group.fn,this.totals),this},t.prototype.render=function(){if(!this.data.length)return"";this.thead=o.buildHeaders(this.headers),this.tfoot=o.buildFooter(this.headers,this.data,this.totals);var t=this.thead+this.tbody+this.tfoot;return this.tableHtml?this.tableHtml:(this.tableHtml=n("table",this.attributes,t),this.tableHtml)},t}();t.exports=n}])});