158 lines
2.7 KiB
JavaScript
158 lines
2.7 KiB
JavaScript
/*
|
|
|
|
Copyright (c) 2020, 2023, The Unified Company.
|
|
|
|
This code is part of Unify.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE,
|
|
as published by the Free Software Foundation.
|
|
See the GNU AFFERO GENERAL PUBLIC LICENSE, for more details.
|
|
|
|
https://unifyjs.org
|
|
|
|
*/
|
|
|
|
import document from "./document.js"
|
|
|
|
class cookieManager{
|
|
|
|
constructor() {
|
|
|
|
|
|
if( document.cookie == "" ) {
|
|
|
|
this.createEmptyCookie( "cookieObject" )
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
createEmptyCookie( name ) {
|
|
|
|
this.setCookie( name, "" );
|
|
|
|
}
|
|
|
|
set( name, value ) {
|
|
|
|
var cookieObject = this.getCookieObject( "cookieObject" );
|
|
|
|
|
|
cookieObject[ name ] = value;
|
|
|
|
console.log("cookieObject", cookieObject);
|
|
|
|
this.setCookie( "cookieObject", JSON.stringify( cookieObject ) );
|
|
|
|
/*
|
|
if( !value ) {
|
|
|
|
this.createEmptyCookie( name )
|
|
|
|
} else {
|
|
|
|
var userObject = this.createCookieUser( value );
|
|
|
|
this.setCookie( name, JSON.stringify( userObject ) );
|
|
|
|
}
|
|
*/
|
|
}
|
|
|
|
get( name ) {
|
|
|
|
var cookieObject = this.getCookieObject( "cookieObject" );
|
|
|
|
return cookieObject[name];
|
|
|
|
}
|
|
|
|
getCookieObject( name ) {
|
|
|
|
var item = this.getCookie( name ).replace( name + "=", "" );
|
|
|
|
if( item == "undefined" || item == "" ) {
|
|
|
|
return new Object();
|
|
|
|
}
|
|
|
|
console.log(item);
|
|
|
|
return JSON.parse( item );
|
|
|
|
}
|
|
|
|
setCookie( name, value ){
|
|
|
|
var expirydate = new Date();
|
|
|
|
expirydate.setTime( expirydate.getTime() + ( 100 * 60 * 60 * 24 * 100 ) );
|
|
|
|
var jsonString = name + "=" + value + "; expires=" + expirydate.toGMTString();
|
|
|
|
document.cookie = jsonString;
|
|
|
|
}
|
|
|
|
getCookieStartIndex( cookieName ) {
|
|
|
|
return this.docCookie.indexOf( cookieName );
|
|
|
|
}
|
|
|
|
getCookieEndIndex( cookieName, cookieStart ) {
|
|
|
|
if ( cookieStart != -1 ) {
|
|
|
|
cookieStart = cookieStart + cookieName.length;
|
|
|
|
var end = this.docCookie.indexOf(";",cookieStart);
|
|
|
|
if ( end == -1 ) {
|
|
|
|
end = this.docCookie.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
getCookieString( cookieStartIndex, cookieEndIndex ) {
|
|
|
|
return this.docCookie.substring( cookieStartIndex, cookieEndIndex );
|
|
|
|
}
|
|
|
|
getCookie( name ) {
|
|
|
|
var cookieName = name + "=";
|
|
|
|
this.docCookie = document.cookie;
|
|
|
|
if ( this.docCookie.length > 0 ) {
|
|
|
|
var cookieStartIndex = this.getCookieStartIndex( cookieName );
|
|
|
|
var cookieEndIndex = this.getCookieEndIndex( cookieName, cookieStartIndex );
|
|
|
|
var cookieString = this.getCookieString( cookieStartIndex, cookieEndIndex );
|
|
|
|
return unescape( cookieString );
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default new cookieManager();
|