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