added cookie class for network to be saved

This commit is contained in:
Maximilian Bethke 2021-02-01 11:58:07 +01:00 committed by GitHub
parent 745f32f0e0
commit 23dd62cfd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 30 deletions

View File

@ -75,8 +75,8 @@
<li><a href="#wallet" data-toggle="tab"><span class="glyphicon glyphicon-briefcase"></span> Wallet</a></li>
<li><a href="#about" data-toggle="tab"><span class="glyphicon glyphicon-info-sign"></span> About</a></li>
<li class="hidden"><a href="#settings" data-toggle="tab"><span class="glyphicon glyphicon-cog"></span> Settings</a></li>
<li class="hidden"><a href="#fees" data-toggle="tab"><span class="glyphicon glyphicon-tag"></span> Fees</a></li>
<li><a href="#settings" data-toggle="tab"><span class="glyphicon glyphicon-cog"></span> Settings</a></li>
<li><a href="#fees" data-toggle="tab"><span class="glyphicon glyphicon-tag"></span> Fees</a></li>
</ul>
</div>
</div>

View File

@ -1932,13 +1932,11 @@ $(document).ready(function() {
});
$("#coinjs_coin").change(function(){
// set cookie for use after page (re)load
let cookie = new Cookie("coinbin_coin_option");
cookie.setValue($(this).val());
var optionSeleted = ($("option:selected",this).attr("rel")).split(";");
let cookieSelectedCoin = new Cookie("coinbin_coin");
cookieSelectedCoin.setCookie(optionSeleted)
console.log(cookieSelectedCoin.cookieValue);
optionSeleted = ($("option:selected",this).attr("rel")).split(";");
// deal with broadcasting settings
if(optionSeleted[5]=="false"){
@ -2290,4 +2288,16 @@ $(document).ready(function() {
return true;
};
/* set network from cookie on page load */
function setNetwork () {
let cookie = new Cookie("coinbin_coin_option");
if(val = cookie.getValue())
{
$("#coinjs_coin").val(val)
$("#coinjs_coin").trigger("change")
}
}
setNetwork()
});

View File

@ -1,43 +1,85 @@
class Cookie {
#name;
#value;
constructor(name) {
this.cookieName = name;
// read the cookies value (initializes cookieValue)
this.readCookie();
this.name = name;
this.value;
// read the cookies value (initializes value)
this.get();
}
readCookie() {
get() {
var allCookies = document.cookie;
var cookie;
// find the cookie that is currently being looked at.
allCookies.split(";").forEach((_, cookie) => {
var currentCookieName, currentCookieValue = cookie.split("=");
// find out how much cookies are set to be able to pick the right one
if(!allCookies)
{
// the tray is empty. leave fields as initialized
return;
}
if(currentCookieName = this.cookieName)
if(allCookies.indexOf(";") > 0)
{
// many cookies found, pick one
cookie = this.pickCookie(allCookies);
} else {
// only one cookie found. Check if its the right one
if(this.validateCookie(allCookies))
{
// set cookieValue to the value of this cookie
this.cookieValue = currentCookieValue;
break;
cookie = allCookies;
}
}
// read contents of the picked cookie
if(cookie) {
this.value = this.readCookieContents(cookie);
} else {
console.log("Coudn't get cookie");
}
}
pickCookie(manyCookies) {
manyCookies.split(";").forEach((cookie, _) => {
if(this.validateCookie(cookie))
{
return cookie;
}
});
// default to NULL if cookie was not found
this.cookieValue = NULL;
}
setCookie(cookieValue) {
validateCookie(cookie) {
if(cookie.indexOf("=") >= 0)
{
var cookieCredentials = cookie.split("=");
var name = cookieCredentials[0]
return name == this.name;
} else {
return false;
}
}
readCookieContents(cookie) {
var cookieCredentials = cookie.split("=");
var value = cookieCredentials[1];
return JSON.parse(value);
}
setValue(valueToBeSet) {
// update internal record
this.cookieValue = currentCookieValue;
this.value = valueToBeSet;
// prepare value for storing
cookieValueJSON = JSON.stringify(cookieValue);
var valueJSON = JSON.stringify(valueToBeSet);
// set cookie with expiry on browser close
document.cookie = this.cookieName+"="+cookieValueJSON;
document.cookie = this.name+"="+valueJSON;
}
getCookieValue() {
this.readCookie();
return this.cookieValue;
getValue() {
this.get();
return this.value;
}
}