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