3 var Persistence =
function Persistence(table) {
8 this.defWatcherBlock =
false;
10 this.readFunc =
false;
11 this.writeFunc =
false;
15 Persistence.prototype.localStorageTest =
function () {
16 var testKey =
"_tabulator_test";
19 window.localStorage.setItem(testKey, testKey);
20 window.localStorage.removeItem(testKey);
28 Persistence.prototype.initialize =
function () {
31 var mode = this.table.options.persistenceMode,
32 id = this.table.options.persistenceID,
35 this.mode = mode !==
true ? mode : this.localStorageTest() ?
"local" :
"cookie";
37 if (this.table.options.persistenceReaderFunc) {
38 if (typeof this.table.options.persistenceReaderFunc ===
"function") {
39 this.readFunc = this.table.options.persistenceReaderFunc;
41 if (this.readers[this.table.options.persistenceReaderFunc]) {
42 this.readFunc = this.readers[this.table.options.persistenceReaderFunc];
44 console.warn(
"Persistence Read Error - invalid reader set", this.table.options.persistenceReaderFunc);
48 if (this.readers[this.mode]) {
49 this.readFunc = this.readers[this.mode];
51 console.warn(
"Persistence Read Error - invalid reader set", this.mode);
55 if (this.table.options.persistenceWriterFunc) {
56 if (typeof this.table.options.persistenceWriterFunc ===
"function") {
57 this.writeFunc = this.table.options.persistenceWriterFunc;
59 if (this.readers[this.table.options.persistenceWriterFunc]) {
60 this.writeFunc = this.readers[this.table.options.persistenceWriterFunc];
62 console.warn(
"Persistence Write Error - invalid reader set", this.table.options.persistenceWriterFunc);
66 if (this.writers[this.mode]) {
67 this.writeFunc = this.writers[this.mode];
69 console.warn(
"Persistence Write Error - invalid writer set", this.mode);
74 this.
id =
"tabulator-" + (
id || this.table.element.getAttribute(
"id") ||
"");
77 sort: this.table.options.persistence ===
true || this.table.options.persistence.sort,
78 filter: this.table.options.persistence ===
true || this.table.options.persistence.filter,
79 group: this.table.options.persistence ===
true || this.table.options.persistence.group,
80 page: this.table.options.persistence ===
true || this.table.options.persistence.page,
81 columns: this.table.options.persistence ===
true ? [
"title",
"width",
"visible"] : this.table.options.persistence.columns
85 if (this.config.page) {
86 retreivedData = this.retreiveData(
"page");
89 if (typeof retreivedData.paginationSize !==
"undefined" && (
this.config.page ===
true ||
this.config.page.size)) {
90 this.table.options.paginationSize = retreivedData.paginationSize;
93 if (typeof retreivedData.paginationInitialPage !==
"undefined" && (
this.config.page ===
true ||
this.config.page.page)) {
94 this.table.options.paginationInitialPage = retreivedData.paginationInitialPage;
100 if (this.config.group) {
101 retreivedData = this.retreiveData(
"group");
104 if (typeof retreivedData.groupBy !==
"undefined" && (
this.config.group ===
true ||
this.config.group.groupBy)) {
105 this.table.options.groupBy = retreivedData.groupBy;
107 if (typeof retreivedData.groupStartOpen !==
"undefined" && (
this.config.group ===
true ||
this.config.group.groupStartOpen)) {
108 this.table.options.groupStartOpen = retreivedData.groupStartOpen;
110 if (typeof retreivedData.groupHeader !==
"undefined" && (
this.config.group ===
true ||
this.config.group.groupHeader)) {
111 this.table.options.groupHeader = retreivedData.groupHeader;
117 Persistence.prototype.initializeColumn =
function (column) {
122 if (this.config.columns) {
123 this.defWatcherBlock =
true;
125 def = column.getDefinition();
127 keys = this.config.columns ===
true ? Object.keys(def) : this.config.columns;
129 keys.forEach(
function (key) {
130 var props = Object.getOwnPropertyDescriptor(def, key);
131 var value = def[key];
133 Object.defineProperty(def, key, {
134 set:
function set(newValue) {
137 if (!
self.defWatcherBlock) {
138 self.save(
"columns");
145 get:
function get() {
155 this.defWatcherBlock =
false;
160 Persistence.prototype.load =
function (type, current) {
161 var data = this.retreiveData(type);
164 data = data ? this.mergeDefinition(current, data) : current;
171 Persistence.prototype.retreiveData =
function (type) {
172 return this.readFunc ? this.readFunc(this.
id, type) : false;
176 Persistence.prototype.mergeDefinition =
function (oldCols, newCols) {
181 newCols = newCols || [];
183 newCols.forEach(
function (column, to) {
185 var from =
self._findColumn(oldCols, column),
190 if (
self.config.columns ===
true ||
self.config.columns == undefined) {
191 keys = Object.keys(from);
194 keys =
self.config.columns;
197 keys.forEach(
function (key) {
198 if (typeof column[key] !==
"undefined") {
199 from[key] = column[key];
204 from.columns =
self.mergeDefinition(from.columns, column.columns);
211 oldCols.forEach(
function (column, i) {
212 var from =
self._findColumn(newCols, column);
214 if (output.length > i) {
215 output.splice(i, 0, column);
226 Persistence.prototype._findColumn =
function (columns, subject) {
227 var type = subject.columns ?
"group" : subject.field ?
"field" :
"object";
229 return columns.find(
function (col) {
232 return col.title === subject.title && col.columns.length === subject.columns.length;
236 return col.field === subject.field;
240 return col === subject;
247 Persistence.prototype.save =
function (type) {
252 data = this.parseColumns(this.table.columnManager.getColumns());
256 data = this.table.modules.filter.getFilters();
260 data = this.validateSorters(this.table.modules.sort.getSort());
264 data = this.getGroupConfig();
268 data = this.getPageConfig();
272 if (this.writeFunc) {
273 this.writeFunc(this.
id, type, data);
278 Persistence.prototype.validateSorters =
function (data) {
279 data.forEach(
function (item) {
280 item.column = item.field;
287 Persistence.prototype.getGroupConfig =
function () {
288 if (this.config.group) {
289 if (this.config.group ===
true ||
this.config.group.groupBy) {
290 data.groupBy = this.table.options.groupBy;
293 if (this.config.group ===
true ||
this.config.group.groupStartOpen) {
294 data.groupStartOpen = this.table.options.groupStartOpen;
297 if (this.config.group ===
true ||
this.config.group.groupHeader) {
298 data.groupHeader = this.table.options.groupHeader;
305 Persistence.prototype.getPageConfig =
function () {
308 if (this.config.page) {
309 if (this.config.page ===
true ||
this.config.page.size) {
310 data.paginationSize = this.table.modules.page.getPageSize();
313 if (this.config.page ===
true ||
this.config.page.page) {
314 data.paginationInitialPage = this.table.modules.page.getPage();
322 Persistence.prototype.parseColumns =
function (columns) {
326 columns.forEach(
function (column) {
328 colDef = column.getDefinition(),
331 if (column.isGroup) {
332 defStore.title = colDef.title;
333 defStore.columns =
self.parseColumns(column.getColumns());
335 defStore.field = column.getField();
337 if (
self.config.columns ===
true ||
self.config.columns == undefined) {
338 keys = Object.keys(colDef);
341 keys =
self.config.columns;
344 keys.forEach(
function (key) {
348 defStore.width = column.getWidth();
351 defStore.visible = column.visible;
355 defStore[key] = colDef[key];
360 definitions.push(defStore);
367 Persistence.prototype.readers = {
368 local:
function local(
id, type) {
369 var data = localStorage.getItem(
id +
"-" + type);
371 return data ? JSON.parse(data) :
false;
373 cookie:
function cookie(
id, type) {
374 var cookie = document.cookie,
375 key =
id +
"-" + type,
376 cookiePos = cookie.indexOf(key +
"="),
380 if (cookiePos > -1) {
381 cookie = cookie.substr(cookiePos);
383 end = cookie.indexOf(
";");
386 cookie = cookie.substr(0, end);
389 data = cookie.replace(key +
"=",
"");
392 return data ? JSON.parse(data) :
false;
397 Persistence.prototype.writers = {
398 local:
function local(
id, type, data) {
399 localStorage.setItem(
id +
"-" + type, JSON.stringify(data));
401 cookie:
function cookie(
id, type, data) {
402 var expireDate =
new Date();
404 expireDate.setDate(expireDate.getDate() + 10000);
406 document.cookie =
id +
"_" + type +
"=" + JSON.stringify(data) +
"; expires=" + expireDate.toUTCString();
410 Tabulator.prototype.registerModule(
"persistence", Persistence);