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