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