Built files from Bizgaze WebServer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

persistence.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  2. var Persistence = function Persistence(table) {
  3. this.table = table; //hold Tabulator object
  4. this.mode = "";
  5. this.id = "";
  6. // this.persistProps = ["field", "width", "visible"];
  7. this.defWatcherBlock = false;
  8. this.config = {};
  9. this.readFunc = false;
  10. this.writeFunc = false;
  11. };
  12. // Test for whether localStorage is available for use.
  13. Persistence.prototype.localStorageTest = function () {
  14. var testKey = "_tabulator_test";
  15. try {
  16. window.localStorage.setItem(testKey, testKey);
  17. window.localStorage.removeItem(testKey);
  18. return true;
  19. } catch (e) {
  20. return false;
  21. }
  22. };
  23. //setup parameters
  24. Persistence.prototype.initialize = function () {
  25. //determine persistent layout storage type
  26. var mode = this.table.options.persistenceMode,
  27. id = this.table.options.persistenceID,
  28. retreivedData;
  29. this.mode = mode !== true ? mode : this.localStorageTest() ? "local" : "cookie";
  30. if (this.table.options.persistenceReaderFunc) {
  31. if (typeof this.table.options.persistenceReaderFunc === "function") {
  32. this.readFunc = this.table.options.persistenceReaderFunc;
  33. } else {
  34. if (this.readers[this.table.options.persistenceReaderFunc]) {
  35. this.readFunc = this.readers[this.table.options.persistenceReaderFunc];
  36. } else {
  37. console.warn("Persistence Read Error - invalid reader set", this.table.options.persistenceReaderFunc);
  38. }
  39. }
  40. } else {
  41. if (this.readers[this.mode]) {
  42. this.readFunc = this.readers[this.mode];
  43. } else {
  44. console.warn("Persistence Read Error - invalid reader set", this.mode);
  45. }
  46. }
  47. if (this.table.options.persistenceWriterFunc) {
  48. if (typeof this.table.options.persistenceWriterFunc === "function") {
  49. this.writeFunc = this.table.options.persistenceWriterFunc;
  50. } else {
  51. if (this.readers[this.table.options.persistenceWriterFunc]) {
  52. this.writeFunc = this.readers[this.table.options.persistenceWriterFunc];
  53. } else {
  54. console.warn("Persistence Write Error - invalid reader set", this.table.options.persistenceWriterFunc);
  55. }
  56. }
  57. } else {
  58. if (this.writers[this.mode]) {
  59. this.writeFunc = this.writers[this.mode];
  60. } else {
  61. console.warn("Persistence Write Error - invalid writer set", this.mode);
  62. }
  63. }
  64. //set storage tag
  65. this.id = "tabulator-" + (id || this.table.element.getAttribute("id") || "");
  66. this.config = {
  67. sort: this.table.options.persistence === true || this.table.options.persistence.sort,
  68. filter: this.table.options.persistence === true || this.table.options.persistence.filter,
  69. group: this.table.options.persistence === true || this.table.options.persistence.group,
  70. page: this.table.options.persistence === true || this.table.options.persistence.page,
  71. columns: this.table.options.persistence === true ? ["title", "width", "visible"] : this.table.options.persistence.columns
  72. };
  73. //load pagination data if needed
  74. if (this.config.page) {
  75. retreivedData = this.retreiveData("page");
  76. if (retreivedData) {
  77. if (typeof retreivedData.paginationSize !== "undefined" && (this.config.page === true || this.config.page.size)) {
  78. this.table.options.paginationSize = retreivedData.paginationSize;
  79. }
  80. if (typeof retreivedData.paginationInitialPage !== "undefined" && (this.config.page === true || this.config.page.page)) {
  81. this.table.options.paginationInitialPage = retreivedData.paginationInitialPage;
  82. }
  83. }
  84. }
  85. //load group data if needed
  86. if (this.config.group) {
  87. retreivedData = this.retreiveData("group");
  88. if (retreivedData) {
  89. if (typeof retreivedData.groupBy !== "undefined" && (this.config.group === true || this.config.group.groupBy)) {
  90. this.table.options.groupBy = retreivedData.groupBy;
  91. }
  92. if (typeof retreivedData.groupStartOpen !== "undefined" && (this.config.group === true || this.config.group.groupStartOpen)) {
  93. this.table.options.groupStartOpen = retreivedData.groupStartOpen;
  94. }
  95. if (typeof retreivedData.groupHeader !== "undefined" && (this.config.group === true || this.config.group.groupHeader)) {
  96. this.table.options.groupHeader = retreivedData.groupHeader;
  97. }
  98. }
  99. }
  100. };
  101. Persistence.prototype.initializeColumn = function (column) {
  102. var self = this,
  103. def,
  104. keys;
  105. if (this.config.columns) {
  106. this.defWatcherBlock = true;
  107. def = column.getDefinition();
  108. keys = this.config.columns === true ? Object.keys(def) : this.config.columns;
  109. keys.forEach(function (key) {
  110. var props = Object.getOwnPropertyDescriptor(def, key);
  111. var value = def[key];
  112. if (props) {
  113. Object.defineProperty(def, key, {
  114. set: function set(newValue) {
  115. value = newValue;
  116. if (!self.defWatcherBlock) {
  117. self.save("columns");
  118. }
  119. if (props.set) {
  120. props.set(newValue);
  121. }
  122. },
  123. get: function get() {
  124. if (props.get) {
  125. props.get();
  126. }
  127. return value;
  128. }
  129. });
  130. }
  131. });
  132. this.defWatcherBlock = false;
  133. }
  134. };
  135. //load saved definitions
  136. Persistence.prototype.load = function (type, current) {
  137. var data = this.retreiveData(type);
  138. if (current) {
  139. data = data ? this.mergeDefinition(current, data) : current;
  140. }
  141. return data;
  142. };
  143. //retreive data from memory
  144. Persistence.prototype.retreiveData = function (type) {
  145. return this.readFunc ? this.readFunc(this.id, type) : false;
  146. };
  147. //merge old and new column definitions
  148. Persistence.prototype.mergeDefinition = function (oldCols, newCols) {
  149. var self = this,
  150. output = [];
  151. // oldCols = oldCols || [];
  152. newCols = newCols || [];
  153. newCols.forEach(function (column, to) {
  154. var from = self._findColumn(oldCols, column),
  155. keys;
  156. if (from) {
  157. if (self.config.columns === true || self.config.columns == undefined) {
  158. keys = Object.keys(from);
  159. keys.push("width");
  160. } else {
  161. keys = self.config.columns;
  162. }
  163. keys.forEach(function (key) {
  164. if (typeof column[key] !== "undefined") {
  165. from[key] = column[key];
  166. }
  167. });
  168. if (from.columns) {
  169. from.columns = self.mergeDefinition(from.columns, column.columns);
  170. }
  171. output.push(from);
  172. }
  173. });
  174. oldCols.forEach(function (column, i) {
  175. var from = self._findColumn(newCols, column);
  176. if (!from) {
  177. if (output.length > i) {
  178. output.splice(i, 0, column);
  179. } else {
  180. output.push(column);
  181. }
  182. }
  183. });
  184. return output;
  185. };
  186. //find matching columns
  187. Persistence.prototype._findColumn = function (columns, subject) {
  188. var type = subject.columns ? "group" : subject.field ? "field" : "object";
  189. return columns.find(function (col) {
  190. switch (type) {
  191. case "group":
  192. return col.title === subject.title && col.columns.length === subject.columns.length;
  193. break;
  194. case "field":
  195. return col.field === subject.field;
  196. break;
  197. case "object":
  198. return col === subject;
  199. break;
  200. }
  201. });
  202. };
  203. //save data
  204. Persistence.prototype.save = function (type) {
  205. var data = {};
  206. switch (type) {
  207. case "columns":
  208. data = this.parseColumns(this.table.columnManager.getColumns());
  209. break;
  210. case "filter":
  211. data = this.table.modules.filter.getFilters();
  212. break;
  213. case "sort":
  214. data = this.validateSorters(this.table.modules.sort.getSort());
  215. break;
  216. case "group":
  217. data = this.getGroupConfig();
  218. break;
  219. case "page":
  220. data = this.getPageConfig();
  221. break;
  222. }
  223. if (this.writeFunc) {
  224. this.writeFunc(this.id, type, data);
  225. }
  226. };
  227. //ensure sorters contain no function data
  228. Persistence.prototype.validateSorters = function (data) {
  229. data.forEach(function (item) {
  230. item.column = item.field;
  231. delete item.field;
  232. });
  233. return data;
  234. };
  235. Persistence.prototype.getGroupConfig = function () {
  236. if (this.config.group) {
  237. if (this.config.group === true || this.config.group.groupBy) {
  238. data.groupBy = this.table.options.groupBy;
  239. }
  240. if (this.config.group === true || this.config.group.groupStartOpen) {
  241. data.groupStartOpen = this.table.options.groupStartOpen;
  242. }
  243. if (this.config.group === true || this.config.group.groupHeader) {
  244. data.groupHeader = this.table.options.groupHeader;
  245. }
  246. }
  247. return data;
  248. };
  249. Persistence.prototype.getPageConfig = function () {
  250. var data = {};
  251. if (this.config.page) {
  252. if (this.config.page === true || this.config.page.size) {
  253. data.paginationSize = this.table.modules.page.getPageSize();
  254. }
  255. if (this.config.page === true || this.config.page.page) {
  256. data.paginationInitialPage = this.table.modules.page.getPage();
  257. }
  258. }
  259. return data;
  260. };
  261. //parse columns for data to store
  262. Persistence.prototype.parseColumns = function (columns) {
  263. var self = this,
  264. definitions = [];
  265. columns.forEach(function (column) {
  266. var defStore = {},
  267. colDef = column.getDefinition(),
  268. keys;
  269. if (column.isGroup) {
  270. defStore.title = colDef.title;
  271. defStore.columns = self.parseColumns(column.getColumns());
  272. } else {
  273. defStore.field = column.getField();
  274. if (self.config.columns === true || self.config.columns == undefined) {
  275. keys = Object.keys(colDef);
  276. keys.push("width");
  277. } else {
  278. keys = self.config.columns;
  279. }
  280. keys.forEach(function (key) {
  281. switch (key) {
  282. case "width":
  283. defStore.width = column.getWidth();
  284. break;
  285. case "visible":
  286. defStore.visible = column.visible;
  287. break;
  288. default:
  289. defStore[key] = colDef[key];
  290. }
  291. });
  292. }
  293. definitions.push(defStore);
  294. });
  295. return definitions;
  296. };
  297. // read peristence information from storage
  298. Persistence.prototype.readers = {
  299. local: function local(id, type) {
  300. var data = localStorage.getItem(id + "-" + type);
  301. return data ? JSON.parse(data) : false;
  302. },
  303. cookie: function cookie(id, type) {
  304. var cookie = document.cookie,
  305. key = id + "-" + type,
  306. cookiePos = cookie.indexOf(key + "="),
  307. end,
  308. data;
  309. //if cookie exists, decode and load column data into tabulator
  310. if (cookiePos > -1) {
  311. cookie = cookie.substr(cookiePos);
  312. end = cookie.indexOf(";");
  313. if (end > -1) {
  314. cookie = cookie.substr(0, end);
  315. }
  316. data = cookie.replace(key + "=", "");
  317. }
  318. return data ? JSON.parse(data) : false;
  319. }
  320. };
  321. //write persistence information to storage
  322. Persistence.prototype.writers = {
  323. local: function local(id, type, data) {
  324. localStorage.setItem(id + "-" + type, JSON.stringify(data));
  325. },
  326. cookie: function cookie(id, type, data) {
  327. var expireDate = new Date();
  328. expireDate.setDate(expireDate.getDate() + 10000);
  329. document.cookie = id + "-" + type + "=" + JSON.stringify(data) + "; expires=" + expireDate.toUTCString();
  330. }
  331. };
  332. Tabulator.prototype.registerModule("persistence", Persistence);