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