Browse Source

Fixing some issues with cookie

djhvscf 4 years ago
parent
commit
95fe29bbcb

+ 24 - 0
site/docs/extensions/filter-control.md

@@ -192,6 +192,30 @@ toc: true
 
 
 - **Default:** `undefined`
 - **Default:** `undefined`
 
 
+### filterControlMultipleSelect
+
+- **Attribute:** `data-filter-multiple-select`
+
+- **type:** `boolean`
+
+- **Detail:**
+
+   Use this option to configure the select as multipleSelect.
+
+- **Default:** `false`
+
+### filterControlMultipleSelectMultiple
+
+- **Attribute:** `data-filter-multiple-select-multiple`
+
+- **type:** `boolean`
+
+- **Detail:**
+
+   If the filterControlMultipleSelect option is set to true, use this option to configure the select as multiple select.
+
+- **Default:** `false`
+
 ### filterMultipleSelectOptions
 ### filterMultipleSelectOptions
 
 
 - **Attribute:** `data-filter-multiple-select-options`
 - **Attribute:** `data-filter-multiple-select-options`

+ 53 - 25
src/extensions/cookie/bootstrap-table-cookie.js

@@ -34,27 +34,18 @@ const UtilsCookie = {
 
 
     return searchControls
     return searchControls
   },
   },
-  cookieEnabled () {
+  isCookieSupportedByBrowser () {
     return !!(navigator.cookieEnabled)
     return !!(navigator.cookieEnabled)
   },
   },
-  inArrayCookiesEnabled (cookieName, cookiesEnabled) {
-    let index = -1
-
-    for (let i = 0; i < cookiesEnabled.length; i++) {
-      if (cookieName.toLowerCase() === cookiesEnabled[i].toLowerCase()) {
-        index = i
-        break
-      }
-    }
-
-    return index
+  isCookieEnabled (that, cookieName) {
+    return that.options.cookiesEnabled.indexOf(cookieName) !== -1
   },
   },
   setCookie (that, cookieName, cookieValue) {
   setCookie (that, cookieName, cookieValue) {
-    if ((!that.options.cookie) || (!UtilsCookie.cookieEnabled()) || (that.options.cookieIdTable === '')) {
+    if (!that.options.cookie) {
       return
       return
     }
     }
 
 
-    if (UtilsCookie.inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
+    if (UtilsCookie.isCookieEnabled(that, cookieName)) {
       return
       return
     }
     }
 
 
@@ -99,7 +90,7 @@ const UtilsCookie = {
       return null
       return null
     }
     }
 
 
-    if (UtilsCookie.inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
+    if (UtilsCookie.isCookieEnabled(that, cookieName)) {
       return null
       return null
     }
     }
 
 
@@ -129,8 +120,8 @@ const UtilsCookie = {
         return null
         return null
     }
     }
   },
   },
