Skip to content

Commit

Permalink
using closures to give the effect of private class fields
Browse files Browse the repository at this point in the history
  • Loading branch information
e2tha-e committed Oct 27, 2019
1 parent e03d4f0 commit edd0d72
Show file tree
Hide file tree
Showing 15 changed files with 1,539 additions and 1,563 deletions.
375 changes: 186 additions & 189 deletions scripts/classes/annotations-viewer.js

Large diffs are not rendered by default.

779 changes: 390 additions & 389 deletions scripts/classes/code-viewer.js

Large diffs are not rendered by default.

185 changes: 93 additions & 92 deletions scripts/classes/data-saver.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,122 @@
let fepperUiInst;
// TODO: Replace closure with private class field when there is greater browser support.
export default function (cookieName, fepperUiInst) {
class DataSaver {

/**
* Data Saver. Namespaced cookie-handling methods.
*
* @param {string} cookieName - The name of the cookie to store the data in.
* @param {object} fepperUi - The Fepper UI instance.
*/
constructor(cookieName, fepperUi) { // eslint-disable-line no-unused-vars
this.cookieName = cookieName;
}

export default class {
// In case fepperUi.cookies is undefined at instantiation.
get cookies() {
return fepperUiInst.cookies;
}

/**
* Data Saver. Namespaced cookie-handling methods.
*
* @param {string} cookieName - The name of the cookie to store the data in.
* @param {object} fepperUi - The Fepper UI instance.
*/
constructor(cookieName, fepperUi) {
fepperUiInst = fepperUi;
// Methods.

this.cookieName = cookieName;
}
/**
* Add a given value to the cookie. Do not update if it already exists.
*
* @param {string} name - The name of the key.
* @param {string} val - The value.
*/
addValue(name, val) {
const cookieValOrig = this.cookies.get(this.cookieName);
let cookieValNew = name + '~' + val;

// In case fepperUi.cookies is undefined at instantiation.
get cookies() {
return fepperUiInst.cookies;
}
if (cookieValOrig) {
cookieValNew = cookieValOrig + '|' + cookieValNew;
}

// Methods.
this.cookies.set(this.cookieName, cookieValNew);
}

/**
* Add a given value to the cookie. Do not update if it already exists.
*
* @param {string} name - The name of the key.
* @param {string} val - The value.
*/
addValue(name, val) {
const cookieValOrig = this.cookies.get(this.cookieName);
let cookieValNew = name + '~' + val;
/**
* Update a value found in the cookie. If the key doesn't exist, add the value.
*
* @param {string} name - The name of the key.
* @param {string} val - The value.
*/
updateValue(name, val) {
if (this.findValue(name)) {
const cookieVal = this.cookies.get(this.cookieName) || '';
const cookieVals = cookieVal.split('|');
let cookieValNew = '';

for (let i = 0; i < cookieVals.length; i++) {
const fieldVals = cookieVals[i].split('~');

if (fieldVals[0] === name) {
fieldVals[1] = val;
}

if (i) {
cookieValNew += '|';
}

cookieValNew += fieldVals[0] + '~' + fieldVals[1];
}

if (cookieValOrig) {
cookieValNew = cookieValOrig + '|' + cookieValNew;
this.cookies.set(this.cookieName, cookieValNew);
}
else {
this.addValue(name, val);
}
}

this.cookies.set(this.cookieName, cookieValNew);
}

/**
* Update a value found in the cookie. If the key doesn't exist, add the value.
*
* @param {string} name - The name of the key.
* @param {string} val - The value.
*/
updateValue(name, val) {
if (this.findValue(name)) {
/**
* Remove the given key.
*
* @param {string} name - The name of the key.
*/
removeValue(name) {
const cookieVal = this.cookies.get(this.cookieName) || '';
const cookieVals = cookieVal.split('|');
let k = 0;
let cookieValNew = '';

for (let i = 0; i < cookieVals.length; i++) {
const fieldVals = cookieVals[i].split('~');

if (fieldVals[0] === name) {
fieldVals[1] = val;
}
if (fieldVals[0] !== name) {
if (k) {
cookieValNew += '|';
}

if (i) {
cookieValNew += '|';
cookieValNew += fieldVals[0] + '~' + fieldVals[1];
k++;
}

cookieValNew += fieldVals[0] + '~' + fieldVals[1];
}

this.cookies.set(this.cookieName, cookieValNew);
}
else {
this.addValue(name, val);
}
}

/**
* Remove the given key.
*
* @param {string} name - The name of the key.
*/
removeValue(name) {
const cookieVal = this.cookies.get(this.cookieName) || '';
const cookieVals = cookieVal.split('|');
let k = 0;
let cookieValNew = '';

for (let i = 0; i < cookieVals.length; i++) {
const fieldVals = cookieVals[i].split('~');

if (fieldVals[0] !== name) {
if (k) {
cookieValNew += '|';
}

cookieValNew += fieldVals[0] + '~' + fieldVals[1];
k++;
}
}
/**
* Find the value using the given key.
*
* @param {string} name - The name of the key.
* @returns {string} The value of the key or empty string if the value isn't found.
*/
findValue(name) {
const cookieVal = this.cookies.get(this.cookieName) || '';
const cookieVals = cookieVal.split('|');

this.cookies.set(this.cookieName, cookieValNew);
}
for (let i = 0; i < cookieVals.length; i++) {
const fieldVals = cookieVals[i].split('~');

/**
* Find the value using the given key.
*
* @param {string} name - The name of the key.
* @returns {string} The value of the key or empty string if the value isn't found.
*/
findValue(name) {
const cookieVal = this.cookies.get(this.cookieName) || '';
const cookieVals = cookieVal.split('|');

for (let i = 0; i < cookieVals.length; i++) {
const fieldVals = cookieVals[i].split('~');

if (fieldVals[0] === name) {
return fieldVals[1];
if (fieldVals[0] === name) {
return fieldVals[1];
}
}
}

return '';
return '';
}
}

return new DataSaver(cookieName, fepperUiInst);
}
Loading

0 comments on commit edd0d72

Please sign in to comment.