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 | |
| 90 | |
| 91 | var that = this; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 92 | |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 93 | this._inputField.container().addEventListener('click', function (e) { |
| 94 | if (!this.classList.contains('active')) { |
| 95 | that.show(false); |
| 96 | }; |
| 97 | }); |
| 98 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 99 | var _down = function (e) { |
| 100 | var code = _codeFromEvent(e); |
| 101 | if (code === 40) { |
| 102 | this.show(false); |
| 103 | e.halt(); |
| 104 | }; |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // Move infobox |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 108 | inputFieldElement.addEventListener("keyup", this.update.bind(this)); |
| 109 | inputFieldElement.addEventListener("click", this.update.bind(this)); |
| 110 | |
| 111 | // Add event listener for key pressed down |
| 112 | inputFieldElement.addEventListener( |
| 113 | "keydown", _down.bind(this), false |
| 114 | ); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 115 | |
| 116 | // Set Analyzer for context |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 117 | this._analyzer = analyzerClass.create( |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 118 | param["context"] || KorAP.context |
| 119 | ); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 120 | return this; |
| 121 | }, |
| 122 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Return the input field attached to the hint helper. |
| 126 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 127 | inputField : function () { |
| 128 | return this._inputField; |
| 129 | }, |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 130 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Altert at a specific character position. |
| 134 | */ |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 135 | alert : function (charPos, msg) { |
| 136 | |
| 137 | if (arguments.length === 0) |
| 138 | return this._alert; |
| 139 | |
| 140 | // Do not alert if already alerted! |
| 141 | if (this._alert.active) |
| 142 | return false; |
| 143 | |
| 144 | // Move to the correct position |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 145 | this._inputField.moveto(charPos); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 146 | |
| 147 | // Set container to active (aka hide the hint helper button) |
| 148 | |
| 149 | this._alert.show(msg); |
| 150 | this.active(true); |
| 151 | return true; |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 152 | }, |
| 153 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 154 | _unshowAlert : function () { |
| 155 | this._alert.unshow(); |
| 156 | this.active(false); |
| 157 | }, |
| 158 | |
| 159 | update : function () { |
| 160 | this._inputField.update(); |
| 161 | if (this._alert.unshow()) |
| 162 | this.active(false); |
| 163 | }, |
| 164 | |
| 165 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 166 | /** |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 167 | * Return hint menu and probably init based on an action |
| 168 | */ |
| 169 | menu : function (action) { |
| 170 | |
| 171 | if (this._menu[action] === undefined) { |
| 172 | |
| 173 | // No matching hint menu |
| 174 | if (KorAP.hintArray[action] === undefined) |
| 175 | return; |
| 176 | |
| 177 | // Create matching hint menu |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 178 | this._menu[action] = menuClass.create( |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 179 | this, action, KorAP.hintArray[action] |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 180 | ); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | // Return matching hint menu |
| 184 | return this._menu[action]; |
| 185 | }, |
| 186 | |
| 187 | /** |
| 188 | * Get the correct menu based on the context |
| 189 | */ |
| 190 | contextMenu : function (ifContext) { |
| 191 | var context = this._inputField.context(); |
| 192 | if (context === undefined || context.length == 0) |
| 193 | return ifContext ? undefined : this.menu("-"); |
| 194 | |
| 195 | context = this._analyzer.test(context); |
| 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 | }); |