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