| Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 1 | /** |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 2 | * Hint menu for Kalamar. |
| Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 3 | * Based on menu object. |
| Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 4 | * |
| 5 | * @author Nils Diewald |
| 6 | */ |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 7 | /* |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 8 | * TODO: Check for cnx/syn= |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 9 | * TODO: List can be shown when prefix is like 'base/s=pcorenlp/' |
| 10 | * TODO: Sometimes the drop-down box down vanish when list is shown |
| 11 | * TODO: Create should expect an input text field |
| 12 | * TODO: Embed only one single menu (not multiple) |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 13 | * By holding the current menu in _active |
| 14 | * TODO: show() should accept a context field (especially for no-context fields, |
| 15 | * like fragments) |
| 16 | * TODO: Improve context analyzer from hint! |
| 17 | * TODO: Marked annotations should be addable to "fragments" |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 18 | */ |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 19 | |
| 20 | "use strict"; |
| 21 | |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 22 | define([ |
| 23 | 'hint/input', |
| 24 | 'hint/menu', |
| 25 | 'hint/contextanalyzer', |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 26 | 'hint/alert', |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 27 | 'util' |
| 28 | ], function (inputClass, |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 29 | menuClass, |
| 30 | analyzerClass, |
| 31 | alertClass) { |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 32 | |
| 33 | // Initialize hint array |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * KorAP.Hint.create({ |
| 37 | * inputField : node, |
| 38 | * context : context regex |
| 39 | * }); |
| 40 | */ |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 41 | return { |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 42 | |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Create new hint helper. |
| 45 | */ |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 46 | create : function (param) { |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 47 | return Object.create(this)._init(param); |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 48 | }, |
| 49 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 50 | |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 51 | // Initialize hint helper |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 52 | _init : function (param) { |
| 53 | param = param || {}; |
| 54 | |
| 55 | // Holds all menus per prefix context |
| Leo Repp | 9b26ba9 | 2021-09-17 17:38:22 +0200 | [diff] [blame] | 56 | this._menuCollection = {}; |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 57 | this._alert = alertClass.create(); |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 58 | |
| 59 | // Active may either hold an alert or a menu |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 60 | this._active = null; |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 61 | |
| Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 62 | // No annotation helper available |
| 63 | if (!KorAP.annotationHelper) { |
| 64 | console.log("No annotationhelper defined"); |
| 65 | return; |
| 66 | }; |
| Marc Kupietz | aa6709c | 2025-12-19 20:03:54 +0100 | [diff] [blame^] | 67 | |
| 68 | // Apply configured foundry filter from data-hint-foundries attribute |
| 69 | if (KorAP.annotationHelper.filterByConfig) { |
| 70 | KorAP.annotationHelper.filterByConfig(); |
| 71 | }; |
| Akron | fac1647 | 2018-07-26 16:47:21 +0200 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * @define {regex} Regular expression for context |
| 75 | */ |
| 76 | KorAP.context = KorAP.context || |
| 77 | "(?:^|[^-_a-zA-Z0-9])" + // Anchor |
| 78 | "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry |
| 79 | "(?:" + |
| 80 | "(?:[-_a-zA-Z0-9]+?)=" + // Layer |
| 81 | "(?:"+ |
| 82 | "(?:[^:=\/ ]+?):|" + // Key |
| 83 | "(?:[^-=\/ ]+?)-" + // Node |
| 84 | ")?" + |
| 85 | ")?" + |
| 86 | ")$"; |
| Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 87 | |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 88 | // Get input field |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 89 | const qfield = param["inputField"] || document.getElementById("q-field"); |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 90 | if (!qfield) |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 91 | return null; |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 92 | |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 93 | // Create input field |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 94 | this._inputField = inputClass.create(qfield); |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 95 | |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 96 | // create alert |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 97 | const that = this; |
| 98 | |
| 99 | const c = this._inputField.container(); |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 100 | c.appendChild(this._alert.element()); |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 101 | c.addEventListener('click', function (e) { |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 102 | if (!this.classList.contains('active')) { |
| 103 | that.show(false); |
| 104 | }; |
| Akron | dadd1d1 | 2021-11-12 16:20:28 +0100 | [diff] [blame] | 105 | e.halt(); |
| Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 106 | }); |
| 107 | |
| Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 108 | // Move infobox |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 109 | const inputFieldElement = this._inputField.element(); |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 110 | inputFieldElement.addEventListener("keyup", this.update.bind(this)); |
| 111 | inputFieldElement.addEventListener("click", this.update.bind(this)); |
| 112 | |
| 113 | // Add event listener for key pressed down |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 114 | let _down = function (e) { |
| 115 | if (_codeFromEvent(e) === 40) { |
| 116 | this.show(false); |
| 117 | e.halt(); |
| 118 | }; |
| 119 | }; |
| 120 | |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 121 | inputFieldElement.addEventListener( |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 122 | "keydown", _down.bind(this), false |
| 123 | ); |
| 124 | |
| 125 | // Add touch events |
| 126 | inputFieldElement.addEventListener( |
| 127 | 'touchstart', |
| 128 | this._touch.bind(this), |
| 129 | false |
| 130 | ); |
| 131 | inputFieldElement.addEventListener( |
| 132 | 'touchend', |
| 133 | this._touch.bind(this), |
| 134 | false |
| 135 | ); |
| 136 | inputFieldElement.addEventListener( |
| 137 | 'touchmove', |
| 138 | this._touch.bind(this), |
| 139 | false |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 140 | ); |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 141 | |
| 142 | // Set Analyzer for context |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 143 | this._analyzer = analyzerClass.create( |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 144 | param["context"] || KorAP.context |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 145 | ); |
| Akron | 1c18f10 | 2024-11-19 16:31:06 +0100 | [diff] [blame] | 146 | |
| 147 | this.update(); |
| Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 148 | return this; |
| 149 | }, |
| 150 | |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Return the input field attached to the hint helper. |
| 154 | */ |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 155 | inputField : function () { |
| 156 | return this._inputField; |
| 157 | }, |
| Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 158 | |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 159 | |
| 160 | /** |
| Akron | b759ee9 | 2024-11-19 18:02:56 +0100 | [diff] [blame] | 161 | * Return selection range of the input field. |
| 162 | */ |
| 163 | selectionRange : function () { |
| 164 | return this._inputField.selectionRange(); |
| 165 | }, |
| 166 | |
| 167 | |
| 168 | /** |
| Akron | dadd1d1 | 2021-11-12 16:20:28 +0100 | [diff] [blame] | 169 | * Alert at a specific character position. |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 170 | */ |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 171 | alert : function (charPos, msg) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 172 | const t = this; |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 173 | if (arguments.length === 0) |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 174 | return t._alert; |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 175 | |
| 176 | // Do not alert if already alerted! |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 177 | if (t._alert.active) |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 178 | return false; |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 179 | |
| 180 | // Move to the correct position |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 181 | t._inputField.moveto(charPos); |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 182 | |
| 183 | // Set container to active (aka hide the hint helper button) |
| 184 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 185 | t._alert.show(msg); |
| 186 | t.active(t._alert); |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 187 | return true; |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 188 | }, |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 189 | |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 190 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 191 | /** |
| 192 | * Update input field. |
| 193 | */ |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 194 | update : function () { |
| 195 | this._inputField.update(); |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 196 | if (this._alert.hide()) |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 197 | this.active(null); |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 198 | }, |
| 199 | |
| 200 | |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 201 | /** |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 202 | * Return hint menu and probably init based on an action |
| 203 | */ |
| 204 | menu : function (action) { |
| Akron | dadd1d1 | 2021-11-12 16:20:28 +0100 | [diff] [blame] | 205 | |
| Leo Repp | 9b26ba9 | 2021-09-17 17:38:22 +0200 | [diff] [blame] | 206 | if (this._menuCollection[action] === undefined) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 207 | |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 208 | // No matching hint menu |
| 209 | if (KorAP.annotationHelper[action] === undefined) |
| 210 | return; |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 211 | |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 212 | // Create matching hint menu |
| Leo Repp | 9b26ba9 | 2021-09-17 17:38:22 +0200 | [diff] [blame] | 213 | this._menuCollection[action] = menuClass.create( |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 214 | this, action, KorAP.annotationHelper[action] |
| 215 | ); |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | // Return matching hint menu |
| Leo Repp | 9b26ba9 | 2021-09-17 17:38:22 +0200 | [diff] [blame] | 219 | return this._menuCollection[action]; |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 220 | }, |
| 221 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 222 | |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 223 | /** |
| 224 | * Get the correct menu based on the context |
| 225 | */ |
| 226 | contextMenu : function (ifContext) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 227 | const noC = ifContext ? undefined : this.menu("-"); |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 228 | |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 229 | // Get context (aka left text) |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 230 | let context = this._inputField.context(); |
| Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 231 | |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 232 | if (context === undefined || context.length === 0) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 233 | return noC; |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 234 | }; |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 235 | |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 236 | // Get context (aka left text matching regex) |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 237 | context = this._analyzer.test(context); |
| Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 238 | |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 239 | if (context === undefined || context.length == 0) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 240 | return noC; |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 241 | }; |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 242 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 243 | return this.menu(context) || noC; |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 244 | }, |
| 245 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 246 | |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 247 | /** |
| 248 | * Activate a certain menu. |
| 249 | * If a menu is passed, the menu will be activated. |
| 250 | * If null is passed, the active menu will be deactivated. |
| 251 | * If nothing is passed, returns the active menu. |
| 252 | */ |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 253 | active : function (obj) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 254 | |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 255 | // A menu or null was passed |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 256 | if (arguments.length === 1) { |
| 257 | const c = this._inputField.container(); |
| Akron | dadd1d1 | 2021-11-12 16:20:28 +0100 | [diff] [blame] | 258 | |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 259 | // Make the menu active |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 260 | if (obj !== null) { |
| 261 | c.classList.add('active'); |
| 262 | this._active = obj; |
| 263 | } |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 264 | |
| 265 | // Make the menu inactive |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 266 | else { |
| 267 | c.classList.remove('active'); |
| 268 | this._active = null; |
| 269 | } |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 270 | }; |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 271 | |
| 272 | // Return |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 273 | return this._active; |
| 274 | }, |
| 275 | |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 276 | |
| 277 | /** |
| Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 278 | * Show the menu. |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 279 | * Remove all old menus. |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 280 | * |
| 281 | * @param {boolean} Boolean value to indicate if context |
| 282 | * is necessary (true) or if the main context should |
| 283 | * be shown if context fails. |
| 284 | * |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 285 | */ |
| 286 | show : function (ifContext) { |
| 287 | |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 288 | // Remove the active object |
| 289 | this._unshow(); |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 290 | |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 291 | // Get the menu |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 292 | let menu; |
| Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 293 | if (menu = this.contextMenu(ifContext)) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 294 | |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 295 | this.active(menu); |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 296 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 297 | let e = menu.element(); |
| Akron | dadd1d1 | 2021-11-12 16:20:28 +0100 | [diff] [blame] | 298 | // Chrome may send a blur on the old menu here |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 299 | this._active.element().blur(); |
| 300 | this._inputField.container().appendChild(e); |
| Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 301 | |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 302 | menu.show(); |
| 303 | menu.focus(); |
| 304 | // Focus on input field |
| 305 | // this.inputField.element.focus(); |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 306 | } |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 307 | |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 308 | else { |
| 309 | this._inputField.element().focus(); |
| Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 310 | }; |
| Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 311 | }, |
| Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 312 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 313 | |
| Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 314 | // This will get the context of the field |
| 315 | getContext : function () {}, |
| 316 | |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * Deactivate the current menu and focus on the input field. |
| 320 | */ |
| 321 | unshow : function () { |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 322 | this._unshow(); |
| 323 | this._inputField.element().focus(); |
| 324 | }, |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 325 | |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 326 | |
| 327 | // Catch touch events |
| 328 | _touch : function (e) { |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 329 | if (e.type === 'touchstart') { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 330 | this._lastTouch = e.touches[0].clientY; |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 331 | } |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 332 | |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 333 | else if (e.type === 'touchend') { |
| 334 | this._lastTouch = undefined; |
| 335 | } |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 336 | |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 337 | else if (e.type == 'touchmove') { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 338 | if ((this._lastTouch + 10) < e.touches[0].clientY) { |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 339 | this.show(); |
| 340 | this._lastTouch = undefined; |
| 341 | }; |
| 342 | e.halt(); |
| 343 | } |
| 344 | }, |
| 345 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 346 | |
| 347 | // Unshow the hint menu |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 348 | _unshow : function () { |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 349 | if (this.active() !== null) { |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 350 | |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 351 | // This does not work for alert currently! |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 352 | if (!this._alert.active) { |
| Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 353 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 354 | // TODO: This does not work for webkit? |
| 355 | this._inputField |
| 356 | .container() |
| 357 | .removeChild(this._active.element()); |
| Akron | c14cbfc | 2018-08-31 13:15:55 +0200 | [diff] [blame] | 358 | } |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 359 | |
| Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame] | 360 | else { |
| 361 | this._unshowAlert(); |
| 362 | }; |
| 363 | |
| Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 364 | this.active(null); |
| 365 | }; |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 366 | }, |
| 367 | |
| 368 | // Unshow alert |
| 369 | _unshowAlert : function () { |
| 370 | this._alert.hide(); |
| 371 | this.active(null); |
| Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 372 | } |
| 373 | }; |
| Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 374 | }); |