Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 1 | /** |
| 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 | |
| 12 | define(['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 = {}; |
Akron | a8d45c4 | 2021-11-23 11:31:31 +0100 | [diff] [blame] | 27 | this._defaults = {}; |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 28 | this._parse(element.value); |
| 29 | |
| 30 | return this; |
| 31 | }, |
| 32 | |
| 33 | |
| 34 | // Parse a value and populate states |
| 35 | _parse : function (value) { |
Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame^] | 36 | if (value == null || value == undefined || value == '') |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 37 | return; |
Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame^] | 38 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 39 | this._states = JSON.parse('{' + value + '}'); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 40 | }, |
| 41 | |
| 42 | |
| 43 | // Return the string representation of all states |
| 44 | toString : function () { |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 45 | |
| 46 | if (this._states.size === 0) |
| 47 | return undefined; |
| 48 | |
| 49 | return JSON.stringify(this._states).slice(1,-1); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 50 | }, |
| 51 | |
| 52 | |
| 53 | // Update the query component for states |
| 54 | _update : function () { |
Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame^] | 55 | this._e.setAttribute("value", this.toString()); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 56 | }, |
| 57 | |
| 58 | |
| 59 | // Create new state that is automatically associated |
| 60 | // with the state manager |
Akron | a8d45c4 | 2021-11-23 11:31:31 +0100 | [diff] [blame] | 61 | newState : function (name, values, defValue) { |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 62 | |
| 63 | const t = this; |
| 64 | let s = stateClass.create(values); |
| 65 | |
Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame^] | 66 | if (this._states[name] != undefined) { |
| 67 | if (values.includes(this._states[name])) |
| 68 | s.setIfNotYet(this._states[name]); |
| 69 | }; |
| 70 | |
Akron | a8d45c4 | 2021-11-23 11:31:31 +0100 | [diff] [blame] | 71 | // Set default value |
| 72 | // TODO: It would be better to make this part |
| 73 | // of the state and serialize correctly using TOJSON() |
| 74 | if (defValue !== undefined) { |
| 75 | s.setIfNotYet(defValue); |
| 76 | t._defaults[name] = defValue; |
| 77 | }; |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 78 | |
| 79 | // Associate with dummy object |
| 80 | s.associate({ |
| 81 | setState : function (value) { |
Akron | a8d45c4 | 2021-11-23 11:31:31 +0100 | [diff] [blame] | 82 | if (t._defaults[name] !== undefined && t._defaults[name] == value) { |
| 83 | delete t._states[name]; |
| 84 | } else { |
| 85 | t._states[name] = value; |
| 86 | }; |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 87 | t._update(); |
| 88 | } |
| 89 | }); |
| 90 | |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 91 | // Load state |
| 92 | if (t._states[name] !== undefined) { |
| 93 | s.set(t._states[name]); |
| 94 | }; |
| 95 | |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 96 | return s; |
| 97 | } |
| 98 | }; |
| 99 | }); |