bootstrap-table-cookie.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.0.0
  5. *
  6. * @update zhixin wen <wenzhixin2010@gmail.com>
  7. */
  8. (function ($) {
  9. 'use strict';
  10. var idStateSave = '',
  11. idsStateSaveList = {
  12. sortOrder: 'bs.table.sortOrder',
  13. sortName: 'bs.table.sortName',
  14. pageNumber: 'bs.table.pageNumber',
  15. pageList: 'bs.table.pageList'
  16. };
  17. var cookieEnabled = function (){
  18. return (navigator.cookieEnabled) ? true : false;
  19. };
  20. var setCookie = function (cookieName, sValue, vEnd, sPath, sDomain, bSecure) {
  21. cookieName = idStateSave + cookieName;
  22. if (!cookieName || /^(?:expires|max\-age|path|domain|secure)$/i.test(cookieName)) {
  23. return false;
  24. }
  25. var sExpires = '',
  26. time = '';
  27. time = vEnd.replace(/[0-9]/,''); //s,mi,h,d,m,y
  28. vEnd = vEnd.replace(/[A-Za-z]/,''); //number
  29. switch (time.toLowerCase()) {
  30. case 's':
  31. vEnd = +vEnd;
  32. break;
  33. case 'mi':
  34. vEnd = vEnd * 60;
  35. break;
  36. case 'h':
  37. vEnd = vEnd * 60 * 60;
  38. break;
  39. case 'd':
  40. vEnd = vEnd * 24 * 60 * 60;
  41. break;
  42. case 'm':
  43. vEnd = vEnd * 30 * 24 * 60 * 60;
  44. break;
  45. case 'y':
  46. vEnd = vEnd * 365 * 30 * 24 * 60 * 60;
  47. break;
  48. default:
  49. vEnd = undefined;
  50. break;
  51. }
  52. sExpires = vEnd === undefined ? '' : '; max-age=' + vEnd;
  53. document.cookie = encodeURIComponent(cookieName) + '=' + encodeURIComponent(sValue) + sExpires + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '') + (bSecure ? '; secure' : '');
  54. return true;
  55. };
  56. var getCookie = function (cookieName) {
  57. cookieName = idStateSave + cookieName;
  58. if (!cookieName) {
  59. return null;
  60. }
  61. return decodeURIComponent(document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')) || null;
  62. };
  63. var hasCookie = function (cookieName) {
  64. if (!cookieName) {
  65. return false;
  66. }
  67. return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=')).test(document.cookie);
  68. };
  69. var deleteCookie = function (cookieName, sPath, sDomain) {
  70. cookieName = idStateSave + cookieName;
  71. if (!hasCookie(cookieName)) {
  72. return false;
  73. }
  74. document.cookie = encodeURIComponent(cookieName) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT' + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '');
  75. return true;
  76. };
  77. $.extend($.fn.bootstrapTable.defaults, {
  78. stateSave: false,
  79. stateSaveExpire: '2h',
  80. stateSaveIdTable: ''
  81. });
  82. $.fn.bootstrapTable.methods.push('deleteCookie');
  83. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  84. _init = BootstrapTable.prototype.init,
  85. _onSort = BootstrapTable.prototype.onSort,
  86. _onPageNumber = BootstrapTable.prototype.onPageNumber,
  87. _onPageListChange = BootstrapTable.prototype.onPageListChange;
  88. BootstrapTable.prototype.init = function () {
  89. this.initStateSave();
  90. _init.apply(this, Array.prototype.slice.apply(arguments));
  91. };
  92. BootstrapTable.prototype.initStateSave = function () {
  93. if (!this.options.stateSave) {
  94. return;
  95. }
  96. if (!cookieEnabled()) {
  97. return;
  98. }
  99. if (this.options.stateSaveIdTable === '') {
  100. return;
  101. }
  102. idStateSave = this.options.stateSaveIdTable + '.';
  103. var sortOrderStateSave = getCookie(idsStateSaveList.sortOrder),
  104. sortOrderStateName = getCookie(idsStateSaveList.sortName),
  105. pageNumberStateSave = getCookie(idsStateSaveList.pageNumber),
  106. pageListStateSave = getCookie(idsStateSaveList.pageList);
  107. if (sortOrderStateSave !== undefined && sortOrderStateSave !== null) {
  108. this.options.sortOrder = sortOrderStateSave;
  109. this.options.sortName = sortOrderStateName;
  110. }
  111. if (pageNumberStateSave !== undefined && pageNumberStateSave !== null) {
  112. this.options.pageNumber = +pageNumberStateSave;
  113. }
  114. if (pageListStateSave !== undefined && pageListStateSave !== null) {
  115. this.options.pageSize = pageListStateSave ===
  116. this.options.formatAllRows() ? pageListStateSave : +pageListStateSave;
  117. }
  118. };
  119. BootstrapTable.prototype.onSort = function () {
  120. _onSort.apply(this, Array.prototype.slice.apply(arguments));
  121. if (this.options.stateSave && cookieEnabled() && (this.options.stateSaveIdTable !== '')) {
  122. setCookie(idsStateSaveList.sortOrder, this.options.sortOrder, this.options.stateSaveExpire);
  123. setCookie(idsStateSaveList.sortName, this.options.sortName, this.options.stateSaveExpire);
  124. }
  125. };
  126. BootstrapTable.prototype.onPageNumber = function () {
  127. _onPageNumber.apply(this, Array.prototype.slice.apply(arguments));
  128. if (this.options.stateSave && cookieEnabled() && this.options.stateSaveIdTable !== '') {
  129. setCookie(idsStateSaveList.pageNumber, this.options.pageNumber, this.options.stateSaveExpire);
  130. }
  131. };
  132. BootstrapTable.prototype.onPageListChange = function () {
  133. _onPageListChange.apply(this, Array.prototype.slice.apply(arguments));
  134. if (this.options.stateSave && cookieEnabled() && this.options.stateSaveIdTable !== '') {
  135. setCookie(idsStateSaveList.pageList, this.options.pageSize, this.options.stateSaveExpire);
  136. }
  137. };
  138. BootstrapTable.prototype.deleteCookie = function (cookieName) {
  139. if (cookieName === '') {
  140. return;
  141. }
  142. if (!cookieEnabled()) {
  143. return;
  144. }
  145. deleteCookie(idsStateSaveList[cookieName]);
  146. }
  147. })(jQuery);