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