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