1 var Persistence =
function(table){
6 this.defWatcherBlock =
false;
9 this.writeFunc =
false;
13 Persistence.prototype.localStorageTest =
function() {
14 var testKey =
"_tabulator_test";
17 window.localStorage.setItem( testKey, testKey);
18 window.localStorage.removeItem( testKey );
26 Persistence.prototype.initialize =
function(){
29 var mode = this.table.options.persistenceMode,
30 id = this.table.options.persistenceID,
33 this.mode = mode !==
true ? mode : (this.localStorageTest() ?
"local" :
"cookie");
35 if(this.table.options.persistenceReaderFunc){
36 if(typeof this.table.options.persistenceReaderFunc ===
"function"){
37 this.readFunc = this.table.options.persistenceReaderFunc;
39 if(this.readers[this.table.options.persistenceReaderFunc]){
40 this.readFunc = this.readers[this.table.options.persistenceReaderFunc];
42 console.warn(
"Persistence Read Error - invalid reader set", this.table.options.persistenceReaderFunc);
46 if(this.readers[this.mode]){
47 this.readFunc = this.readers[this.mode];
49 console.warn(
"Persistence Read Error - invalid reader set", this.mode);
53 if(this.table.options.persistenceWriterFunc){
54 if(typeof this.table.options.persistenceWriterFunc ===
"function"){
55 this.writeFunc = this.table.options.persistenceWriterFunc;
57 if(this.readers[this.table.options.persistenceWriterFunc]){
58 this.writeFunc = this.readers[this.table.options.persistenceWriterFunc];
60 console.warn(
"Persistence Write Error - invalid reader set", this.table.options.persistenceWriterFunc);
64 if(this.writers[this.mode]){
65 this.writeFunc = this.writers[this.mode];
67 console.warn(
"Persistence Write Error - invalid writer set", this.mode);
72 this.
id =
"tabulator-" + (
id || (this.table.element.getAttribute(
"id") ||
""));
75 sort:this.table.options.persistence ===
true || this.table.options.persistence.sort,
76 filter:this.table.options.persistence ===
true || this.table.options.persistence.filter,
77 group:this.table.options.persistence ===
true || this.table.options.persistence.group,
78 page:this.table.options.persistence ===
true || this.table.options.persistence.page,
79 columns:this.table.options.persistence ===
true ? [
"title",
"width",
"visible"] : this.table.options.persistence.columns,
84 retreivedData = this.retreiveData(
"page");
87 if(typeof retreivedData.paginationSize !==
"undefined" && (
this.config.page ===
true ||
this.config.page.size)){
88 this.table.options.paginationSize = retreivedData.paginationSize;
91 if(typeof retreivedData.paginationInitialPage !==
"undefined" && (
this.config.page ===
true ||
this.config.page.page)){
92 this.table.options.paginationInitialPage = retreivedData.paginationInitialPage;
98 if(this.config.group){
99 retreivedData = this.retreiveData(
"group");
102 if(typeof retreivedData.groupBy !==
"undefined" && (
this.config.group ===
true ||
this.config.group.groupBy)){
103 this.table.options.groupBy = retreivedData.groupBy;
105 if(typeof retreivedData.groupStartOpen !==
"undefined" && (
this.config.group ===
true ||
this.config.group.groupStartOpen)){
106 this.table.options.groupStartOpen = retreivedData.groupStartOpen;
108 if(typeof retreivedData.groupHeader !==
"undefined" && (
this.config.group ===
true ||
this.config.group.groupHeader)){
109 this.table.options.groupHeader = retreivedData.groupHeader;
116 Persistence.prototype.initializeColumn =
function(column){
120 if(this.config.columns){
121 this.defWatcherBlock =
true;
123 def = column.getDefinition();
125 keys = this.config.columns ===
true ? Object.keys(def) : this.config.columns;
127 keys.forEach((key)=>{
128 var props = Object.getOwnPropertyDescriptor(def, key);
129 var value = def[key];
131 Object.defineProperty(def, key, {
132 set:
function(newValue){
135 if(!
self.defWatcherBlock){
136 self.save(
"columns");
153 this.defWatcherBlock =
false;
159 Persistence.prototype.load =
function(type, current){
160 var data = this.retreiveData(type);
163 data = data ? this.mergeDefinition(current, data) : current;
170 Persistence.prototype.retreiveData =
function(type){
171 return this.readFunc ? this.readFunc(this.
id, type) : false;
175 Persistence.prototype.mergeDefinition =
function(oldCols, newCols){
180 newCols = newCols || [];
182 newCols.forEach(
function(column, to){
184 var from =
self._findColumn(oldCols, column),
189 if(
self.config.columns ===
true ||
self.config.columns == undefined){
190 keys = Object.keys(from);
193 keys =
self.config.columns;
196 keys.forEach((key)=>{
197 if(typeof column[key] !==
"undefined"){
198 from[key] = column[key];
203 from.columns =
self.mergeDefinition(from.columns, column.columns);
212 oldCols.forEach(
function (column, i) {
213 var from =
self._findColumn(newCols, column);
216 output.splice(i, 0, column);
227 Persistence.prototype._findColumn =
function(columns, subject){
228 var type = subject.columns ?
"group" : (subject.field ?
"field" :
"object");
230 return columns.find(
function(col){
233 return col.title === subject.title && col.columns.length === subject.columns.length;
237 return col.field === subject.field;
241 return col === subject;
248 Persistence.prototype.save =
function(type){
254 data = this.parseColumns(this.table.columnManager.getColumns())
258 data = this.table.modules.filter.getFilters();
262 data = this.validateSorters(this.table.modules.sort.getSort());
266 data = this.getGroupConfig();
270 data = this.getPageConfig();
275 this.writeFunc(this.
id, type, data);
281 Persistence.prototype.validateSorters =
function(data){
282 data.forEach(
function(item){
283 item.column = item.field;
290 Persistence.prototype.getGroupConfig =
function(){
291 if(this.config.group){
292 if(this.config.group ===
true ||
this.config.group.groupBy){
293 data.groupBy = this.table.options.groupBy;
296 if(this.config.group ===
true ||
this.config.group.groupStartOpen){
297 data.groupStartOpen = this.table.options.groupStartOpen;
300 if(this.config.group ===
true ||
this.config.group.groupHeader){
301 data.groupHeader = this.table.options.groupHeader;
308 Persistence.prototype.getPageConfig =
function(){
311 if(this.config.page){
312 if(this.config.page ===
true ||
this.config.page.size){
313 data.paginationSize = this.table.modules.page.getPageSize();
316 if(this.config.page ===
true ||
this.config.page.page){
317 data.paginationInitialPage = this.table.modules.page.getPage();
326 Persistence.prototype.parseColumns =
function(columns){
330 columns.forEach(
function(column){
332 colDef = column.getDefinition(),
336 defStore.title = colDef.title;
337 defStore.columns =
self.parseColumns(column.getColumns());
339 defStore.field = column.getField();
341 if(
self.config.columns ===
true ||
self.config.columns == undefined){
342 keys = Object.keys(colDef);
345 keys =
self.config.columns;
348 keys.forEach((key)=>{
352 defStore.width = column.getWidth();
355 defStore.visible = column.visible;
359 defStore[key] = colDef[key];
365 definitions.push(defStore);
372 Persistence.prototype.readers = {
373 local:
function(id, type){
374 var data = localStorage.getItem(
id +
"-" + type);
376 return data ? JSON.parse(data) :
false;
378 cookie:
function(id, type){
379 var cookie = document.cookie,
380 key =
id +
"-" + type,
381 cookiePos = cookie.indexOf(key +
"="),
386 cookie = cookie.substr(cookiePos);
388 end = cookie.indexOf(
";");
391 cookie = cookie.substr(0, end);
394 data = cookie.replace(key +
"=",
"");
397 return data ? JSON.parse(data) :
false;
402 Persistence.prototype.writers = {
403 local:
function(id, type, data){
404 localStorage.setItem(
id +
"-" + type, JSON.stringify(data));
406 cookie:
function(id, type, data){
407 var expireDate =
new Date();
409 expireDate.setDate(expireDate.getDate() + 10000);
411 document.cookie =
id +
"_" + type +
"=" + JSON.stringify(data) +
"; expires=" + expireDate.toUTCString();
416 Tabulator.prototype.registerModule(
"persistence", Persistence);