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