UNPKG

slickgrid

Version:

A lightning fast JavaScript grid/spreadsheet

9 lines 16.9 kB
/** * SlickGrid v3.0.4 * (c) 2009-present Michael Leibman * homepage: http://github.com/mleibman/slickgrid * license: MIT * date: 2023-02-15 * file: ./slick.dataview.js */ !function(he){he.extend(!0,window,{Slick:{Data:{DataView:function(r){var i,o,l,a,j,s,u=this,g="id",c=[],d=[],f=new Slick.Map,h=null,p=null,m=null,P=!1,w=!1,v=new Slick.Map,_=!0,y={},B={},S=[],z=[],C=null,K={getter:null,formatter:null,comparer:function(e,t){return e.value===t.value?0:e.value>t.value?1:-1},predefinedValues:[],aggregators:[],aggregateEmpty:!1,aggregateCollapsed:!1,aggregateChildGroups:!1,collapsed:!1,displayTotalsRow:!0,lazyTotalsCalculation:!1},I=[],$=[],R=[],x=":|:",k=null,E=0,G=0,M=0,b=new Slick.Event,n=new Slick.Event,q=new Slick.Event,U=new Slick.Event,L=new Slick.Event,H=new Slick.Event,Q=new Slick.Event,A=new Slick.Event,F=new Slick.Event;function D(e){if(!w)for(var t,n=e=e||0,i=c.length;n<i;n++){if(void 0===(t=c[n][g]))throw new Error("[SlickGrid DataView] Each data element must implement a unique 'id' property");f.set(t,n)}}function W(){if(!w)for(var e,t=0,n=c.length;t<n;t++)if(void 0===(e=c[t][g])||f.get(e)!==t)throw new Error("[SlickGrid DataView] Each data element must implement a unique 'id' property")}function V(){var e=E?Math.max(1,Math.ceil(M/E)):1;return{pageSize:E,pageNum:G,totalRows:M,totalPages:e,dataView:u}}function e(e,t){o=e,i=null,!1===(_=t)&&c.reverse(),c.sort(e),!1===t&&c.reverse(),f=new Slick.Map,D(),T()}function t(e,t){_=t,i=e,o=null;var n=Object.prototype.toString;Object.prototype.toString="function"==typeof e?e:function(){return this[e]},!1===t&&c.reverse(),c.sort(),Object.prototype.toString=n,!1===t&&c.reverse(),f=new Slick.Map,D(),T()}function J(e){r.groupItemMetadataProvider||(r.groupItemMetadataProvider=new Slick.Data.GroupItemMetadataProvider),$=[],R=[],I=(e=e||[])instanceof Array?e:[e];for(var t=0;t<I.length;t++){for(var n=I[t]=he.extend(!0,{},K,I[t]),i=(n.getterIsAFn="function"==typeof n.getter,n.compiledAccumulators=[],n.aggregators.length);i--;)n.compiledAccumulators[i]=function(e){{var t;return e.accumulate?(e=ae(e.accumulate),e=new Function("_items","for (var "+e.params[0]+", _i=0, _il=_items.length; _i<_il; _i++) {"+e.params[0]+" = _items[_i]; "+e.body+"}"),t="compiledAccumulatorLoop",e.displayName=t,e.name=se(e,t),e):function(){}}}(n.aggregators[i]);R[t]={}}T()}function N(){if(!h){h={};for(var e=0,t=d.length;e<t;e++)h[d[e][g]]=e}}function X(e){return c[f.get(e)]}function Y(e,t){if(!f.has(e))throw new Error("[SlickGrid DataView] Invalid id");if(e!==t[g]){var n=t[g];if(null==n)throw new Error("[SlickGrid DataView] Cannot update item to associate with a null id");if(f.has(n))throw new Error("[SlickGrid DataView] Cannot update item to associate with a non-unique id");f.set(n,f.get(e)),f.delete(e),m&&m[e]&&delete m[e],e=n}c[f.get(e)]=t,(m=m||{})[e]=!0}function Z(e,t){Y(e,t),T()}function ee(e,t){c.splice(e,0,t),D(e),T()}function te(e){if(w)v.set(e,!0);else{var t=f.get(e);if(void 0===t)throw new Error("[SlickGrid DataView] Invalid id");f.delete(e),c.splice(t,1),D(t),T()}}function ne(e){if(!o)throw new Error("[SlickGrid DataView] sortedAddItem() requires a sort comparer, use sort()");ee(function(e){var t=0,n=c.length;for(;t<n;){var i=t+n>>>1;-1===o(c[i],e)?t=1+i:n=i}return t}(e),e)}function ie(e,t){if(null==e)for(var n=0;n<I.length;n++)R[n]={},(!0===(I[n].collapsed=t)?F:A).notify({level:n,groupingKey:null});else R[e]={},(!0===(I[e].collapsed=t)?F:A).notify({level:e,groupingKey:null});T()}function re(e,t,n){R[e][t]=I[e].collapsed^n,T()}function oe(e,t){for(var n,i,r,o=[],l={},a=t?t.level+1:0,s=I[a],u=0,g=s.predefinedValues.length;u<g;u++)(n=l[i=s.predefinedValues[u]])||((n=new Slick.Group).value=i,n.level=a,n.groupingKey=(t?t.groupingKey+x:"")+i,o[o.length]=n,l[i]=n);for(u=0,g=e.length;u<g;u++)r=e[u],(n=l[i=s.getterIsAFn?s.getter(r):r[s.getter]])||((n=new Slick.Group).value=i,n.level=a,n.groupingKey=(t?t.groupingKey+x:"")+i,o[o.length]=n,l[i]=n),n.rows[n.count++]=r;if(a<I.length-1)for(u=0;u<o.length;u++)(n=o[u]).groups=oe(n.rows,n);return o.length&&function e(t,n){n=n||0;var i=I[n];var r=i.collapsed;var o=R[n];var l,a=t.length;for(;a--;)(l=t[a]).collapsed&&!i.aggregateCollapsed||(l.groups&&e(l.groups,n+1),i.aggregators.length&&(i.aggregateEmpty||l.rows.length||l.groups&&l.groups.length)&&le(l),l.collapsed=r^o[l.groupingKey],l.title=i.formatter?i.formatter(l):l.value)}(o,a),o.sort(I[a].comparer),o}function O(e){var t,n=e.group,i=I[n.level],r=n.level==I.length,o=i.aggregators.length;if(!r&&i.aggregateChildGroups)for(var l=n.groups.length;l--;)n.groups[l].totals.initialized||O(n.groups[l].totals);for(;o--;)(t=i.aggregators[o]).init(),!r&&i.aggregateChildGroups?i.compiledAccumulators[o].call(t,n.groups):i.compiledAccumulators[o].call(t,n.rows),t.storeResult(e);e.initialized=!0}function le(e){var t=I[e.level],n=new Slick.GroupTotals;(n.group=e).totals=n,t.lazyTotalsCalculation||O(n)}function ae(e){var t=0<=e.toString().indexOf("function")?/^function[^(]*\(([^)]*)\)\s*{([\s\S]*)}$/:/^[^(]*\(([^)]*)\)\s*{([\s\S]*)}$/,e=e.toString().match(t);return{params:e[1].split(","),body:e[2]}}function se(t,n){try{Object.defineProperty(t,"name",{writable:!0,value:n})}catch(e){t.name=n}}function ue(e,t){for(var n=[],i=0,r=0,o=e.length;r<o;r++)p(e[r],t)&&(n[i++]=e[r]);return n}function ge(e,t,n){for(var i,r=[],o=0,l=0,a=e.length;l<a;l++)i=e[l],n[l]?r[o++]=i:p(i,t)&&(r[o++]=i,n[l]=!0);return r}function ce(e){h=null,y.isFilterNarrowing==B.isFilterNarrowing&&y.isFilterExpanding==B.isFilterExpanding||(z=[]);e=e,p?(t=r.inlineFilters?a:ue,n=r.inlineFilters?j:ge,y.isFilterNarrowing?S=t(S,l):y.isFilterExpanding?S=n(e,l,z):y.isFilterUnchanged||(S=t(e,l))):S=E?e:e.concat(),n=E?(S.length<=G*E&&(G=0===S.length?0:Math.floor((S.length-1)/E)),S.slice(E*G,E*G+E)):S;var t={totalRows:S.length,rows:n},e=(M=t.totalRows,t.rows),n=($=[],I.length&&($=oe(e)).length&&(e=function e(t,n){for(var i,r=I[n=n||0],o=[],l=0,a=0,s=t.length;a<s;a++){if(i=t[a],!(o[l++]=i).collapsed)for(var u,g=0,c=(u=i.groups?e(i.groups,n+1):i.rows).length;g<c;g++)o[l++]=u[g];i.totals&&r.displayTotalsRow&&(!i.collapsed||r.aggregateCollapsed)&&(o[l++]=i.totals)}return o}($)),function(e,t){var n,i,r,o=[],l=0,a=Math.max(t.length,e.length);y&&y.ignoreDiffsBefore&&(l=Math.max(0,Math.min(t.length,y.ignoreDiffsBefore))),y&&y.ignoreDiffsAfter&&(a=Math.min(t.length,Math.max(0,y.ignoreDiffsAfter)));for(var s=l,u=e.length;s<a;s++)(u<=s||(n=t[s],i=e[s],!n||I.length&&(r=n.__nonDataRow||i.__nonDataRow)&&n.__group!==i.__group||n.__group&&!n.equals(i)||r&&(n.__groupTotals||i.__groupTotals)||n[g]!=i[g]||m&&m[n[g]]))&&(o[o.length]=s);return o}(d,e));return d=e,n}function T(){var e,t,n,i;P||(e=he.extend(!0,{},V()),t=d.length,n=M,i=ce(c),E&&M<G*E&&(G=Math.max(0,Math.ceil(M/E)-1),i=ce(c)),m=null,B=y,y={},n!==M&&!1!==H.notify(e,null,u)&&Q.notify(V(),null,u),t!==d.length&&q.notify({previous:t,current:d.length,itemCount:c.length,dataView:u,callingOnRowsChanged:0<i.length},null,u),0<i.length&&U.notify({rows:i,itemCount:c.length,dataView:u,calledOnRowCountChanged:t!==d.length},null,u),(t!==d.length||0<i.length)&&L.notify({rowsDiff:i,previousRowCount:t,currentRowCount:d.length,itemCount:c.length,rowCountChanged:t!==d.length,rowsChanged:0<i.length,dataView:u},null,u))}function de(){return k}function fe(){return Array.isArray(k)&&S.filter(function(t){return k.some(function(e){return t[g]===e})})||[]}r=he.extend(!0,{},{groupItemMetadataProvider:null,inlineFilters:!1},r),he.extend(this,{beginUpdate:function(e){w=(P=!0)===e},endUpdate:function(){var e=w;if(P=w=!1,e){for(var t,n,i=0,r=0,o=c.length;r<o;r++){if(n=c[r],void 0===(t=n[g]))throw new Error("[SlickGrid DataView] Each data element must implement a unique 'id' property");v.has(t)?f.delete(t):(c[i]=n,f.set(t,i),++i)}c.length=i,v=new Slick.Map,W()}T()},destroy:function(){c=[],z=[],S=[],j=a=o=m=p=h=f=null,C&&C.onSelectedRowsChanged&&C.onCellCssStylesChanged&&(C.onSelectedRowsChanged.unsubscribe(),C.onCellCssStylesChanged.unsubscribe()),u.onRowsOrCountChanged&&u.onRowsOrCountChanged.unsubscribe()},setPagingOptions:function(e){!1!==H.notify(V(),null,u)&&(null!=e.pageSize&&(E=e.pageSize,G=E?Math.min(G,Math.max(0,Math.ceil(M/E)-1)):0),null!=e.pageNum&&(G=Math.min(e.pageNum,Math.max(0,Math.ceil(M/E)-1))),Q.notify(V(),null,u),T())},getPagingInfo:V,getIdPropertyName:function(){return g},getItems:function(){return c},setItems:function(e,t){void 0!==t&&(g=t),c=S=e,n.notify({idProperty:t,itemCount:c.length},null,u),f=new Slick.Map,D(),W(),T()},setFilter:function(e){var t,n;p=e,r.inlineFilters&&(e=ae(p),t="{ continue _coreloop; }$1",n="{ _retval[_idx++] = $item$; continue _coreloop; }$1",t=e.body.replace(/return false\s*([;}]|\}|$)/gi,t).replace(/return!1([;}]|\}|$)/gi,t).replace(/return true\s*([;}]|\}|$)/gi,n).replace(/return!0([;}]|\}|$)/gi,n).replace(/return ([^;}]+?)\s*([;}]|$)/gi,"{ if ($1) { _retval[_idx++] = $item$; }; continue _coreloop; }$2"),n=(n=(n=(n=["var _retval = [], _idx = 0; ","var $item$, $args$ = _args; ","_coreloop: ","for (var _i = 0, _il = _items.length; _i < _il; _i++) { ","$item$ = _items[_i]; ","$filter$; ","} ","return _retval; "].join("")).replace(/\$filter\$/gi,t)).replace(/\$item\$/gi,e.params[0])).replace(/\$args\$/gi,e.params[1]),t=new Function("_items,_args",n),e="compiledFilter",t.displayName=e,t.name=se(t,e),a=t,n=ae(p),e="{ continue _coreloop; }$1",t="{ _cache[_i] = true;_retval[_idx++] = $item$; continue _coreloop; }$1",e=n.body.replace(/return false\s*([;}]|\}|$)/gi,e).replace(/return!1([;}]|\}|$)/gi,e).replace(/return true\s*([;}]|\}|$)/gi,t).replace(/return!0([;}]|\}|$)/gi,t).replace(/return ([^;}]+?)\s*([;}]|$)/gi,"{ if ((_cache[_i] = $1)) { _retval[_idx++] = $item$; }; continue _coreloop; }$2"),t=(t=(t=(t=["var _retval = [], _idx = 0; ","var $item$, $args$ = _args; ","_coreloop: ","for (var _i = 0, _il = _items.length; _i < _il; _i++) { ","$item$ = _items[_i]; ","if (_cache[_i]) { ","_retval[_idx++] = $item$; ","continue _coreloop; ","} ","$filter$; ","} ","return _retval; "].join("")).replace(/\$filter\$/gi,e)).replace(/\$item\$/gi,n.params[0])).replace(/\$args\$/gi,n.params[1]),e=new Function("_items,_args,_cache",t),n="compiledFilterWithCaching",e.displayName=n,e.name=se(e,n),j=e),T()},getFilter:function(){return p},getFilteredItems:function(){return S},getFilteredItemCount:function(){return S.length},sort:e,fastSort:t,reSort:function(){o?e(o,_):i&&t(i,_)},setGrouping:J,getGrouping:function(){return I},groupBy:function(e,t,n){J(null==e?[]:{getter:e,formatter:t,comparer:n})},setAggregators:function(e,t){if(!I.length)throw new Error("[SlickGrid DataView] At least one grouping must be specified before calling setAggregators().");I[0].aggregators=e,I[0].aggregateCollapsed=t,J(I)},collapseAllGroups:function(e){ie(e,!0)},expandAllGroups:function(e){ie(e,!1)},collapseGroup:function(e){var t,n=Array.prototype.slice.call(arguments),i=n[0];re(i=1===n.length&&-1!==i.indexOf(x)?(t=i).split(x).length-1:(t=n.join(x),n.length-1),t,!0),F.notify({level:i,groupingKey:t})},expandGroup:function(e){var t,n=Array.prototype.slice.call(arguments),i=n[0],i=1===n.length&&-1!==i.indexOf(x)?(t=i.split(x).length-1,i):(t=n.length-1,n.join(x));re(t,i,!1),A.notify({level:t,groupingKey:i})},getGroups:function(){return $},getAllSelectedIds:de,getAllSelectedItems:function(){var t=[];return k.forEach(function(e){t.push(u.getItemById(e))}),t},getAllSelectedFilteredIds:function(){return fe().map(function(e){return e[g]})},getAllSelectedFilteredItems:fe,setSelectedIds:function(e,t){var n=t&&t.isRowBeingAdded,i=t&&t.shouldTriggerEvent,t=t&&t.applyRowSelectionToGrid,r=(!1!==n&&(n=!0),u.mapIdsToRows(e));s(e={grid:C,ids:e,rows:r,added:n,dataView:u}),!1!==i&&b.notify(Object.assign(e,{selectedRowIds:k,filteredIds:u.getAllSelectedFilteredIds()}),new Slick.EventData,u),!1!==t&&C&&C.setSelectedRows(r)},getIdxById:function(e){return f.get(e)},getRowByItem:function(e){return N(),h[e[g]]},getRowById:function(e){return N(),h[e]},getItemById:X,getItemByIdx:function(e){return c[e]},mapItemsToRows:function(e){var t=[];N();for(var n=0,i=e.length;n<i;n++){var r=h[e[n][g]];null!=r&&(t[t.length]=r)}return t},mapRowsToIds:function(e){for(var t=[],n=0,i=e.length;n<i;n++)e[n]<d.length&&(t[t.length]=d[e[n]][g]);return t},mapIdsToRows:function(e){var t=[];N();for(var n=0,i=e.length;n<i;n++){var r=h[e[n]];null!=r&&(t[t.length]=r)}return t},setRefreshHints:function(e){y=e},setFilterArgs:function(e){l=e},refresh:T,updateItem:Z,updateItems:function(e,t){if(e.length!==t.length)throw new Error("[SlickGrid DataView] Mismatch on the length of ids and items provided to update");for(var n=0,i=t.length;n<i;n++)Y(e[n],t[n]);T()},insertItem:ee,insertItems:function(e,t){Array.prototype.splice.apply(c,[e,0].concat(t)),D(e),T()},addItem:function(e){c.push(e),D(c.length-1),T()},addItems:function(e){D((c=c.concat(e)).length-e.length),T()},deleteItem:te,deleteItems:function(e){if(0!==e.length)if(w)for(var t=0,n=e.length;t<n;t++){var i=e[t];if(void 0===(o=f.get(i)))throw new Error("[SlickGrid DataView] Invalid id");v.set(i,!0)}else{for(var r=[],t=0,n=e.length;t<n;t++){var o,i=e[t];if(void 0===(o=f.get(i)))throw new Error("[SlickGrid DataView] Invalid id");f.delete(i),r.push(o)}r.sort();for(t=r.length-1;0<=t;--t)c.splice(r[t],1);D(r[0]),T()}},sortedAddItem:ne,sortedUpdateItem:function(e,t){if(!f.has(e)||e!==t[g])throw new Error("[SlickGrid DataView] Invalid or non-matching id "+f.get(e));if(!o)throw new Error("[SlickGrid DataView] sortedUpdateItem() requires a sort comparer, use sort()");var n=X(e);0!==o(n,t)?(te(e),ne(t)):Z(e,t)},syncGridSelection:function(n,i,e){var r,o=this;function l(e){!1===e?k=[]:k.sort().join(",")!==e.sort().join(",")&&(k=e)}return C=n,k=o.mapRowsToIds(n.getSelectedRows()),n.onSelectedRowsChanged.subscribe(function(e,t){var n;r||(n=o.mapRowsToIds(t.rows),n={grid:C,ids:n,rows:t.rows,added:!0,dataView:o},s(n),b.notify(Object.assign(n,{selectedRowIds:k,filteredIds:o.getAllSelectedFilteredIds()}),new Slick.EventData,o))}),s=function(t){r||(r=!0,l(void 0===t.added?t.ids:t.added?e&&n.getOptions().multiSelect?he.grep(k,function(e){return void 0===o.getRowById(e)}).concat(t.ids):t.ids:e&&n.getOptions().multiSelect?he.grep(k,function(e){return-1===t.ids.indexOf(e)}):[]),r=!1)},this.onRowsOrCountChanged.subscribe(function(){var e,t;0<k.length&&!r&&(r=!0,e=o.mapIdsToRows(k),i||(t={grid:C,ids:o.mapRowsToIds(e),rows:e,dataView:o},s(t),b.notify(Object.assign(t,{selectedRowIds:k,filteredIds:o.getAllSelectedFilteredIds()}),new Slick.EventData,o)),n.setSelectedRows(e),r=!1)}),b},syncGridCellCssStyles:function(i,r){var o,l;function n(e){for(var t in o={},e){var n=d[t][g];o[n]=e[t]}}function a(){if(o){l=!0,N();var e,t={};for(e in o){var n=h[e];null!=n&&(t[n]=o[e])}i.setCellCssStyles(r,t),l=!1}}n(i.getCellCssStyles(r)),i.onCellCssStylesChanged.subscribe(function(e,t){l||r==t.key&&(t.hash?n(t.hash):(i.onCellCssStylesChanged.unsubscribe(),u.onRowsOrCountChanged.unsubscribe(a)))}),this.onRowsOrCountChanged.subscribe(a)},getItemCount:function(){return c.length},getLength:function(){return d.length},getItem:function(e){var t;return(e=d[e])&&e.__group&&e.totals&&!e.totals.initialized?(t=I[e.level]).displayTotalsRow||(O(e.totals),e.title=t.formatter?t.formatter(e):e.value):e&&e.__groupTotals&&!e.initialized&&O(e),e},getItemMetadata:function(e){return void 0===(e=d[e])?null:e.__group?r.groupItemMetadataProvider.getGroupRowMetadata(e):e.__groupTotals?r.groupItemMetadataProvider.getTotalsRowMetadata(e):null},onSelectedRowIdsChanged:b,onSetItemsCalled:n,onRowCountChanged:q,onRowsChanged:U,onRowsOrCountChanged:L,onBeforePagingInfoChanged:H,onPagingInfoChanged:Q,onGroupExpanded:A,onGroupCollapsed:F})},Aggregators:{Avg:function(e){this.field_=e,this.init=function(){this.count_=0,this.nonNullCount_=0,this.sum_=0},this.accumulate=function(e){e=e[this.field_];this.count_++,null==e||""===e||isNaN(e)||(this.nonNullCount_++,this.sum_+=parseFloat(e))},this.storeResult=function(e){e.avg||(e.avg={}),0!==this.nonNullCount_&&(e.avg[this.field_]=this.sum_/this.nonNullCount_)}},Min:function(e){this.field_=e,this.init=function(){this.min_=null},this.accumulate=function(e){e=e[this.field_];null==e||""===e||isNaN(e)||(null==this.min_||e<this.min_)&&(this.min_=e)},this.storeResult=function(e){e.min||(e.min={}),e.min[this.field_]=this.min_}},Max:function(e){this.field_=e,this.init=function(){this.max_=null},this.accumulate=function(e){e=e[this.field_];null==e||""===e||isNaN(e)||(null==this.max_||e>this.max_)&&(this.max_=e)},this.storeResult=function(e){e.max||(e.max={}),e.max[this.field_]=this.max_}},Sum:function(e){this.field_=e,this.init=function(){this.sum_=null},this.accumulate=function(e){e=e[this.field_];null==e||""===e||isNaN(e)||(this.sum_+=parseFloat(e))},this.storeResult=function(e){e.sum||(e.sum={}),e.sum[this.field_]=this.sum_}},Count:function(e){this.field_=e,this.init=function(){},this.storeResult=function(e){e.count||(e.count={}),e.count[this.field_]=e.group.rows.length}}}}}})}(jQuery);