blob: 238b737cb1c3b84a965ef0d19173ad6ca88e55a1 [file] [log] [blame]
Nils Diewald19ccee92014-12-08 11:30:08 +00001/**
Nils Diewald5c5a7472015-04-02 22:13:38 +00002 * Hint menu for Kalamar.
Nils Diewald47f366b2015-04-15 20:06:35 +00003 * Based on menu object.
Nils Diewald19ccee92014-12-08 11:30:08 +00004 *
5 * @author Nils Diewald
6 */
Akron308db382016-05-30 22:34:07 +02007/*
Akron65c74352016-09-02 17:23:39 +02008 * TODO: Check for cnx/syn=
Akron308db382016-05-30 22:34:07 +02009 * TODO: List can be shown when prefix is like 'base/s=pcorenlp/'
10 * TODO: Sometimes the drop-down box down vanish when list is shown
11 * TODO: Create should expect an input text field
12 * TODO: Embed only one single menu (not multiple)
Akron02360e42016-06-07 13:41:12 +020013 * By holding the current menu in _active
14 * TODO: show() should accept a context field (especially for no-context fields,
15 * like fragments)
16 * TODO: Improve context analyzer from hint!
17 * TODO: Marked annotations should be addable to "fragments"
Akron308db382016-05-30 22:34:07 +020018 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000019define([
20 'hint/input',
21 'hint/menu',
22 'hint/contextanalyzer',
Akron00cd4d12016-05-31 21:01:11 +020023 'hint/alert',
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 'util'
25], function (inputClass,
Akron00cd4d12016-05-31 21:01:11 +020026 menuClass,
27 analyzerClass,
28 alertClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +000029 "use strict";
30
Nils Diewald19ccee92014-12-08 11:30:08 +000031 /**
32 * @define {regex} Regular expression for context
33 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000034 KorAP.context = KorAP.context ||
Nils Diewald19ccee92014-12-08 11:30:08 +000035 "(?:^|[^-_a-zA-Z0-9])" + // Anchor
36 "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry
37 "(?:" +
38 "(?:[-_a-zA-Z0-9]+?)=" + // Layer
Akron113cc1a2016-01-22 21:17:57 +010039 "(?:"+
40 "(?:[^:=\/ ]+?):|" + // Key
Akron308db382016-05-30 22:34:07 +020041 "(?:[^-=\/ ]+?)-" + // Node
Akron113cc1a2016-01-22 21:17:57 +010042 ")?" +
Nils Diewald19ccee92014-12-08 11:30:08 +000043 ")?" +
44 ")$";
Nils Diewald19ccee92014-12-08 11:30:08 +000045 KorAP.hintArray = KorAP.hintArray || {};
46
Nils Diewald0e6992a2015-04-14 20:13:52 +000047 /**
48 * Return keycode based on event
49 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000050
51 // Initialize hint array
Nils Diewald5c5a7472015-04-02 22:13:38 +000052
53 /**
54 * KorAP.Hint.create({
55 * inputField : node,
56 * context : context regex
57 * });
58 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 return {
Nils Diewald5c5a7472015-04-02 22:13:38 +000060
61 // Some variables
62 // _firstTry : true,
Akron00cd4d12016-05-31 21:01:11 +020063 // active : false,
Nils Diewald5c5a7472015-04-02 22:13:38 +000064
Nils Diewald0e6992a2015-04-14 20:13:52 +000065 /**
66 * Create new hint helper.
67 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000068 create : function (param) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 return Object.create(this)._init(param);
Nils Diewald5c5a7472015-04-02 22:13:38 +000070 },
71
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 // Initialize hint helper
Nils Diewald5c5a7472015-04-02 22:13:38 +000073 _init : function (param) {
74 param = param || {};
75
76 // Holds all menus per prefix context
Akron00cd4d12016-05-31 21:01:11 +020077 this._menu = {};
78 this._alert = alertClass.create();
Akron02360e42016-06-07 13:41:12 +020079 this._active = null;
Nils Diewald5c5a7472015-04-02 22:13:38 +000080
81 // Get input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000082 var qfield = param["inputField"] || document.getElementById("q-field");
83 if (!qfield)
Akron8eaeb2e2016-08-29 18:26:28 +020084 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000085
Akron00cd4d12016-05-31 21:01:11 +020086 // Create input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000087 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000088
89 var inputFieldElement = this._inputField.element();
90
Akron00cd4d12016-05-31 21:01:11 +020091 var c = this._inputField.container();
Nils Diewald5c5a7472015-04-02 22:13:38 +000092
Akron00cd4d12016-05-31 21:01:11 +020093 // create alert
94 c.appendChild(this._alert.element());
95
Akron00cd4d12016-05-31 21:01:11 +020096 var that = this;
Nils Diewald5c5a7472015-04-02 22:13:38 +000097
Nils Diewald47f366b2015-04-15 20:06:35 +000098 this._inputField.container().addEventListener('click', function (e) {
Akron8eaeb2e2016-08-29 18:26:28 +020099 if (!this.classList.contains('active')) {
100 that.show(false);
101 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000102 });
103
Akron00cd4d12016-05-31 21:01:11 +0200104 var _down = function (e) {
Akron8eaeb2e2016-08-29 18:26:28 +0200105 var code = _codeFromEvent(e);
106 if (code === 40) {
107 this.show(false);
108 e.halt();
109 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000110 };
Akron8eaeb2e2016-08-29 18:26:28 +0200111
Nils Diewald47f366b2015-04-15 20:06:35 +0000112 // Move infobox
Akron00cd4d12016-05-31 21:01:11 +0200113 inputFieldElement.addEventListener("keyup", this.update.bind(this));
114 inputFieldElement.addEventListener("click", this.update.bind(this));
115
116 // Add event listener for key pressed down
117 inputFieldElement.addEventListener(
Akron8eaeb2e2016-08-29 18:26:28 +0200118 "keydown", _down.bind(this), false
Akron00cd4d12016-05-31 21:01:11 +0200119 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000120
121 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000122 this._analyzer = analyzerClass.create(
Akron8eaeb2e2016-08-29 18:26:28 +0200123 param["context"] || KorAP.context
Nils Diewald5c5a7472015-04-02 22:13:38 +0000124 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000125 return this;
126 },
127
Akron308db382016-05-30 22:34:07 +0200128
129 /**
130 * Return the input field attached to the hint helper.
131 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000132 inputField : function () {
133 return this._inputField;
134 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000135
Akron308db382016-05-30 22:34:07 +0200136
137 /**
138 * Altert at a specific character position.
139 */
Akron00cd4d12016-05-31 21:01:11 +0200140 alert : function (charPos, msg) {
141
142 if (arguments.length === 0)
Akron8eaeb2e2016-08-29 18:26:28 +0200143 return this._alert;
Akron00cd4d12016-05-31 21:01:11 +0200144
145 // Do not alert if already alerted!
146 if (this._alert.active)
Akron8eaeb2e2016-08-29 18:26:28 +0200147 return false;
Akron00cd4d12016-05-31 21:01:11 +0200148
149 // Move to the correct position
Akron308db382016-05-30 22:34:07 +0200150 this._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200151
152 // Set container to active (aka hide the hint helper button)
153
154 this._alert.show(msg);
Akron02360e42016-06-07 13:41:12 +0200155 this.active(this._alert);
Akron00cd4d12016-05-31 21:01:11 +0200156 return true;
Akron308db382016-05-30 22:34:07 +0200157 },
Akron8eaeb2e2016-08-29 18:26:28 +0200158
Akron00cd4d12016-05-31 21:01:11 +0200159 _unshowAlert : function () {
Akron02360e42016-06-07 13:41:12 +0200160 this._alert.hide();
161 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200162 },
163
164 update : function () {
165 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200166 if (this._alert.hide())
Akron8eaeb2e2016-08-29 18:26:28 +0200167 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200168 },
169
170
Nils Diewald5c5a7472015-04-02 22:13:38 +0000171 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000172 * Return hint menu and probably init based on an action
173 */
174 menu : function (action) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000175 if (this._menu[action] === undefined) {
176
Akron8eaeb2e2016-08-29 18:26:28 +0200177 // No matching hint menu
178 if (KorAP.hintArray[action] === undefined)
179 return;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000180
Akron8eaeb2e2016-08-29 18:26:28 +0200181 // Create matching hint menu
182 this._menu[action] = menuClass.create(
183 this, action, KorAP.hintArray[action]
184 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000185 };
186
187 // Return matching hint menu
188 return this._menu[action];
189 },
190
191 /**
192 * Get the correct menu based on the context
193 */
194 contextMenu : function (ifContext) {
Akron1a5a5872016-09-05 20:17:14 +0200195
Akron02360e42016-06-07 13:41:12 +0200196 // Get context (aka left text)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000197 var context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200198
Akron65c74352016-09-02 17:23:39 +0200199 if (context === undefined || context.length === 0) {
Akron1a5a5872016-09-05 20:17:14 +0200200 return ifContext ? undefined : this.menu("-");
Akron65c74352016-09-02 17:23:39 +0200201 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000202
Akron02360e42016-06-07 13:41:12 +0200203 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000204 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200205
Akron1a5a5872016-09-05 20:17:14 +0200206 if (context === undefined || context.length == 0) {
Akron8eaeb2e2016-08-29 18:26:28 +0200207 return ifContext ? undefined : this.menu("-");
Akron1a5a5872016-09-05 20:17:14 +0200208 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000209
Akron1a5a5872016-09-05 20:17:14 +0200210 return this.menu(context) || (ifContext ? undefined : this.menu('-'));
Nils Diewald5c5a7472015-04-02 22:13:38 +0000211 },
212
Akron8eaeb2e2016-08-29 18:26:28 +0200213 /**
214 * Activate a certain menu.
215 * If a menu is passed, the menu will be activated.
216 * If null is passed, the active menu will be deactivated.
217 * If nothing is passed, returns the active menu.
218 */
Akron02360e42016-06-07 13:41:12 +0200219 active : function (obj) {
Akron8eaeb2e2016-08-29 18:26:28 +0200220
221 // A menu or null was passed
Akron00cd4d12016-05-31 21:01:11 +0200222 if (arguments.length === 1) {
Akron8eaeb2e2016-08-29 18:26:28 +0200223 var c = this._inputField.container();
224
225 // Make the menu active
226 if (obj !== null) {
227 c.classList.add('active');
228 this._active = obj;
229 }
230
231 // Make the menu inactive
232 else {
233 c.classList.remove('active');
234 this._active = null;
235 }
Akron00cd4d12016-05-31 21:01:11 +0200236 };
Akron8eaeb2e2016-08-29 18:26:28 +0200237
238 // Return
Akron00cd4d12016-05-31 21:01:11 +0200239 return this._active;
240 },
241
Nils Diewald5c5a7472015-04-02 22:13:38 +0000242
243 /**
Akron308db382016-05-30 22:34:07 +0200244 * Show the menu.
Akron65c74352016-09-02 17:23:39 +0200245 * Remove all old menus.
Akron02360e42016-06-07 13:41:12 +0200246 *
247 * @param {boolean} Boolean value to indicate if context
248 * is necessary (true) or if the main context should
249 * be shown if context fails.
250 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000251 */
252 show : function (ifContext) {
253
Akron1a5a5872016-09-05 20:17:14 +0200254 // Remove the active object
255 this._unshow();
256
Nils Diewald5c5a7472015-04-02 22:13:38 +0000257 // Get the menu
258 var menu;
259 if (menu = this.contextMenu(ifContext)) {
Akron8eaeb2e2016-08-29 18:26:28 +0200260 this.active(menu);
Akron02360e42016-06-07 13:41:12 +0200261
Akron1a5a5872016-09-05 20:17:14 +0200262 var c = this._inputField.container();
Akron8eaeb2e2016-08-29 18:26:28 +0200263 c.appendChild(menu.element());
264 menu.show();
265 menu.focus();
266 // Focus on input field
267 // this.inputField.element.focus();
Akron65c74352016-09-02 17:23:39 +0200268 }
269 else {
270 this._inputField.element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000271 };
Akron00cd4d12016-05-31 21:01:11 +0200272 },
Akron8eaeb2e2016-08-29 18:26:28 +0200273
Akron02360e42016-06-07 13:41:12 +0200274 // This will get the context of the field
275 getContext : function () {},
276
Akron8eaeb2e2016-08-29 18:26:28 +0200277 /**
278 * Deactivate the current menu and focus on the input field.
279 */
Akron65c74352016-09-02 17:23:39 +0200280 unshow_old : function () {
Akron02360e42016-06-07 13:41:12 +0200281 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200282 this.inputField().element().focus();
Akron65c74352016-09-02 17:23:39 +0200283 },
284
285 /**
286 * Deactivate the current menu and focus on the input field.
287 */
288 unshow : function () {
Akron1a5a5872016-09-05 20:17:14 +0200289 this._unshow();
290 this._inputField.element().focus();
291 },
Akron65c74352016-09-02 17:23:39 +0200292
Akron1a5a5872016-09-05 20:17:14 +0200293
294 _unshow : function () {
Akron65c74352016-09-02 17:23:39 +0200295 if (this.active() !== null) {
Akron1a5a5872016-09-05 20:17:14 +0200296 // var act = this.active();
Akron65c74352016-09-02 17:23:39 +0200297
298 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200299 //if (act._type !== 'alert') {
300 if (!this._alert.active) {
301 var c = this._inputField.container();
Akron65c74352016-09-02 17:23:39 +0200302 c.removeChild(this._active.element());
Akron1a5a5872016-09-05 20:17:14 +0200303 }
304 else {
305 this._unshowAlert();
306 };
307
Akron65c74352016-09-02 17:23:39 +0200308 // this._active.hide();
309 this.active(null);
310 };
Nils Diewald19ccee92014-12-08 11:30:08 +0000311 }
312 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000313});