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