-  deleteCookie (that, tableName, cookieName) {
-    cookieName = `${tableName}.${cookieName}`
+  deleteCookie (that, cookieName) {
+    cookieName = `${that.options.cookieIdTable}.${cookieName}`
 
 
     switch (that.options.cookieStorage) {
     switch (that.options.cookieStorage) {
       case 'cookieStorage':
       case 'cookieStorage':
@@ -301,6 +292,9 @@ $.extend($.fn.bootstrapTable.utils, {
 $.BootstrapTable = class extends $.BootstrapTable {
 $.BootstrapTable = class extends $.BootstrapTable {
   init () {
   init () {
     if (this.options.cookie) {
     if (this.options.cookie) {
+      if (this.options.cookieStorage === 'cookieStorage' && !UtilsCookie.isCookieSupportedByBrowser()) {
+        throw new Error('Cookies are not enabled in this browser.')
+      }
       // FilterBy logic
       // FilterBy logic
       const filterByCookieValue = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy)
       const filterByCookieValue = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy)
 
 
@@ -370,12 +364,19 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
 
   initTable (...args) {
   initTable (...args) {
     super.initTable(...args)
     super.initTable(...args)
+    if (!this.options.cookie) {
+      return
+    }
     this.initCookie()
     this.initCookie()
   }
   }
 
 
   onSort (...args) {
   onSort (...args) {
     super.onSort(...args)
     super.onSort(...args)
 
 
+    if (!this.options.cookie) {
+      return
+    }
+
     if (this.options.sortName === undefined || this.options.sortOrder === undefined) {
     if (this.options.sortName === undefined || this.options.sortOrder === undefined) {
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortName)
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortName)
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder)
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder)
@@ -388,44 +389,66 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
 
   onPageNumber (...args) {
   onPageNumber (...args) {
     super.onPageNumber(...args)
     super.onPageNumber(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
   }
 
 
   onPageListChange (...args) {
   onPageListChange (...args) {
     super.onPageListChange(...args)
     super.onPageListChange(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageList, this.options.pageSize)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageList, this.options.pageSize)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
   }
 
 
   onPagePre (...args) {
   onPagePre (...args) {
     super.onPagePre(...args)
     super.onPagePre(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
   }
 
 
   onPageNext (...args) {
   onPageNext (...args) {
     super.onPageNext(...args)
     super.onPageNext(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
   }
 
 
   _toggleColumn (...args) {
   _toggleColumn (...args) {
     super._toggleColumn(...args)
     super._toggleColumn(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
   }
   }
 
 
   _toggleAllColumns (...args) {
   _toggleAllColumns (...args) {
     super._toggleAllColumns(...args)
     super._toggleAllColumns(...args)
-
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
   }
   }
 
 
   selectPage (page) {
   selectPage (page) {
     super.selectPage(page)
     super.selectPage(page)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, page)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, page)
   }
   }
 
 
   onSearch (event) {
   onSearch (event) {
-    super.onSearch(event)
-
+    super.onSearch(event, false)
+    if (!this.options.cookie) {
+      return
+    }
     if (this.options.search) {
     if (this.options.search) {
       UtilsCookie.setCookie(this, UtilsCookie.cookieIds.searchText, this.searchText)
       UtilsCookie.setCookie(this, UtilsCookie.cookieIds.searchText, this.searchText)
     }
     }
@@ -433,7 +456,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
   }
   }
 
 
   initHeader (...args) {
   initHeader (...args) {
-    if (this.options.reorderableColumns) {
+    if (this.options.reorderableColumns && this.options.cookie) {
       this.columnsSortOrder = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.reorderColumns))
       this.columnsSortOrder = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.reorderColumns))
     }
     }
     super.initHeader(...args)
     super.initHeader(...args)
@@ -445,6 +468,9 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
 
   filterBy (...args) {
   filterBy (...args) {
     super.filterBy(...args)
     super.filterBy(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.filterBy, JSON.stringify(this.filterColumns))
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.filterBy, JSON.stringify(this.filterColumns))
   }
   }
 
 
@@ -453,7 +479,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
       return
       return
     }
     }
 
 
-    if ((this.options.cookieIdTable === '') || (this.options.cookieExpire === '') || (!UtilsCookie.cookieEnabled())) {
+    if ((this.options.cookieIdTable === '') || (this.options.cookieExpire === '')) {
       console.error('Configuration error. Please review the cookieIdTable and the cookieExpire property. If the properties are correct, then this browser does not support cookies.')
       console.error('Configuration error. Please review the cookieIdTable and the cookieExpire property. If the properties are correct, then this browser does not support cookies.')
       this.options.cookie = false // Make sure that the cookie extension is disabled
       this.options.cookie = false // Make sure that the cookie extension is disabled
       return
       return
@@ -488,7 +514,9 @@ $.BootstrapTable = class extends $.BootstrapTable {
     // pageSize
     // pageSize
     this.options.pageSize = pageListCookie ? pageListCookie === this.options.formatAllRows() ? pageListCookie : +pageListCookie : this.options.pageSize
     this.options.pageSize = pageListCookie ? pageListCookie === this.options.formatAllRows() ? pageListCookie : +pageListCookie : this.options.pageSize
     // searchText
     // searchText
-    this.options.searchText = searchTextCookie ? searchTextCookie : ''
+    if (UtilsCookie.isCookieEnabled(this, 'bs.table.searchText') && this.options.searchText === '') {
+      this.options.searchText = searchTextCookie ? searchTextCookie : ''
+    }
 
 
     if (columnsCookie) {
     if (columnsCookie) {
       for (const column of this.columns) {
       for (const column of this.columns) {
@@ -525,10 +553,10 @@ $.BootstrapTable = class extends $.BootstrapTable {
   }
   }
 
 
   deleteCookie (cookieName) {
   deleteCookie (cookieName) {
-    if ((cookieName === '') || (!UtilsCookie.cookieEnabled())) {
+    if (!cookieName) {
       return
       return
     }
     }
 
 
-    UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds[cookieName])
+    UtilsCookie.deleteCookie(this, UtilsCookie.cookieIds[cookieName])
   }
   }
 }
 }

+ 16 - 7
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -33,7 +33,7 @@ $.extend($.fn.bootstrapTable.defaults, {
         '<select class="form-control bootstrap-table-filter-control-%s %s" %s style="width: 100%;" dir="%s"></select>',
         '<select class="form-control bootstrap-table-filter-control-%s %s" %s style="width: 100%;" dir="%s"></select>',
         column.field,
         column.field,
         column.filterControlMultipleSelect ? 'fc-multipleselect' : '',
         column.filterControlMultipleSelect ? 'fc-multipleselect' : '',
-        column.filterControlMultipleSelect ? 'multiple="multiple"' : '',
+        column.filterControlMultipleSelectMultiple ? 'multiple="multiple"' : '',
         UtilsFilterControl.getDirectionOfSelectOptions(
         UtilsFilterControl.getDirectionOfSelectOptions(
           options.alignmentSelectControlOptions
           options.alignmentSelectControlOptions
         )
         )
@@ -59,10 +59,11 @@ $.extend($.fn.bootstrapTable.defaults, {
 $.extend($.fn.bootstrapTable.columnDefaults, {
 $.extend($.fn.bootstrapTable.columnDefaults, {
   filterControl: undefined, // input, select, datepicker
   filterControl: undefined, // input, select, datepicker
   filterControlMultipleSelect: false,
   filterControlMultipleSelect: false,
+  filterControlMultipleSelectMultiple: false,
+  filterMultipleSelectOptions: {},
   filterDataCollector: undefined,
   filterDataCollector: undefined,
   filterData: undefined,
   filterData: undefined,
   filterDatepickerOptions: {},
   filterDatepickerOptions: {},
-  filterMultipleSelectOptions: {},
   filterStrictSearch: false,
   filterStrictSearch: false,
   filterStartsWithSearch: false,
   filterStartsWithSearch: false,
   filterControlPlaceholder: '',
   filterControlPlaceholder: '',
@@ -170,10 +171,12 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
 
   initBody () {
   initBody () {
     super.initBody()
     super.initBody()
-    setTimeout(() => {
-      UtilsFilterControl.initFilterSelectControls(this)
-      UtilsFilterControl.setValues(this)
-    }, 3)
+    if (this.options.filterControl) {
+      setTimeout(() => {
+        UtilsFilterControl.initFilterSelectControls(this)
+        UtilsFilterControl.setValues(this)
+      }, 3)
+    }
   }
   }
 
 
   initHeader () {
   initHeader () {
@@ -457,6 +460,8 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
 
         filterObj[$field] = filterByArray
         filterObj[$field] = filterByArray
         this.filterBy(filterObj)
         this.filterBy(filterObj)
+      } else {
+        this.filterBy({})
       }
       }
     } else {
     } else {
       const text = currentTargetValue ? currentTargetValue.trim() : ''
       const text = currentTargetValue ? currentTargetValue.trim() : ''
@@ -471,7 +476,11 @@ $.BootstrapTable = class extends $.BootstrapTable {
       }
       }
     }
     }
 
 
-    this.options.pageNumber = 1
+    // Cookie support
+    if (!this.options.cookie) {
+      this.options.pageNumber = 1
+    }
+
     this.onSearch({ currentTarget }, false)
     this.onSearch({ currentTarget }, false)
   }
   }
 
 

+ 6 - 1
src/extensions/filter-control/utils.js

@@ -55,7 +55,9 @@ export function existOptionInSelectControl (selectControl, value) {
 }
 }
 
 
 export function addOptionToSelectControl (selectControl, _value, text, selected) {
 export function addOptionToSelectControl (selectControl, _value, text, selected) {
-  const value = (_value === undefined || _value === null) ? '' : _value.toString().trim()
+  let value = (_value === undefined || _value === null) ? '' : _value.toString().trim()
+
+  value = value.replace(/(<([^>]+)>)/ig, '')
   const $selectControl = $(selectControl.get(selectControl.length - 1))
   const $selectControl = $(selectControl.get(selectControl.length - 1))
 
 
   if (!existOptionInSelectControl(selectControl, value)) {
   if (!existOptionInSelectControl(selectControl, value)) {
@@ -84,6 +86,9 @@ export function sortSelectControl (selectControl, orderBy) {
 }
 }
 
 
 export function fixHeaderCSS ({ $tableHeader }, pixels = '89px') {
 export function fixHeaderCSS ({ $tableHeader }, pixels = '89px') {
+  if ($tableHeader.hasClass('table-sm') && pixels === '89px') {
+    $tableHeader.css('height', '49px')
+  }
   $tableHeader.css('height', pixels)
   $tableHeader.css('height', pixels)
 }
 }