From 13e2f120a8a71dcf3d32880dee0c4c029df62ce5 Mon Sep 17 00:00:00 2001 From: tconfrey Date: Wed, 21 Feb 2024 17:54:52 -0500 Subject: [PATCH] refactored configManager, added tests, moved metaProps out of parser and off AllNodes, simplified some stuff --- app/BTAppNode.js | 2 +- app/bt.js | 14 ++-- app/configManager.js | 78 +++++++++++++------ app/fileManager.js | 1 + app/parser.js | 40 ---------- versions/Release-Candidate/app/BTAppNode.js | 2 +- versions/Release-Candidate/app/bt.js | 14 ++-- .../Release-Candidate/app/configManager.js | 78 +++++++++++++------ versions/Release-Candidate/app/fileManager.js | 1 + versions/Release-Candidate/app/parser.js | 40 ---------- 10 files changed, 122 insertions(+), 148 deletions(-) diff --git a/app/BTAppNode.js b/app/BTAppNode.js index c718fc2..b885897 100644 --- a/app/BTAppNode.js +++ b/app/BTAppNode.js @@ -622,7 +622,7 @@ class BTAppNode extends BTNode { static generateOrgFile() { // iterate thru nodes to do the work - let orgText = metaPropertiesToString(AllNodes.metaProperties); + let orgText = configManager.metaPropertiesToString(); // find and order the top level nodes according to table position const topNodes = AllNodes.filter(node => node && !node.parentId); diff --git a/app/bt.js b/app/bt.js index 9558efc..cd544a8 100644 --- a/app/bt.js +++ b/app/bt.js @@ -40,14 +40,14 @@ async function launchApp(msg) { // BTId in local store and from org data should be the same. local store is primary if (msg?.Config?.BTId) { BTId = msg.Config.BTId; - if (getMetaProp('BTId') && (BTId != getMetaProp('BTId'))) - alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${getMetaProp('BTId')}`); - setMetaProp('BTId', BTId); + if (configManager.getProp('BTId') && (BTId != configManager.getProp('BTId'))) + alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${configManager.getProp('BTId')}`); + configManager.setProp('BTId', BTId); } else { // get from file if not in local storage and save locally (will allow for recovery if lost) - if (getMetaProp('BTId')) { - BTId = getMetaProp('BTId'); - configManager.setProp('BTId', BTId); + if (configManager.getProp('BTId')) { + BTId = configManager.getProp('BTId'); + //configManager.setProp('BTId', BTId); } } @@ -1774,8 +1774,6 @@ function loadBookmarks(msg) { }); gtag('event', 'BookmarkImport', {'event_category': 'Import'}); - // remmember this import - configManager.setProp('BTLastBookmarkImport', dateString); processImport(importNode.id); // see above } diff --git a/app/configManager.js b/app/configManager.js index c5f1e97..439aa71 100644 --- a/app/configManager.js +++ b/app/configManager.js @@ -16,7 +16,7 @@ const configManager = (() => { const Properties = { 'keys': ['CLIENT_ID', 'API_KEY', 'FB_KEY', 'STRIPE_KEY'], 'localStorageProps': ['BTId', 'BTTimestamp', 'BTFileID', 'BTGDriveConnected', 'BTStats', 'BTLastShownMessageIndex'], - 'orgProps': ['BTCohort', 'BTVersion', 'BTGroupingMode', 'BTLastBookmarkImport', 'BTId', 'BTManagerHome', 'BTTheme', 'BTFavicons', 'BTNotes', 'BTDense', 'BTSize'], + 'orgProps': ['BTCohort', 'BTVersion', 'BTGroupingMode', 'BTId', 'BTManagerHome', 'BTTheme', 'BTFavicons', 'BTNotes', 'BTDense', 'BTSize'], 'stats': ['BTNumTabOperations', 'BTNumSaves', 'BTNumLaunches', 'BTInstallDate', 'BTSessionStartTime', 'BTLastActivityTime', 'BTSessionStartSaves', 'BTSessionStartOps', 'BTDaysOfUse'], }; let Config, Keys = {CLIENT_ID: '', API_KEY: '', FB_KEY: '', STRIPE_KEY: ''}; @@ -39,26 +39,69 @@ const configManager = (() => { window.postMessage({'function': 'localStore', 'data': {'Config': Config}}); } if (Properties.orgProps.includes(prop)) { - setMetaProp(prop, value); // see parser.js - saveBT(); + Config[prop] = value; + //setMetaProp(prop, value); // see parser.js + //saveBT(); } + if (Properties.stats.includes(prop)) { + Config['BTStats'][prop] = value; + window.postMessage({'function': 'localStore', 'data': {'Config': Config}}); + } }; function getProp(prop) { - // getter for sync props + // getter for sync props, fetch from appropriate place based on Config array above if (Properties.localStorageProps.includes(prop)) { return Config[prop]; } if (Properties.orgProps.includes(prop)) { - return getMetaProp(prop); + return Config[prop]; } if (Properties.keys.includes(prop)) { return Keys[prop]; } + if (Properties.stats.includes(prop)) { + return Config['BTStats'][prop]; + } return null; }; + function metaPropertiesToString(ary) { + // return the string to be used to output meta properties to .org file + let str = ""; + if (!configManager.getProp('BTVersion')) + configManager.setProp('BTVersion', 1); + + Properties['orgProps'].forEach(function(prop) { + if (getProp(prop)) + str += `#+PROPERTY: ${prop} ${getProp(prop)}\n`; + }); + return str; + } + + /* + function getMetaProp(propName) { + // return the value of the meta property if it exists + let val = ''; + if (!AllNodes.metaProperties || !AllNodes.metaProperties.length) return val; + AllNodes.metaProperties.forEach(prop => { + if (prop.name == propName) + val = prop.value; + }); + return val; + } + function setMetaProp(propName, val) { + // set or change the value of the meta property + if (!AllNodes.metaProperties) AllNodes.metaProperties = []; + const index = AllNodes.metaProperties.findIndex(prop => prop.name == propName); + if (index > -1) + AllNodes.metaProperties[index] = {'name': propName, 'value': val}; + else + AllNodes.metaProperties.push({'name': propName, 'value': val}); + } + */ + function checkNewDayOfUse(prev, current) { // last active timestamp same day as this timestamp? const prevDate = new Date(prev).toLocaleDateString(); // eg 2/8/1966 @@ -84,24 +127,16 @@ const configManager = (() => { }; function setStat(statName, statValue) { - // eg sessionStartTime - Config['BTStats'][statName] = statValue; - window.postMessage({'function': 'localStore', 'data': {'Config': Config}}); + // just another prop eg sessionStartTime + setProp(statName, statValue); }; function updatePrefs() { - // update preferences based on data read into AllNodes.metaProperties + // update preferences based on orgProperties read in from file let groupMode = configManager.getProp('BTGroupingMode'); if (groupMode) { const $radio = $('#tabGroupToggle :radio[name=grouping]'); - - // v099 move away from have a WINDOW default, new window now a choice on opening - if (groupMode == 'WINDOW') { - groupMode = 'TABGROUP'; - configManager.setProp('BTGroupingMode', groupMode); - } - $radio.filter(`[value=${groupMode}]`).prop('checked', true); GroupingMode = groupMode; } @@ -309,23 +344,16 @@ const configManager = (() => { // best guess at install date cos wasn't previously set // Use bookmark import data if set. Otherwise assume 2x saves/day up to 1 year if (Config['BTStats']['BTInstallDate']) return; - if (getProp('BTLastBookmarkImport')) { - const datestr = getProp('BTLastBookmarkImport').replace(';', ':'); - const date = Date.parse(datestr); - if (date) { - setStat('BTInstallDate', date); - return; - } - } const saveDays = Math.min(Config['BTStats']['BTNumSaves'] / 2, 365); const guessedInstallDate = Date.now() - (saveDays * 24 * 60 * 60000); - setStat('BTInstallDate', guessedInstallDate); + setProp('BTInstallDate', guessedInstallDate); } return { setConfigAndKeys: setConfigAndKeys, setProp: setProp, getProp: getProp, + metaPropertiesToString: metaPropertiesToString, setStat: setStat, incrementStat: incrementStat, updatePrefs: updatePrefs, diff --git a/app/fileManager.js b/app/fileManager.js index 8a0485a..b358a48 100644 --- a/app/fileManager.js +++ b/app/fileManager.js @@ -66,6 +66,7 @@ async function saveBT(localOnly = false) { messageManager.removeWarning(); // remove stale warning if any gtag('event', event, {'event_category': 'Save', 'event_label': 'NumNodes', 'value': AllNodes.length}); configManager.incrementStat('BTNumSaves'); + configManager.setProp('BTVersion', parseInt(configManager.getProp('BTVersion')) + 1); } async function authorizeLocalFile() { diff --git a/app/parser.js b/app/parser.js index 7d4d836..8216d8a 100644 --- a/app/parser.js +++ b/app/parser.js @@ -172,46 +172,6 @@ function orgaNodeRawText(organode) { return string; } -function metaPropertiesToString(ary) { - // return the string to be used to output meta properties to .org file - // ary is as captured in original parse, array of {name: value:} - if (!ary || !ary.length) return ""; - let str = ""; - let metaprops = []; - - if (!getMetaProp('BTVersion')) - ary.push({'name' : 'BTVersion', 'value' : 0}); - ary.forEach(function(prop) { - if (prop.name == 'BTVersion') // increment version - prop.value++; - str += `#+PROPERTY: ${prop.name} ${prop.value}\n`; - metaprops.push(prop); - }); - AllNodes.metaProperties = metaprops; // update AllNodes for next time around - return str; -} - -function getMetaProp(propName) { - // return the value of the meta property if it exists - let val = ''; - if (!AllNodes.metaProperties || !AllNodes.metaProperties.length) return val; - AllNodes.metaProperties.forEach(prop => { - if (prop.name == propName) - val = prop.value; - }); - return val; -} - -function setMetaProp(propName, val) { - // set or change the value of the meta property - if (!AllNodes.metaProperties) AllNodes.metaProperties = []; - const index = AllNodes.metaProperties.findIndex(prop => prop.name == propName); - if (index > -1) - AllNodes.metaProperties[index] = {'name': propName, 'value': val}; - else - AllNodes.metaProperties.push({'name': propName, 'value': val}); -} - function generateLinesAndColumns(filetext) { // return an array of the original lines and columns for use in regnerating orga diff --git a/versions/Release-Candidate/app/BTAppNode.js b/versions/Release-Candidate/app/BTAppNode.js index c718fc2..b885897 100644 --- a/versions/Release-Candidate/app/BTAppNode.js +++ b/versions/Release-Candidate/app/BTAppNode.js @@ -622,7 +622,7 @@ class BTAppNode extends BTNode { static generateOrgFile() { // iterate thru nodes to do the work - let orgText = metaPropertiesToString(AllNodes.metaProperties); + let orgText = configManager.metaPropertiesToString(); // find and order the top level nodes according to table position const topNodes = AllNodes.filter(node => node && !node.parentId); diff --git a/versions/Release-Candidate/app/bt.js b/versions/Release-Candidate/app/bt.js index 9558efc..cd544a8 100644 --- a/versions/Release-Candidate/app/bt.js +++ b/versions/Release-Candidate/app/bt.js @@ -40,14 +40,14 @@ async function launchApp(msg) { // BTId in local store and from org data should be the same. local store is primary if (msg?.Config?.BTId) { BTId = msg.Config.BTId; - if (getMetaProp('BTId') && (BTId != getMetaProp('BTId'))) - alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${getMetaProp('BTId')}`); - setMetaProp('BTId', BTId); + if (configManager.getProp('BTId') && (BTId != configManager.getProp('BTId'))) + alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${configManager.getProp('BTId')}`); + configManager.setProp('BTId', BTId); } else { // get from file if not in local storage and save locally (will allow for recovery if lost) - if (getMetaProp('BTId')) { - BTId = getMetaProp('BTId'); - configManager.setProp('BTId', BTId); + if (configManager.getProp('BTId')) { + BTId = configManager.getProp('BTId'); + //configManager.setProp('BTId', BTId); } } @@ -1774,8 +1774,6 @@ function loadBookmarks(msg) { }); gtag('event', 'BookmarkImport', {'event_category': 'Import'}); - // remmember this import - configManager.setProp('BTLastBookmarkImport', dateString); processImport(importNode.id); // see above } diff --git a/versions/Release-Candidate/app/configManager.js b/versions/Release-Candidate/app/configManager.js index c5f1e97..439aa71 100644 --- a/versions/Release-Candidate/app/configManager.js +++ b/versions/Release-Candidate/app/configManager.js @@ -16,7 +16,7 @@ const configManager = (() => { const Properties = { 'keys': ['CLIENT_ID', 'API_KEY', 'FB_KEY', 'STRIPE_KEY'], 'localStorageProps': ['BTId', 'BTTimestamp', 'BTFileID', 'BTGDriveConnected', 'BTStats', 'BTLastShownMessageIndex'], - 'orgProps': ['BTCohort', 'BTVersion', 'BTGroupingMode', 'BTLastBookmarkImport', 'BTId', 'BTManagerHome', 'BTTheme', 'BTFavicons', 'BTNotes', 'BTDense', 'BTSize'], + 'orgProps': ['BTCohort', 'BTVersion', 'BTGroupingMode', 'BTId', 'BTManagerHome', 'BTTheme', 'BTFavicons', 'BTNotes', 'BTDense', 'BTSize'], 'stats': ['BTNumTabOperations', 'BTNumSaves', 'BTNumLaunches', 'BTInstallDate', 'BTSessionStartTime', 'BTLastActivityTime', 'BTSessionStartSaves', 'BTSessionStartOps', 'BTDaysOfUse'], }; let Config, Keys = {CLIENT_ID: '', API_KEY: '', FB_KEY: '', STRIPE_KEY: ''}; @@ -39,26 +39,69 @@ const configManager = (() => { window.postMessage({'function': 'localStore', 'data': {'Config': Config}}); } if (Properties.orgProps.includes(prop)) { - setMetaProp(prop, value); // see parser.js - saveBT(); + Config[prop] = value; + //setMetaProp(prop, value); // see parser.js + //saveBT(); } + if (Properties.stats.includes(prop)) { + Config['BTStats'][prop] = value; + window.postMessage({'function': 'localStore', 'data': {'Config': Config}}); + } }; function getProp(prop) { - // getter for sync props + // getter for sync props, fetch from appropriate place based on Config array above if (Properties.localStorageProps.includes(prop)) { return Config[prop]; } if (Properties.orgProps.includes(prop)) { - return getMetaProp(prop); + return Config[prop]; } if (Properties.keys.includes(prop)) { return Keys[prop]; } + if (Properties.stats.includes(prop)) { + return Config['BTStats'][prop]; + } return null; }; + function metaPropertiesToString(ary) { + // return the string to be used to output meta properties to .org file + let str = ""; + if (!configManager.getProp('BTVersion')) + configManager.setProp('BTVersion', 1); + + Properties['orgProps'].forEach(function(prop) { + if (getProp(prop)) + str += `#+PROPERTY: ${prop} ${getProp(prop)}\n`; + }); + return str; + } + + /* + function getMetaProp(propName) { + // return the value of the meta property if it exists + let val = ''; + if (!AllNodes.metaProperties || !AllNodes.metaProperties.length) return val; + AllNodes.metaProperties.forEach(prop => { + if (prop.name == propName) + val = prop.value; + }); + return val; + } + function setMetaProp(propName, val) { + // set or change the value of the meta property + if (!AllNodes.metaProperties) AllNodes.metaProperties = []; + const index = AllNodes.metaProperties.findIndex(prop => prop.name == propName); + if (index > -1) + AllNodes.metaProperties[index] = {'name': propName, 'value': val}; + else + AllNodes.metaProperties.push({'name': propName, 'value': val}); + } + */ + function checkNewDayOfUse(prev, current) { // last active timestamp same day as this timestamp? const prevDate = new Date(prev).toLocaleDateString(); // eg 2/8/1966 @@ -84,24 +127,16 @@ const configManager = (() => { }; function setStat(statName, statValue) { - // eg sessionStartTime - Config['BTStats'][statName] = statValue; - window.postMessage({'function': 'localStore', 'data': {'Config': Config}}); + // just another prop eg sessionStartTime + setProp(statName, statValue); }; function updatePrefs() { - // update preferences based on data read into AllNodes.metaProperties + // update preferences based on orgProperties read in from file let groupMode = configManager.getProp('BTGroupingMode'); if (groupMode) { const $radio = $('#tabGroupToggle :radio[name=grouping]'); - - // v099 move away from have a WINDOW default, new window now a choice on opening - if (groupMode == 'WINDOW') { - groupMode = 'TABGROUP'; - configManager.setProp('BTGroupingMode', groupMode); - } - $radio.filter(`[value=${groupMode}]`).prop('checked', true); GroupingMode = groupMode; } @@ -309,23 +344,16 @@ const configManager = (() => { // best guess at install date cos wasn't previously set // Use bookmark import data if set. Otherwise assume 2x saves/day up to 1 year if (Config['BTStats']['BTInstallDate']) return; - if (getProp('BTLastBookmarkImport')) { - const datestr = getProp('BTLastBookmarkImport').replace(';', ':'); - const date = Date.parse(datestr); - if (date) { - setStat('BTInstallDate', date); - return; - } - } const saveDays = Math.min(Config['BTStats']['BTNumSaves'] / 2, 365); const guessedInstallDate = Date.now() - (saveDays * 24 * 60 * 60000); - setStat('BTInstallDate', guessedInstallDate); + setProp('BTInstallDate', guessedInstallDate); } return { setConfigAndKeys: setConfigAndKeys, setProp: setProp, getProp: getProp, + metaPropertiesToString: metaPropertiesToString, setStat: setStat, incrementStat: incrementStat, updatePrefs: updatePrefs, diff --git a/versions/Release-Candidate/app/fileManager.js b/versions/Release-Candidate/app/fileManager.js index 8a0485a..b358a48 100644 --- a/versions/Release-Candidate/app/fileManager.js +++ b/versions/Release-Candidate/app/fileManager.js @@ -66,6 +66,7 @@ async function saveBT(localOnly = false) { messageManager.removeWarning(); // remove stale warning if any gtag('event', event, {'event_category': 'Save', 'event_label': 'NumNodes', 'value': AllNodes.length}); configManager.incrementStat('BTNumSaves'); + configManager.setProp('BTVersion', parseInt(configManager.getProp('BTVersion')) + 1); } async function authorizeLocalFile() { diff --git a/versions/Release-Candidate/app/parser.js b/versions/Release-Candidate/app/parser.js index 7d4d836..8216d8a 100644 --- a/versions/Release-Candidate/app/parser.js +++ b/versions/Release-Candidate/app/parser.js @@ -172,46 +172,6 @@ function orgaNodeRawText(organode) { return string; } -function metaPropertiesToString(ary) { - // return the string to be used to output meta properties to .org file - // ary is as captured in original parse, array of {name: value:} - if (!ary || !ary.length) return ""; - let str = ""; - let metaprops = []; - - if (!getMetaProp('BTVersion')) - ary.push({'name' : 'BTVersion', 'value' : 0}); - ary.forEach(function(prop) { - if (prop.name == 'BTVersion') // increment version - prop.value++; - str += `#+PROPERTY: ${prop.name} ${prop.value}\n`; - metaprops.push(prop); - }); - AllNodes.metaProperties = metaprops; // update AllNodes for next time around - return str; -} - -function getMetaProp(propName) { - // return the value of the meta property if it exists - let val = ''; - if (!AllNodes.metaProperties || !AllNodes.metaProperties.length) return val; - AllNodes.metaProperties.forEach(prop => { - if (prop.name == propName) - val = prop.value; - }); - return val; -} - -function setMetaProp(propName, val) { - // set or change the value of the meta property - if (!AllNodes.metaProperties) AllNodes.metaProperties = []; - const index = AllNodes.metaProperties.findIndex(prop => prop.name == propName); - if (index > -1) - AllNodes.metaProperties[index] = {'name': propName, 'value': val}; - else - AllNodes.metaProperties.push({'name': propName, 'value': val}); -} - function generateLinesAndColumns(filetext) { // return an array of the original lines and columns for use in regnerating orga