Skip to content

Commit

Permalink
Merge pull request wenzhixin#3279 from vladimirgrezin/fix-cookie-expa…
Browse files Browse the repository at this point in the history
…ration-feature

Fix cookie-expiration feature
  • Loading branch information
wenzhixin authored Jan 8, 2018
2 parents 43ef979 + 34a5c65 commit efa66f0
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/extensions/cookie/bootstrap-table-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,18 @@
switch(that.options.cookieStorage) {
case 'cookieStorage':
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':
localStorage.setItem(cookieName, cookieValue);
break;
break;
case 'sessionStorage':
sessionStorage.setItem(cookieName, cookieValue);
break;
break;
default:
return false;
}
Expand Down Expand Up @@ -119,26 +118,26 @@
switch(that.options.cookieStorage) {
case 'cookieStorage':
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;
case 'localStorage':
localStorage.removeItem(cookieName);
break;
break;
case 'sessionStorage':
sessionStorage.removeItem(cookieName);
break;
break;

}
return true;
};

var calculateExpiration = function(cookieExpire) {
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()) {
case 's':
Expand All @@ -163,8 +162,12 @@
cookieExpire = undefined;
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) {
Expand Down Expand Up @@ -249,7 +252,7 @@
this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
this.options.cookiesEnabled.replace('[', '').replace(']', '')
.replace(/ /g, '').toLowerCase().split(',') :
this.options.cookiesEnabled;
this.options.cookiesEnabled;

if (this.options.filterControl) {
var that = this;
Expand Down Expand Up @@ -383,7 +386,7 @@
_onSearch.apply(this, target);

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);
};
Expand Down

0 comments on commit efa66f0

Please sign in to comment.