UNPKG

slickgrid

Version:

A lightning fast JavaScript grid/spreadsheet

9 lines 7.71 kB
/** * SlickGrid v3.0.2 * (c) 2009-present Michael Leibman * homepage: http://github.com/mleibman/slickgrid * license: MIT * date: 2022-11-14 * file: ./plugins/slick.rowdetailview.js */ !function(A){A.extend(!0,window,{Slick:{Plugins:{RowDetailView:function(e){var d,r,u,c,p="id",n=null,o=this,l=null,s=[],t=new Slick.EventHandler,w=5,g=0,i={columnId:"_detail_selector",cssClass:"detailView-toggle",expandedClass:null,collapsedClass:null,keyPrefix:"_",loadOnce:!1,collapseAllOnSort:!0,saveDetailViewOnScroll:!0,singleRowExpand:!1,useSimpleViewportCalc:!1,alwaysRenderColumn:!0,toolTip:"",width:30,maxRows:null},f=i.keyPrefix,m=0,R=[],h=A.extend(!0,{},i,e);"function"==typeof h.expandableOverride&&k(h.expandableOverride);function v(e,t){if(e)for(var i=0;i<e.length;i++)if(e[i]===t)return i;return-1}function a(e,t){var i,n=d.getDataItem(t.row);E(t.row,n,d)&&(h.useRowClick||d.getColumns()[t.cell].id===h.columnId&&A(e.target).hasClass(h.cssClass))&&(d.getEditorLock().isActive()&&!d.getEditorLock().commitCurrentEdit()?e.preventDefault():(o.onBeforeRowDetailToggle.notify({grid:d,item:n},e,o),E(t=t.row,i=n,d)&&(c.beginUpdate(),(t=i)&&(t[f+"collapsed"]?I:V)(t),c.endUpdate()),o.onAfterRowDetailToggle.notify({grid:d,item:n,expandedRows:s},e,o),e.stopPropagation()),e.stopImmediatePropagation())}function b(e,t){var i;if(h.useSimpleViewportCalc)d&&(i=d.getRenderedRange(),s.forEach(function(e){var t=function(e,t){if(Math.abs(t.bottom-m-e)>2*g)return!0;return!1}(c.getRowById(e[p]),i);!t&&0<=v(R,e[p])?y(e,e[p]):t&&x(e,e[p])}));else if(d){var o=d.getRenderedRange();if(0<s.length){var a="DOWN";if(l){if(l.top===o.top&&l.bottom===o.bottom)return;(l.top>o.top||0===l.top&&0===o.top&&l.bottom>o.bottom)&&(a="UP")}}s.forEach(function(e){var t=c.getRowById(e[p]),i=e[f+"sizePadding"],n=0<=v(R,e[p]);"UP"===a?(h.saveDetailViewOnScroll&&t>=o.bottom-m&&P(e),n&&t-w<o.top&&t>=o.top?y(e,e[p]):!n&&t+i>o.bottom&&x(e,e[p])):"DOWN"===a&&(h.saveDetailViewOnScroll&&t<=o.top+m&&P(e),n&&t+i+w>o.bottom&&t<t+i?y(e,e[p]):!n&&t<o.top&&x(e,e[p]))}),l=o}}function x(e,t){var i=e.rowIndex||c.getRowById(e[p]);o.onRowOutOfViewportRange.notify({grid:d,item:e,rowId:t,rowIndex:i,expandedRows:s,rowIdsOutOfViewport:C(t,!0)},null,o)}function y(e,t){var i=e.rowIndex||c.getRowById(e[p]);setTimeout(function(){A(".cellDetailView_"+e[p]).length&&o.onRowBackToViewportRange.notify({grid:d,item:e,rowId:t,rowIndex:i,expandedRows:s,rowIdsOutOfViewport:C(t,!1)},null,o)},100)}function C(e,t){var i=v(R,e);return t&&i<0?R.push(e):!t&&0<=i&&R.splice(i,1),R}function D(){c.beginUpdate();for(var e=s.length-1;0<=e;e--)V(s[e],!0);c.endUpdate()}function V(t,e){e||c.beginUpdate(),h.loadOnce&&P(t),t[f+"collapsed"]=!0;for(var i=1;i<=t[f+"sizePadding"];i++)c.deleteItem(t[p]+"."+i);t[f+"sizePadding"]=0,c.updateItem(t[p],t),s=s.filter(function(e){return e[p]!==t[p]}),e||c.endUpdate()}function I(e){h&&h.singleRowExpand&&D(),e[f+"collapsed"]=!1,s.push(e),e[f+"detailContent"]||(e[f+"detailViewLoaded"]=!1),e[f+"detailViewLoaded"]&&!0===h.loadOnce?(o.onAsyncResponse.notify({item:e,itemDetail:e,detailView:e[f+"detailContent"]},void 0,this),S(e),c.updateItem(e[p],e)):(e[f+"detailContent"]=h.preTemplate(e),S(e),c.updateItem(e[p],e),h.process(e))}function P(e){var t;A("."+u+" .innerDetailView_"+e[p])&&void 0!==(t=A("."+u+" .innerDetailView_"+e[p]).html())&&(e[f+"detailContent"]=t)}var O=function(e,t){var i,n={};for(i in d.getData())n[i]=null;return n[p]=e[p]+"."+t,n[f+"collapsed"]=!0,n[f+"isPadding"]=!0,n[f+"parent"]=e,n[f+"offset"]=t,n};function S(e){for(var t=h.panelRows,i=(e[f+"sizePadding"]=Math.ceil(2*t*13/r.rowHeight),e[f+"height"]=e[f+"sizePadding"]*r.rowHeight,c.getIdxById(e[p])),n=1;n<=e[f+"sizePadding"];n++)c.insertItem(i+n,O(e,n))}function z(e,t,i,n,o,a){var l,s;return!E(e,o,a)||(null==o[f+"collapsed"]&&(o[f+"collapsed"]=!0,o[f+"sizePadding"]=0,o[f+"height"]=0,o[f+"isPadding"]=!1,o[f+"parent"]=void 0,o[f+"offset"]=0),o[f+"isPadding"])?null:o[f+"collapsed"]?(e=h.cssClass+" expand ",h.collapsedClass&&(e+=h.collapsedClass),'<div class="'+e+'"></div>'):(a=[],e=r.rowHeight,l=o[f+"sizePadding"]*r.rowHeight,null!==h.maxRows&&o[f+"sizePadding"]>h.maxRows&&(l=h.maxRows*e,o[f+"sizePadding"]=h.maxRows),s=h.cssClass+" collapse ",h.expandedClass&&(s+=h.expandedClass),a.push('<div class="'+s+'"></div></div>'),a.push('<div class="dynamic-cell-detail cellDetailView_',o[p],'" '),a.push('style="height:',l,"px;"),a.push("top:",e,'px">'),a.push('<div class="detail-container detailViewContainer_',o[p],'">'),a.push('<div class="innerDetailView_',o[p],'">',o[f+"detailContent"],"</div></div>"),a.join(""))}function E(e,t,i){return"function"!=typeof n||n(e,t,i)}function k(e){n=e}A.extend(this,{init:function(e){if(!e)throw new Error('RowDetailView Plugin requires the Grid instance to be passed as argument to the "init()" method');u=(d=e).getUID(),r=e.getOptions()||{},c=d.getData(),f=h&&h.keyPrefix||"_",m=d.getOptions().minRowBuffer,d.getOptions().minRowBuffer=h.panelRows+3,t.subscribe(d.onClick,a).subscribe(d.onScroll,b),h.collapseAllOnSort&&(t.subscribe(d.onSort,D),s=[],R=[]),t.subscribe(d.getData().onRowCountChanged,function(){d.updateRowCount(),d.render()}),t.subscribe(d.getData().onRowsChanged,function(e,t){d.invalidateRows(t.rows),d.render()}),o.onAsyncResponse.subscribe(function(e,t){if(!t||!t.item&&!t.itemDetail)throw'Slick.RowDetailView plugin requires the onAsyncResponse() to supply "args.item" property.';var i=t.item||t.itemDetail;t.detailView?i[f+"detailContent"]=t.detailView:i[f+"detailContent"]=h.postTemplate(i),i[f+"detailViewLoaded"]=!0,c.updateItem(i[p],i),o.onAsyncEndUpdate.notify({grid:d,item:i,itemDetail:i},e,o)}),t.subscribe(c.onSetItemsCalled,function(e,t){p=c&&c.getIdPropertyName()||"id"}),h.useSimpleViewportCalc&&t.subscribe(d.onRendered,function(e,t){t&&t.endRow&&(g=t.endRow-t.startRow)})},destroy:function(){t.unsubscribeAll(),o.onAsyncResponse.unsubscribe(),o.onAsyncEndUpdate.unsubscribe(),o.onAfterRowDetailToggle.unsubscribe(),o.onBeforeRowDetailToggle.unsubscribe(),o.onRowOutOfViewportRange.unsubscribe(),o.onRowBackToViewportRange.unsubscribe()},pluginName:"RowDetailView",collapseAll:D,collapseDetailView:V,expandDetailView:I,expandableOverride:k,getColumnDefinition:function(){return{id:h.columnId,name:"",toolTip:h.toolTip,field:"sel",width:h.width,resizable:!1,sortable:!1,alwaysRenderColumn:h.alwaysRenderColumn,cssClass:h.cssClass,formatter:z}},getExpandedRows:function(){return s},getFilterItem:function(e){return e=e[f+"isPadding"]&&e[f+"parent"]?e[f+"parent"]:e},getOptions:function(){return h},resizeDetailView:function(e){if(e){var t=document.querySelector("."+u+" .detailViewContainer_"+e[p]),i=document.querySelector("."+u+" .cellDetailView_"+e[p]),n=document.querySelector("."+u+" .innerDetailView_"+e[p]);if(t&&i&&n){for(var o=1;o<=e[f+"sizePadding"];o++)c.deleteItem(e[p]+"."+o);for(var n=r.rowHeight,a=(t.style.minHeight=null,t.scrollHeight),l=Math.ceil(a/n),l=(e[f+"sizePadding"]=Math.ceil(2*l*13/n),e[f+"height"]=a,e[f+"sizePadding"]*n),s=(null!==h.maxRows&&e[f+"sizePadding"]>h.maxRows&&(l=h.maxRows*n,e[f+"sizePadding"]=h.maxRows),d.getOptions().minRowBuffer<e[f+"sizePadding"]&&(d.getOptions().minRowBuffer=e[f+"sizePadding"]+3),t.setAttribute("style","min-height: "+e[f+"height"]+"px"),i&&i.setAttribute("style","height: "+l+"px; top:"+n+"px"),c.getIdxById(e[p])),o=1;o<=e[f+"sizePadding"];o++)c.insertItem(s+o,O(e,o));P(e)}}},saveDetailView:P,setOptions:function(e){(h=A.extend(!0,{},h,e))&&h.singleRowExpand&&D()},onAsyncResponse:new Slick.Event,onAsyncEndUpdate:new Slick.Event,onAfterRowDetailToggle:new Slick.Event,onBeforeRowDetailToggle:new Slick.Event,onRowOutOfViewportRange:new Slick.Event,onRowBackToViewportRange:new Slick.Event})}}}})}(jQuery);