Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Add string values to the virtual collection |
| 3 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 4 | define(['util'], { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 5 | |
| 6 | /** |
| 7 | * Create new string value helper. |
| 8 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 9 | create : function () { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 10 | var regexOp = true; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 11 | var regex = false; |
| 12 | var value = ''; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 13 | |
| 14 | // Set value |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 15 | if (arguments.length >= 1) { |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame] | 16 | if (arguments[0] !== undefined) |
| 17 | value = arguments[0]; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 18 | }; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 19 | |
| 20 | // Set regex |
| 21 | if (arguments.length >= 2) { |
| 22 | if (arguments[1] !== undefined) |
| 23 | regex = arguments[1]; |
| 24 | }; |
| 25 | |
| 26 | // Set regexOp |
| 27 | if (arguments.length >= 3) { |
| 28 | regexOp = arguments[2]; |
| 29 | if (regexOp === false) { |
| 30 | regex = false; |
| 31 | } |
| 32 | }; |
| 33 | return Object.create(this)._init(value, regex, regexOp); |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 34 | }, |
| 35 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 36 | |
| 37 | // Initialize the string value |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 38 | _init : function (value, regex, regexOp) { |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 39 | this.value(value); |
| 40 | this.regex(regex); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 41 | this._regexOp(regexOp); |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 42 | return this; |
| 43 | }, |
| 44 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Get or set the regex boolean value. |
| 48 | * |
| 49 | * @param bool Either true or false |
| 50 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 51 | regex : function (bool) { |
| 52 | if (arguments.length === 1) { |
| 53 | if (bool) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 54 | this._regex = true; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 55 | } |
| 56 | else { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 57 | this._regex = false; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 58 | }; |
| 59 | this._update(); |
| 60 | }; |
| 61 | return this._regex; |
| 62 | }, |
| 63 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 64 | _regexOp : function (regexOp) { |
| 65 | if (arguments.length === 1) { |
| 66 | if (regexOp) { |
| 67 | this.__regexOp = true; |
| 68 | } |
| 69 | else { |
| 70 | this.__regexOp = false; |
| 71 | }; |
| 72 | this._update(); |
| 73 | }; |
| 74 | return this.__regexOp; |
| 75 | }, |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Toggle the regex, make it either true, |
| 79 | * if it is false, or make it false, if it is true. |
| 80 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 81 | toggleRegex : function () { |
| 82 | if (this._regex === false) |
| 83 | this.regex(true); |
| 84 | else |
| 85 | this.regex(false); |
| 86 | }, |
| 87 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Get or set the string value. |
| 90 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 91 | value : function (val) { |
| 92 | if (arguments.length === 1) { |
| 93 | this._value = val; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 94 | // this._input.value = val; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 95 | this._update(); |
| 96 | }; |
| 97 | return this._value; |
| 98 | }, |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 99 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 100 | // Update dom element |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 101 | _update : function () { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 102 | if (this._element === undefined) |
| 103 | return; |
| 104 | |
Nils Diewald | 7991a3f | 2015-05-19 14:12:37 +0000 | [diff] [blame] | 105 | this._value = this._input.value; |
| 106 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 107 | if (this._regexOp() && this._regex) { |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 108 | this._element.classList.add('regex'); |
| 109 | } |
| 110 | else { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 111 | this._element.classList.remove('regex'); |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 112 | }; |
| 113 | }, |
| 114 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Store the string value. |
| 118 | * This method should be overwritten. |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 119 | * The method receives the value and the regex. |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 120 | */ |
| 121 | store : function (v,r) {}, |
| 122 | |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 123 | /** |
| 124 | * Put focus on element |
| 125 | */ |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 126 | focus : function () { |
| 127 | this._element.children[0].focus(); |
| 128 | }, |
| 129 | |
| 130 | /** |
| 131 | * Get the associated dom element. |
| 132 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 133 | element : function () { |
| 134 | if (this._element !== undefined) |
| 135 | return this._element; |
| 136 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 137 | // Create element |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 138 | this._element = document.createElement('div'); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 139 | var e = this._element; |
| 140 | e.setAttribute('tabindex', 0); |
| 141 | e.style.outline = 0; |
| 142 | |
| 143 | var cl = e.classList; |
| 144 | cl.add('value'); |
| 145 | if (this.regex() === true) |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 146 | cl.add('regex'); |
| 147 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 148 | // Add input field |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 149 | this._input = e.addE('input'); |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame] | 150 | |
| 151 | if (this.value() !== undefined) { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 152 | this._input.value = this.value(); |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame] | 153 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 154 | |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 155 | // Add regex button |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 156 | if (this._regexOp()) { |
| 157 | var re = e.addE('div'); |
| 158 | re.addEventListener( |
| 159 | 'click', |
| 160 | function (ev) { |
| 161 | this.toggleRegex(); |
| 162 | // ev.halt(); |
| 163 | }.bind(this), |
| 164 | true |
| 165 | ); |
| 166 | re.addT('RE'); |
| 167 | }; |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 168 | |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 169 | // If the focus is not on the text field anymore, |
| 170 | // delegate focus to |
| 171 | this._input.addEventListener( |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 172 | 'blur', |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame] | 173 | function (ev) { |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 174 | if (!this._inField) { |
| 175 | this.value(this._input.value); |
| 176 | this.store(this.value(), this.regex()); |
| 177 | }; |
| 178 | ev.halt(); |
| 179 | }.bind(this) |
| 180 | ); |
| 181 | |
| 182 | // Workaround to check the click is in the field |
| 183 | e.addEventListener( |
| 184 | 'mousedown', |
| 185 | function (ev) { |
| 186 | this._inField = true; |
| 187 | }.bind(this) |
| 188 | ); |
| 189 | |
| 190 | e.addEventListener( |
| 191 | 'mouseup', |
| 192 | function (ev) { |
| 193 | this._inField = false; |
| 194 | this._input.focus(); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 195 | }.bind(this) |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 196 | ); |
| 197 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 198 | this._input.addEventListener( |
| 199 | 'keypress', |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 200 | function (ev) { |
| 201 | if (ev.keyCode == 13) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 202 | this.value(this._input.value); |
| 203 | this.store(this.value(), this.regex()); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 204 | return false; |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 205 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 206 | }.bind(this) |
| 207 | ); |
| 208 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 209 | // Add store button |
| 210 | /* |
| 211 | e.appendChild( |
| 212 | document.createElement('div') |
| 213 | ).addEventListener('click', function () { |
| 214 | this.store(this.value(), this.regex()); |
| 215 | }.bind(this)); |
| 216 | */ |
| 217 | return e; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 218 | } |
| 219 | }); |