blob: 80262cc6c4f2d59c0fc07de0af98ad17ef9d52f9 [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 */
Akronda5bd3a2020-10-16 17:37:49 +020019
20"use strict";
21
Nils Diewald0e6992a2015-04-14 20:13:52 +000022define([
23 'hint/input',
24 'hint/menu',
25 'hint/contextanalyzer',
Akron00cd4d12016-05-31 21:01:11 +020026 'hint/alert',
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 'util'
28], function (inputClass,
Akronc14cbfc2018-08-31 13:15:55 +020029 menuClass,
30 analyzerClass,
31 alertClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000032
33 // Initialize hint array
Nils Diewald5c5a7472015-04-02 22:13:38 +000034
35 /**
36 * KorAP.Hint.create({
37 * inputField : node,
38 * context : context regex
39 * });
40 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000041 return {
Nils Diewald5c5a7472015-04-02 22:13:38 +000042
Nils Diewald0e6992a2015-04-14 20:13:52 +000043 /**
44 * Create new hint helper.
45 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000046 create : function (param) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000047 return Object.create(this)._init(param);
Nils Diewald5c5a7472015-04-02 22:13:38 +000048 },
49
Akronda5bd3a2020-10-16 17:37:49 +020050
Nils Diewald0e6992a2015-04-14 20:13:52 +000051 // Initialize hint helper
Nils Diewald5c5a7472015-04-02 22:13:38 +000052 _init : function (param) {
53 param = param || {};
54
55 // Holds all menus per prefix context
Leo Repp9b26ba92021-09-17 17:38:22 +020056 this._menuCollection = {};
Akron00cd4d12016-05-31 21:01:11 +020057 this._alert = alertClass.create();
Akronda5bd3a2020-10-16 17:37:49 +020058
59 // Active may either hold an alert or a menu
Akron02360e42016-06-07 13:41:12 +020060 this._active = null;
Nils Diewald5c5a7472015-04-02 22:13:38 +000061
Akron80055992017-12-20 16:30:52 +010062 // No annotation helper available
63 if (!KorAP.annotationHelper) {
64 console.log("No annotationhelper defined");
65 return;
66 };
Akronfac16472018-07-26 16:47:21 +020067
68 /**
69 * @define {regex} Regular expression for context
70 */
71 KorAP.context = KorAP.context ||
72 "(?:^|[^-_a-zA-Z0-9])" + // Anchor
73 "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry
74 "(?:" +
75 "(?:[-_a-zA-Z0-9]+?)=" + // Layer
76 "(?:"+
77 "(?:[^:=\/ ]+?):|" + // Key
78 "(?:[^-=\/ ]+?)-" + // Node
79 ")?" +
80 ")?" +
81 ")$";
Akron80055992017-12-20 16:30:52 +010082
Nils Diewald5c5a7472015-04-02 22:13:38 +000083 // Get input field
Akronda5bd3a2020-10-16 17:37:49 +020084 const qfield = param["inputField"] || document.getElementById("q-field");
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 if (!qfield)
Akron8eaeb2e2016-08-29 18:26:28 +020086 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000087
Akron00cd4d12016-05-31 21:01:11 +020088 // Create input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000090
Akron00cd4d12016-05-31 21:01:11 +020091 // create alert
Akronda5bd3a2020-10-16 17:37:49 +020092 const that = this;
93
94 const c = this._inputField.container();
Akron00cd4d12016-05-31 21:01:11 +020095 c.appendChild(this._alert.element());
Akronda5bd3a2020-10-16 17:37:49 +020096 c.addEventListener('click', function (e) {
Akronc14cbfc2018-08-31 13:15:55 +020097 if (!this.classList.contains('active')) {
98 that.show(false);
99 };
Akrondadd1d12021-11-12 16:20:28 +0100100 e.halt();
Nils Diewald47f366b2015-04-15 20:06:35 +0000101 });
102
Nils Diewald47f366b2015-04-15 20:06:35 +0000103 // Move infobox
Akronda5bd3a2020-10-16 17:37:49 +0200104 const inputFieldElement = this._inputField.element();
Akron00cd4d12016-05-31 21:01:11 +0200105 inputFieldElement.addEventListener("keyup", this.update.bind(this));
106 inputFieldElement.addEventListener("click", this.update.bind(this));
107
108 // Add event listener for key pressed down
Akronda5bd3a2020-10-16 17:37:49 +0200109 let _down = function (e) {
110 if (_codeFromEvent(e) === 40) {
111 this.show(false);
112 e.halt();
113 };
114 };
115
Akron00cd4d12016-05-31 21:01:11 +0200116 inputFieldElement.addEventListener(
Akronc14cbfc2018-08-31 13:15:55 +0200117 "keydown", _down.bind(this), false
118 );
119
120 // Add touch events
121 inputFieldElement.addEventListener(
122 'touchstart',
123 this._touch.bind(this),
124 false
125 );
126 inputFieldElement.addEventListener(
127 'touchend',
128 this._touch.bind(this),
129 false
130 );
131 inputFieldElement.addEventListener(
132 'touchmove',
133 this._touch.bind(this),
134 false
Akron00cd4d12016-05-31 21:01:11 +0200135 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000136
137 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000138 this._analyzer = analyzerClass.create(
Akronc14cbfc2018-08-31 13:15:55 +0200139 param["context"] || KorAP.context
Nils Diewald5c5a7472015-04-02 22:13:38 +0000140 );
Akron1c18f102024-11-19 16:31:06 +0100141
142 this.update();
Nils Diewald19ccee92014-12-08 11:30:08 +0000143 return this;
144 },
145
Akron308db382016-05-30 22:34:07 +0200146
147 /**
148 * Return the input field attached to the hint helper.
149 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000150 inputField : function () {
151 return this._inputField;
152 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000153
Akron308db382016-05-30 22:34:07 +0200154
155 /**
Akrondadd1d12021-11-12 16:20:28 +0100156 * Alert at a specific character position.
Akron308db382016-05-30 22:34:07 +0200157 */
Akron00cd4d12016-05-31 21:01:11 +0200158 alert : function (charPos, msg) {
Akronda5bd3a2020-10-16 17:37:49 +0200159 const t = this;
Akron00cd4d12016-05-31 21:01:11 +0200160 if (arguments.length === 0)
Akronda5bd3a2020-10-16 17:37:49 +0200161 return t._alert;
Akron00cd4d12016-05-31 21:01:11 +0200162
163 // Do not alert if already alerted!
Akronda5bd3a2020-10-16 17:37:49 +0200164 if (t._alert.active)
Akronc14cbfc2018-08-31 13:15:55 +0200165 return false;
Akron00cd4d12016-05-31 21:01:11 +0200166
167 // Move to the correct position
Akronda5bd3a2020-10-16 17:37:49 +0200168 t._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200169
170 // Set container to active (aka hide the hint helper button)
171
Akronda5bd3a2020-10-16 17:37:49 +0200172 t._alert.show(msg);
173 t.active(t._alert);
Akron00cd4d12016-05-31 21:01:11 +0200174 return true;
Akron308db382016-05-30 22:34:07 +0200175 },
Akron8eaeb2e2016-08-29 18:26:28 +0200176
Akron00cd4d12016-05-31 21:01:11 +0200177
Akronda5bd3a2020-10-16 17:37:49 +0200178 /**
179 * Update input field.
180 */
Akron00cd4d12016-05-31 21:01:11 +0200181 update : function () {
182 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200183 if (this._alert.hide())
Akronc14cbfc2018-08-31 13:15:55 +0200184 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200185 },
186
187
Nils Diewald5c5a7472015-04-02 22:13:38 +0000188 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000189 * Return hint menu and probably init based on an action
190 */
191 menu : function (action) {
Akrondadd1d12021-11-12 16:20:28 +0100192
Leo Repp9b26ba92021-09-17 17:38:22 +0200193 if (this._menuCollection[action] === undefined) {
Akronda5bd3a2020-10-16 17:37:49 +0200194
Akronc14cbfc2018-08-31 13:15:55 +0200195 // No matching hint menu
196 if (KorAP.annotationHelper[action] === undefined)
197 return;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000198
Akronc14cbfc2018-08-31 13:15:55 +0200199 // Create matching hint menu
Leo Repp9b26ba92021-09-17 17:38:22 +0200200 this._menuCollection[action] = menuClass.create(
Akronc14cbfc2018-08-31 13:15:55 +0200201 this, action, KorAP.annotationHelper[action]
202 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000203 };
204
205 // Return matching hint menu
Leo Repp9b26ba92021-09-17 17:38:22 +0200206 return this._menuCollection[action];
Nils Diewald5c5a7472015-04-02 22:13:38 +0000207 },
208
Akronda5bd3a2020-10-16 17:37:49 +0200209
Nils Diewald5c5a7472015-04-02 22:13:38 +0000210 /**
211 * Get the correct menu based on the context
212 */
213 contextMenu : function (ifContext) {
Akronda5bd3a2020-10-16 17:37:49 +0200214 const noC = ifContext ? undefined : this.menu("-");
Akron1a5a5872016-09-05 20:17:14 +0200215
Akron02360e42016-06-07 13:41:12 +0200216 // Get context (aka left text)
Akronda5bd3a2020-10-16 17:37:49 +0200217 let context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200218
Akron65c74352016-09-02 17:23:39 +0200219 if (context === undefined || context.length === 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200220 return noC;
Akron65c74352016-09-02 17:23:39 +0200221 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000222
Akron02360e42016-06-07 13:41:12 +0200223 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000224 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200225
Akron1a5a5872016-09-05 20:17:14 +0200226 if (context === undefined || context.length == 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200227 return noC;
Akron1a5a5872016-09-05 20:17:14 +0200228 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000229
Akronda5bd3a2020-10-16 17:37:49 +0200230 return this.menu(context) || noC;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000231 },
232
Akronda5bd3a2020-10-16 17:37:49 +0200233
Akron8eaeb2e2016-08-29 18:26:28 +0200234 /**
235 * Activate a certain menu.
236 * If a menu is passed, the menu will be activated.
237 * If null is passed, the active menu will be deactivated.
238 * If nothing is passed, returns the active menu.
239 */
Akron02360e42016-06-07 13:41:12 +0200240 active : function (obj) {
Akronda5bd3a2020-10-16 17:37:49 +0200241
Akron8eaeb2e2016-08-29 18:26:28 +0200242 // A menu or null was passed
Akronda5bd3a2020-10-16 17:37:49 +0200243 if (arguments.length === 1) {
244 const c = this._inputField.container();
Akrondadd1d12021-11-12 16:20:28 +0100245
Akron8eaeb2e2016-08-29 18:26:28 +0200246 // Make the menu active
Akronc14cbfc2018-08-31 13:15:55 +0200247 if (obj !== null) {
248 c.classList.add('active');
249 this._active = obj;
250 }
Akron8eaeb2e2016-08-29 18:26:28 +0200251
252 // Make the menu inactive
Akronc14cbfc2018-08-31 13:15:55 +0200253 else {
254 c.classList.remove('active');
255 this._active = null;
256 }
Akron00cd4d12016-05-31 21:01:11 +0200257 };
Akron8eaeb2e2016-08-29 18:26:28 +0200258
259 // Return
Akron00cd4d12016-05-31 21:01:11 +0200260 return this._active;
261 },
262
Nils Diewald5c5a7472015-04-02 22:13:38 +0000263
264 /**
Akron308db382016-05-30 22:34:07 +0200265 * Show the menu.
Akron65c74352016-09-02 17:23:39 +0200266 * Remove all old menus.
Akron02360e42016-06-07 13:41:12 +0200267 *
268 * @param {boolean} Boolean value to indicate if context
269 * is necessary (true) or if the main context should
270 * be shown if context fails.
271 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000272 */
273 show : function (ifContext) {
274
Akron1a5a5872016-09-05 20:17:14 +0200275 // Remove the active object
276 this._unshow();
Akronc14cbfc2018-08-31 13:15:55 +0200277
Nils Diewald5c5a7472015-04-02 22:13:38 +0000278 // Get the menu
Akronda5bd3a2020-10-16 17:37:49 +0200279 let menu;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000280 if (menu = this.contextMenu(ifContext)) {
Akronda5bd3a2020-10-16 17:37:49 +0200281
Akronc14cbfc2018-08-31 13:15:55 +0200282 this.active(menu);
Akron02360e42016-06-07 13:41:12 +0200283
Akronda5bd3a2020-10-16 17:37:49 +0200284 let e = menu.element();
Akrondadd1d12021-11-12 16:20:28 +0100285 // Chrome may send a blur on the old menu here
Akronda5bd3a2020-10-16 17:37:49 +0200286 this._active.element().blur();
287 this._inputField.container().appendChild(e);
Akroncae907d2018-08-20 17:22:15 +0200288
Akronc14cbfc2018-08-31 13:15:55 +0200289 menu.show();
290 menu.focus();
291 // Focus on input field
292 // this.inputField.element.focus();
Akron65c74352016-09-02 17:23:39 +0200293 }
Akronda5bd3a2020-10-16 17:37:49 +0200294
Akron65c74352016-09-02 17:23:39 +0200295 else {
296 this._inputField.element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000297 };
Akron00cd4d12016-05-31 21:01:11 +0200298 },
Akron8eaeb2e2016-08-29 18:26:28 +0200299
Akronda5bd3a2020-10-16 17:37:49 +0200300
Akron02360e42016-06-07 13:41:12 +0200301 // This will get the context of the field
302 getContext : function () {},
303
Akron65c74352016-09-02 17:23:39 +0200304
305 /**
306 * Deactivate the current menu and focus on the input field.
307 */
308 unshow : function () {
Akron1a5a5872016-09-05 20:17:14 +0200309 this._unshow();
310 this._inputField.element().focus();
311 },
Akron65c74352016-09-02 17:23:39 +0200312
Akronc14cbfc2018-08-31 13:15:55 +0200313
314 // Catch touch events
315 _touch : function (e) {
Akronc14cbfc2018-08-31 13:15:55 +0200316 if (e.type === 'touchstart') {
Akronda5bd3a2020-10-16 17:37:49 +0200317 this._lastTouch = e.touches[0].clientY;
Akronc14cbfc2018-08-31 13:15:55 +0200318 }
Akronda5bd3a2020-10-16 17:37:49 +0200319
Akronc14cbfc2018-08-31 13:15:55 +0200320 else if (e.type === 'touchend') {
321 this._lastTouch = undefined;
322 }
Akronda5bd3a2020-10-16 17:37:49 +0200323
Akronc14cbfc2018-08-31 13:15:55 +0200324 else if (e.type == 'touchmove') {
Akronda5bd3a2020-10-16 17:37:49 +0200325 if ((this._lastTouch + 10) < e.touches[0].clientY) {
Akronc14cbfc2018-08-31 13:15:55 +0200326 this.show();
327 this._lastTouch = undefined;
328 };
329 e.halt();
330 }
331 },
332
Akronda5bd3a2020-10-16 17:37:49 +0200333
334 // Unshow the hint menu
Akron1a5a5872016-09-05 20:17:14 +0200335 _unshow : function () {
Akron65c74352016-09-02 17:23:39 +0200336 if (this.active() !== null) {
Akronda5bd3a2020-10-16 17:37:49 +0200337
Akron65c74352016-09-02 17:23:39 +0200338 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200339 if (!this._alert.active) {
Akroncae907d2018-08-20 17:22:15 +0200340
Akronda5bd3a2020-10-16 17:37:49 +0200341 // TODO: This does not work for webkit?
342 this._inputField
343 .container()
344 .removeChild(this._active.element());
Akronc14cbfc2018-08-31 13:15:55 +0200345 }
Akronda5bd3a2020-10-16 17:37:49 +0200346
Akron1a5a5872016-09-05 20:17:14 +0200347 else {
348 this._unshowAlert();
349 };
350
Akron65c74352016-09-02 17:23:39 +0200351 this.active(null);
352 };
Akronda5bd3a2020-10-16 17:37:49 +0200353 },
354
355 // Unshow alert
356 _unshowAlert : function () {
357 this._alert.hide();
358 this.active(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000359 }
360 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000361});