blob: 6aed54433cb1437544e7c06139d98171725e4301 [file] [log] [blame]
Nils Diewald19ccee92014-12-08 11:30:08 +00001/**
Nils Diewald5c5a7472015-04-02 22:13:38 +00002 * Hint menu for Kalamar.
Nils Diewald19ccee92014-12-08 11:30:08 +00003 *
4 * @author Nils Diewald
5 */
Nils Diewald0e6992a2015-04-14 20:13:52 +00006define([
7 'hint/input',
8 'hint/menu',
9 'hint/contextanalyzer',
10 'util'
11], function (inputClass,
12 menuClass,
13 analyzerClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +000014 "use strict";
15
Nils Diewald19ccee92014-12-08 11:30:08 +000016 /**
17 * @define {regex} Regular expression for context
18 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 KorAP.context = KorAP.context ||
Nils Diewald19ccee92014-12-08 11:30:08 +000020 "(?:^|[^-_a-zA-Z0-9])" + // Anchor
21 "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry
22 "(?:" +
23 "(?:[-_a-zA-Z0-9]+?)=" + // Layer
24 "(?:(?:[^:=\/ ]+?):)?" + // Key
25 ")?" +
26 ")$";
Nils Diewald19ccee92014-12-08 11:30:08 +000027 KorAP.hintArray = KorAP.hintArray || {};
28
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 /**
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 Diewald5c5a7472015-04-02 22:13:38 +000039
40 /**
41 * KorAP.Hint.create({
42 * inputField : node,
43 * context : context regex
44 * });
45 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000046 return {
Nils Diewald5c5a7472015-04-02 22:13:38 +000047
48 // Some variables
49 // _firstTry : true,
50 active : false,
51
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 /**
53 * Create new hint helper.
54 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000055 create : function (param) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000056 return Object.create(this)._init(param);
Nils Diewald5c5a7472015-04-02 22:13:38 +000057 },
58
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 // Initialize hint helper
Nils Diewald5c5a7472015-04-02 22:13:38 +000060 _init : function (param) {
61 param = param || {};
62
63 // Holds all menus per prefix context
64 this._menu = {};
65
66 // Get input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000067 var qfield = param["inputField"] || document.getElementById("q-field");
68 if (!qfield)
69 return null;
70
71 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000072
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 Diewald5c5a7472015-04-02 22:13:38 +000093 }
94 );
95
96 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +000097 this._analyzer = analyzerClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +000098 param["context"] || KorAP.context
99 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000100 return this;
101 },
102
Nils Diewald5c5a7472015-04-02 22:13:38 +0000103 inputField : function () {
104 return this._inputField;
105 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000106
Nils Diewald5c5a7472015-04-02 22:13:38 +0000107 /**
108 * A new update by keypress
109 */
110 /*
111updateKeyPress : function (e) {
112 if (!this._active)
113 return;
Nils Diewald19ccee92014-12-08 11:30:08 +0000114
Nils Diewald5c5a7472015-04-02 22:13:38 +0000115 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 Diewald0e6992a2015-04-14 20:13:52 +0000138 this._menu[action] = menuClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000139 this, action, KorAP.hintArray[action]
Nils Diewald19ccee92014-12-08 11:30:08 +0000140 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000141 };
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 Diewald2488d052015-04-09 21:46:02 +0000185 var c = this._inputField.container();
186 c.classList.add('active');
187 c.appendChild(menu.element());
Nils Diewald5c5a7472015-04-02 22:13:38 +0000188 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 Diewald19ccee92014-12-08 11:30:08 +0000199 };
200 }
201 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000202});