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")) { |
| 52 | this._assoc.push(obj); |
| 53 | obj.setState(this.value); |
| 54 | } else { |
| 55 | console.log("Object " + obj + " has no setState() method"); |
| 56 | } |
| 57 | }, |
| 58 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 59 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 60 | /** |
| 61 | * Set the state to a certain value. |
| 62 | * This will set the state to all associated objects as well. |
| 63 | */ |
| 64 | set : function (value) { |
| 65 | if (value != this.value) { |
| 66 | this.value = value; |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 67 | this._assoc.forEach(i => i.setState(value)); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 68 | }; |
| 69 | }, |
| 70 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 71 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 72 | /** |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 73 | * Set the state to a default value. |
| 74 | * This will only be set, if no other value is set yet. |
| 75 | */ |
| 76 | setIfNotYet : function (value) { |
| 77 | if (this.value == undefined) { |
| 78 | this.set(value); |
| 79 | }; |
| 80 | }, |
| 81 | |
| 82 | |
| 83 | /** |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 84 | * Get the state value |
| 85 | */ |
| 86 | get : function () { |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 87 | if (this.value == undefined) { |
| 88 | this.value = this.values[0]; |
| 89 | }; |
| 90 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 91 | return this.value; |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 92 | }, |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Get the number of associated objects |
| 97 | */ |
| 98 | associates : function () { |
| 99 | return this._assoc.length; |
Akron | 38ed5dc | 2020-10-01 17:33:00 +0200 | [diff] [blame] | 100 | }, |
| 101 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 102 | |
Akron | 38ed5dc | 2020-10-01 17:33:00 +0200 | [diff] [blame] | 103 | /** |
| 104 | * Clear all associated objects |
| 105 | */ |
| 106 | clear : function () { |
| 107 | return this._assoc = []; |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 108 | }, |
| 109 | |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 110 | |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 111 | /** |
| 112 | * Roll to the next value. |
| 113 | * This may be used for toggling. |
| 114 | */ |
| 115 | roll : function () { |
| 116 | let next = 0; |
| 117 | for (let i = 0; i < this.values.length - 1; i++) { |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 118 | if (this.get() == this.values[i]) { |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 119 | next = i+1; |
| 120 | break; |
| 121 | }; |
| 122 | }; |
| 123 | this.set(this.values[next]); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | }); |