feat(exam-correct): setup basic session storage manager, add util stub

This commit is contained in:
Sarah Vaupel 2020-01-09 11:08:43 +01:00 committed by Gregor Kleen
parent 87bda1607e
commit 9cb64f2a8f
4 changed files with 85 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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 <table> element!');
}
}
destroy() {
console.log('TBD: destroy ExamCorrect');
}
}

View File

@ -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,
];

View File

@ -2,7 +2,7 @@ $newline never
<section>
<div uw-hide-columns="exam-correct">
<table uw-exam-correct .table .table--striped>
<table .table .table--striped uw-exam-correct=#{intercalate "-" [toPathPiece tid, toPathPiece ssh, toPathPiece csh, toPathPiece examn]}>
<thead>
<tr .table__row .table__row--head>
<th .table__th .sortable .sorted-desc uw-hide-column-header="date">