Browse Source

Merge pull request #3279 from vladimirgrezin/fix-cookie-exparation-feature

Fix cookie-expiration feature
文翼 8 years ago
parent
commit
efa66f044a
1 changed files with 24 additions and 21 deletions
  1. 24 21
      src/extensions/cookie/bootstrap-table-cookie.js

+ 24 - 21
src/extensions/cookie/bootstrap-table-cookie.js

@@ -68,19 +68,18 @@
         switch(that.options.cookieStorage) {
         switch(that.options.cookieStorage) {
             case 'cookieStorage':
             case 'cookieStorage':
                 document.cookie = [
                 document.cookie = [
-                        cookieName, '=', cookieValue,
-                        '; expires=' + that.options.cookieExpire,
-                        that.options.cookiePath ? '; path=' + that.options.cookiePath : '',
-                        that.options.cookieDomain ? '; domain=' + that.options.cookieDomain : '',
-                        that.options.cookieSecure ? '; secure' : ''
-                    ].join('');
-            break;
+                    cookieName, '=', cookieValue,
+                    '; expires=' + calculateExpiration(that.options.cookieExpire),
+                    that.options.cookiePath ? '; path=' + that.options.cookiePath : '',
+                    that.options.cookieDomain ? '; domain=' + that.options.cookieDomain : '',
+                    that.options.cookieSecure ? '; secure' : ''
+                ].join('');
             case 'localStorage':
             case 'localStorage':
                 localStorage.setItem(cookieName, cookieValue);
                 localStorage.setItem(cookieName, cookieValue);
-            break;
+                break;
             case 'sessionStorage':
             case 'sessionStorage':
                 sessionStorage.setItem(cookieName, cookieValue);
                 sessionStorage.setItem(cookieName, cookieValue);
-            break;
+                break;
             default:
             default:
                 return false;
                 return false;
         }
         }
@@ -119,18 +118,18 @@
         switch(that.options.cookieStorage) {
         switch(that.options.cookieStorage) {
             case 'cookieStorage':
             case 'cookieStorage':
                 document.cookie = [
                 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('');
+                    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('');
                 break;
                 break;
             case 'localStorage':
             case 'localStorage':
                 localStorage.removeItem(cookieName);
                 localStorage.removeItem(cookieName);
-            break;
+                break;
             case 'sessionStorage':
             case 'sessionStorage':
                 sessionStorage.removeItem(cookieName);
                 sessionStorage.removeItem(cookieName);
-            break;
+                break;
 
 
         }
         }
         return true;
         return true;
@@ -138,7 +137,7 @@
 
 
     var calculateExpiration = function(cookieExpire) {
     var calculateExpiration = function(cookieExpire) {
         var time = cookieExpire.replace(/[0-9]*/, ''); //s,mi,h,d,m,y
         var time = cookieExpire.replace(/[0-9]*/, ''); //s,mi,h,d,m,y
-        cookieExpire = cookieExpire.replace(/[A-Za-z]{1,2}}/, ''); //number
+        cookieExpire = cookieExpire.replace(/[A-Za-z]{1,2}/, ''); //number
 
 
         switch (time.toLowerCase()) {
         switch (time.toLowerCase()) {
             case 's':
             case 's':
@@ -163,8 +162,12 @@
                 cookieExpire = undefined;
                 cookieExpire = undefined;
                 break;
                 break;
         }
         }
-
-        return cookieExpire === undefined ? '' : '; max-age=' + cookieExpire;
+        if (!cookieExpire) {
+            return '';
+        }
+        var d = new Date();
+        d.setTime(d.getTime() + cookieExpire * 1000);
+        return d.toGMTString();
     };
     };
 
 
     var initCookieFilters = function (bootstrapTable) {
     var initCookieFilters = function (bootstrapTable) {
@@ -249,7 +252,7 @@
         this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
         this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
             this.options.cookiesEnabled.replace('[', '').replace(']', '')
             this.options.cookiesEnabled.replace('[', '').replace(']', '')
                 .replace(/ /g, '').toLowerCase().split(',') :
                 .replace(/ /g, '').toLowerCase().split(',') :
-                this.options.cookiesEnabled;
+            this.options.cookiesEnabled;
 
 
         if (this.options.filterControl) {
         if (this.options.filterControl) {
             var that = this;
             var that = this;
@@ -383,7 +386,7 @@
         _onSearch.apply(this, target);
         _onSearch.apply(this, target);
 
 
         if ($(target[0].currentTarget).parent().hasClass('search')) {
         if ($(target[0].currentTarget).parent().hasClass('search')) {
-          setCookie(this, cookieIds.searchText, this.searchText);
+            setCookie(this, cookieIds.searchText, this.searchText);
         }
         }
         setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
         setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
     };
     };