1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-04-28 04:43:15 +02:00
TableFilter/src/cookie.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

import {root} from './root';
2014-11-16 01:29:07 +01:00
/**
* Cookie utilities
*/
const doc = root.document;
2015-05-30 14:23:33 +02:00
export default {
2014-11-16 01:29:07 +01:00
2016-07-02 12:25:23 +02:00
/**
* Write a cookie
* @param {String} name Name of the cookie
* @param {String} value Value of the cookie
* @param {Number} hours Cookie duration in hours
*/
write(name, value, hours) {
2015-05-30 14:23:33 +02:00
let expire = '';
if (hours) {
2015-05-30 14:23:33 +02:00
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = '; expires=' + expire.toGMTString();
}
doc.cookie = name + '=' + escape(value) + expire;
2015-05-30 14:23:33 +02:00
},
2014-11-16 01:29:07 +01:00
2016-07-02 12:25:23 +02:00
/**
* Read a cookie
* @param {String} name Name of the cookie
* @returns {String} Value of the cookie
*/
read(name) {
2015-05-30 14:23:33 +02:00
let cookieValue = '',
search = name + '=';
if (doc.cookie.length > 0) {
let cookie = doc.cookie,
2015-05-30 14:23:33 +02:00
offset = cookie.indexOf(search);
if (offset !== -1) {
2015-05-30 14:23:33 +02:00
offset += search.length;
let end = cookie.indexOf(';', offset);
if (end === -1) {
2015-05-30 14:23:33 +02:00
end = cookie.length;
}
cookieValue = unescape(cookie.substring(offset, end));
2014-11-16 01:29:07 +01:00
}
}
2015-05-30 14:23:33 +02:00
return cookieValue;
},
2014-11-16 01:29:07 +01:00
2016-07-02 12:25:23 +02:00
/**
* Remove a cookie
* @param {String} name Name of the cookie
*/
remove(name) {
2015-05-30 14:23:33 +02:00
this.write(name, '', -1);
2014-11-16 01:29:07 +01:00
}
2015-05-30 14:23:33 +02:00
};