blob: d62827a394b4a622b6d8c684117b10abe62f57ad [file] [log] [blame]
Akronb7aab872021-11-04 17:11:04 +01001/**
2 * Create a state manager object, that can deserialize and
3 * serialize states of associated states.
4 * At the moment this requires an element for serialization,
5 * but it may very well serialize in a cookie.
6 *
7 * @author Nils Diewald
8 */
9
10"use strict";
11
12define(['state'], function(stateClass) {
13
14 return {
15 // Create new state amanger.
16 // Expects an object with a value
17 // to contain the serialization of all states.
18 create : function (element) {
19 return Object.create(this)._init(element);
20 },
21
22
23 // Initialize state manager
24 _init : function (element) {
25 this._e = element;
26 this._states = {};
Akrona8d45c42021-11-23 11:31:31 +010027 this._defaults = {};
Akronb7aab872021-11-04 17:11:04 +010028 this._parse(element.value);
29
30 return this;
31 },
32
33
34 // Parse a value and populate states
35 _parse : function (value) {
Akroned223be2024-12-10 13:01:46 +010036 if (value == null || value == undefined || value == '')
Akronb7aab872021-11-04 17:11:04 +010037 return;
Akroned223be2024-12-10 13:01:46 +010038
Akron25cd9282021-11-22 16:50:27 +010039 this._states = JSON.parse('{' + value + '}');
Akronb7aab872021-11-04 17:11:04 +010040 },
41
Akronaa3dcfe2024-12-10 15:29:40 +010042 // Return element
43 element : function() {
44 return this._e;
45 },
Akronb7aab872021-11-04 17:11:04 +010046
47 // Return the string representation of all states
48 toString : function () {
Akron25cd9282021-11-22 16:50:27 +010049
50 if (this._states.size === 0)
51 return undefined;
52
53 return JSON.stringify(this._states).slice(1,-1);
Akronb7aab872021-11-04 17:11:04 +010054 },
55
56
57 // Update the query component for states
58 _update : function () {
Akroned223be2024-12-10 13:01:46 +010059 this._e.setAttribute("value", this.toString());
Akronb7aab872021-11-04 17:11:04 +010060 },
61
62
63 // Create new state that is automatically associated
64 // with the state manager
Akrona8d45c42021-11-23 11:31:31 +010065 newState : function (name, values, defValue) {
Akronb7aab872021-11-04 17:11:04 +010066
67 const t = this;
68 let s = stateClass.create(values);
69
Akroned223be2024-12-10 13:01:46 +010070 if (this._states[name] != undefined) {
71 if (values.includes(this._states[name]))
72 s.setIfNotYet(this._states[name]);
73 };
74
Akrona8d45c42021-11-23 11:31:31 +010075 // Set default value
76 // TODO: It would be better to make this part
77 // of the state and serialize correctly using TOJSON()
78 if (defValue !== undefined) {
79 s.setIfNotYet(defValue);
80 t._defaults[name] = defValue;
81 };
Akronb7aab872021-11-04 17:11:04 +010082
83 // Associate with dummy object
84 s.associate({
85 setState : function (value) {
Akrona8d45c42021-11-23 11:31:31 +010086 if (t._defaults[name] !== undefined && t._defaults[name] == value) {
87 delete t._states[name];
88 } else {
89 t._states[name] = value;
90 };
Akronb7aab872021-11-04 17:11:04 +010091 t._update();
92 }
93 });
94
Akronda32e7a2021-11-16 17:28:57 +010095 // Load state
96 if (t._states[name] !== undefined) {
97 s.set(t._states[name]);
98 };
99
Akronb7aab872021-11-04 17:11:04 +0100100 return s;
101 }
102 };
103});