UNPKG

21.2 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define([], factory);
4 } else if (typeof exports !== "undefined") {
5 factory();
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory();
11 global.bootstrapTableCookie = mod.exports;
12 }
13})(this, function () {
14 'use strict';
15
16 function _classCallCheck(instance, Constructor) {
17 if (!(instance instanceof Constructor)) {
18 throw new TypeError("Cannot call a class as a function");
19 }
20 }
21
22 var _createClass = function () {
23 function defineProperties(target, props) {
24 for (var i = 0; i < props.length; i++) {
25 var descriptor = props[i];
26 descriptor.enumerable = descriptor.enumerable || false;
27 descriptor.configurable = true;
28 if ("value" in descriptor) descriptor.writable = true;
29 Object.defineProperty(target, descriptor.key, descriptor);
30 }
31 }
32
33 return function (Constructor, protoProps, staticProps) {
34 if (protoProps) defineProperties(Constructor.prototype, protoProps);
35 if (staticProps) defineProperties(Constructor, staticProps);
36 return Constructor;
37 };
38 }();
39
40 function _possibleConstructorReturn(self, call) {
41 if (!self) {
42 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
43 }
44
45 return call && (typeof call === "object" || typeof call === "function") ? call : self;
46 }
47
48 var _get = function get(object, property, receiver) {
49 if (object === null) object = Function.prototype;
50 var desc = Object.getOwnPropertyDescriptor(object, property);
51
52 if (desc === undefined) {
53 var parent = Object.getPrototypeOf(object);
54
55 if (parent === null) {
56 return undefined;
57 } else {
58 return get(parent, property, receiver);
59 }
60 } else if ("value" in desc) {
61 return desc.value;
62 } else {
63 var getter = desc.get;
64
65 if (getter === undefined) {
66 return undefined;
67 }
68
69 return getter.call(receiver);
70 }
71 };
72
73 function _inherits(subClass, superClass) {
74 if (typeof superClass !== "function" && superClass !== null) {
75 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
76 }
77
78 subClass.prototype = Object.create(superClass && superClass.prototype, {
79 constructor: {
80 value: subClass,
81 enumerable: false,
82 writable: true,
83 configurable: true
84 }
85 });
86 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
87 }
88
89 /**
90 * @author: Dennis Hernández
91 * @webSite: http://djhvscf.github.io/Blog
92 * @update zhixin wen <wenzhixin2010@gmail.com>
93 */
94
95 (function ($) {
96 var UtilsCookie = {
97 cookieIds: {
98 sortOrder: 'bs.table.sortOrder',
99 sortName: 'bs.table.sortName',
100 pageNumber: 'bs.table.pageNumber',
101 pageList: 'bs.table.pageList',
102 columns: 'bs.table.columns',
103 searchText: 'bs.table.searchText',
104 filterControl: 'bs.table.filterControl',
105 filterBy: 'bs.table.filterBy'
106 },
107 getCurrentHeader: function getCurrentHeader(that) {
108 var header = that.$header;
109 if (that.options.height) {
110 header = that.$tableHeader;
111 }
112
113 return header;
114 },
115 getCurrentSearchControls: function getCurrentSearchControls(that) {
116 var searchControls = 'select, input';
117 if (that.options.height) {
118 searchControls = 'table select, table input';
119 }
120
121 return searchControls;
122 },
123 cookieEnabled: function cookieEnabled() {
124 return !!navigator.cookieEnabled;
125 },
126 inArrayCookiesEnabled: function inArrayCookiesEnabled(cookieName, cookiesEnabled) {
127 var index = -1;
128
129 for (var i = 0; i < cookiesEnabled.length; i++) {
130 if (cookieName.toLowerCase() === cookiesEnabled[i].toLowerCase()) {
131 index = i;
132 break;
133 }
134 }
135
136 return index;
137 },
138 setCookie: function setCookie(that, cookieName, cookieValue) {
139 if (!that.options.cookie || !UtilsCookie.cookieEnabled() || that.options.cookieIdTable === '') {
140 return;
141 }
142
143 if (UtilsCookie.inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
144 return;
145 }
146
147 cookieName = that.options.cookieIdTable + '.' + cookieName;
148
149 switch (that.options.cookieStorage) {
150 case 'cookieStorage':
151 document.cookie = [cookieName, '=', encodeURIComponent(cookieValue), '; expires=' + UtilsCookie.calculateExpiration(that.options.cookieExpire), that.options.cookiePath ? '; path=' + that.options.cookiePath : '', that.options.cookieDomain ? '; domain=' + that.options.cookieDomain : '', that.options.cookieSecure ? '; secure' : ''].join('');
152 break;
153 case 'localStorage':
154 localStorage.setItem(cookieName, cookieValue);
155 break;
156 case 'sessionStorage':
157 sessionStorage.setItem(cookieName, cookieValue);
158 break;
159 default:
160 return false;
161 }
162
163 return true;
164 },
165 getCookie: function getCookie(that, tableName, cookieName) {
166 if (!cookieName) {
167 return null;
168 }
169
170 if (UtilsCookie.inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
171 return null;
172 }
173
174 cookieName = tableName + '.' + cookieName;
175
176 switch (that.options.cookieStorage) {
177 case 'cookieStorage':
178 var value = '; ' + document.cookie;
179 var parts = value.split('; ' + cookieName + '=');
180 return parts.length === 2 ? decodeURIComponent(parts.pop().split(';').shift()) : null;
181 case 'localStorage':
182 return localStorage.getItem(cookieName);
183 case 'sessionStorage':
184 return sessionStorage.getItem(cookieName);
185 default:
186 return null;
187 }
188 },
189 deleteCookie: function deleteCookie(that, tableName, cookieName) {
190 cookieName = tableName + '.' + cookieName;
191
192 switch (that.options.cookieStorage) {
193 case 'cookieStorage':
194 document.cookie = [encodeURIComponent(cookieName), '=', '; expires=Thu, 01 Jan 1970 00:00:00 GMT', that.options.cookiePath ? '; path=' + that.options.cookiePath : '', that.options.cookieDomain ? '; domain=' + that.options.cookieDomain : ''].join('');
195 break;
196 case 'localStorage':
197 localStorage.removeItem(cookieName);
198 break;
199 case 'sessionStorage':
200 sessionStorage.removeItem(cookieName);
201 break;
202 default:
203 return false;
204 }
205 return true;
206 },
207 calculateExpiration: function calculateExpiration(cookieExpire) {
208 var time = cookieExpire.replace(/[0-9]*/, ''); // s,mi,h,d,m,y
209 cookieExpire = cookieExpire.replace(/[A-Za-z]{1,2}/, ''); // number
210
211 switch (time.toLowerCase()) {
212 case 's':
213 cookieExpire = +cookieExpire;
214 break;
215 case 'mi':
216 cookieExpire *= 60;
217 break;
218 case 'h':
219 cookieExpire = cookieExpire * 60 * 60;
220 break;
221 case 'd':
222 cookieExpire = cookieExpire * 24 * 60 * 60;
223 break;
224 case 'm':
225 cookieExpire = cookieExpire * 30 * 24 * 60 * 60;
226 break;
227 case 'y':
228 cookieExpire = cookieExpire * 365 * 24 * 60 * 60;
229 break;
230 default:
231 cookieExpire = undefined;
232 break;
233 }
234 if (!cookieExpire) {
235 return '';
236 }
237 var d = new Date();
238 d.setTime(d.getTime() + cookieExpire * 1000);
239 return d.toGMTString();
240 },
241 initCookieFilters: function initCookieFilters(bootstrapTable) {
242 setTimeout(function () {
243 var parsedCookieFilters = JSON.parse(UtilsCookie.getCookie(bootstrapTable, bootstrapTable.options.cookieIdTable, UtilsCookie.cookieIds.filterControl));
244
245 if (!bootstrapTable.options.filterControlValuesLoaded && parsedCookieFilters) {
246
247 var cachedFilters = {};
248 var header = UtilsCookie.getCurrentHeader(bootstrapTable);
249 var searchControls = UtilsCookie.getCurrentSearchControls(bootstrapTable);
250
251 var applyCookieFilters = function applyCookieFilters(element, filteredCookies) {
252 $(filteredCookies).each(function (i, cookie) {
253 if (cookie.text !== '') {
254 $(element).val(cookie.text);
255 cachedFilters[cookie.field] = cookie.text;
256 }
257 });
258 };
259
260 header.find(searchControls).each(function () {
261 var field = $(this).closest('[data-field]').data('field');
262 var filteredCookies = parsedCookieFilters.filter(function (cookie) {
263 return cookie.field === field;
264 });
265
266 applyCookieFilters(this, filteredCookies);
267 });
268
269 bootstrapTable.initColumnSearch(cachedFilters);
270 bootstrapTable.options.filterControlValuesLoaded = true;
271 bootstrapTable.initServer();
272 }
273 }, 250);
274 }
275 };
276
277 $.extend($.fn.bootstrapTable.defaults, {
278 cookie: false,
279 cookieExpire: '2h',
280 cookiePath: null,
281 cookieDomain: null,
282 cookieSecure: null,
283 cookieIdTable: '',
284 cookiesEnabled: ['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl', 'bs.table.filterBy'],
285 cookieStorage: 'cookieStorage', // localStorage, sessionStorage
286 // internal variable
287 filterControls: [],
288 filterControlValuesLoaded: false
289 });
290
291 $.fn.bootstrapTable.methods.push('getCookies');
292 $.fn.bootstrapTable.methods.push('deleteCookie');
293
294 $.extend($.fn.bootstrapTable.utils, {
295 setCookie: UtilsCookie.setCookie,
296 getCookie: UtilsCookie.getCookie
297 });
298
299 $.BootstrapTable = function (_$$BootstrapTable) {
300 _inherits(_class, _$$BootstrapTable);
301
302 function _class() {
303 _classCallCheck(this, _class);
304
305 return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
306 }
307
308 _createClass(_class, [{
309 key: 'init',
310 value: function init() {
311 // FilterBy logic
312 var filterByCookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy));
313 this.filterColumns = filterByCookie ? filterByCookie : {};
314
315 // FilterControl logic
316 this.options.filterControls = [];
317 this.options.filterControlValuesLoaded = false;
318
319 this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ? this.options.cookiesEnabled.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') : this.options.cookiesEnabled;
320
321 if (this.options.filterControl) {
322 var that = this;
323 this.$el.on('column-search.bs.table', function (e, field, text) {
324 var isNewField = true;
325
326 for (var i = 0; i < that.options.filterControls.length; i++) {
327 if (that.options.filterControls[i].field === field) {
328 that.options.filterControls[i].text = text;
329 isNewField = false;
330 break;
331 }
332 }
333 if (isNewField) {
334 that.options.filterControls.push({
335 field: field,
336 text: text
337 });
338 }
339
340 UtilsCookie.setCookie(that, UtilsCookie.cookieIds.filterControl, JSON.stringify(that.options.filterControls));
341 }).on('created-controls.bs.table', UtilsCookie.initCookieFilters(that));
342 }
343 _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'init', this).call(this);
344 }
345 }, {
346 key: 'initServer',
347 value: function initServer() {
348 var _get2;
349
350 if (this.options.cookie && this.options.filterControl && !this.options.filterControlValuesLoaded) {
351 var cookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterControl));
352 if (cookie) {
353 return;
354 }
355 }
356
357 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
358 args[_key] = arguments[_key];
359 }
360
361 (_get2 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initServer', this)).call.apply(_get2, [this].concat(args));
362 }
363 }, {
364 key: 'initTable',
365 value: function initTable() {
366 var _get3;
367
368 for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
369 args[_key2] = arguments[_key2];
370 }
371
372 (_get3 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initTable', this)).call.apply(_get3, [this].concat(args));
373 this.initCookie();
374 }
375 }, {
376 key: 'onSort',
377 value: function onSort() {
378 var _get4;
379
380 for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
381 args[_key3] = arguments[_key3];
382 }
383
384 (_get4 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'onSort', this)).call.apply(_get4, [this].concat(args));
385 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.sortOrder, this.options.sortOrder);
386 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.sortName, this.options.sortName);
387 }
388 }, {
389 key: 'onPageNumber',
390 value: function onPageNumber() {
391 var _get5;
392
393 for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
394 args[_key4] = arguments[_key4];
395 }
396
397 (_get5 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'onPageNumber', this)).call.apply(_get5, [this].concat(args));
398 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber);
399 }
400 }, {
401 key: 'onPageListChange',
402 value: function onPageListChange() {
403 var _get6;
404
405 for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
406 args[_key5] = arguments[_key5];
407 }
408
409 (_get6 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'onPageListChange', this)).call.apply(_get6, [this].concat(args));
410 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageList, this.options.pageSize);
411 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber);
412 }
413 }, {
414 key: 'onPagePre',
415 value: function onPagePre() {
416 var _get7;
417
418 for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
419 args[_key6] = arguments[_key6];
420 }
421
422 (_get7 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'onPagePre', this)).call.apply(_get7, [this].concat(args));
423 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber);
424 }
425 }, {
426 key: 'onPageNext',
427 value: function onPageNext() {
428 var _get8;
429
430 for (var _len7 = arguments.length, args = Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
431 args[_key7] = arguments[_key7];
432 }
433
434 (_get8 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'onPageNext', this)).call.apply(_get8, [this].concat(args));
435 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber);
436 }
437 }, {
438 key: 'toggleColumn',
439 value: function toggleColumn() {
440 var _get9;
441
442 for (var _len8 = arguments.length, args = Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
443 args[_key8] = arguments[_key8];
444 }
445
446 (_get9 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'toggleColumn', this)).call.apply(_get9, [this].concat(args));
447
448 var visibleColumns = [];
449
450 $.each(this.columns, function (i, column) {
451 if (column.visible) {
452 visibleColumns.push(column.field);
453 }
454 });
455
456 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(visibleColumns));
457 }
458 }, {
459 key: 'selectPage',
460 value: function selectPage(page) {
461 _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'selectPage', this).call(this, page);
462 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, page);
463 }
464 }, {
465 key: 'onSearch',
466 value: function onSearch(event) {
467 _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'onSearch', this).call(this, event);
468
469 if ($(event.currentTarget).parent().hasClass('search')) {
470 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.searchText, this.searchText);
471 }
472 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber);
473 }
474 }, {
475 key: 'filterBy',
476 value: function filterBy() {
477 var _get10;
478
479 for (var _len9 = arguments.length, args = Array(_len9), _key9 = 0; _key9 < _len9; _key9++) {
480 args[_key9] = arguments[_key9];
481 }
482
483 (_get10 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'filterBy', this)).call.apply(_get10, [this].concat(args));
484 UtilsCookie.setCookie(this, UtilsCookie.cookieIds.filterBy, JSON.stringify(this.filterColumns));
485 }
486 }, {
487 key: 'initCookie',
488 value: function initCookie() {
489 if (!this.options.cookie) {
490 return;
491 }
492
493 if (this.options.cookieIdTable === '' || this.options.cookieExpire === '' || !UtilsCookie.cookieEnabled()) {
494 console.error('Configuration error. Please review the cookieIdTable and the cookieExpire property. If the properties are correct, then this browser does not support cookies.');
495 this.options.cookie = false; // Make sure that the cookie extension is disabled
496 return;
497 }
498
499 var sortOrderCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder);
500 var sortOrderNameCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortName);
501 var pageNumberCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageNumber);
502 var pageListCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageList);
503 var columnsCookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.columns));
504 var searchTextCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.searchText);
505
506 // sortOrder
507 this.options.sortOrder = sortOrderCookie ? sortOrderCookie : this.options.sortOrder;
508 // sortName
509 this.options.sortName = sortOrderNameCookie ? sortOrderNameCookie : this.options.sortName;
510 // pageNumber
511 this.options.pageNumber = pageNumberCookie ? +pageNumberCookie : this.options.pageNumber;
512 // pageSize
513 this.options.pageSize = pageListCookie ? pageListCookie === this.options.formatAllRows() ? pageListCookie : +pageListCookie : this.options.pageSize;
514 // searchText
515 this.options.searchText = searchTextCookie ? searchTextCookie : '';
516
517 if (columnsCookie) {
518 $.each(this.columns, function (i, column) {
519 column.visible = $.inArray(column.field, columnsCookie) !== -1;
520 });
521 }
522 }
523 }, {
524 key: 'getCookies',
525 value: function getCookies() {
526 var bootstrapTable = this;
527 var cookies = {};
528 $.each(UtilsCookie.cookieIds, function (key, value) {
529 cookies[key] = UtilsCookie.getCookie(bootstrapTable, bootstrapTable.options.cookieIdTable, value);
530 if (key === 'columns') {
531 cookies[key] = JSON.parse(cookies[key]);
532 }
533 });
534 return cookies;
535 }
536 }, {
537 key: 'deleteCookie',
538 value: function deleteCookie(cookieName) {
539 if (cookieName === '' || !UtilsCookie.cookieEnabled()) {
540 return;
541 }
542
543 UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds[cookieName]);
544 }
545 }]);
546
547 return _class;
548 }($.BootstrapTable);
549 })(jQuery);
550});
\No newline at end of file