bootstrap-table-cookie.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.2.0
  5. *
  6. * @update zhixin wen <wenzhixin2010@gmail.com>
  7. */
  8. (function ($) {
  9. 'use strict';
  10. var cookieIds = {
  11. sortOrder: 'bs.table.sortOrder',
  12. sortName: 'bs.table.sortName',
  13. pageNumber: 'bs.table.pageNumber',
  14. pageList: 'bs.table.pageList',
  15. columns: 'bs.table.columns',
  16. searchText: 'bs.table.searchText',
  17. filterControl: 'bs.table.filterControl'
  18. };
  19. var getCurrentHeader = function (that) {
  20. var header = that.$header;
  21. if (that.options.height) {
  22. header = that.$tableHeader;
  23. }
  24. return header;
  25. };
  26. var getCurrentSearchControls = function (that) {
  27. var searchControls = 'select, input';
  28. if (that.options.height) {
  29. searchControls = 'table select, table input';
  30. }
  31. return searchControls;
  32. };
  33. var cookieEnabled = function () {
  34. return !!(navigator.cookieEnabled);
  35. };
  36. var setCookie = function (that, cookieName, cookieValue) {
  37. if ((!that.options.cookie) || (!cookieEnabled()) || (that.options.cookieIdTable === '')) {
  38. return;
  39. }
  40. if ($.inArray(cookieName.toLowerCase(), that.options.cookiesEnabled) === -1) {
  41. return;
  42. }
  43. cookieName = that.options.cookieIdTable + '.' + cookieName;
  44. if (!cookieName || /^(?:expires|max\-age|path|domain|secure)$/i.test(cookieName)) {
  45. return false;
  46. }
  47. document.cookie = encodeURIComponent(cookieName) + '=' + encodeURIComponent(cookieValue) + calculateExpiration(that.options.cookieExpire) + (that.options.cookieDomain ? '; domain=' + that.options.cookieDomain : '') + (that.options.cookiePath ? '; path=' + that.options.cookiePath : '') + (that.cookieSecure ? '; secure' : '');
  48. return true;
  49. };
  50. var getCookie = function (that, tableName, cookieName) {
  51. if (!cookieName) {
  52. return null;
  53. }
  54. if ($.inArray(cookieName.toLowerCase(), that.options.cookiesEnabled) === -1) {
  55. return null;
  56. }
  57. cookieName = tableName + '.' + cookieName;
  58. return decodeURIComponent(document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')) || null;
  59. };
  60. var hasCookie = function (cookieName) {
  61. if (!cookieName) {
  62. return false;
  63. }
  64. return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=')).test(document.cookie);
  65. };
  66. var deleteCookie = function (tableName, cookieName, sPath, sDomain) {
  67. cookieName = tableName + '.' + cookieName;
  68. if (!hasCookie(cookieName)) {
  69. return false;
  70. }
  71. document.cookie = encodeURIComponent(cookieName) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT' + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '');
  72. return true;
  73. };
  74. var calculateExpiration = function(cookieExpire) {
  75. var time = cookieExpire.replace(/[0-9]*/, ''); //s,mi,h,d,m,y
  76. cookieExpire = cookieExpire.replace(/[A-Za-z]/, ''); //number
  77. switch (time.toLowerCase()) {
  78. case 's':
  79. cookieExpire = +cookieExpire;
  80. break;
  81. case 'mi':
  82. cookieExpire = cookieExpire * 60;
  83. break;
  84. case 'h':
  85. cookieExpire = cookieExpire * 60 * 60;
  86. break;
  87. case 'd':
  88. cookieExpire = cookieExpire * 24 * 60 * 60;
  89. break;
  90. case 'm':
  91. cookieExpire = cookieExpire * 30 * 24 * 60 * 60;
  92. break;
  93. case 'y':
  94. cookieExpire = cookieExpire * 365 * 30 * 24 * 60 * 60;
  95. break;
  96. default:
  97. cookieExpire = undefined;
  98. break;
  99. }
  100. return cookieExpire === undefined ? '' : '; max-age=' + cookieExpire;
  101. };
  102. $.extend($.fn.bootstrapTable.defaults, {
  103. cookie: false,
  104. cookieExpire: '2h',
  105. cookiePath: null,
  106. cookieDomain: null,
  107. cookieSecure: null,
  108. cookieIdTable: '',
  109. cookiesEnabled: ['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl'],
  110. //internal variable
  111. filterControls: [],
  112. filterControlValuesLoaded: false
  113. });
  114. $.fn.bootstrapTable.methods.push('deleteCookie');
  115. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  116. _init = BootstrapTable.prototype.init,
  117. _initTable = BootstrapTable.prototype.initTable,
  118. _onSort = BootstrapTable.prototype.onSort,
  119. _onPageNumber = BootstrapTable.prototype.onPageNumber,
  120. _onPageListChange = BootstrapTable.prototype.onPageListChange,
  121. _onPageFirst = BootstrapTable.prototype.onPageFirst,
  122. _onPagePre = BootstrapTable.prototype.onPagePre,
  123. _onPageNext = BootstrapTable.prototype.onPageNext,
  124. _onPageLast = BootstrapTable.prototype.onPageLast,
  125. _toggleColumn = BootstrapTable.prototype.toggleColumn,
  126. _selectPage = BootstrapTable.prototype.selectPage,
  127. _onSearch = BootstrapTable.prototype.onSearch;
  128. BootstrapTable.prototype.init = function () {
  129. var timeoutId = 0;
  130. this.options.filterControls = [];
  131. this.options.filterControlValuesLoaded = false;
  132. this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
  133. this.options.cookiesEnabled.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') : this.options.cookiesEnabled;
  134. if (this.options.filterControl) {
  135. var that = this;
  136. this.$el.on('column-search.bs.table', function (e, field, text) {
  137. var isNewField = true;
  138. for (var i = 0; i < that.options.filterControls.length; i++) {
  139. if (that.options.filterControls[i].field === field) {
  140. that.options.filterControls[i].text = text;
  141. isNewField = false;
  142. break;
  143. }
  144. }
  145. if (isNewField) {
  146. that.options.filterControls.push({
  147. field: field,
  148. text: text
  149. });
  150. }
  151. setCookie(that, cookieIds.filterControl, JSON.stringify(that.options.filterControls));
  152. }).on('post-body.bs.table', function () {
  153. setTimeout(function () {
  154. if (!that.options.filterControlValuesLoaded) {
  155. that.options.filterControlValuesLoaded = true;
  156. var filterControl = JSON.parse(getCookie(that, that.options.cookieIdTable, cookieIds.filterControl));
  157. if (filterControl) {
  158. var field = null,
  159. result = [],
  160. header = getCurrentHeader(that),
  161. searchControls = getCurrentSearchControls(that);
  162. header.find(searchControls).each(function (index, ele) {
  163. field = $(this).parent().parent().parent().data('field');
  164. result = $.grep(filterControl, function (valueObj) {
  165. return valueObj.field === field;
  166. });
  167. if (result.length > 0) {
  168. $(this).val(result[0].text);
  169. that.onColumnSearch({currentTarget: $(this)});
  170. }
  171. });
  172. }
  173. }
  174. }, 250);
  175. });
  176. }
  177. _init.apply(this, Array.prototype.slice.apply(arguments));
  178. };
  179. BootstrapTable.prototype.initTable = function () {
  180. _initTable.apply(this, Array.prototype.slice.apply(arguments));
  181. this.initCookie();
  182. };
  183. BootstrapTable.prototype.initCookie = function () {
  184. if (!this.options.cookie) {
  185. return;
  186. }
  187. if ((this.options.cookieIdTable === '') || (this.options.cookieExpire === '') || (!cookieEnabled())) {
  188. throw new Error("Configuration error. Please review the cookieIdTable, cookieExpire properties, if those properties are ok, then this browser does not support the cookies");
  189. return;
  190. }
  191. var sortOrderCookie = getCookie(this, this.options.cookieIdTable, cookieIds.sortOrder),
  192. sortOrderNameCookie = getCookie(this, this.options.cookieIdTable, cookieIds.sortName),
  193. pageNumberCookie = getCookie(this, this.options.cookieIdTable, cookieIds.pageNumber),
  194. pageListCookie = getCookie(this, this.options.cookieIdTable, cookieIds.pageList),
  195. columnsCookie = JSON.parse(getCookie(this, this.options.cookieIdTable, cookieIds.columns)),
  196. searchTextCookie = getCookie(this, this.options.cookieIdTable, cookieIds.searchText);
  197. //sortOrder
  198. this.options.sortOrder = sortOrderCookie ? sortOrderCookie : 'asc';
  199. //sortName
  200. this.options.sortName = sortOrderNameCookie ? sortOrderNameCookie : undefined;
  201. //pageNumber
  202. this.options.pageNumber = pageNumberCookie ? +pageNumberCookie : this.options.pageNumber;
  203. //pageSize
  204. this.options.pageSize = pageListCookie ? pageListCookie === this.options.formatAllRows() ? pageListCookie : +pageListCookie : this.options.pageSize;
  205. //searchText
  206. this.options.searchText = searchTextCookie ? searchTextCookie : '';
  207. if (columnsCookie) {
  208. $.each(this.columns, function (i, column) {
  209. column.visible = columnsCookie.indexOf(column.field) !== -1;
  210. });
  211. }
  212. };
  213. BootstrapTable.prototype.onSort = function () {
  214. _onSort.apply(this, Array.prototype.slice.apply(arguments));
  215. setCookie(this, cookieIds.sortOrder, this.options.sortOrder);
  216. setCookie(this, cookieIds.sortName, this.options.sortName);
  217. };
  218. BootstrapTable.prototype.onPageNumber = function () {
  219. _onPageNumber.apply(this, Array.prototype.slice.apply(arguments));
  220. setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
  221. };
  222. BootstrapTable.prototype.onPageListChange = function () {
  223. _onPageListChange.apply(this, Array.prototype.slice.apply(arguments));
  224. setCookie(this, cookieIds.pageList, this.options.pageSize);
  225. };
  226. BootstrapTable.prototype.onPageFirst = function () {
  227. _onPageFirst.apply(this, Array.prototype.slice.apply(arguments));
  228. setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
  229. };
  230. BootstrapTable.prototype.onPagePre = function () {
  231. _onPagePre.apply(this, Array.prototype.slice.apply(arguments));
  232. setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
  233. };
  234. BootstrapTable.prototype.onPageNext = function () {
  235. _onPageNext.apply(this, Array.prototype.slice.apply(arguments));
  236. setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
  237. };
  238. BootstrapTable.prototype.onPageLast = function () {
  239. _onPageLast.apply(this, Array.prototype.slice.apply(arguments));
  240. setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
  241. };
  242. BootstrapTable.prototype.toggleColumn = function () {
  243. _toggleColumn.apply(this, Array.prototype.slice.apply(arguments));
  244. var visibleColumns = [];
  245. $.each(this.columns, function (i, column) {
  246. if (column.visible) {
  247. visibleColumns.push(column.field);
  248. }
  249. });
  250. setCookie(this, cookieIds.columns, JSON.stringify(visibleColumns));
  251. };
  252. BootstrapTable.prototype.selectPage = function (page) {
  253. _selectPage.apply(this, Array.prototype.slice.apply(arguments));
  254. setCookie(this, idsStateSaveList.pageNumber, page);
  255. };
  256. BootstrapTable.prototype.onSearch = function () {
  257. _onSearch.apply(this, Array.prototype.slice.apply(arguments));
  258. setCookie(this, cookieIds.searchText, this.searchText);
  259. };
  260. BootstrapTable.prototype.deleteCookie = function (cookieName) {
  261. if ((cookieName === '') || (!cookieEnabled())) {
  262. return;
  263. }
  264. deleteCookie(this.options.cookieIdTable, cookieIds[cookieName], this.options.cookiePath, this.options.cookieDomain);
  265. };
  266. })(jQuery);