mirror of
https://github.com/koalyptus/TableFilter.git
synced 2024-06-08 08:52:32 +02:00
63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
import {root} from './root';
|
|
|
|
/**
|
|
* Cookie utilities
|
|
*/
|
|
|
|
const doc = root.document;
|
|
|
|
export default {
|
|
|
|
write(name, value, hours) {
|
|
let expire = '';
|
|
if (hours) {
|
|
expire = new Date((new Date()).getTime() + hours * 3600000);
|
|
expire = '; expires=' + expire.toGMTString();
|
|
}
|
|
doc.cookie = name + '=' + escape(value) + expire;
|
|
},
|
|
|
|
read(name) {
|
|
let cookieValue = '',
|
|
search = name + '=';
|
|
if (doc.cookie.length > 0) {
|
|
let cookie = doc.cookie,
|
|
offset = cookie.indexOf(search);
|
|
if (offset !== -1) {
|
|
offset += search.length;
|
|
let end = cookie.indexOf(';', offset);
|
|
if (end === -1) {
|
|
end = cookie.length;
|
|
}
|
|
cookieValue = unescape(cookie.substring(offset, end));
|
|
}
|
|
}
|
|
return cookieValue;
|
|
},
|
|
|
|
remove(name) {
|
|
this.write(name, '', -1);
|
|
},
|
|
|
|
valueToArray(name, separator) {
|
|
if (!separator) {
|
|
separator = ',';
|
|
}
|
|
//reads the cookie
|
|
let val = this.read(name);
|
|
//creates an array with filters' values
|
|
let arr = val.split(separator);
|
|
return arr;
|
|
},
|
|
|
|
getValueByIndex(name, index, separator) {
|
|
if (!separator) {
|
|
separator = ',';
|
|
}
|
|
//reads the cookie
|
|
let val = this.valueToArray(name, separator);
|
|
return val[index];
|
|
}
|
|
|
|
};
|