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 | /* |
| 8 | * TODO: List can be shown when prefix is like 'base/s=pcorenlp/' |
| 9 | * TODO: Sometimes the drop-down box down vanish when list is shown |
| 10 | * TODO: Create should expect an input text field |
| 11 | * TODO: Embed only one single menu (not multiple) |
| 12 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 13 | define([ |
| 14 | 'hint/input', |
| 15 | 'hint/menu', |
| 16 | 'hint/contextanalyzer', |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 17 | 'hint/alert', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | 'util' |
| 19 | ], function (inputClass, |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 20 | menuClass, |
| 21 | analyzerClass, |
| 22 | alertClass) { |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 23 | "use strict"; |
| 24 | |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 25 | /** |
| 26 | * @define {regex} Regular expression for context |
| 27 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | KorAP.context = KorAP.context || |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 29 | "(?:^|[^-_a-zA-Z0-9])" + // Anchor |
| 30 | "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry |
| 31 | "(?:" + |
| 32 | "(?:[-_a-zA-Z0-9]+?)=" + // Layer |
Akron | 113cc1a | 2016-01-22 21:17:57 +0100 | [diff] [blame] | 33 | "(?:"+ |
| 34 | "(?:[^:=\/ ]+?):|" + // Key |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 35 | "(?:[^-=\/ ]+?)-" + // Node |
Akron | 113cc1a | 2016-01-22 21:17:57 +0100 | [diff] [blame] | 36 | ")?" + |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 37 | ")?" + |
| 38 | ")$"; |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 39 | KorAP.hintArray = KorAP.hintArray || {}; |
| 40 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Return keycode based on event |
| 43 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 44 | |
| 45 | // Initialize hint array |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * KorAP.Hint.create({ |
| 49 | * inputField : node, |
| 50 | * context : context regex |
| 51 | * }); |
| 52 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 53 | return { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 54 | |
| 55 | // Some variables |
| 56 | // _firstTry : true, |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 57 | // active : false, |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 58 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Create new hint helper. |
| 61 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 62 | create : function (param) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 63 | return Object.create(this)._init(param); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 64 | }, |
| 65 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 66 | // Initialize hint helper |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 67 | _init : function (param) { |
| 68 | param = param || {}; |
| 69 | |
| 70 | // Holds all menus per prefix context |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 71 | this._menu = {}; |
| 72 | this._alert = alertClass.create(); |
| 73 | this._active = false; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 74 | |
| 75 | // Get input field |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 76 | var qfield = param["inputField"] || document.getElementById("q-field"); |
| 77 | if (!qfield) |
| 78 | return null; |
| 79 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 80 | // Create input field |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 81 | this._inputField = inputClass.create(qfield); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 82 | |
| 83 | var inputFieldElement = this._inputField.element(); |
| 84 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 85 | var c = this._inputField.container(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 86 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 87 | // create alert |
| 88 | c.appendChild(this._alert.element()); |
| 89 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 90 | var that = this; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 91 | |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 92 | this._inputField.container().addEventListener('click', function (e) { |
| 93 | if (!this.classList.contains('active')) { |
| 94 | that.show(false); |
| 95 | }; |
| 96 | }); |
| 97 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 98 | var _down = function (e) { |
| 99 | var code = _codeFromEvent(e); |
| 100 | if (code === 40) { |
| 101 | this.show(false); |
| 102 | e.halt(); |
| 103 | }; |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | // Move infobox |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 107 | inputFieldElement.addEventListener("keyup", this.update.bind(this)); |
| 108 | inputFieldElement.addEventListener("click", this.update.bind(this)); |
| 109 | |
| 110 | // Add event listener for key pressed down |
| 111 | inputFieldElement.addEventListener( |
| 112 | "keydown", _down.bind(this), false |
| 113 | ); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 114 | |
| 115 | // Set Analyzer for context |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 116 | this._analyzer = analyzerClass.create( |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 117 | param["context"] || KorAP.context |
| 118 | ); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 119 | return this; |
| 120 | }, |
| 121 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Return the input field attached to the hint helper. |
| 125 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 126 | inputField : function () { |
| 127 | return this._inputField; |
| 128 | }, |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 129 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Altert at a specific character position. |
| 133 | */ |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 134 | alert : function (charPos, msg) { |
| 135 | |
| 136 | if (arguments.length === 0) |
| 137 | return this._alert; |
| 138 | |
| 139 | // Do not alert if already alerted! |
| 140 | if (this._alert.active) |
| 141 | return false; |
| 142 | |
| 143 | // Move to the correct position |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 144 | this._inputField.moveto(charPos); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 145 | |
| 146 | // Set container to active (aka hide the hint helper button) |
| 147 | |
| 148 | this._alert.show(msg); |
| 149 | this.active(true); |
| 150 | return true; |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 151 | }, |
| 152 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 153 | _unshowAlert : function () { |
| 154 | this._alert.unshow(); |
| 155 | this.active(false); |
| 156 | }, |
| 157 | |
| 158 | update : function () { |
| 159 | this._inputField.update(); |
| 160 | if (this._alert.unshow()) |
| 161 | this.active(false); |
| 162 | }, |
| 163 | |
| 164 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 165 | /** |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 166 | * Return hint menu and probably init based on an action |
| 167 | */ |
| 168 | menu : function (action) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 169 | if (this._menu[action] === undefined) { |
| 170 | |
| 171 | // No matching hint menu |
| 172 | if (KorAP.hintArray[action] === undefined) |
| 173 | return; |
| 174 | |
| 175 | // Create matching hint menu |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 176 | this._menu[action] = menuClass.create( |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 177 | this, action, KorAP.hintArray[action] |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 178 | ); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // Return matching hint menu |
| 182 | return this._menu[action]; |
| 183 | }, |
| 184 | |
| 185 | /** |
| 186 | * Get the correct menu based on the context |
| 187 | */ |
| 188 | contextMenu : function (ifContext) { |
| 189 | var context = this._inputField.context(); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 190 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 191 | if (context === undefined || context.length == 0) |
| 192 | return ifContext ? undefined : this.menu("-"); |
| 193 | |
| 194 | context = this._analyzer.test(context); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 195 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 196 | if (context === undefined || context.length == 0) |
| 197 | return ifContext ? undefined : this.menu("-"); |
| 198 | |
| 199 | return this.menu(context); |
| 200 | }, |
| 201 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 202 | active : function (bool) { |
| 203 | if (arguments.length === 1) { |
| 204 | var c = this._inputField.container(); |
| 205 | if (bool && !this._active) { |
| 206 | c.classList.add('active'); |
| 207 | this._active = true; |
| 208 | } |
| 209 | else { |
| 210 | c.classList.remove('active'); |
| 211 | this._active = false; |
| 212 | } |
| 213 | }; |
| 214 | return this._active; |
| 215 | }, |
| 216 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 217 | |
| 218 | /** |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 219 | * Show the menu. |
| 220 | * Currently this means that multiple menus may be loaded |
| 221 | * but not shown. |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 222 | */ |
| 223 | show : function (ifContext) { |
| 224 | |
| 225 | // Menu is already active |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 226 | if (this.active()) { |
| 227 | |
| 228 | // Alert is not active |
| 229 | if (!this._alert.unshow()) |
| 230 | return; |
| 231 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 232 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 233 | // Get the menu |
| 234 | var menu; |
| 235 | if (menu = this.contextMenu(ifContext)) { |
Nils Diewald | 2488d05 | 2015-04-09 21:46:02 +0000 | [diff] [blame] | 236 | var c = this._inputField.container(); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 237 | this.active(true); |
| 238 | // c.classList.add('active'); |
Nils Diewald | 2488d05 | 2015-04-09 21:46:02 +0000 | [diff] [blame] | 239 | c.appendChild(menu.element()); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 240 | menu.show(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 241 | menu.focus(); |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 242 | // Focus on input field |
| 243 | // this.inputField.element.focus(); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 244 | }; |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 245 | }, |
| 246 | |
| 247 | unshow : function () { |
| 248 | this.active(false); |
| 249 | this.inputField().element().focus(); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 250 | } |
| 251 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 252 | }); |