Comparing version 0.3.0 to 0.3.1
@@ -243,7 +243,14 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
dp.setData = function (data) { | ||
this.renderData = {} | ||
this.emit('beforeSetData', data) | ||
this.renderData = {} | ||
this.setColumns(data.columns) | ||
this.setBody(data.rows) | ||
this.setWidth(data.width) | ||
this.empty = !(data.columns && data.columns.length) || (!(data.rows && data.rows.length)) | ||
if (this.empty) { | ||
var ui = this.ui | ||
ui.$body.classList.add('hidden') | ||
ui.$noData.classList.remove('hidden') | ||
} else { | ||
this.setColumns(data.columns) | ||
this.setBody(data.rows) | ||
this.setWidth(data.width) | ||
} | ||
this.emit('afterSetData', data) | ||
@@ -301,3 +308,3 @@ } | ||
columnsWidth = columnsMinWidth.map(function (w) { | ||
return w + diff | ||
return Math.round(w + diff) | ||
}) | ||
@@ -460,13 +467,5 @@ } | ||
var ui = this.ui | ||
var $body = ui.$body | ||
var $noData = ui.$noData | ||
var $bodyTbody = ui.$bodyTbody | ||
if (this.empty) { | ||
$body.classList.add('hidden') | ||
$noData.classList.remove('hidden') | ||
return | ||
} | ||
$bodyTbody.innerHTML = trsArr.join('') | ||
$body.classList.remove('hidden') | ||
$noData.classList.add('hidden') | ||
ui.$bodyTbody.innerHTML = trsArr.join('') | ||
ui.$body.classList.remove('hidden') | ||
ui.$noData.classList.add('hidden') | ||
} | ||
@@ -473,0 +472,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Datagrid=t():e.Datagrid=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}(function(e){for(var t in e)if(Object.prototype.hasOwnProperty.call(e,t))switch(typeof e[t]){case"function":break;case"object":e[t]=function(t){var r=t.slice(1),n=e[t[0]];return function(e,t,o){n.apply(this,[e,t,o].concat(r))}}(e[t]);break;default:e[t]=e[e[t]]}return e}([function(e,t,r){e.exports=r(11),e.exports.Selection=r(15),e.exports.Paging=r(14),e.exports.Sorting=r(16),e.exports.ColumnMove=r(12),e.exports.ColumnFixed=r(13)},function(e,t){e.exports=function(e,t,r,n){return e.addEventListener(t,r,n),function(){e.removeEventListener(t,r,n)}}},function(e,t){e.exports=function(e,t,r){r||(r=document.body);var n=e.toUpperCase(),o=t,i=null;do if(o.tagName===n){i=o;break}while(o!==r&&(o=o.parentElement));return i}},function(e,t){},3,3,3,3,3,function(e,t){e.exports='<div class=grid-wrapper> <div class=grid-columns-wrapper> <table class=grid-columns> <colgroup></colgroup> <thead></thead> </table> </div> <div class=grid-body-wrapper> <table class=grid-body> <colgroup></colgroup> <tbody></tbody> </table> </div> </div> <div class="grid-no-data hidden"> <div class=no-data-content> 暂无数据 </div> </div> '},function(e,t){e.exports="<div class=grid-pager> <div>显示第 {{start}} - {{end}} 条数据, 共 {{total}} 条数据</div> <div> <span data-jump=first>首页</span> <span data-jump=prev>上一页</span> <span>第 <input type=text data-page value={{cur}}> 页, 共 {{totalPage}} 页</span> <span data-jump=next>下一页</span> <span data-jump=end>尾页</span> </div> </div> "},function(e,t,r){function n(e){return e.name}function o(e,t){return t[e.key]}function i(e,t){s.call(this);var r=this.el=document.createElement("div");this.options=u({},{height:e.offsetHeight},t),this.origin=e.parentElement.replaceChild(r,e),h.forEach(function(e){e(this)},this),this._init()}r(3);var a=r(9),s=r(17),c=r(1),u=r(19),d=r(2),l=r(18),p=100,f="data-grid",h=[];i.use=function(e){e(i)},i.hook=function(e){return h.push(e),function(){var t=h.indexOf(e);t>=0&&h.splice(t,1)}};var v=i.prototype=Object.create(s.prototype);v.constructor=i,v._init=function(){var e=this._unbindEvents=[];this.emit("beforeInit");var t=this.el;t.classList.add(f),t.innerHTML=a;var r={$gridWrapper:".grid-wrapper",$columnsWrapper:".grid-columns-wrapper",$columns:".grid-columns",$columnsColGroup:".grid-columns colgroup",$columnsThead:".grid-columns thead",$bodyWrapper:".grid-body-wrapper",$body:".grid-body",$bodyColGroup:".grid-body colgroup",$bodyTbody:".grid-body tbody",$noData:".grid-no-data"};for(var n in r)r[n]=t.querySelector(r[n]);r.$gridWrapper.style.height=this.options.height+"px";var o=this;e.push(c(r.$bodyWrapper,"scroll",function(){r.$columns.style.left="-"+r.$bodyWrapper.scrollLeft+"px"}),c(r.$bodyWrapper,"mouseover",function(e){var t=d("tr",e.target);if(t){var r=t.getAttribute("data-index");r&&o.trHover(!0,Number(r))}}),c(r.$bodyWrapper,"mouseout",function(e){var t=d("tr",e.target);if(t){var r=t.getAttribute("data-index");r&&o.trHover(!1,Number(r))}}),c(r.$bodyWrapper,"click",function(e){var t=d("td",e.target);if(t){var r=d("tr",t);if(r){var n=t.getAttribute("data-index"),i=r.getAttribute("data-index"),a=o.renderData;o.emit("cellClick",a.columnsDef[n],o.empty?null:a.rows[i])}}})),this.ui=r,this.options.fit&&e.push(c(window,"resize",l(function(){o.setWidth()}))),this.emit("afterInit")},v.trHover=function(e,t,r){var n=this.ui.$bodyTbody,o=n.querySelector('tr[data-index="'+t+'"]');o.classList[e?"add":"remove"]("hover"),r!==!1&&this.emit(e?"trHoverTo":"clearHover",t,o,this.rows&&this.rows[t])},v.setData=function(e){this.emit("beforeSetData",e),this.renderData={},this.setColumns(e.columns),this.setBody(e.rows),this.setWidth(e.width),this.emit("afterSetData",e)},v.normalizeWidth=function(e){var t=this.renderData,r=t.columnsDef;Array.isArray(e)||(e=[]);for(var n=[],o=0;o<r.length;o++)n[o]=e[o]||Math.max(15*(r[o].name||"").length,p);return n},v.setWidth=function(e){var t={width:e};this.emit("beforeSetWidth",t);var r=this.renderData,n=t.width;!n&&r.columnsMinWidth||(r.columnsMinWidth=this.normalizeWidth(n));var o,i=r.columnsMinWidth;if(this.options.fit){var a=i.reduce(function(e,t){return e+t}),s=this.ui.$bodyWrapper.clientWidth;if(a>=s)o=i;else{var c=(s-a)/r.columnsDef.length;o=i.map(function(e){return e+c})}}else o=i;r.columnsWidth=o;var u=this._colGroupsHTML(o);this._renderCols(u),this._resize(o)},v._colGroupsHTML=function(e){return e?e.map(function(e){return'<col style="width:'+e+'px">'}):[]},v._renderCols=function(e){var t=this.ui;t.$columnsColGroup.innerHTML=t.$bodyColGroup.innerHTML=e.join("")},v._resize=function(e){var t=this.ui,r=t.$columns,n=t.$noData,o=t.$body,i=e.reduce(function(e,t){return e+t});r.style.width=o.style.width=i+"px",this.empty?n.classList.remove("hidden"):n.classList.add("hidden")},v.setColumns=function(e){var t=this.renderData.columnsDef=this._normalize(e),r=this._columnsHTML(t);this.emit("beforeRenderColumns",r),this._renderColumns(r)},v._normalize=function(e){return e.map(function(e){var t="string"==typeof e?{name:e}:e;return t.key||(t.key=t.name),t})},v._columnsHTML=function(e){var t=this.options.thRenderer;return e.map(function(e,r){var o;return t&&(o=t(e)),null==o&&(e.thRenderer&&(o=e.thRenderer(e)),null==o&&(o=n(e))),'<th data-index="'+r+'">'+o+"</th>"})},v._renderColumns=function(e){this.ui.$columnsThead.innerHTML=e.join("")},v.setBody=function(e){var t=this.renderData;this.empty=!e||!e.length,t.rows=e,this._renderBody(t.trsArr=this._bodyHTML(t.columnsDef,e))},v._bodyHTML=function(e,t){var r=this.options.tdRenderer;return this.empty?[]:t.map(function(t,n){var i='<tr data-index="'+n+'">';return e.forEach(function(e,n){var a;r&&(a=r(e,t)),null==a&&(e.tdRenderer&&(a=e.tdRenderer(e,t)),null==a&&(a=o(e,t))),i+='<td data-index="'+n+'">'+a+"</td>"}),i+="</tr>"})},v._renderBody=function(e){var t=this.ui,r=t.$body,n=t.$noData,o=t.$bodyTbody;return this.empty?(r.classList.add("hidden"),void n.classList.remove("hidden")):(o.innerHTML=e.join(""),r.classList.remove("hidden"),void n.classList.add("hidden"))},v.destroy=function(){this.emit("beforeDestroy"),this._unbindEvents.forEach(function(e){e()});var e=this.el;try{e.parentElement.replaceChild(this.origin,e)}catch(t){}this.emit("afterDestroy")},e.exports=i},function(e,t,r){r(4);var n=r(1),o=r(2),i=100,a=Array.prototype.indexOf,s="ontouchstart"in window,c=s?"touchstart":"mousedown",u=s?"touchmove":"mousemove",d=s?"touchend":"mouseup";e.exports=function(e){e.hook(function(e){if(e.options.columnResize){var t=document.createElement("div");t.classList.add("dragging-line");var r=[];e.once("afterInit",function(){e.ui.$draggingLine=t,e.el.appendChild(t)});var s,l,p,f,h,v,m=!1;r.push(e.on("beforeRenderColumns",function(e){e.forEach(function(t,r){e[r]=t.replace("</th>",'<span class="drag-lever"></span></th>')})}),n(e.el,c,function(r){if(r.target.classList.contains("drag-lever")&&0===r.button){var n=o("th",r.target,e.el);if(!n)return;n.classList.add("resizing"),l=n,v=-(n.clientWidth-i);var c=e.ui,u=c.$columnsWrapper,d=c.$bodyWrapper;t.style.height=u.offsetHeight+d.offsetHeight+"px",h=n.offsetLeft+n.clientWidth-d.scrollLeft,t.style.left=h+"px",t.classList.add("show"),m=!0,s=r.target,s.classList.add("dragging"),f=r.pageX,p=a.call(n.parentElement.children,n)}}),n(document,u,function(e){if(m){e.preventDefault();var r=e.pageX-f;r>v&&(t.style.left=h+(e.pageX-f)+"px")}}),n(document,d,function(r){if(m){setTimeout(function(){l.classList.remove("resizing")},0),t.classList.remove("show"),m=!1,s.classList.remove("dragging");var n=r.pageX-f;n<v&&(n=v);var o=e.renderData.columnsMinWidth;o[p]=o[p]+n,e.setWidth(o)}})),e.once("beforeDestroy",function(){r.forEach(function(e){e()})})}})}},function(e,t,r){r(5);var n=(Array.prototype.slice,r(1));e.exports=function(e){e.hook(function(t){function r(){function r(e,r){var n=o.el.contains(r)?t:o;n.trHover(!0,e,!1)}function s(e,r){var n=o.el.contains(r)?t:o;n.trHover(!1,e,!1)}var c=document.createElement("div");c.classList.add("fixed-datagrid");var u=document.createElement("div");u.classList.add("hidden"),c.appendChild(u),t.el.appendChild(c),o=new e(u,{height:t.ui.$columnsWrapper.offsetHeight+t.ui.$bodyWrapper.offsetHeight,columnSorting:t.options.columnSorting,columnResize:t.options.columnResize,selection:t.options.selection});var d,l,p=!1,f=!1;a.push(n(t.ui.$bodyWrapper,"scroll",function(){f||(p=!0,d&&clearTimeout(d),d=setTimeout(function(){p=!1},250),o.ui.$bodyWrapper.scrollTop=this.scrollTop)}),n(o.ui.$bodyWrapper,"scroll",function(){p||(f=!0,l&&clearTimeout(l),l=setTimeout(function(){f=!1},250),t.ui.$bodyWrapper.scrollTop=this.scrollTop)})),a.push(t.on("trHoverTo",r),o.on("trHoverTo",r),t.on("clearHover",s),o.on("clearHover",s)),t.options.selection&&(t.on("selectedChanged",function(e){o.selectRow(e,!1)}),o.on("selectedChanged",function(e){t.selectRow(e)})),t.options.columnSorting&&(o.on("sort",function(e,r,n){t.sortBy(n)}),t.on("sort",function(e,t,r){if(!(r<i)){var n=o.sort&&o.sort.index;if("number"==typeof n){o.sort.direction=0;var a=o.ui.$columnsWrapper.querySelector('th[data-index="'+n+'"]');a&&a.classList.remove("order-by-asc","order-by-desc")}}}))}if(t.options.fixedColumns){var o,i,a=[];t.on("afterSetData",function(e){return(i=e.fixedColumnsLeft)?(o||r(t.el),o.el.classList.remove("hidden"),void o.setData({columns:t.renderData.columnsDef.slice(0,i),width:t.renderData.columnsWidth.slice(0,i),rows:t.renderData.rows})):void(o&&o.el.classList.add("hidden"))}),t.once("beforeDestroy",function(){a.forEach(function(e){e()})})}})}},function(e,t,r){r(6);var n=r(10),o=r(1);e.exports=function(e){e.hook(function(e){function t(t){return Number.isNaN(t)||t<1||t>i.totalPage?void(s.querySelector("[data-page]").value=i.cur):(s.querySelector("[data-page]").value=i.cur=t,void e.emit("switchPage",t))}if(e.options.pagination){var r=e.options.pagingTemplate||n,i=e.pager={cur:1,total:null,size:null,start:null,end:null,totalPage:null},a=[],s=document.createElement("div");s.classList.add("grid-pager-wrapper"),a.push(o(s,"click",function(e){var r=e.target.dataset.jump;if(r){var n;switch(r){case"first":n=1;break;case"end":n=i.totalPage;break;case"prev":n=i.cur-1;break;case"next":n=i.cur+1;break;default:return}t(n)}}),o(s,"keydown",function(e){13===e.keyCode&&void 0!==e.target.dataset.page&&t(Number(e.target.value))}),e.on("beforeSetData",function(e){if(!e.rows||!e.rows.length)return void s.classList.add("hidden");var t=e.size||0,n=e.total||0,o=i.cur,a=e.rows.length;i.total=n,i.size=t,i.start=(o-1)*t+1,i.end=i.start+a-1,i.total=n,i.totalPage=Math.ceil(n/t)||0,s.innerHTML=r.replace(/\{\{(\w+)\}\}/g,function(e,t){return i.hasOwnProperty(t)?i[t]:e}),s.classList.remove("hidden")})),e.once("afterInit",function(){e.ui.$gridWrapper.appendChild(s),e.ui.$pagerWrapper=s}),e.once("beforeDestroy",function(){a.forEach(function(e){e()})})}})}},function(e,t,r){r(7);var n=r(2),o=r(1),i="selected";e.exports=function(e){e.prototype.selectRow=function(e,t){var r=this.ui.$body,n=r.querySelector('tr[data-index="'+e+'"]');if(n){var o=this._selectRowsIndex.indexOf(e);if(this._isMultipleSelect)o>=0?this._selectRowsIndex.splice(o,1):this._selectRowsIndex.push(e);else{if(o>=0)return;var a=r.querySelector("tr."+i);a&&a.classList.remove(i),n.classList.add("selected"),this._selectRowsIndex=[e]}t!==!1&&this.emit("selectedChanged",this._isMultipleSelect?this._selectRowsIndex:e)}},e.prototype.selectAll=function(e,t){if(this._isMultipleSelect){this._selectRowsIndex=[];var r=this;Array.prototype.forEach.call(this.ui.$bodyWrapper.querySelectorAll("tr[data-index]"),function(t){if(e){var n=Number(t.getAttribute("data-index"));r._selectRowsIndex.push(n)}t.querySelector("input[type=checkbox]").checked=e}),t!==!1&&this.emit("selectedChanged",this._selectRowsIndex)}},e.hook(function(e){var t=e.options.selection;if(t){var r=e._isMultipleSelect="multiple"===t,i=[];i.push(e.on("beforeSetData",function(){e._selectRowsIndex=[]})),r&&i.push(e.on("beforeSetData",function(e){e.columns.unshift({sortable:!1,thRenderer:function(){return'<input type="checkbox" class="select-all">'},tdRenderer:function(){return'<input type="checkbox">'}})})),e.once("afterInit",function(){var t=e.ui.$bodyWrapper,a=e.ui.$columnsWrapper;r?(i.push(o(t,"change",function(r){if("INPUT"===r.target.tagName&&"checkbox"===r.target.type){a.querySelector("input.select-all").checked=!1;var o=n("tr",r.target,t);if(o){var i=Number(o.getAttribute("data-index"));Number.isNaN(i)||e.selectRow(i),a.querySelector("input[type=checkbox].select-all").checked=!e.empty&&e._selectRowsIndex.length===e.renderData.rows.length}}})),i.push(o(a,"change",function(t){var r=t.target;"INPUT"===r.tagName&&"checkbox"===r.type&&r.classList.contains("select-all")&&e.selectAll(r.checked)}))):i.push(o(t,"click",function(r){var o=n("tr",r.target,t);if(o){var i=Number(o.getAttribute("data-index"));Number.isNaN(i)||e.selectRow(i)}}))}),e.once("beforeDestroy",function(){i.forEach(function(e){e()})})}})}},function(e,t,r){r(8);var n=r(2),o=r(1),i=-1,a=1,s=0,c="order-by-asc",u="order-by-desc";e.exports=function(e){e.prototype.sortBy=function(e,t){var r=this.renderData.columnsDef[e];if(r&&r.sortable!==!1){var n=this.sort||(this.sort={}),o=this.ui.$columnsWrapper,d=n.index;if(e!==d){if("number"==typeof d){var l=o.querySelector('th[data-index="'+d+'"]');l&&l.classList.remove(c,u)}n.index=e,n.direction=s}var p=o.querySelector('th[data-index="'+e+'"]');switch(p.classList.remove(c,u),n.direction){case s:n.direction=a,p.classList.add(c);break;case a:n.direction=i,p.classList.add(u);break;case i:n.direction=s}t!==!1&&this.emit("sort",r,n.direction,e,p)}},e.hook(function(e){if(e.options.columnSorting){var t=[];t.push(e.on("beforeRenderColumns",function(e){e.forEach(function(t,r){e[r]=t.replace("</th>",'<span class="order-ico"></span></th>')})})),e.once("afterInit",function(){var r=e.ui.$columnsWrapper;t.push(o(r,"click",function(t){var o=n("th",t.target,r);if(o&&!o.classList.contains("resizing")){var i=Number(o.getAttribute("data-index"));window.isNaN(i)||e.sortBy(i)}}),e.on("afterSetData",function(){var t=e.sort&&e.sort.index;if(!("number"!=typeof t||t<0)){var r=e.ui.$columnsWrapper.querySelector('th[data-index="'+t+'"]');if(r)switch(e.sort.direction){case a:r.classList.add(c);break;case i:r.classList.add(u)}}}))}),e.once("beforeDestroy",function(){t.forEach(function(e){e()})})}})}},function(e,t){function r(){this._callbacks={}}var n=Array.prototype.slice,o=r.prototype;o.on=function(e,t){var r=this._callbacks,n=r[e]||(r[e]=[]);return n.push(t),function(){var e=n.indexOf(t);e>=0&&n.splice(e,1)}},o.once=function(e,t){var r=this.on(e,function(){t.apply(null,arguments),window.setTimeout(r,0)});return r},o.emit=function(e){var t=this._callbacks[e];if(t&&t.length){var r=n.call(arguments,1);t.forEach(function(e){e.apply(null,r)})}},e.exports=r},function(e,t){e.exports=function(e,t){var r;return t="number"==typeof t?t:250,function(){"number"==typeof r&&window.clearTimeout(r);var n=arguments;r=window.setTimeout(function(){e.apply(null,n)},t)}}},function(e,t){var r=Object.prototype.hasOwnProperty;e.exports=function(e){for(var t=1;t<arguments.length;t+=1){var n=arguments[t];if(n&&"object"==typeof n)for(var o in n)r.call(n,o)&&(e[o]=n[o])}return e}}]))}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Datagrid=t():e.Datagrid=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}(function(e){for(var t in e)if(Object.prototype.hasOwnProperty.call(e,t))switch(typeof e[t]){case"function":break;case"object":e[t]=function(t){var n=t.slice(1),r=e[t[0]];return function(e,t,o){r.apply(this,[e,t,o].concat(n))}}(e[t]);break;default:e[t]=e[e[t]]}return e}([function(e,t,n){e.exports=n(11),e.exports.Selection=n(15),e.exports.Paging=n(14),e.exports.Sorting=n(16),e.exports.ColumnMove=n(12),e.exports.ColumnFixed=n(13)},function(e,t){e.exports=function(e,t,n,r){return e.addEventListener(t,n,r),function(){e.removeEventListener(t,n,r)}}},function(e,t){e.exports=function(e,t,n){n||(n=document.body);var r=e.toUpperCase(),o=t,i=null;do if(o.tagName===r){i=o;break}while(o!==n&&(o=o.parentElement));return i}},function(e,t){},3,3,3,3,3,function(e,t){e.exports='<div class=grid-wrapper> <div class=grid-columns-wrapper> <table class=grid-columns> <colgroup></colgroup> <thead></thead> </table> </div> <div class=grid-body-wrapper> <table class=grid-body> <colgroup></colgroup> <tbody></tbody> </table> </div> </div> <div class="grid-no-data hidden"> <div class=no-data-content> 暂无数据 </div> </div> '},function(e,t){e.exports="<div class=grid-pager> <div>显示第 {{start}} - {{end}} 条数据, 共 {{total}} 条数据</div> <div> <span data-jump=first>首页</span> <span data-jump=prev>上一页</span> <span>第 <input type=text data-page value={{cur}}> 页, 共 {{totalPage}} 页</span> <span data-jump=next>下一页</span> <span data-jump=end>尾页</span> </div> </div> "},function(e,t,n){function r(e){return e.name}function o(e,t){return t[e.key]}function i(e,t){s.call(this);var n=this.el=document.createElement("div");this.options=u({},{height:e.offsetHeight},t),this.origin=e.parentElement.replaceChild(n,e),h.forEach(function(e){e(this)},this),this._init()}n(3);var a=n(9),s=n(17),c=n(1),u=n(19),d=n(2),l=n(18),p=100,f="data-grid",h=[];i.use=function(e){e(i)},i.hook=function(e){return h.push(e),function(){var t=h.indexOf(e);t>=0&&h.splice(t,1)}};var v=i.prototype=Object.create(s.prototype);v.constructor=i,v._init=function(){var e=this._unbindEvents=[];this.emit("beforeInit");var t=this.el;t.classList.add(f),t.innerHTML=a;var n={$gridWrapper:".grid-wrapper",$columnsWrapper:".grid-columns-wrapper",$columns:".grid-columns",$columnsColGroup:".grid-columns colgroup",$columnsThead:".grid-columns thead",$bodyWrapper:".grid-body-wrapper",$body:".grid-body",$bodyColGroup:".grid-body colgroup",$bodyTbody:".grid-body tbody",$noData:".grid-no-data"};for(var r in n)n[r]=t.querySelector(n[r]);n.$gridWrapper.style.height=this.options.height+"px";var o=this;e.push(c(n.$bodyWrapper,"scroll",function(){n.$columns.style.left="-"+n.$bodyWrapper.scrollLeft+"px"}),c(n.$bodyWrapper,"mouseover",function(e){var t=d("tr",e.target);if(t){var n=t.getAttribute("data-index");n&&o.trHover(!0,Number(n))}}),c(n.$bodyWrapper,"mouseout",function(e){var t=d("tr",e.target);if(t){var n=t.getAttribute("data-index");n&&o.trHover(!1,Number(n))}}),c(n.$bodyWrapper,"click",function(e){var t=d("td",e.target);if(t){var n=d("tr",t);if(n){var r=t.getAttribute("data-index"),i=n.getAttribute("data-index"),a=o.renderData;o.emit("cellClick",a.columnsDef[r],o.empty?null:a.rows[i])}}})),this.ui=n,this.options.fit&&e.push(c(window,"resize",l(function(){o.setWidth()}))),this.emit("afterInit")},v.trHover=function(e,t,n){var r=this.ui.$bodyTbody,o=r.querySelector('tr[data-index="'+t+'"]');o.classList[e?"add":"remove"]("hover"),n!==!1&&this.emit(e?"trHoverTo":"clearHover",t,o,this.rows&&this.rows[t])},v.setData=function(e){if(this.renderData={},this.emit("beforeSetData",e),this.empty=!(e.columns&&e.columns.length&&e.rows&&e.rows.length),this.empty){var t=this.ui;t.$body.classList.add("hidden"),t.$noData.classList.remove("hidden")}else this.setColumns(e.columns),this.setBody(e.rows),this.setWidth(e.width);this.emit("afterSetData",e)},v.normalizeWidth=function(e){var t=this.renderData,n=t.columnsDef;Array.isArray(e)||(e=[]);for(var r=[],o=0;o<n.length;o++)r[o]=e[o]||Math.max(15*(n[o].name||"").length,p);return r},v.setWidth=function(e){var t={width:e};this.emit("beforeSetWidth",t);var n=this.renderData,r=t.width;!r&&n.columnsMinWidth||(n.columnsMinWidth=this.normalizeWidth(r));var o,i=n.columnsMinWidth;if(this.options.fit){var a=i.reduce(function(e,t){return e+t}),s=this.ui.$bodyWrapper.clientWidth;if(a>=s)o=i;else{var c=(s-a)/n.columnsDef.length;o=i.map(function(e){return Math.round(e+c)})}}else o=i;n.columnsWidth=o;var u=this._colGroupsHTML(o);this._renderCols(u),this._resize(o)},v._colGroupsHTML=function(e){return e?e.map(function(e){return'<col style="width:'+e+'px">'}):[]},v._renderCols=function(e){var t=this.ui;t.$columnsColGroup.innerHTML=t.$bodyColGroup.innerHTML=e.join("")},v._resize=function(e){var t=this.ui,n=t.$columns,r=t.$noData,o=t.$body,i=e.reduce(function(e,t){return e+t});n.style.width=o.style.width=i+"px",this.empty?r.classList.remove("hidden"):r.classList.add("hidden")},v.setColumns=function(e){var t=this.renderData.columnsDef=this._normalize(e),n=this._columnsHTML(t);this.emit("beforeRenderColumns",n),this._renderColumns(n)},v._normalize=function(e){return e.map(function(e){var t="string"==typeof e?{name:e}:e;return t.key||(t.key=t.name),t})},v._columnsHTML=function(e){var t=this.options.thRenderer;return e.map(function(e,n){var o;return t&&(o=t(e)),null==o&&(e.thRenderer&&(o=e.thRenderer(e)),null==o&&(o=r(e))),'<th data-index="'+n+'">'+o+"</th>"})},v._renderColumns=function(e){this.ui.$columnsThead.innerHTML=e.join("")},v.setBody=function(e){var t=this.renderData;this.empty=!e||!e.length,t.rows=e,this._renderBody(t.trsArr=this._bodyHTML(t.columnsDef,e))},v._bodyHTML=function(e,t){var n=this.options.tdRenderer;return this.empty?[]:t.map(function(t,r){var i='<tr data-index="'+r+'">';return e.forEach(function(e,r){var a;n&&(a=n(e,t)),null==a&&(e.tdRenderer&&(a=e.tdRenderer(e,t)),null==a&&(a=o(e,t))),i+='<td data-index="'+r+'">'+a+"</td>"}),i+="</tr>"})},v._renderBody=function(e){var t=this.ui;t.$bodyTbody.innerHTML=e.join(""),t.$body.classList.remove("hidden"),t.$noData.classList.add("hidden")},v.destroy=function(){this.emit("beforeDestroy"),this._unbindEvents.forEach(function(e){e()});var e=this.el;try{e.parentElement.replaceChild(this.origin,e)}catch(t){}this.emit("afterDestroy")},e.exports=i},function(e,t,n){n(4);var r=n(1),o=n(2),i=100,a=Array.prototype.indexOf,s="ontouchstart"in window,c=s?"touchstart":"mousedown",u=s?"touchmove":"mousemove",d=s?"touchend":"mouseup";e.exports=function(e){e.hook(function(e){if(e.options.columnResize){var t=document.createElement("div");t.classList.add("dragging-line");var n=[];e.once("afterInit",function(){e.ui.$draggingLine=t,e.el.appendChild(t)});var s,l,p,f,h,v,m=!1;n.push(e.on("beforeRenderColumns",function(e){e.forEach(function(t,n){e[n]=t.replace("</th>",'<span class="drag-lever"></span></th>')})}),r(e.el,c,function(n){if(n.target.classList.contains("drag-lever")&&0===n.button){var r=o("th",n.target,e.el);if(!r)return;r.classList.add("resizing"),l=r,v=-(r.clientWidth-i);var c=e.ui,u=c.$columnsWrapper,d=c.$bodyWrapper;t.style.height=u.offsetHeight+d.offsetHeight+"px",h=r.offsetLeft+r.clientWidth-d.scrollLeft,t.style.left=h+"px",t.classList.add("show"),m=!0,s=n.target,s.classList.add("dragging"),f=n.pageX,p=a.call(r.parentElement.children,r)}}),r(document,u,function(e){if(m){e.preventDefault();var n=e.pageX-f;n>v&&(t.style.left=h+(e.pageX-f)+"px")}}),r(document,d,function(n){if(m){setTimeout(function(){l.classList.remove("resizing")},0),t.classList.remove("show"),m=!1,s.classList.remove("dragging");var r=n.pageX-f;r<v&&(r=v);var o=e.renderData.columnsMinWidth;o[p]=o[p]+r,e.setWidth(o)}})),e.once("beforeDestroy",function(){n.forEach(function(e){e()})})}})}},function(e,t,n){n(5);var r=(Array.prototype.slice,n(1));e.exports=function(e){e.hook(function(t){function n(){function n(e,n){var r=o.el.contains(n)?t:o;r.trHover(!0,e,!1)}function s(e,n){var r=o.el.contains(n)?t:o;r.trHover(!1,e,!1)}var c=document.createElement("div");c.classList.add("fixed-datagrid");var u=document.createElement("div");u.classList.add("hidden"),c.appendChild(u),t.el.appendChild(c),o=new e(u,{height:t.ui.$columnsWrapper.offsetHeight+t.ui.$bodyWrapper.offsetHeight,columnSorting:t.options.columnSorting,columnResize:t.options.columnResize,selection:t.options.selection});var d,l,p=!1,f=!1;a.push(r(t.ui.$bodyWrapper,"scroll",function(){f||(p=!0,d&&clearTimeout(d),d=setTimeout(function(){p=!1},250),o.ui.$bodyWrapper.scrollTop=this.scrollTop)}),r(o.ui.$bodyWrapper,"scroll",function(){p||(f=!0,l&&clearTimeout(l),l=setTimeout(function(){f=!1},250),t.ui.$bodyWrapper.scrollTop=this.scrollTop)})),a.push(t.on("trHoverTo",n),o.on("trHoverTo",n),t.on("clearHover",s),o.on("clearHover",s)),t.options.selection&&(t.on("selectedChanged",function(e){o.selectRow(e,!1)}),o.on("selectedChanged",function(e){t.selectRow(e)})),t.options.columnSorting&&(o.on("sort",function(e,n,r){t.sortBy(r)}),t.on("sort",function(e,t,n){if(!(n<i)){var r=o.sort&&o.sort.index;if("number"==typeof r){o.sort.direction=0;var a=o.ui.$columnsWrapper.querySelector('th[data-index="'+r+'"]');a&&a.classList.remove("order-by-asc","order-by-desc")}}}))}if(t.options.fixedColumns){var o,i,a=[];t.on("afterSetData",function(e){return(i=e.fixedColumnsLeft)?(o||n(t.el),o.el.classList.remove("hidden"),void o.setData({columns:t.renderData.columnsDef.slice(0,i),width:t.renderData.columnsWidth.slice(0,i),rows:t.renderData.rows})):void(o&&o.el.classList.add("hidden"))}),t.once("beforeDestroy",function(){a.forEach(function(e){e()})})}})}},function(e,t,n){n(6);var r=n(10),o=n(1);e.exports=function(e){e.hook(function(e){function t(t){return Number.isNaN(t)||t<1||t>i.totalPage?void(s.querySelector("[data-page]").value=i.cur):(s.querySelector("[data-page]").value=i.cur=t,void e.emit("switchPage",t))}if(e.options.pagination){var n=e.options.pagingTemplate||r,i=e.pager={cur:1,total:null,size:null,start:null,end:null,totalPage:null},a=[],s=document.createElement("div");s.classList.add("grid-pager-wrapper"),a.push(o(s,"click",function(e){var n=e.target.dataset.jump;if(n){var r;switch(n){case"first":r=1;break;case"end":r=i.totalPage;break;case"prev":r=i.cur-1;break;case"next":r=i.cur+1;break;default:return}t(r)}}),o(s,"keydown",function(e){13===e.keyCode&&void 0!==e.target.dataset.page&&t(Number(e.target.value))}),e.on("beforeSetData",function(e){if(!e.rows||!e.rows.length)return void s.classList.add("hidden");var t=e.size||0,r=e.total||0,o=i.cur,a=e.rows.length;i.total=r,i.size=t,i.start=(o-1)*t+1,i.end=i.start+a-1,i.total=r,i.totalPage=Math.ceil(r/t)||0,s.innerHTML=n.replace(/\{\{(\w+)\}\}/g,function(e,t){return i.hasOwnProperty(t)?i[t]:e}),s.classList.remove("hidden")})),e.once("afterInit",function(){e.ui.$gridWrapper.appendChild(s),e.ui.$pagerWrapper=s}),e.once("beforeDestroy",function(){a.forEach(function(e){e()})})}})}},function(e,t,n){n(7);var r=n(2),o=n(1),i="selected";e.exports=function(e){e.prototype.selectRow=function(e,t){var n=this.ui.$body,r=n.querySelector('tr[data-index="'+e+'"]');if(r){var o=this._selectRowsIndex.indexOf(e);if(this._isMultipleSelect)o>=0?this._selectRowsIndex.splice(o,1):this._selectRowsIndex.push(e);else{if(o>=0)return;var a=n.querySelector("tr."+i);a&&a.classList.remove(i),r.classList.add("selected"),this._selectRowsIndex=[e]}t!==!1&&this.emit("selectedChanged",this._isMultipleSelect?this._selectRowsIndex:e)}},e.prototype.selectAll=function(e,t){if(this._isMultipleSelect){this._selectRowsIndex=[];var n=this;Array.prototype.forEach.call(this.ui.$bodyWrapper.querySelectorAll("tr[data-index]"),function(t){if(e){var r=Number(t.getAttribute("data-index"));n._selectRowsIndex.push(r)}t.querySelector("input[type=checkbox]").checked=e}),t!==!1&&this.emit("selectedChanged",this._selectRowsIndex)}},e.hook(function(e){var t=e.options.selection;if(t){var n=e._isMultipleSelect="multiple"===t,i=[];i.push(e.on("beforeSetData",function(){e._selectRowsIndex=[]})),n&&i.push(e.on("beforeSetData",function(e){e.columns.unshift({sortable:!1,thRenderer:function(){return'<input type="checkbox" class="select-all">'},tdRenderer:function(){return'<input type="checkbox">'}})})),e.once("afterInit",function(){var t=e.ui.$bodyWrapper,a=e.ui.$columnsWrapper;n?(i.push(o(t,"change",function(n){if("INPUT"===n.target.tagName&&"checkbox"===n.target.type){a.querySelector("input.select-all").checked=!1;var o=r("tr",n.target,t);if(o){var i=Number(o.getAttribute("data-index"));Number.isNaN(i)||e.selectRow(i),a.querySelector("input[type=checkbox].select-all").checked=!e.empty&&e._selectRowsIndex.length===e.renderData.rows.length}}})),i.push(o(a,"change",function(t){var n=t.target;"INPUT"===n.tagName&&"checkbox"===n.type&&n.classList.contains("select-all")&&e.selectAll(n.checked)}))):i.push(o(t,"click",function(n){var o=r("tr",n.target,t);if(o){var i=Number(o.getAttribute("data-index"));Number.isNaN(i)||e.selectRow(i)}}))}),e.once("beforeDestroy",function(){i.forEach(function(e){e()})})}})}},function(e,t,n){n(8);var r=n(2),o=n(1),i=-1,a=1,s=0,c="order-by-asc",u="order-by-desc";e.exports=function(e){e.prototype.sortBy=function(e,t){var n=this.renderData.columnsDef[e];if(n&&n.sortable!==!1){var r=this.sort||(this.sort={}),o=this.ui.$columnsWrapper,d=r.index;if(e!==d){if("number"==typeof d){var l=o.querySelector('th[data-index="'+d+'"]');l&&l.classList.remove(c,u)}r.index=e,r.direction=s}var p=o.querySelector('th[data-index="'+e+'"]');switch(p.classList.remove(c,u),r.direction){case s:r.direction=a,p.classList.add(c);break;case a:r.direction=i,p.classList.add(u);break;case i:r.direction=s}t!==!1&&this.emit("sort",n,r.direction,e,p)}},e.hook(function(e){if(e.options.columnSorting){var t=[];t.push(e.on("beforeRenderColumns",function(e){e.forEach(function(t,n){e[n]=t.replace("</th>",'<span class="order-ico"></span></th>')})})),e.once("afterInit",function(){var n=e.ui.$columnsWrapper;t.push(o(n,"click",function(t){var o=r("th",t.target,n);if(o&&!o.classList.contains("resizing")){var i=Number(o.getAttribute("data-index"));window.isNaN(i)||e.sortBy(i)}}),e.on("afterSetData",function(){var t=e.sort&&e.sort.index;if(!("number"!=typeof t||t<0)){var n=e.ui.$columnsWrapper.querySelector('th[data-index="'+t+'"]');if(n)switch(e.sort.direction){case a:n.classList.add(c);break;case i:n.classList.add(u)}}}))}),e.once("beforeDestroy",function(){t.forEach(function(e){e()})})}})}},function(e,t){function n(){this._callbacks={}}var r=Array.prototype.slice,o=n.prototype;o.on=function(e,t){var n=this._callbacks,r=n[e]||(n[e]=[]);return r.push(t),function(){var e=r.indexOf(t);e>=0&&r.splice(e,1)}},o.once=function(e,t){var n=this.on(e,function(){t.apply(null,arguments),window.setTimeout(n,0)});return n},o.emit=function(e){var t=this._callbacks[e];if(t&&t.length){var n=r.call(arguments,1);t.forEach(function(e){e.apply(null,n)})}},e.exports=n},function(e,t){e.exports=function(e,t){var n;return t="number"==typeof t?t:250,function(){"number"==typeof n&&window.clearTimeout(n);var r=arguments;n=window.setTimeout(function(){e.apply(null,r)},t)}}},function(e,t){var n=Object.prototype.hasOwnProperty;e.exports=function(e){for(var t=1;t<arguments.length;t+=1){var r=arguments[t];if(r&&"object"==typeof r)for(var o in r)n.call(r,o)&&(e[o]=r[o])}return e}}]))}); |
{ | ||
"name": "datagrid", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Datagrid.", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -175,7 +175,14 @@ require('./index.scss') | ||
dp.setData = function (data) { | ||
this.renderData = {} | ||
this.emit('beforeSetData', data) | ||
this.renderData = {} | ||
this.setColumns(data.columns) | ||
this.setBody(data.rows) | ||
this.setWidth(data.width) | ||
this.empty = !(data.columns && data.columns.length) || (!(data.rows && data.rows.length)) | ||
if (this.empty) { | ||
var ui = this.ui | ||
ui.$body.classList.add('hidden') | ||
ui.$noData.classList.remove('hidden') | ||
} else { | ||
this.setColumns(data.columns) | ||
this.setBody(data.rows) | ||
this.setWidth(data.width) | ||
} | ||
this.emit('afterSetData', data) | ||
@@ -233,3 +240,3 @@ } | ||
columnsWidth = columnsMinWidth.map(function (w) { | ||
return w + diff | ||
return Math.round(w + diff) | ||
}) | ||
@@ -392,13 +399,5 @@ } | ||
var ui = this.ui | ||
var $body = ui.$body | ||
var $noData = ui.$noData | ||
var $bodyTbody = ui.$bodyTbody | ||
if (this.empty) { | ||
$body.classList.add('hidden') | ||
$noData.classList.remove('hidden') | ||
return | ||
} | ||
$bodyTbody.innerHTML = trsArr.join('') | ||
$body.classList.remove('hidden') | ||
$noData.classList.add('hidden') | ||
ui.$bodyTbody.innerHTML = trsArr.join('') | ||
ui.$body.classList.remove('hidden') | ||
ui.$noData.classList.add('hidden') | ||
} | ||
@@ -405,0 +404,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99736
2167