From 9cb64f2a8f5c86d5b5ef5ad65fd7f3c9f0ef7a13 Mon Sep 17 00:00:00 2001 From: Sarah Vaupel Date: Thu, 9 Jan 2020 11:08:43 +0100 Subject: [PATCH] feat(exam-correct): setup basic session storage manager, add util stub --- .../lib/storage-manager/storage-manager.js | 50 ++++++++++++++++++- .../src/utils/exam-correct/exam-correct.js | 33 ++++++++++++ frontend/src/utils/utils.js | 2 + templates/exam-correct.hamlet | 2 +- 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 frontend/src/utils/exam-correct/exam-correct.js diff --git a/frontend/src/lib/storage-manager/storage-manager.js b/frontend/src/lib/storage-manager/storage-manager.js index 9b6202329..d071d0c2b 100644 --- a/frontend/src/lib/storage-manager/storage-manager.js +++ b/frontend/src/lib/storage-manager/storage-manager.js @@ -4,10 +4,11 @@ import * as semver from 'semver'; export const LOCATION = { LOCAL: 'local', + SESSION: 'session', WINDOW: 'window', }; -const LOCATION_SHADOWING = [ LOCATION.WINDOW, LOCATION.LOCAL ]; +const LOCATION_SHADOWING = [ LOCATION.WINDOW, LOCATION.SESSION, LOCATION.LOCAL ]; export class StorageManager { @@ -80,6 +81,10 @@ export class StorageManager { val = this._getFromLocalStorage()[key]; break; } + case LOCATION.SESSION: { + val = this._getFromSessionStorage()[key]; + break; + } case LOCATION.WINDOW: { val = this._getFromWindow()[key]; break; @@ -220,4 +225,47 @@ export class StorageManager { delete this._global.App.Storage[this.namespace]; } } + + + _getFromSessionStorage() { + let state; + + try { + state = JSON.parse(window.sessionStorage.getItem(this.namespace)); + } catch { + state = null; + } + + if (state === null || !state.version || !semver.satisfies(this.version, `^${state.version}`)) { + // remove item from session if it stores an invalid state + this._clearSessionStorage(); + return {}; + } + + if ('state' in state) + return state.state; + else { + delete state.version; + return state; + } + } + + _saveToSessionStorage(state) { + if (!state) + return this._clearSessionStorage(); + + let versionedState; + + if ('version' in state || 'state' in state) { + versionedState = { version: this.version, state: state }; + } else { + versionedState = { version: this.version, ...state }; + } + + window.sessionStorage.setItem(this.namespace, JSON.stringify(versionedState)); + } + + _clearSessionStorage() { + window.sessionStorage.removeItem(this.namespace); + } } diff --git a/frontend/src/utils/exam-correct/exam-correct.js b/frontend/src/utils/exam-correct/exam-correct.js new file mode 100644 index 000000000..cc8705345 --- /dev/null +++ b/frontend/src/utils/exam-correct/exam-correct.js @@ -0,0 +1,33 @@ +import { Utility } from '../../core/utility'; +import { StorageManager, LOCATION } from '../../lib/storage-manager/storage-manager'; +// import './exam-correct.sass'; + +const EXAM_CORRECT_IDENT = 'uw-exam-correct'; + +@Utility({ + selector: `[${EXAM_CORRECT_IDENT}]`, +}) +export class ExamCorrect { + + _storageManager = new StorageManager('EXAM_CORRECT', '0.0.0', { location: LOCATION.SESSION, encrypted: true }); + + _element; + + constructor(element) { + if (!element) { + throw new Error('Exam Correct utility cannot be setup without an element!'); + } + + this._element = element; + + if (this._element.tagName !== 'TABLE') { + throw new Error('Exam Correct utility can only be setup with a element!'); + } + + } + + destroy() { + console.log('TBD: destroy ExamCorrect'); + } + +} diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index ebcb76c5b..f18964cc6 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -13,6 +13,7 @@ import { CourseTeaser } from './course-teaser/course-teaser'; import { NavbarUtils } from './navbar/navbar'; import { PageActionsUtils } from './pageactions/pageactions'; import { HideColumns } from './hide-columns/hide-columns'; +import { ExamCorrect } from './exam-correct/exam-correct'; export const Utils = [ Alerts, @@ -31,4 +32,5 @@ export const Utils = [ ...NavbarUtils, ...PageActionsUtils, HideColumns, + ExamCorrect, ]; diff --git a/templates/exam-correct.hamlet b/templates/exam-correct.hamlet index 953a85c7c..42e3f4879 100644 --- a/templates/exam-correct.hamlet +++ b/templates/exam-correct.hamlet @@ -2,7 +2,7 @@ $newline never
-
+