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 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 39 | * Get or set the activity status of the prefix. |
| 40 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 41 | active : function (bool) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 42 | const cl = this.element().classList; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 43 | if (bool === undefined) |
| 44 | return cl.contains("active"); |
| 45 | else if (bool) |
| 46 | cl.add("active"); |
| 47 | else |
| 48 | cl.remove("active"); |
| 49 | }, |
| 50 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 51 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 52 | /** |
| 53 | * Check, if a prefix is given or not. |
| 54 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 55 | isSet : function () { |
| 56 | return this._string.length > 0 ? |
| 57 | true : false; |
| 58 | }, |
| 59 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 60 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 61 | /** |
| 62 | * Get or set the prefix string. |
| 63 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 64 | value : function (string) { |
| 65 | if (arguments.length === 1) { |
| 66 | this._string = string; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 67 | return this._update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 68 | }; |
| 69 | return this._string; |
| 70 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | /** |
| 74 | * Add string to prefix. |
| 75 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 76 | add : function (string) { |
| 77 | this._string += string; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 78 | return this._update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 79 | }, |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 80 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Clear prefix |
| 84 | */ |
| 85 | clear : function () { |
| 86 | this._string = ''; |
| 87 | return this._update(); |
| 88 | }, |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Action method. |
| 93 | * Expected to be overridden. |
| 94 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 95 | onclick : function () {}, |
| 96 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 97 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Remove the last character of the string |
| 100 | */ |
| 101 | chop : function () { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 102 | const t = this; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 103 | |
| 104 | // Prefix is long enough for backspace |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 105 | if (t._string.length > 1) { |
| 106 | t._string = t._string.substring( |
| 107 | 0, t._string.length - 1 |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 108 | ); |
| 109 | } |
| 110 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 111 | t._string = ''; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 114 | return t._update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 115 | }, |
| 116 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Get the associated dom element. |
| 120 | */ |
| 121 | element : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 122 | return this._el; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 123 | }, |
| 124 | |
| 125 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 126 | /** |
| 127 | * Return menu list. |
| 128 | */ |
| 129 | menu : function () { |
| 130 | return this._menu; |
| 131 | } |
| 132 | }); |