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