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', |
| 17 | 'util' |
| 18 | ], function (inputClass, |
| 19 | menuClass, |
| 20 | analyzerClass) { |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 21 | "use strict"; |
| 22 | |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 23 | /** |
| 24 | * @define {regex} Regular expression for context |
| 25 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 26 | KorAP.context = KorAP.context || |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 27 | "(?:^|[^-_a-zA-Z0-9])" + // Anchor |
| 28 | "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry |
| 29 | "(?:" + |
| 30 | "(?:[-_a-zA-Z0-9]+?)=" + // Layer |
Akron | 113cc1a | 2016-01-22 21:17:57 +0100 | [diff] [blame] | 31 | "(?:"+ |
| 32 | "(?:[^:=\/ ]+?):|" + // Key |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame^] | 33 | "(?:[^-=\/ ]+?)-" + // Node |
Akron | 113cc1a | 2016-01-22 21:17:57 +0100 | [diff] [blame] | 34 | ")?" + |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 35 | ")?" + |
| 36 | ")$"; |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 37 | KorAP.hintArray = KorAP.hintArray || {}; |
| 38 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Return keycode based on event |
| 41 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 42 | |
| 43 | // Initialize hint array |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * KorAP.Hint.create({ |
| 47 | * inputField : node, |
| 48 | * context : context regex |
| 49 | * }); |
| 50 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 51 | return { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 52 | |
| 53 | // Some variables |
| 54 | // _firstTry : true, |
| 55 | active : false, |
| 56 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Create new hint helper. |
| 59 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 60 | create : function (param) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 61 | return Object.create(this)._init(param); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 62 | }, |
| 63 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 64 | // Initialize hint helper |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 65 | _init : function (param) { |
| 66 | param = param || {}; |
| 67 | |
| 68 | // Holds all menus per prefix context |
| 69 | this._menu = {}; |
| 70 | |
| 71 | // Get input field |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 72 | var qfield = param["inputField"] || document.getElementById("q-field"); |
| 73 | if (!qfield) |
| 74 | return null; |
| 75 | |
| 76 | this._inputField = inputClass.create(qfield); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 77 | |
| 78 | var inputFieldElement = this._inputField.element(); |
| 79 | |
| 80 | var that = this; |
| 81 | |
| 82 | // Add event listener for key pressed down |
| 83 | inputFieldElement.addEventListener( |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 84 | "keydown", function (e) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 85 | var code = _codeFromEvent(e); |
| 86 | if (code === 40) { |
| 87 | that.show(false); |
| 88 | e.halt(); |
| 89 | }; |
| 90 | }, false |
| 91 | ); |
| 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 | |
| 99 | var _up = function (e) { |
| 100 | var input = that._inputField; |
| 101 | input.update(); |
| 102 | }; |
| 103 | |
| 104 | // Move infobox |
| 105 | inputFieldElement.addEventListener("keyup", _up); |
| 106 | inputFieldElement.addEventListener("click", _up); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 107 | |
| 108 | // Set Analyzer for context |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 109 | this._analyzer = analyzerClass.create( |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 110 | param["context"] || KorAP.context |
| 111 | ); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 112 | return this; |
| 113 | }, |
| 114 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame^] | 115 | |
| 116 | /** |
| 117 | * Return the input field attached to the hint helper. |
| 118 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 119 | inputField : function () { |
| 120 | return this._inputField; |
| 121 | }, |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 122 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame^] | 123 | |
| 124 | /** |
| 125 | * Altert at a specific character position. |
| 126 | */ |
| 127 | charAlert : function (charPos, msg) { |
| 128 | this._inputField.moveto(charPos); |
| 129 | var c = this._inputField.container(); |
| 130 | c.classList.add('active'); |
| 131 | var error = c.appendChild(document.createElement('div')); |
| 132 | error.classList.add('alert', 'hint'); |
| 133 | error.appendChild(document.createTextNode(msg)); |
| 134 | }, |
| 135 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 136 | /** |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 137 | * Return hint menu and probably init based on an action |
| 138 | */ |
| 139 | menu : function (action) { |
| 140 | |
| 141 | if (this._menu[action] === undefined) { |
| 142 | |
| 143 | // No matching hint menu |
| 144 | if (KorAP.hintArray[action] === undefined) |
| 145 | return; |
| 146 | |
| 147 | // Create matching hint menu |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 148 | this._menu[action] = menuClass.create( |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 149 | this, action, KorAP.hintArray[action] |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 150 | ); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | // Return matching hint menu |
| 154 | return this._menu[action]; |
| 155 | }, |
| 156 | |
| 157 | /** |
| 158 | * Get the correct menu based on the context |
| 159 | */ |
| 160 | contextMenu : function (ifContext) { |
| 161 | var context = this._inputField.context(); |
| 162 | if (context === undefined || context.length == 0) |
| 163 | return ifContext ? undefined : this.menu("-"); |
| 164 | |
| 165 | context = this._analyzer.test(context); |
| 166 | if (context === undefined || context.length == 0) |
| 167 | return ifContext ? undefined : this.menu("-"); |
| 168 | |
| 169 | return this.menu(context); |
| 170 | }, |
| 171 | |
| 172 | |
| 173 | /** |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame^] | 174 | * Show the menu. |
| 175 | * Currently this means that multiple menus may be loaded |
| 176 | * but not shown. |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 177 | */ |
| 178 | show : function (ifContext) { |
| 179 | |
| 180 | // Menu is already active |
| 181 | if (this.active) |
| 182 | return; |
| 183 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 184 | // Get the menu |
| 185 | var menu; |
| 186 | if (menu = this.contextMenu(ifContext)) { |
Nils Diewald | 2488d05 | 2015-04-09 21:46:02 +0000 | [diff] [blame] | 187 | var c = this._inputField.container(); |
| 188 | c.classList.add('active'); |
| 189 | c.appendChild(menu.element()); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 190 | menu.show(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 191 | menu.focus(); |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame^] | 192 | // Focus on input field |
| 193 | // this.inputField.element.focus(); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 194 | }; |
| 195 | } |
| 196 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 197 | }); |