Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Create a state object, that can have a single value |
| 3 | * (mostly boolean) and multiple objects associated to it. |
| 4 | * Whenever the state changes, all objects are informed |
| 5 | * by their setState() method of the value change. |
| 6 | * |
| 7 | * @author Nils Diewald |
| 8 | */ |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 9 | /* |
| 10 | * TODO: |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 11 | * Require names for states, that should be quite short, so they |
| 12 | * can easily be serialized and kept between page turns (via cookie |
| 13 | * and/or query param) |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 14 | */ |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 15 | "use strict"; |
| 16 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 17 | define(function () { |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 18 | return { |
| 19 | |
| 20 | /** |
| 21 | * Constructor |
| 22 | */ |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 23 | create : function (values) { |
| 24 | return Object.create(this)._init(values); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 25 | }, |
| 26 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 27 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 28 | // Initialize |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 29 | _init : function (values) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 30 | const t = this; |
| 31 | t._assoc = []; |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 32 | if (values == undefined) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 33 | t.values = [false,true]; |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 34 | } |
| 35 | else if (Array.isArray(values)) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 36 | t.values = values; |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 37 | } |
| 38 | else { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 39 | t.values = [values]; |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 40 | } |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 41 | return t; |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 42 | }, |
| 43 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 44 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 45 | /** |
| 46 | * Associate the state with some objects. |
| 47 | */ |
| 48 | associate : function (obj) { |
| 49 | |
| 50 | // Check if the object has a setState() method |
| 51 | if (obj.hasOwnProperty("setState")) { |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 52 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 53 | this._assoc.push(obj); |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 54 | if (this.value != undefined) { |
| 55 | obj.setState(this.value); |
| 56 | }; |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 57 | } else { |
| 58 | console.log("Object " + obj + " has no setState() method"); |
| 59 | } |
| 60 | }, |
| 61 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 62 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 63 | /** |
| 64 | * Set the state to a certain value. |
| 65 | * This will set the state to all associated objects as well. |
| 66 | */ |
| 67 | set : function (value) { |
| 68 | if (value != this.value) { |
| 69 | this.value = value; |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 70 | this._assoc.forEach(i => i.setState(value)); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 71 | }; |
| 72 | }, |
| 73 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 74 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 75 | /** |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 76 | * Set the state to a default value. |
| 77 | * This will only be set, if no other value is set yet. |
| 78 | */ |
| 79 | setIfNotYet : function (value) { |
| 80 | if (this.value == undefined) { |
| 81 | this.set(value); |
| 82 | }; |
| 83 | }, |
| 84 | |
| 85 | |
| 86 | /** |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 87 | * Get the state value |
| 88 | */ |
| 89 | get : function () { |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 90 | if (this.value == undefined) { |
| 91 | this.value = this.values[0]; |
| 92 | }; |
| 93 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 94 | return this.value; |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 95 | }, |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * Get the number of associated objects |
| 100 | */ |
| 101 | associates : function () { |
| 102 | return this._assoc.length; |
Akron | 38ed5dc | 2020-10-01 17:33:00 +0200 | [diff] [blame] | 103 | }, |
| 104 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 105 | |
Akron | 38ed5dc | 2020-10-01 17:33:00 +0200 | [diff] [blame] | 106 | /** |
| 107 | * Clear all associated objects |
| 108 | */ |
| 109 | clear : function () { |
| 110 | return this._assoc = []; |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 111 | }, |
| 112 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 113 | |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 114 | /** |
| 115 | * Roll to the next value. |
| 116 | * This may be used for toggling. |
| 117 | */ |
| 118 | roll : function () { |
| 119 | let next = 0; |
| 120 | for (let i = 0; i < this.values.length - 1; i++) { |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 121 | if (this.get() == this.values[i]) { |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 122 | next = i+1; |
| 123 | break; |
| 124 | }; |
| 125 | }; |
| 126 | this.set(this.values[next]); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | }); |