From 574ad0cc0e91fe0e63b56ffb5204050db18e0126 Mon Sep 17 00:00:00 2001 From: Julien Wilhelm Date: Fri, 6 Jan 2023 11:46:09 +0100 Subject: [PATCH 01/19] =?UTF-8?q?Makes=20Num=C3=89coDiag=20able=20to=20be?= =?UTF-8?q?=20served=20as=20webApp=20and=20makes=20debugging=20easier.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.svelte | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index bb6e6a0..0132374 100755 --- a/src/App.svelte +++ b/src/App.svelte @@ -25,7 +25,37 @@ } ]; let render = false; // Makes Results & AuditForm components reactives - const env = window.browser === undefined ? window.chrome : window.browser; // Makes extension cross platform + const webAppMode = false; // Makes NumÉcoDiag able to be served as webApp and makes debugging easier (entry point: /public/index.html) + const env = window.browser === undefined ? window.chrome : window.browser; // Makes NumÉcoDiag cross browser as webExtension + + const setLocalData = (name, value) => { + return new Promise( + (resolve) => { + if(!webAppMode) { // Asynchronous + env.storage.local.set({[name]: value}) + .then(resolve()); + } + else { // Synchronous + localStorage.setItem(name, value); + resolve(); + } + } + ) + } + + const getLocalData = name => { + return new Promise( + (resolve) => { // Asynchronous + if(!webAppMode) { + env.storage.local.get(name) + .then(data => resolve(data[name])); + } + else { // Synchronous + resolve(localStorage.getItem(name)); + } + } + ) + } /* ### FUNCTIONS ### */ @@ -75,7 +105,7 @@ audits[index].byCounters.notApplicable = 0; audits[index].selectedVersion = currentVersion; // Udpates storage - env.storage.local.set({'audits': JSON.stringify(audits)}); + setLocalData('audits', JSON.stringify(audits)); // Updates view render = !render; return true; @@ -129,13 +159,13 @@ } // Sets the new value and saves updated audit audits[index].byCriteria[criterion.id][criterion.prop] = criterion.value; - env.storage.local.set({'audits': JSON.stringify(audits)}); + setLocalData('audits', JSON.stringify(audits)); } function getAudits() { return new Promise((resolve ,reject) => { - env.storage.local.get(['audits']).then((data) => - data.audits !== undefined ? resolve(data) : reject('Absence d\'historique') + getLocalData('audits').then((data) => + (data !== null && data !== undefined) ? resolve(data): reject('Absence d\'historique') ); }); } @@ -205,11 +235,11 @@ /* ### PROCEDURAL ### */ getAudits() - .then(data => audits = JSON.parse(data.audits)) + .then(data => audits = JSON.parse(data)) .catch((warning) => console.warn(warning)) .finally(() => getRGESN(audits[index].selectedVersion).then(() => { - window.onscroll = () => env.storage.local.set({'scrollPosY': window.scrollY}); - env.storage.local.get('scrollPosY').then(data => window.scroll(0, (data.scrollPosY || 0))); + window.onscroll = () => setLocalData('scrollPosY', window.scrollY); + getLocalData('scrollPosY').then(scrollPosY => window.scroll(0, (scrollPosY || 0))); } ) ); From 986a31bfe82c15419124599cf90dc43d3a43d10d Mon Sep 17 00:00:00 2001 From: Julien Wilhelm Date: Tue, 10 Jan 2023 10:45:25 +0100 Subject: [PATCH 02/19] Spellchecking. --- src/App.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.svelte b/src/App.svelte index 0132374..ee5a982 100755 --- a/src/App.svelte +++ b/src/App.svelte @@ -216,7 +216,7 @@ } function buildBadge() { - alert(`Retrouvez votre badge en HTML dans votre dossier téléchargement. Vous pouvez afficher ce badge dans vos communications losque 100% des critères sont évalués.`); + alert(`Retrouvez votre badge en HTML dans votre dossier téléchargement. Vous pouvez afficher ce badge dans vos communications lorsque 100% des critères sont évalués.`); const nbOfCriteria= referential.criteres.length; const counters = audits[index].byCounters; From ade66a7e7018a32151997ff989044172bd4af7b7 Mon Sep 17 00:00:00 2001 From: Julien Wilhelm Date: Tue, 10 Jan 2023 11:26:58 +0100 Subject: [PATCH 03/19] =?UTF-8?q?Makes=20Num=C3=89coDiag=20able=20to=20exp?= =?UTF-8?q?ort=20current=20data=20as=20JSON.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.svelte | 26 +++++++++++++++++--------- src/components/Options.svelte | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index ee5a982..926533f 100755 --- a/src/App.svelte +++ b/src/App.svelte @@ -170,7 +170,16 @@ }); } - function exportAudit() { + function exportFile(content, mimetype, filename) { + let blob = new Blob([content], {type: mimetype}); + let aElm = document.createElement('a'); + aElm.setAttribute('href', window.URL.createObjectURL(blob)); + aElm.setAttribute('download', filename); + aElm.click(); + aElm.remove(); + } + + function exportResults() { let csv = 'ID;Thématique;Libellé du critère;Évaluation;Commentaire;\n'; for(const criterion of referential.criteres) { csv += `${criterion.id};${criterion.thematique};${criterion.critere};`; @@ -207,17 +216,15 @@ } csv += ';\n'; } - let blob = new Blob([csv], {}); - let aElm = document.createElement('a'); - aElm.setAttribute('href', window.URL.createObjectURL(blob)); - aElm.setAttribute('download', 'NumÉcoDiag.csv'); - aElm.click(); - aElm.remove(); + exportFile(csv, "text/csv", "NumÉcoDiag.csv"); + } + + function exportData() { + exportFile(JSON.stringify(audits[index]), "application/json", "NumÉcoDiag.json"); } function buildBadge() { alert(`Retrouvez votre badge en HTML dans votre dossier téléchargement. Vous pouvez afficher ce badge dans vos communications lorsque 100% des critères sont évalués.`); - const nbOfCriteria= referential.criteres.length; const counters = audits[index].byCounters; const assessed = counters.satisfied + counters.rejected + counters.notApplicable; @@ -258,7 +265,8 @@ on:changeVersion="{(e) => changeRGESN(e.detail.versionToApply)}" on:resetAudit="{() => resetAudit(undefined)}" on:buildBadge="{() => buildBadge()}" - on:exportAudit="{exportAudit}" /> + on:exportResults="{exportResults}" + on:exportData="{exportData}" /> Valider {/if} --> - + + \ No newline at end of file diff --git a/src/components/Options.svelte b/src/components/Options.svelte deleted file mode 100644 index 4518b73..0000000 --- a/src/components/Options.svelte +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - -
- - -
- - \ No newline at end of file From 3f2aea758c98d8ca3cbad4ac7405a30d089eaf6b Mon Sep 17 00:00:00 2001 From: Julien Wilhelm Date: Thu, 12 Jan 2023 15:36:37 +0100 Subject: [PATCH 14/19] Refactoring. --- src/App.svelte | 2 +- src/Options.svelte | 5 +---- src/components/Navigation.svelte | 3 ++- src/modules/env.mjs | 2 ++ src/modules/{module.mjs => helpers.mjs} | 4 ++-- src/modules/webAppMode.mjs | 2 ++ src/stores.js | 0 7 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 src/modules/env.mjs rename src/modules/{module.mjs => helpers.mjs} (88%) create mode 100644 src/modules/webAppMode.mjs create mode 100644 src/stores.js diff --git a/src/App.svelte b/src/App.svelte index 96558ce..7dd731c 100755 --- a/src/App.svelte +++ b/src/App.svelte @@ -3,7 +3,7 @@ /* ### IMPORTS ### */ // Shared code - import { getLocalData, setLocalData, getAudits, getRGESN } from './modules/module.mjs'; + import { getLocalData, setLocalData, getAudits, getRGESN } from './modules/helpers.mjs'; // Svelte components import Navigation from './components/Navigation.svelte'; diff --git a/src/Options.svelte b/src/Options.svelte index df928a5..c96cef1 100644 --- a/src/Options.svelte +++ b/src/Options.svelte @@ -3,7 +3,7 @@ /* ### IMPORTS ### */ // Shared code - import { setLocalData, getAudits, getRGESN } from './modules/module.mjs'; + import { setLocalData, getAudits, getRGESN } from './modules/helpers.mjs'; /* ### VARIABLES ### */ @@ -21,7 +21,6 @@ selectedVersion: defaultVersion, // By default. } ]; - let render = false; // Makes Results & AuditForm components reactives /* ### FUNCTIONS ### */ @@ -121,8 +120,6 @@ audits[index].selectedVersion = defaultVersion; // Udpates storage setLocalData('audits', JSON.stringify(audits)); - // Updates view - render = !render; return true; } return false diff --git a/src/components/Navigation.svelte b/src/components/Navigation.svelte index 98a0f77..c92c895 100644 --- a/src/components/Navigation.svelte +++ b/src/components/Navigation.svelte @@ -1,5 +1,6 @@