blob: e700b94b303d6330bd3b6d63a3033500d2568a96 [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 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000141 return this;
142 },
143
Akron308db382016-05-30 22:34:07 +0200144
145 /**
146 * Return the input field attached to the hint helper.
147 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000148 inputField : function () {
149 return this._inputField;
150 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000151
Akron308db382016-05-30 22:34:07 +0200152
153 /**
Akrondadd1d12021-11-12 16:20:28 +0100154 * Alert at a specific character position.
Akron308db382016-05-30 22:34:07 +0200155 */
Akron00cd4d12016-05-31 21:01:11 +0200156 alert : function (charPos, msg) {
Akronda5bd3a2020-10-16 17:37:49 +0200157 const t = this;
Akron00cd4d12016-05-31 21:01:11 +0200158 if (arguments.length === 0)
Akronda5bd3a2020-10-16 17:37:49 +0200159 return t._alert;
Akron00cd4d12016-05-31 21:01:11 +0200160
161 // Do not alert if already alerted!
Akronda5bd3a2020-10-16 17:37:49 +0200162 if (t._alert.active)
Akronc14cbfc2018-08-31 13:15:55 +0200163 return false;
Akron00cd4d12016-05-31 21:01:11 +0200164
165 // Move to the correct position
Akronda5bd3a2020-10-16 17:37:49 +0200166 t._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200167
168 // Set container to active (aka hide the hint helper button)
169
Akronda5bd3a2020-10-16 17:37:49 +0200170 t._alert.show(msg);
171 t.active(t._alert);
Akron00cd4d12016-05-31 21:01:11 +0200172 return true;
Akron308db382016-05-30 22:34:07 +0200173 },
Akron8eaeb2e2016-08-29 18:26:28 +0200174
Akron00cd4d12016-05-31 21:01:11 +0200175
Akronda5bd3a2020-10-16 17:37:49 +0200176 /**
177 * Update input field.
178 */
Akron00cd4d12016-05-31 21:01:11 +0200179 update : function () {
180 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200181 if (this._alert.hide())
Akronc14cbfc2018-08-31 13:15:55 +0200182 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200183 },
184
185
Nils Diewald5c5a7472015-04-02 22:13:38 +0000186 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000187 * Return hint menu and probably init based on an action
188 */
189 menu : function (action) {
Akrondadd1d12021-11-12 16:20:28 +0100190
Leo Repp9b26ba92021-09-17 17:38:22 +0200191 if (this._menuCollection[action] === undefined) {
Akronda5bd3a2020-10-16 17:37:49 +0200192
Akronc14cbfc2018-08-31 13:15:55 +0200193 // No matching hint menu
194 if (KorAP.annotationHelper[action] === undefined)
195 return;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000196
Akronc14cbfc2018-08-31 13:15:55 +0200197 // Create matching hint menu
Leo Repp9b26ba92021-09-17 17:38:22 +0200198 this._menuCollection[action] = menuClass.create(
Akronc14cbfc2018-08-31 13:15:55 +0200199 this, action, KorAP.annotationHelper[action]
200 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000201 };
202
203 // Return matching hint menu
Leo Repp9b26ba92021-09-17 17:38:22 +0200204 return this._menuCollection[action];
Nils Diewald5c5a7472015-04-02 22:13:38 +0000205 },
206
Akronda5bd3a2020-10-16 17:37:49 +0200207
Nils Diewald5c5a7472015-04-02 22:13:38 +0000208 /**
209 * Get the correct menu based on the context
210 */
211 contextMenu : function (ifContext) {
Akronda5bd3a2020-10-16 17:37:49 +0200212 const noC = ifContext ? undefined : this.menu("-");
Akron1a5a5872016-09-05 20:17:14 +0200213
Akron02360e42016-06-07 13:41:12 +0200214 // Get context (aka left text)
Akronda5bd3a2020-10-16 17:37:49 +0200215 let context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200216
Akron65c74352016-09-02 17:23:39 +0200217 if (context === undefined || context.length === 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200218 return noC;
Akron65c74352016-09-02 17:23:39 +0200219 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000220
Akron02360e42016-06-07 13:41:12 +0200221 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000222 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200223
Akron1a5a5872016-09-05 20:17:14 +0200224 if (context === undefined || context.length == 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200225 return noC;
Akron1a5a5872016-09-05 20:17:14 +0200226 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000227
Akronda5bd3a2020-10-16 17:37:49 +0200228 return this.menu(context) || noC;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000229 },
230
Akronda5bd3a2020-10-16 17:37:49 +0200231
Akron8eaeb2e2016-08-29 18:26:28 +0200232 /**
233 * Activate a certain menu.
234 * If a menu is passed, the menu will be activated.
235 * If null is passed, the active menu will be deactivated.
236 * If nothing is passed, returns the active menu.
237 */
Akron02360e42016-06-07 13:41:12 +0200238 active : function (obj) {
Akronda5bd3a2020-10-16 17:37:49 +0200239
Akron8eaeb2e2016-08-29 18:26:28 +0200240 // A menu or null was passed
Akronda5bd3a2020-10-16 17:37:49 +0200241 if (arguments.length === 1) {
242 const c = this._inputField.container();
Akrondadd1d12021-11-12 16:20:28 +0100243
Akron8eaeb2e2016-08-29 18:26:28 +0200244 // Make the menu active
Akronc14cbfc2018-08-31 13:15:55 +0200245 if (obj !== null) {
246 c.classList.add('active');
247 this._active = obj;
248 }
Akron8eaeb2e2016-08-29 18:26:28 +0200249
250 // Make the menu inactive
Akronc14cbfc2018-08-31 13:15:55 +0200251 else {
252 c.classList.remove('active');
253 this._active = null;
254 }
Akron00cd4d12016-05-31 21:01:11 +0200255 };
Akron8eaeb2e2016-08-29 18:26:28 +0200256
257 // Return
Akron00cd4d12016-05-31 21:01:11 +0200258 return this._active;
259 },
260
Nils Diewald5c5a7472015-04-02 22:13:38 +0000261
262 /**
Akron308db382016-05-30 22:34:07 +0200263 * Show the menu.
Akron65c74352016-09-02 17:23:39 +0200264 * Remove all old menus.
Akron02360e42016-06-07 13:41:12 +0200265 *
266 * @param {boolean} Boolean value to indicate if context
267 * is necessary (true) or if the main context should
268 * be shown if context fails.
269 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000270 */
271 show : function (ifContext) {
272
Akron1a5a5872016-09-05 20:17:14 +0200273 // Remove the active object
274 this._unshow();
Akronc14cbfc2018-08-31 13:15:55 +0200275
Nils Diewald5c5a7472015-04-02 22:13:38 +0000276 // Get the menu
Akronda5bd3a2020-10-16 17:37:49 +0200277 let menu;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000278 if (menu = this.contextMenu(ifContext)) {
Akronda5bd3a2020-10-16 17:37:49 +0200279
Akronc14cbfc2018-08-31 13:15:55 +0200280 this.active(menu);
Akron02360e42016-06-07 13:41:12 +0200281
Akronda5bd3a2020-10-16 17:37:49 +0200282 let e = menu.element();
Akrondadd1d12021-11-12 16:20:28 +0100283 // Chrome may send a blur on the old menu here
Akronda5bd3a2020-10-16 17:37:49 +0200284 this._active.element().blur();
285 this._inputField.container().appendChild(e);
Akroncae907d2018-08-20 17:22:15 +0200286
Akronc14cbfc2018-08-31 13:15:55 +0200287 menu.show();
288 menu.focus();
289 // Focus on input field
290 // this.inputField.element.focus();
Akron65c74352016-09-02 17:23:39 +0200291 }
Akronda5bd3a2020-10-16 17:37:49 +0200292
Akron65c74352016-09-02 17:23:39 +0200293 else {
294 this._inputField.element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000295 };
Akron00cd4d12016-05-31 21:01:11 +0200296 },
Akron8eaeb2e2016-08-29 18:26:28 +0200297
Akronda5bd3a2020-10-16 17:37:49 +0200298
Akron02360e42016-06-07 13:41:12 +0200299 // This will get the context of the field
300 getContext : function () {},
301
Akron65c74352016-09-02 17:23:39 +0200302
303 /**
304 * Deactivate the current menu and focus on the input field.
305 */
306 unshow : function () {
Akron1a5a5872016-09-05 20:17:14 +0200307 this._unshow();
308 this._inputField.element().focus();
309 },
Akron65c74352016-09-02 17:23:39 +0200310
Akronc14cbfc2018-08-31 13:15:55 +0200311
312 // Catch touch events
313 _touch : function (e) {
Akronc14cbfc2018-08-31 13:15:55 +0200314 if (e.type === 'touchstart') {
Akronda5bd3a2020-10-16 17:37:49 +0200315 this._lastTouch = e.touches[0].clientY;
Akronc14cbfc2018-08-31 13:15:55 +0200316 }
Akronda5bd3a2020-10-16 17:37:49 +0200317
Akronc14cbfc2018-08-31 13:15:55 +0200318 else if (e.type === 'touchend') {
319 this._lastTouch = undefined;
320 }
Akronda5bd3a2020-10-16 17:37:49 +0200321
Akronc14cbfc2018-08-31 13:15:55 +0200322 else if (e.type == 'touchmove') {
Akronda5bd3a2020-10-16 17:37:49 +0200323 if ((this._lastTouch + 10) < e.touches[0].clientY) {
Akronc14cbfc2018-08-31 13:15:55 +0200324 this.show();
325 this._lastTouch = undefined;
326 };
327 e.halt();
328 }
329 },
330
Akronda5bd3a2020-10-16 17:37:49 +0200331
332 // Unshow the hint menu
Akron1a5a5872016-09-05 20:17:14 +0200333 _unshow : function () {
Akron65c74352016-09-02 17:23:39 +0200334 if (this.active() !== null) {
Akronda5bd3a2020-10-16 17:37:49 +0200335
Akron65c74352016-09-02 17:23:39 +0200336 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200337 if (!this._alert.active) {
Akroncae907d2018-08-20 17:22:15 +0200338
Akronda5bd3a2020-10-16 17:37:49 +0200339 // TODO: This does not work for webkit?
340 this._inputField
341 .container()
342 .removeChild(this._active.element());
Akronc14cbfc2018-08-31 13:15:55 +0200343 }
Akronda5bd3a2020-10-16 17:37:49 +0200344
Akron1a5a5872016-09-05 20:17:14 +0200345 else {
346 this._unshowAlert();
347 };
348
Akron65c74352016-09-02 17:23:39 +0200349 this.active(null);
350 };
Akronda5bd3a2020-10-16 17:37:49 +0200351 },
352
353 // Unshow alert
354 _unshowAlert : function () {
355 this._alert.hide();
356 this.active(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000357 }
358 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000359});