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