Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 3 | define({ |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 4 | |
| 5 | /** |
| 6 | * Create new prefix object. |
| 7 | */ |
| 8 | create : function () { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 9 | return Object.create(this)._init(); |
| 10 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 11 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 12 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 13 | // Initialize prefix object |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 14 | _init : function () { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 15 | const t = this; |
| 16 | |
| 17 | t._string = ''; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | |
| 19 | // Add prefix span |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 20 | t._el = document.createElement('span'); |
Akron | f21bf74 | 2021-05-19 13:30:38 +0200 | [diff] [blame] | 21 | t._el.classList.add('non-item'); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 22 | t._el.classList.add('pref'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 23 | // Connect action |
| 24 | |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 25 | if (t["onclick"] !== undefined) { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 26 | t._el["onclick"] = t.onclick.bind(t); |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 27 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 28 | return t; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 29 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 30 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 31 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 32 | _update : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 33 | return this._el.innerHTML |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 34 | = this._string; |
| 35 | }, |
| 36 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 37 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Upgrade this object to another object, |
| 40 | * while private data stays intact. |
| 41 | * |
| 42 | * @param {Object} An object with properties. |
| 43 | */ |
| 44 | upgradeTo : function (props) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 45 | for (let prop in props) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 46 | this[prop] = props[prop]; |
| 47 | }; |
| 48 | return this; |
| 49 | }, |
| 50 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Get or set the activity status of the prefix. |
| 54 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 55 | active : function (bool) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 56 | const cl = this.element().classList; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 57 | if (bool === undefined) |
| 58 | return cl.contains("active"); |
| 59 | else if (bool) |
| 60 | cl.add("active"); |
| 61 | else |
| 62 | cl.remove("active"); |
| 63 | }, |
| 64 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 65 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Check, if a prefix is given or not. |
| 68 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 69 | isSet : function () { |
| 70 | return this._string.length > 0 ? |
| 71 | true : false; |
| 72 | }, |
| 73 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 74 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Get or set the prefix string. |
| 77 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 78 | value : function (string) { |
| 79 | if (arguments.length === 1) { |
| 80 | this._string = string; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 81 | return this._update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 82 | }; |
| 83 | return this._string; |
| 84 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | /** |
| 88 | * Add string to prefix. |
| 89 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 90 | add : function (string) { |
| 91 | this._string += string; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 92 | return this._update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 93 | }, |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 94 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Clear prefix |
| 98 | */ |
| 99 | clear : function () { |
| 100 | this._string = ''; |
| 101 | return this._update(); |
| 102 | }, |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Action method. |
| 107 | * Expected to be overridden. |
| 108 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 109 | onclick : function () {}, |
| 110 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 111 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 112 | /** |
| 113 | * Remove the last character of the string |
| 114 | */ |
| 115 | chop : function () { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 116 | const t = this; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 117 | |
| 118 | // Prefix is long enough for backspace |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 119 | if (t._string.length > 1) { |
| 120 | t._string = t._string.substring( |
| 121 | 0, t._string.length - 1 |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 122 | ); |
| 123 | } |
| 124 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 125 | t._string = ''; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 128 | return t._update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 129 | }, |
| 130 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Get the associated dom element. |
| 134 | */ |
| 135 | element : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 136 | return this._el; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 137 | }, |
| 138 | |
| 139 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 140 | /** |
| 141 | * Return menu list. |
| 142 | */ |
| 143 | menu : function () { |
| 144 | return this._menu; |
| 145 | } |
| 146 | }); |