otsdaq_utilities  v2_05_02_indev
persistence.min.js
1 /* Tabulator v4.5.3 (c) Oliver Folkerd */
2 var Persistence=function(e){this.table=e,this.mode="",this.id="",this.defWatcherBlock=!1,this.config={},this.readFunc=!1,this.writeFunc=!1};Persistence.prototype.localStorageTest=function(){var e="_tabulator_test";try{return window.localStorage.setItem(e,e),window.localStorage.removeItem(e),!0}catch(e){return!1}},Persistence.prototype.initialize=function(){var e,t=this.table.options.persistenceMode,i=this.table.options.persistenceID;this.mode=!0!==t?t:this.localStorageTest()?"local":"cookie",this.table.options.persistenceReaderFunc?"function"==typeof this.table.options.persistenceReaderFunc?this.readFunc=this.table.options.persistenceReaderFunc:this.readers[this.table.options.persistenceReaderFunc]?this.readFunc=this.readers[this.table.options.persistenceReaderFunc]:console.warn("Persistence Read Error - invalid reader set",this.table.options.persistenceReaderFunc):this.readers[this.mode]?this.readFunc=this.readers[this.mode]:console.warn("Persistence Read Error - invalid reader set",this.mode),this.table.options.persistenceWriterFunc?"function"==typeof this.table.options.persistenceWriterFunc?this.writeFunc=this.table.options.persistenceWriterFunc:this.readers[this.table.options.persistenceWriterFunc]?this.writeFunc=this.readers[this.table.options.persistenceWriterFunc]:console.warn("Persistence Write Error - invalid reader set",this.table.options.persistenceWriterFunc):this.writers[this.mode]?this.writeFunc=this.writers[this.mode]:console.warn("Persistence Write Error - invalid writer set",this.mode),this.id="tabulator-"+(i||this.table.element.getAttribute("id")||""),this.config={sort:!0===this.table.options.persistence||this.table.options.persistence.sort,filter:!0===this.table.options.persistence||this.table.options.persistence.filter,group:!0===this.table.options.persistence||this.table.options.persistence.group,page:!0===this.table.options.persistence||this.table.options.persistence.page,columns:!0===this.table.options.persistence?["title","width","visible"]:this.table.options.persistence.columns},this.config.page&&(e=this.retreiveData("page"))&&(void 0===e.paginationSize||!0!==this.config.page&&!this.config.page.size||(this.table.options.paginationSize=e.paginationSize),void 0===e.paginationInitialPage||!0!==this.config.page&&!this.config.page.page||(this.table.options.paginationInitialPage=e.paginationInitialPage)),this.config.group&&(e=this.retreiveData("group"))&&(void 0===e.groupBy||!0!==this.config.group&&!this.config.group.groupBy||(this.table.options.groupBy=e.groupBy),void 0===e.groupStartOpen||!0!==this.config.group&&!this.config.group.groupStartOpen||(this.table.options.groupStartOpen=e.groupStartOpen),void 0===e.groupHeader||!0!==this.config.group&&!this.config.group.groupHeader||(this.table.options.groupHeader=e.groupHeader))},Persistence.prototype.initializeColumn=function(e){var t,i,s=this;this.config.columns&&(this.defWatcherBlock=!0,t=e.getDefinition(),i=!0===this.config.columns?Object.keys(t):this.config.columns,i.forEach(function(e){var i=Object.getOwnPropertyDescriptor(t,e),o=t[e];i&&Object.defineProperty(t,e,{set:function(e){o=e,s.defWatcherBlock||s.save("columns"),i.set&&i.set(e)},get:function(){return i.get&&i.get(),o}})}),this.defWatcherBlock=!1)},Persistence.prototype.load=function(e,t){var i=this.retreiveData(e);return t&&(i=i?this.mergeDefinition(t,i):t),i},Persistence.prototype.retreiveData=function(e){return!!this.readFunc&&this.readFunc(this.id,e)},Persistence.prototype.mergeDefinition=function(e,t){var i=this,s=[];return t=t||[],t.forEach(function(t,o){var n,r=i._findColumn(e,t);r&&(!0===i.config.columns||void 0==i.config.columns?(n=Object.keys(r),n.push("width")):n=i.config.columns,n.forEach(function(e){void 0!==t[e]&&(r[e]=t[e])}),r.columns&&(r.columns=i.mergeDefinition(r.columns,t.columns)),s.push(r))}),e.forEach(function(e,o){i._findColumn(t,e)||(s.length>o?s.splice(o,0,e):s.push(e))}),s},Persistence.prototype._findColumn=function(e,t){var i=t.columns?"group":t.field?"field":"object";return e.find(function(e){switch(i){case"group":return e.title===t.title&&e.columns.length===t.columns.length;case"field":return e.field===t.field;case"object":return e===t}})},Persistence.prototype.save=function(e){var t={};switch(e){case"columns":t=this.parseColumns(this.table.columnManager.getColumns());break;case"filter":t=this.table.modules.filter.getFilters();break;case"sort":t=this.validateSorters(this.table.modules.sort.getSort());break;case"group":t=this.getGroupConfig();break;case"page":t=this.getPageConfig()}this.writeFunc&&this.writeFunc(this.id,e,t)},Persistence.prototype.validateSorters=function(e){return e.forEach(function(e){e.column=e.field,delete e.field}),e},Persistence.prototype.getGroupConfig=function(){return this.config.group&&((!0===this.config.group||this.config.group.groupBy)&&(data.groupBy=this.table.options.groupBy),(!0===this.config.group||this.config.group.groupStartOpen)&&(data.groupStartOpen=this.table.options.groupStartOpen),(!0===this.config.group||this.config.group.groupHeader)&&(data.groupHeader=this.table.options.groupHeader)),data},Persistence.prototype.getPageConfig=function(){var e={};return this.config.page&&((!0===this.config.page||this.config.page.size)&&(e.paginationSize=this.table.modules.page.getPageSize()),(!0===this.config.page||this.config.page.page)&&(e.paginationInitialPage=this.table.modules.page.getPage())),e},Persistence.prototype.parseColumns=function(e){var t=this,i=[];return e.forEach(function(e){var s,o={},n=e.getDefinition();e.isGroup?(o.title=n.title,o.columns=t.parseColumns(e.getColumns())):(o.field=e.getField(),!0===t.config.columns||void 0==t.config.columns?(s=Object.keys(n),s.push("width")):s=t.config.columns,s.forEach(function(t){switch(t){case"width":o.width=e.getWidth();break;case"visible":o.visible=e.visible;break;default:o[t]=n[t]}})),i.push(o)}),i},Persistence.prototype.readers={local:function(e,t){var i=localStorage.getItem(e+"-"+t);return!!i&&JSON.parse(i)},cookie:function(e,t){var i,s=document.cookie,o=e+"-"+t,n=s.indexOf(o+"=");return n>-1&&(s=s.substr(n),i=s.indexOf(";"),i>-1&&(s=s.substr(0,i)),data=s.replace(o+"=","")),!!data&&JSON.parse(data)}},Persistence.prototype.writers={local:function(e,t,i){localStorage.setItem(e+"-"+t,JSON.stringify(i))},cookie:function(e,t,i){var s=new Date;s.setDate(s.getDate()+1e4),document.cookie=e+"_"+t+"="+JSON.stringify(i)+"; expires="+s.toUTCString()}},Tabulator.prototype.registerModule("persistence",Persistence);