UNPKG

slickgrid

Version:

A lightning fast JavaScript grid/spreadsheet

9 lines 15.9 kB
/** * SlickGrid v3.0.2 * (c) 2009-present Michael Leibman * homepage: http://github.com/mleibman/slickgrid * license: MIT * date: 2022-11-14 * file: ./slick.dataview.js */ !function(ce){ce.extend(!0,window,{Slick:{Data:{DataView:function(r){var i,o,l,a,u,s=this,g="id",c=[],f=[],d=new Slick.Map,h=null,p=null,m=null,v=!1,w=!1,_=new Slick.Map,y=!0,C={},O={},S=[],P=[],e=null,j={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,M=0,G=0,n=new Slick.Event,B=new Slick.Event,z=new Slick.Event,K=new Slick.Event,q=new Slick.Event,U=new Slick.Event,b=new Slick.Event,A=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");d.set(t,n)}}function L(){if(!w)for(var e,t=0,n=c.length;t<n;t++)if(void 0===(e=c[t][g])||d.get(e)!==t)throw new Error("[SlickGrid DataView] Each data element must implement a unique 'id' property")}function F(){var e=E?Math.max(1,Math.ceil(G/E)):1;return{pageSize:E,pageNum:M,totalRows:G,totalPages:e,dataView:s}}function t(e,t){o=e,i=null,!1===(y=t)&&c.reverse(),c.sort(e),!1===t&&c.reverse(),d=new Slick.Map,D(),T()}function H(e,t){y=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(),d=new Slick.Map,D(),T()}function Q(e){r.groupItemMetadataProvider||(r.groupItemMetadataProvider=new Slick.Data.GroupItemMetadataProvider),I=[],R=[],$=(e=e||[])instanceof Array?e:[e];for(var t=0;t<$.length;t++){for(var n=$[t]=ce.extend(!0,{},j,$[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=oe(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=le(e,t),e):function(){}}}(n.aggregators[i]);R[t]={}}T()}function V(){if(!h){h={};for(var e=0,t=f.length;e<t;e++)h[f[e][g]]=e}}function W(e){return c[d.get(e)]}function J(e,t){if(!d.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(d.has(n))throw new Error("[SlickGrid DataView] Cannot update item to associate with a non-unique id");d.set(n,d.get(e)),d.delete(e),m&&m[e]&&delete m[e],e=n}c[d.get(e)]=t,(m=m||{})[e]=!0}function X(e,t){J(e,t),T()}function Y(e,t){c.splice(e,0,t),D(e),T()}function Z(e){if(w)_.set(e,!0);else{var t=d.get(e);if(void 0===t)throw new Error("[SlickGrid DataView] Invalid id");d.delete(e),c.splice(t,1),D(t),T()}}function ee(e){if(!o)throw new Error("[SlickGrid DataView] sortedAddItem() requires a sort comparer, use sort()");Y(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 te(e,t){if(null==e)for(var n=0;n<$.length;n++)R[n]={},(!0===($[n].collapsed=t)?A:b).notify({level:n,groupingKey:null});else R[e]={},(!0===($[e].collapsed=t)?A:b).notify({level:e,groupingKey:null});T()}function ne(e,t,n){R[e][t]=$[e].collapsed^n,T()}function ie(e,t){for(var n,i,r,o=[],l={},a=t?t.level+1:0,u=$[a],s=0,g=u.predefinedValues.length;s<g;s++)(n=l[i=u.predefinedValues[s]])||((n=new Slick.Group).value=i,n.level=a,n.groupingKey=(t?t.groupingKey+x:"")+i,o[o.length]=n,l[i]=n);for(s=0,g=e.length;s<g;s++)r=e[s],(n=l[i=u.getterIsAFn?u.getter(r):r[u.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<$.length-1)for(s=0;s<o.length;s++)(n=o[s]).groups=ie(n.rows,n);return o.length&&function e(t,n){n=n||0;var 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)&&re(l),l.collapsed=r^o[l.groupingKey],l.title=i.formatter?i.formatter(l):l.value)}(o,a),o.sort($[a].comparer),o}function N(e){var t,n=e.group,i=$[n.level],r=n.level==$.length,o=i.aggregators.length;if(!r&&i.aggregateChildGroups)for(var l=n.groups.length;l--;)n.groups[l].totals.initialized||N(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 re(e){var t=$[e.level],n=new Slick.GroupTotals;(n.group=e).totals=n,t.lazyTotalsCalculation||N(n)}function oe(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 le(t,n){try{Object.defineProperty(t,"name",{writable:!0,value:n})}catch(e){t.name=n}}function ae(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 ue(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 se(e){h=null,C.isFilterNarrowing==O.isFilterNarrowing&&C.isFilterExpanding==O.isFilterExpanding||(P=[]);e=e,p?(t=r.inlineFilters?a:ae,n=r.inlineFilters?u:ue,C.isFilterNarrowing?S=t(S,l):C.isFilterExpanding?S=n(e,l,P):C.isFilterUnchanged||(S=t(e,l))):S=E?e:e.concat(),n=E?(S.length<=M*E&&(M=0===S.length?0:Math.floor((S.length-1)/E)),S.slice(E*M,E*M+E)):S;var t={totalRows:S.length,rows:n},e=(G=t.totalRows,t.rows),n=(I=[],$.length&&(I=ie(e)).length&&(e=function e(t,n){for(var i,r=$[n=n||0],o=[],l=0,a=0,u=t.length;a<u;a++){if(i=t[a],!(o[l++]=i).collapsed)for(var s,g=0,c=(s=i.groups?e(i.groups,n+1):i.rows).length;g<c;g++)o[l++]=s[g];i.totals&&r.displayTotalsRow&&(!i.collapsed||r.aggregateCollapsed)&&(o[l++]=i.totals)}return o}(I)),function(e,t){var n,i,r,o=[],l=0,a=Math.max(t.length,e.length);C&&C.ignoreDiffsBefore&&(l=Math.max(0,Math.min(t.length,C.ignoreDiffsBefore))),C&&C.ignoreDiffsAfter&&(a=Math.min(t.length,Math.max(0,C.ignoreDiffsAfter)));for(var u=l,s=e.length;u<a;u++)(s<=u||(n=t[u],i=e[u],!n||$.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]=u);return o}(f,e));return f=e,n}function T(){var e,t,n,i;v||(e=ce.extend(!0,{},F()),t=f.length,n=G,i=se(c),E&&G<M*E&&(M=Math.max(0,Math.ceil(G/E)-1),i=se(c)),m=null,O=C,C={},n!==G&&!1!==q.notify(e,null,s)&&U.notify(F(),null,s),t!==f.length&&B.notify({previous:t,current:f.length,itemCount:c.length,dataView:s,callingOnRowsChanged:0<i.length},null,s),0<i.length&&z.notify({rows:i,itemCount:c.length,dataView:s,calledOnRowCountChanged:t!==f.length},null,s),(t!==f.length||0<i.length)&&K.notify({rowsDiff:i,previousRowCount:t,currentRowCount:f.length,itemCount:c.length,rowCountChanged:t!==f.length,rowsChanged:0<i.length,dataView:s},null,s))}function ge(){return k}r=ce.extend(!0,{},{groupItemMetadataProvider:null,inlineFilters:!1},r),ce.extend(this,{beginUpdate:function(e){w=(v=!0)===e},endUpdate:function(){var e=w;if(v=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");_.has(t)?d.delete(t):(c[i]=n,d.set(t,i),++i)}c.length=i,_=new Slick.Map,L()}T()},destroy:function(){c=[],P=[],S=[],u=a=o=m=p=h=d=null,e&&e.onSelectedRowsChanged&&e.onCellCssStylesChanged&&(e.onSelectedRowsChanged.unsubscribe(),e.onCellCssStylesChanged.unsubscribe()),s.onRowsOrCountChanged&&s.onRowsOrCountChanged.unsubscribe()},setPagingOptions:function(e){!1!==q.notify(F(),null,s)&&(null!=e.pageSize&&(E=e.pageSize,M=E?Math.min(M,Math.max(0,Math.ceil(G/E)-1)):0),null!=e.pageNum&&(M=Math.min(e.pageNum,Math.max(0,Math.ceil(G/E)-1))),U.notify(F(),null,s),T())},getPagingInfo:F,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,s),d=new Slick.Map,D(),L(),T()},setFilter:function(e){var t,n;p=e,r.inlineFilters&&(e=oe(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=le(t,e),a=t,n=oe(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=le(e,n),u=e),T()},getFilter:function(){return p},getFilteredItems:function(){return S},getFilteredItemCount:function(){return S.length},sort:t,fastSort:H,reSort:function(){o?t(o,y):i&&H(i,y)},setGrouping:Q,getGrouping:function(){return $},groupBy:function(e,t,n){Q(null==e?[]:{getter:e,formatter:t,comparer:n})},setAggregators:function(e,t){if(!$.length)throw new Error("[SlickGrid DataView] At least one grouping must be specified before calling setAggregators().");$[0].aggregators=e,$[0].aggregateCollapsed=t,Q($)},collapseAllGroups:function(e){te(e,!0)},expandAllGroups:function(e){te(e,!1)},collapseGroup:function(e){var t,n=Array.prototype.slice.call(arguments),i=n[0];ne(i=1===n.length&&-1!==i.indexOf(x)?(t=i).split(x).length-1:(t=n.join(x),n.length-1),t,!0),A.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));ne(t,i,!1),b.notify({level:t,groupingKey:i})},getGroups:function(){return I},getAllSelectedIds:ge,getAllSelectedItems:function(){var t=[];return k.forEach(function(e){t.push(s.getItemById(e))}),t},getIdxById:function(e){return d.get(e)},getRowByItem:function(e){return V(),h[e[g]]},getRowById:function(e){return V(),h[e]},getItemById:W,getItemByIdx:function(e){return c[e]},mapItemsToRows:function(e){var t=[];V();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]<f.length&&(t[t.length]=f[e[n]][g]);return t},mapIdsToRows:function(e){var t=[];V();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){C=e},setFilterArgs:function(e){l=e},refresh:T,updateItem:X,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++)J(e[n],t[n]);T()},insertItem:Y,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:Z,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=d.get(i)))throw new Error("[SlickGrid DataView] Invalid id");_.set(i,!0)}else{for(var r=[],t=0,n=e.length;t<n;t++){var o,i=e[t];if(void 0===(o=d.get(i)))throw new Error("[SlickGrid DataView] Invalid id");d.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:ee,sortedUpdateItem:function(e,t){if(!d.has(e)||e!==t[g])throw new Error("[SlickGrid DataView] Invalid or non-matching id "+d.get(e));if(!o)throw new Error("[SlickGrid DataView] sortedUpdateItem() requires a sort comparer, use sort()");var n=W(e);0!==o(n,t)?(Z(e),ee(t)):X(e,t)},syncGridSelection:function(i,t,r){var o,l=this,n=(e=i,k=l.mapRowsToIds(i.getSelectedRows()),new Slick.Event);function a(e){k.join(",")!=e.join(",")&&(k=e,n.notify({grid:i,ids:k,dataView:l},new Slick.EventData,l))}return i.onSelectedRowsChanged.subscribe(function(e,t){var n;o||(n=l.mapRowsToIds(i.getSelectedRows()),r&&i.getOptions().multiSelect?a(ce.grep(k,function(e){return void 0===l.getRowById(e)}).concat(n)):a(n))}),this.onRowsOrCountChanged.subscribe(function(){var e;0<k.length&&(o=!0,e=l.mapIdsToRows(k),t||a(l.mapRowsToIds(e)),i.setSelectedRows(e),o=!1)}),n},syncGridCellCssStyles:function(i,r){var o,l;function n(e){for(var t in o={},e){var n=f[t][g];o[n]=e[t]}}function a(){if(o){l=!0,V();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(),s.onRowsOrCountChanged.unsubscribe(a)))}),this.onRowsOrCountChanged.subscribe(a)},getItemCount:function(){return c.length},getLength:function(){return f.length},getItem:function(e){var t;return(e=f[e])&&e.__group&&e.totals&&!e.totals.initialized?(t=$[e.level]).displayTotalsRow||(N(e.totals),e.title=t.formatter?t.formatter(e):e.value):e&&e.__groupTotals&&!e.initialized&&N(e),e},getItemMetadata:function(e){return void 0===(e=f[e])?null:e.__group?r.groupItemMetadataProvider.getGroupRowMetadata(e):e.__groupTotals?r.groupItemMetadataProvider.getTotalsRowMetadata(e):null},onSetItemsCalled:n,onRowCountChanged:B,onRowsChanged:z,onRowsOrCountChanged:K,onBeforePagingInfoChanged:q,onPagingInfoChanged:U,onGroupExpanded:b,onGroupCollapsed:A})},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);