Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

datagrid

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datagrid - npm Package Compare versions

Comparing version 0.2.7 to 0.2.8

95

dist/datagrid.js

@@ -899,4 +899,2 @@ (function webpackUniversalModuleDefinition(root, factory) {

var indexOf = Array.prototype.indexOf
var DESC = -1 // 降序

@@ -910,2 +908,38 @@ var ASC = 1 // 升序

module.exports = function (DataGrid) {
DataGrid.prototype.sortBy = function (index, fire) {
var columnDef = this.renderData.columnsDef[index]
if (!columnDef || columnDef.sortable === false) return
var sort = this.sort || (this.sort = {})
var $columnsWrapper = this.ui.$columnsWrapper
var lastSortIndex = sort.index
if (index !== lastSortIndex) {
if (typeof lastSortIndex === 'number') {
var lastTH = $columnsWrapper.querySelector('th[data-index="' + lastSortIndex + '"]')
if (lastTH) lastTH.classList.remove(CLASS_ASC, CLASS_DESC)
}
sort.index = index
sort.direction = NONE_ORDER
}
var th = $columnsWrapper.querySelector('th[data-index="' + index + '"]')
th.classList.remove(CLASS_ASC, CLASS_DESC)
switch (sort.direction) {
case NONE_ORDER:
sort.direction = ASC
th.classList.add(CLASS_ASC)
break
case ASC:
sort.direction = DESC
th.classList.add(CLASS_DESC)
break
case DESC:
sort.direction = NONE_ORDER
break
}
if (fire !== false) this.emit('sort', columnDef, sort.direction, index, th)
}
DataGrid.hook(function (datagrid) {

@@ -928,9 +962,2 @@ if (!datagrid.options.columnSorting) return

function clearLastSort () {
if (typeof lastSortColumnIndex === 'number') {
var lastTH = datagrid.ui.$columnsWrapper.querySelector('th:nth-child(' + (lastSortColumnIndex + 1) + ')')
if (lastTH) lastTH.classList.remove(CLASS_ASC, CLASS_DESC)
}
}
datagrid.once('afterInit', function () {

@@ -944,34 +971,11 @@ // 监听字段的点击事件

if (th.classList.contains('resizing')) return
var index = indexOf.call(th.parentElement.children, th)
var columnDef = datagrid.renderData.columnsDef[index]
if (columnDef.sortable === false) return
if (index !== lastSortColumnIndex) {
clearLastSort()
lastSortColumnIndex = index
sortType = NONE_ORDER
}
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)
var index = Number(th.getAttribute('data-index'))
if (!window.isNaN(index)) datagrid.sortBy(index)
}),
datagrid.on('afterSetData', function () {
var lastSortColumnIndex = datagrid.sort && datagrid.sort.index
if (typeof lastSortColumnIndex !== 'number' || lastSortColumnIndex < 0) return
var th = datagrid.ui.$columnsWrapper.querySelector('th[data-index="' + lastSortColumnIndex + '"]')
if (!th) return
switch (sortType) {
switch (datagrid.sort.direction) {
case ASC:

@@ -1122,2 +1126,3 @@ th.classList.add(CLASS_ASC)

__webpack_require__(26)
var slice = Array.prototype.slice
var addEvent = __webpack_require__(8)

@@ -1132,5 +1137,6 @@

var fixedDataGrid
var fixedColumnsLeft
datagrid.on('afterSetData', function (data) {
var fixedColumnsLeft = data.fixedColumnsLeft
fixedColumnsLeft = data.fixedColumnsLeft
if (!fixedColumnsLeft) {

@@ -1226,2 +1232,17 @@ if (fixedDataGrid) fixedDataGrid.el.classList.add('hidden')

}
if (datagrid.options.columnSorting) {
fixedDataGrid.on('sort', function (columnDef, direction, index) {
datagrid.sortBy(index)
})
datagrid.on('sort', function (columnDef, direction, index) {
if (index < fixedColumnsLeft) return
var lastSortIndex = fixedDataGrid.sort && fixedDataGrid.sort.index
if (typeof lastSortIndex === 'number') {
fixedDataGrid.sort.direction = 0
var lastTh = fixedDataGrid.ui.$columnsWrapper.querySelector('th[data-index="' + lastSortIndex + '"]')
if (lastTh) lastTh.classList.remove('order-by-asc', 'order-by-desc')
}
})
}
}

@@ -1228,0 +1249,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 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}}]))});
!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),f.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=[];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 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")},h.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])},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,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},h.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)},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,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")},h.setColumns=function(e){var t=this.renderData.columnsDef=this._normalize(e),r=this._columnsHTML(t);this.emit("beforeRenderColumns",r),this._renderColumns(r)},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,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>"})},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 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>"})},h._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"))},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,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}}]))});
{
"name": "datagrid",
"version": "0.2.7",
"version": "0.2.8",
"description": "Datagrid.",

@@ -5,0 +5,0 @@ "main": "./src/index.js",

require('./index.scss')
var slice = Array.prototype.slice
var addEvent = require('../../utils/addEvent')

@@ -11,5 +12,6 @@

var fixedDataGrid
var fixedColumnsLeft
datagrid.on('afterSetData', function (data) {
var fixedColumnsLeft = data.fixedColumnsLeft
fixedColumnsLeft = data.fixedColumnsLeft
if (!fixedColumnsLeft) {

@@ -105,2 +107,17 @@ if (fixedDataGrid) fixedDataGrid.el.classList.add('hidden')

}
if (datagrid.options.columnSorting) {
fixedDataGrid.on('sort', function (columnDef, direction, index) {
datagrid.sortBy(index)
})
datagrid.on('sort', function (columnDef, direction, index) {
if (index < fixedColumnsLeft) return
var lastSortIndex = fixedDataGrid.sort && fixedDataGrid.sort.index
if (typeof lastSortIndex === 'number') {
fixedDataGrid.sort.direction = 0
var lastTh = fixedDataGrid.ui.$columnsWrapper.querySelector('th[data-index="' + lastSortIndex + '"]')
if (lastTh) lastTh.classList.remove('order-by-asc', 'order-by-desc')
}
})
}
}

@@ -107,0 +124,0 @@ })

