Comparing version 0.2.5 to 0.2.6
@@ -933,20 +933,2 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
function addSortCssClass (th) { | ||
th.classList.remove(CLASS_ASC, CLASS_DESC) | ||
switch (sortType) { | ||
case NONE_ORDER: | ||
sortType = ASC | ||
th.classList.add(CLASS_ASC) | ||
break | ||
case ASC: | ||
sortType = DESC | ||
th.classList.add(CLASS_DESC) | ||
break | ||
case DESC: | ||
sortType = NONE_ORDER | ||
break | ||
} | ||
} | ||
datagrid.once('afterInit', function () { | ||
@@ -970,11 +952,31 @@ // 监听字段的点击事件 | ||
addSortCssClass(th) | ||
th.classList.remove(CLASS_ASC, CLASS_DESC) | ||
switch (sortType) { | ||
case NONE_ORDER: | ||
sortType = ASC | ||
th.classList.add(CLASS_ASC) | ||
break | ||
case ASC: | ||
sortType = DESC | ||
th.classList.add(CLASS_DESC) | ||
break | ||
case DESC: | ||
sortType = NONE_ORDER | ||
break | ||
} | ||
datagrid.emit('sort', columnDef, sortType, th) | ||
}), | ||
// 在每次重新 render 表头之后给表头加上排序状态 | ||
datagrid.on('afterSetData', function () { | ||
if (typeof lastSortColumnIndex !== 'number' || lastSortColumnIndex < 0) return | ||
var th = datagrid.ui.$columnsWrapper.querySelector('th[data-index="' + lastSortColumnIndex + '"]') | ||
var th = datagrid.ui.$columnsWrapper.querySelector('th[data-index=' + lastSortColumnIndex + ']') | ||
if (!th) return | ||
addSortCssClass(th) | ||
switch (sortType) { | ||
case ASC: | ||
th.classList.add(CLASS_ASC) | ||
break | ||
case DESC: | ||
th.classList.add(CLASS_DESC) | ||
break | ||
} | ||
}) | ||
@@ -981,0 +983,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(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),f.forEach(function(e){e(this)},this),this._init()}n(3);var a=n(9),s=n(17),c=n(1),u=n(19),l=n(2),d=n(18),p=100,f=[];i.use=function(e){e(i)},i.hook=function(e){return f.push(e),function(){var t=f.indexOf(e);t>=0&&f.splice(t,1)}};var h=i.prototype=Object.create(s.prototype);h.constructor=i,h._init=function(){var e=this._unbindEvents=[];this.emit("beforeInit");var t=this.el;t.classList.add("datagrid"),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=l("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=l("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=l("td",e.target);if(t){var n=l("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",d(function(){o.setWidth()}))),this.emit("afterInit")},h.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])},h.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)},h.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},h.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 e+c})}}else o=i;n.columnsWidth=o;var u=this._colGroupsHTML(o);this._renderCols(u),this._resize(o)},h._colGroupsHTML=function(e){return e?e.map(function(e){return'<col style="width:'+e+'px">'}):[]},h._renderCols=function(e){var t=this.ui;t.$columnsColGroup.innerHTML=t.$bodyColGroup.innerHTML=e.join("")},h._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")},h.setColumns=function(e){var t=this.renderData.columnsDef=this._normalize(e),n=this._columnsHTML(t);this.emit("beforeRenderColumns",n),this._renderColumns(n)},h._normalize=function(e){return e.map(function(e){var t="string"==typeof e?{name:e}:e;return t.key||(t.key=t.name),t})},h._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>"})},h._renderColumns=function(e){this.ui.$columnsThead.innerHTML=e.join("")},h.setBody=function(e){var t=this.renderData;this.empty=!e||!e.length,t.rows=e,this._renderBody(t.trsArr=this._bodyHTML(t.columnsDef,e))},h._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>"})},h._renderBody=function(e){var t=this.ui,n=t.$body,r=t.$noData,o=t.$bodyTbody;return this.empty?(n.classList.add("hidden"),void r.classList.remove("hidden")):(o.innerHTML=e.join(""),n.classList.remove("hidden"),void r.classList.add("hidden"))},h.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",l=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,d,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"),d=r,v=-(r.clientWidth-i);var c=e.ui,u=c.$columnsWrapper,l=c.$bodyWrapper;t.style.height=u.offsetHeight+l.offsetHeight+"px",h=r.offsetLeft+r.clientWidth-l.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,l,function(n){if(m){setTimeout(function(){d.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=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 a(e,n){var r=o.el.contains(n)?t:o;r.trHover(!1,e,!1)}var s=document.createElement("div");s.classList.add("fixed-datagrid");var c=document.createElement("div");c.classList.add("hidden"),s.appendChild(c),t.el.appendChild(s),o=new e(c,{height:t.ui.$columnsWrapper.offsetHeight+t.ui.$bodyWrapper.offsetHeight,columnSorting:t.options.columnSorting,columnResize:t.options.columnResize,selection:t.options.selection});var u,l,d=!1,p=!1;i.push(r(t.ui.$bodyWrapper,"scroll",function(){p||(d=!0,u&&clearTimeout(u),u=setTimeout(function(){d=!1},250),o.ui.$bodyWrapper.scrollTop=this.scrollTop)}),r(o.ui.$bodyWrapper,"scroll",function(){d||(p=!0,l&&clearTimeout(l),l=setTimeout(function(){p=!1},250),t.ui.$bodyWrapper.scrollTop=this.scrollTop)})),i.push(t.on("trHoverTo",n),o.on("trHoverTo",n),t.on("clearHover",a),o.on("clearHover",a)),t.options.selection&&(t.on("selectedChanged",function(e){o.selectRow(e,!1)}),o.on("selectedChanged",function(e){t.selectRow(e)}))}if(t.options.fixedColumns){var o,i=[];t.on("afterSetData",function(e){var r=e.fixedColumnsLeft;return r?(o||n(t.el),o.el.classList.remove("hidden"),void o.setData({columns:t.renderData.columnsDef.slice(0,r),width:t.renderData.columnsWidth.slice(0,r),rows:t.renderData.rows})):void(o&&o.el.classList.add("hidden"))}),t.once("beforeDestroy",function(){i.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=Array.prototype.indexOf,a=-1,s=1,c=0,u="order-by-asc",l="order-by-desc";e.exports=function(e){e.hook(function(e){function t(){if("number"==typeof d){var t=e.ui.$columnsWrapper.querySelector("th:nth-child("+(d+1)+")");t&&t.classList.remove(u,l)}}function n(e){switch(e.classList.remove(u,l),f){case c:f=s,e.classList.add(u);break;case s:f=a,e.classList.add(l);break;case a:f=c}}if(e.options.columnSorting){var d,p=[],f=c;p.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 a=e.ui.$columnsWrapper;p.push(o(a,"click",function(o){var s=r("th",o.target,a);if(s&&!s.classList.contains("resizing")){var u=i.call(s.parentElement.children,s),l=e.renderData.columnsDef[u];l.sortable!==!1&&(u!==d&&(t(),d=u,f=c),n(s),e.emit("sort",l,f,s))}}),e.on("afterSetData",function(){if(!("number"!=typeof d||d<0)){var t=e.ui.$columnsWrapper.querySelector('th[data-index="'+d+'"]');t&&n(t)}}))}),e.once("beforeDestroy",function(){p.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}}]))}); | ||
!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),f.forEach(function(e){e(this)},this),this._init()}n(3);var a=n(9),s=n(17),c=n(1),u=n(19),l=n(2),d=n(18),p=100,f=[];i.use=function(e){e(i)},i.hook=function(e){return f.push(e),function(){var t=f.indexOf(e);t>=0&&f.splice(t,1)}};var h=i.prototype=Object.create(s.prototype);h.constructor=i,h._init=function(){var e=this._unbindEvents=[];this.emit("beforeInit");var t=this.el;t.classList.add("datagrid"),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=l("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=l("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=l("td",e.target);if(t){var n=l("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",d(function(){o.setWidth()}))),this.emit("afterInit")},h.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])},h.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)},h.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},h.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 e+c})}}else o=i;n.columnsWidth=o;var u=this._colGroupsHTML(o);this._renderCols(u),this._resize(o)},h._colGroupsHTML=function(e){return e?e.map(function(e){return'<col style="width:'+e+'px">'}):[]},h._renderCols=function(e){var t=this.ui;t.$columnsColGroup.innerHTML=t.$bodyColGroup.innerHTML=e.join("")},h._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")},h.setColumns=function(e){var t=this.renderData.columnsDef=this._normalize(e),n=this._columnsHTML(t);this.emit("beforeRenderColumns",n),this._renderColumns(n)},h._normalize=function(e){return e.map(function(e){var t="string"==typeof e?{name:e}:e;return t.key||(t.key=t.name),t})},h._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>"})},h._renderColumns=function(e){this.ui.$columnsThead.innerHTML=e.join("")},h.setBody=function(e){var t=this.renderData;this.empty=!e||!e.length,t.rows=e,this._renderBody(t.trsArr=this._bodyHTML(t.columnsDef,e))},h._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>"})},h._renderBody=function(e){var t=this.ui,n=t.$body,r=t.$noData,o=t.$bodyTbody;return this.empty?(n.classList.add("hidden"),void r.classList.remove("hidden")):(o.innerHTML=e.join(""),n.classList.remove("hidden"),void r.classList.add("hidden"))},h.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",l=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,d,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"),d=r,v=-(r.clientWidth-i);var c=e.ui,u=c.$columnsWrapper,l=c.$bodyWrapper;t.style.height=u.offsetHeight+l.offsetHeight+"px",h=r.offsetLeft+r.clientWidth-l.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,l,function(n){if(m){setTimeout(function(){d.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=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 a(e,n){var r=o.el.contains(n)?t:o;r.trHover(!1,e,!1)}var s=document.createElement("div");s.classList.add("fixed-datagrid");var c=document.createElement("div");c.classList.add("hidden"),s.appendChild(c),t.el.appendChild(s),o=new e(c,{height:t.ui.$columnsWrapper.offsetHeight+t.ui.$bodyWrapper.offsetHeight,columnSorting:t.options.columnSorting,columnResize:t.options.columnResize,selection:t.options.selection});var u,l,d=!1,p=!1;i.push(r(t.ui.$bodyWrapper,"scroll",function(){p||(d=!0,u&&clearTimeout(u),u=setTimeout(function(){d=!1},250),o.ui.$bodyWrapper.scrollTop=this.scrollTop)}),r(o.ui.$bodyWrapper,"scroll",function(){d||(p=!0,l&&clearTimeout(l),l=setTimeout(function(){p=!1},250),t.ui.$bodyWrapper.scrollTop=this.scrollTop)})),i.push(t.on("trHoverTo",n),o.on("trHoverTo",n),t.on("clearHover",a),o.on("clearHover",a)),t.options.selection&&(t.on("selectedChanged",function(e){o.selectRow(e,!1)}),o.on("selectedChanged",function(e){t.selectRow(e)}))}if(t.options.fixedColumns){var o,i=[];t.on("afterSetData",function(e){var r=e.fixedColumnsLeft;return r?(o||n(t.el),o.el.classList.remove("hidden"),void o.setData({columns:t.renderData.columnsDef.slice(0,r),width:t.renderData.columnsWidth.slice(0,r),rows:t.renderData.rows})):void(o&&o.el.classList.add("hidden"))}),t.once("beforeDestroy",function(){i.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=Array.prototype.indexOf,a=-1,s=1,c=0,u="order-by-asc",l="order-by-desc";e.exports=function(e){e.hook(function(e){function t(){if("number"==typeof n){var t=e.ui.$columnsWrapper.querySelector("th:nth-child("+(n+1)+")");t&&t.classList.remove(u,l)}}if(e.options.columnSorting){var n,d=[],p=c;d.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 f=e.ui.$columnsWrapper;d.push(o(f,"click",function(o){var d=r("th",o.target,f);if(d&&!d.classList.contains("resizing")){var h=i.call(d.parentElement.children,d),v=e.renderData.columnsDef[h];if(v.sortable!==!1){switch(h!==n&&(t(),n=h,p=c),d.classList.remove(u,l),p){case c:p=s,d.classList.add(u);break;case s:p=a,d.classList.add(l);break;case a:p=c}e.emit("sort",v,p,d)}}}),e.on("afterSetData",function(){if(!("number"!=typeof n||n<0)){var t=e.ui.$columnsWrapper.querySelector("th[data-index="+n+"]");if(t)switch(p){case s:t.classList.add(u);break;case a:t.classList.add(l)}}}))}),e.once("beforeDestroy",function(){d.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.2.5", | ||
"version": "0.2.6", | ||
"description": "Datagrid.", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -39,20 +39,2 @@ require('./index.scss') | ||
function addSortCssClass (th) { | ||
th.classList.remove(CLASS_ASC, CLASS_DESC) | ||
switch (sortType) { | ||
case NONE_ORDER: | ||
sortType = ASC | ||
th.classList.add(CLASS_ASC) | ||
break | ||
case ASC: | ||
sortType = DESC | ||
th.classList.add(CLASS_DESC) | ||
break | ||
case DESC: | ||
sortType = NONE_ORDER | ||
break | ||
} | ||
} | ||
datagrid.once('afterInit', function () { | ||
@@ -76,11 +58,31 @@ // 监听字段的点击事件 | ||
addSortCssClass(th) | ||
th.classList.remove(CLASS_ASC, CLASS_DESC) | ||
switch (sortType) { | ||
case NONE_ORDER: | ||
sortType = ASC | ||
th.classList.add(CLASS_ASC) | ||
break | ||
case ASC: | ||
sortType = DESC | ||
th.classList.add(CLASS_DESC) | ||
break | ||
case DESC: | ||
sortType = NONE_ORDER | ||
break | ||
} | ||
datagrid.emit('sort', columnDef, sortType, th) | ||
}), | ||
// 在每次重新 render 表头之后给表头加上排序状态 | ||
datagrid.on('afterSetData', function () { | ||
if (typeof lastSortColumnIndex !== 'number' || lastSortColumnIndex < 0) return | ||
var th = datagrid.ui.$columnsWrapper.querySelector('th[data-index="' + lastSortColumnIndex + '"]') | ||
var th = datagrid.ui.$columnsWrapper.querySelector('th[data-index=' + lastSortColumnIndex + ']') | ||
if (!th) return | ||
addSortCssClass(th) | ||
switch (sortType) { | ||
case ASC: | ||
th.classList.add(CLASS_ASC) | ||
break | ||
case DESC: | ||
th.classList.add(CLASS_DESC) | ||
break | ||
} | ||
}) | ||
@@ -87,0 +89,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
96945
2128