PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/public/assets/global/plugins/bootstrap-table/extensions/cookie/bootstrap-table-cookie.js

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