@@ -5,4 +5,2 @@ require('./index.scss')

var indexOf = Array.prototype.indexOf
var DESC = -1 // 降序

@@ -16,2 +14,38 @@ var ASC = 1 // 升序

module.exports = function (DataGrid) {
DataGrid.prototype.sortBy = function (index, fire) {
var columnDef = this.renderData.columnsDef[index]
if (!columnDef || columnDef.sortable === false) return
var sort = this.sort || (this.sort = {})
var $columnsWrapper = this.ui.$columnsWrapper
var lastSortIndex = sort.index
if (index !== lastSortIndex) {
if (typeof lastSortIndex === 'number') {
var lastTH = $columnsWrapper.querySelector('th[data-index="' + lastSortIndex + '"]')
if (lastTH) lastTH.classList.remove(CLASS_ASC, CLASS_DESC)
}
sort.index = index
sort.direction = NONE_ORDER
}
var th = $columnsWrapper.querySelector('th[data-index="' + index + '"]')
th.classList.remove(CLASS_ASC, CLASS_DESC)
switch (sort.direction) {
case NONE_ORDER:
sort.direction = ASC
th.classList.add(CLASS_ASC)
break
case ASC:
sort.direction = DESC
th.classList.add(CLASS_DESC)
break
case DESC:
sort.direction = NONE_ORDER
break
}
if (fire !== false) this.emit('sort', columnDef, sort.direction, index, th)
}
DataGrid.hook(function (datagrid) {

@@ -34,9 +68,2 @@ if (!datagrid.options.columnSorting) return

function clearLastSort () {
if (typeof lastSortColumnIndex === 'number') {
var lastTH = datagrid.ui.$columnsWrapper.querySelector('th:nth-child(' + (lastSortColumnIndex + 1) + ')')
if (lastTH) lastTH.classList.remove(CLASS_ASC, CLASS_DESC)
}
}
datagrid.once('afterInit', function () {

@@ -50,34 +77,11 @@ // 监听字段的点击事件

if (th.classList.contains('resizing')) return
var index = indexOf.call(th.parentElement.children, th)
var columnDef = datagrid.renderData.columnsDef[index]
if (columnDef.sortable === false) return
if (index !== lastSortColumnIndex) {
clearLastSort()
lastSortColumnIndex = index
sortType = NONE_ORDER
}
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)
var index = Number(th.getAttribute('data-index'))
if (!window.isNaN(index)) datagrid.sortBy(index)
}),
datagrid.on('afterSetData', function () {
var lastSortColumnIndex = datagrid.sort && datagrid.sort.index
if (typeof lastSortColumnIndex !== 'number' || lastSortColumnIndex < 0) return
var th = datagrid.ui.$columnsWrapper.querySelector('th[data-index="' + lastSortColumnIndex + '"]')
if (!th) return
switch (sortType) {
switch (datagrid.sort.direction) {
case ASC:

@@ -84,0 +88,0 @@ th.classList.add(CLASS_ASC)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc