blob: de9c09b149d3b5d0f2362222c1a8d09006904b84 [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 /**
Akronb759ee92024-11-19 18:02:56 +0100156 * Return selection range of the input field.
157 */
158 selectionRange : function () {
159 return this._inputField.selectionRange();
160 },
161
162
163 /**
Akrondadd1d12021-11-12 16:20:28 +0100164 * Alert at a specific character position.
Akron308db382016-05-30 22:34:07 +0200165 */
Akron00cd4d12016-05-31 21:01:11 +0200166 alert : function (charPos, msg) {
Akronda5bd3a2020-10-16 17:37:49 +0200167 const t = this;
Akron00cd4d12016-05-31 21:01:11 +0200168 if (arguments.length === 0)
Akronda5bd3a2020-10-16 17:37:49 +0200169 return t._alert;
Akron00cd4d12016-05-31 21:01:11 +0200170
171 // Do not alert if already alerted!
Akronda5bd3a2020-10-16 17:37:49 +0200172 if (t._alert.active)
Akronc14cbfc2018-08-31 13:15:55 +0200173 return false;
Akron00cd4d12016-05-31 21:01:11 +0200174
175 // Move to the correct position
Akronda5bd3a2020-10-16 17:37:49 +0200176 t._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200177
178 // Set container to active (aka hide the hint helper button)
179
Akronda5bd3a2020-10-16 17:37:49 +0200180 t._alert.show(msg);
181 t.active(t._alert);
Akron00cd4d12016-05-31 21:01:11 +0200182 return true;
Akron308db382016-05-30 22:34:07 +0200183 },
Akron8eaeb2e2016-08-29 18:26:28 +0200184
Akron00cd4d12016-05-31 21:01:11 +0200185
Akronda5bd3a2020-10-16 17:37:49 +0200186 /**
187 * Update input field.
188 */
Akron00cd4d12016-05-31 21:01:11 +0200189 update : function () {
190 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200191 if (this._alert.hide())
Akronc14cbfc2018-08-31 13:15:55 +0200192 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200193 },
194
195
Nils Diewald5c5a7472015-04-02 22:13:38 +0000196 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000197 * Return hint menu and probably init based on an action
198 */
199 menu : function (action) {
Akrondadd1d12021-11-12 16:20:28 +0100200
Leo Repp9b26ba92021-09-17 17:38:22 +0200201 if (this._menuCollection[action] === undefined) {
Akronda5bd3a2020-10-16 17:37:49 +0200202
Akronc14cbfc2018-08-31 13:15:55 +0200203 // No matching hint menu
204 if (KorAP.annotationHelper[action] === undefined)
205 return;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000206
Akronc14cbfc2018-08-31 13:15:55 +0200207 // Create matching hint menu
Leo Repp9b26ba92021-09-17 17:38:22 +0200208 this._menuCollection[action] = menuClass.create(
Akronc14cbfc2018-08-31 13:15:55 +0200209 this, action, KorAP.annotationHelper[action]
210 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000211 };
212
213 // Return matching hint menu
Leo Repp9b26ba92021-09-17 17:38:22 +0200214 return this._menuCollection[action];
Nils Diewald5c5a7472015-04-02 22:13:38 +0000215 },
216
Akronda5bd3a2020-10-16 17:37:49 +0200217
Nils Diewald5c5a7472015-04-02 22:13:38 +0000218 /**
219 * Get the correct menu based on the context
220 */
221 contextMenu : function (ifContext) {
Akronda5bd3a2020-10-16 17:37:49 +0200222 const noC = ifContext ? undefined : this.menu("-");
Akron1a5a5872016-09-05 20:17:14 +0200223
Akron02360e42016-06-07 13:41:12 +0200224 // Get context (aka left text)
Akronda5bd3a2020-10-16 17:37:49 +0200225 let context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200226
Akron65c74352016-09-02 17:23:39 +0200227 if (context === undefined || context.length === 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200228 return noC;
Akron65c74352016-09-02 17:23:39 +0200229 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000230
Akron02360e42016-06-07 13:41:12 +0200231 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000232 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200233
Akron1a5a5872016-09-05 20:17:14 +0200234 if (context === undefined || context.length == 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200235 return noC;
Akron1a5a5872016-09-05 20:17:14 +0200236 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000237
Akronda5bd3a2020-10-16 17:37:49 +0200238 return this.menu(context) || noC;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000239 },
240
Akronda5bd3a2020-10-16 17:37:49 +0200241
Akron8eaeb2e2016-08-29 18:26:28 +0200242 /**
243 * Activate a certain menu.
244 * If a menu is passed, the menu will be activated.
245 * If null is passed, the active menu will be deactivated.
246 * If nothing is passed, returns the active menu.
247 */
Akron02360e42016-06-07 13:41:12 +0200248 active : function (obj) {
Akronda5bd3a2020-10-16 17:37:49 +0200249
Akron8eaeb2e2016-08-29 18:26:28 +0200250 // A menu or null was passed
Akronda5bd3a2020-10-16 17:37:49 +0200251 if (arguments.length === 1) {
252 const c = this._inputField.container();
Akrondadd1d12021-11-12 16:20:28 +0100253
Akron8eaeb2e2016-08-29 18:26:28 +0200254 // Make the menu active
Akronc14cbfc2018-08-31 13:15:55 +0200255 if (obj !== null) {
256 c.classList.add('active');
257 this._active = obj;
258 }
Akron8eaeb2e2016-08-29 18:26:28 +0200259
260 // Make the menu inactive
Akronc14cbfc2018-08-31 13:15:55 +0200261 else {
262 c.classList.remove('active');
263 this._active = null;
264 }
Akron00cd4d12016-05-31 21:01:11 +0200265 };
Akron8eaeb2e2016-08-29 18:26:28 +0200266
267 // Return
Akron00cd4d12016-05-31 21:01:11 +0200268 return this._active;
269 },
270
Nils Diewald5c5a7472015-04-02 22:13:38 +0000271
272 /**
Akron308db382016-05-30 22:34:07 +0200273 * Show the menu.
Akron65c74352016-09-02 17:23:39 +0200274 * Remove all old menus.
Akron02360e42016-06-07 13:41:12 +0200275 *
276 * @param {boolean} Boolean value to indicate if context
277 * is necessary (true) or if the main context should
278 * be shown if context fails.
279 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000280 */
281 show : function (ifContext) {
282
Akron1a5a5872016-09-05 20:17:14 +0200283 // Remove the active object
284 this._unshow();
Akronc14cbfc2018-08-31 13:15:55 +0200285
Nils Diewald5c5a7472015-04-02 22:13:38 +0000286 // Get the menu
Akronda5bd3a2020-10-16 17:37:49 +0200287 let menu;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000288 if (menu = this.contextMenu(ifContext)) {
Akronda5bd3a2020-10-16 17:37:49 +0200289
Akronc14cbfc2018-08-31 13:15:55 +0200290 this.active(menu);
Akron02360e42016-06-07 13:41:12 +0200291
Akronda5bd3a2020-10-16 17:37:49 +0200292 let e = menu.element();
Akrondadd1d12021-11-12 16:20:28 +0100293 // Chrome may send a blur on the old menu here
Akronda5bd3a2020-10-16 17:37:49 +0200294 this._active.element().blur();
295 this._inputField.container().appendChild(e);
Akroncae907d2018-08-20 17:22:15 +0200296
Akronc14cbfc2018-08-31 13:15:55 +0200297 menu.show();
298 menu.focus();
299 // Focus on input field
300 // this.inputField.element.focus();
Akron65c74352016-09-02 17:23:39 +0200301 }
Akronda5bd3a2020-10-16 17:37:49 +0200302
Akron65c74352016-09-02 17:23:39 +0200303 else {
304 this._inputField.element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000305 };
Akron00cd4d12016-05-31 21:01:11 +0200306 },
Akron8eaeb2e2016-08-29 18:26:28 +0200307
Akronda5bd3a2020-10-16 17:37:49 +0200308
Akron02360e42016-06-07 13:41:12 +0200309 // This will get the context of the field
310 getContext : function () {},
311
Akron65c74352016-09-02 17:23:39 +0200312
313 /**
314 * Deactivate the current menu and focus on the input field.
315 */
316 unshow : function () {
Akron1a5a5872016-09-05 20:17:14 +0200317 this._unshow();
318 this._inputField.element().focus();
319 },
Akron65c74352016-09-02 17:23:39 +0200320
Akronc14cbfc2018-08-31 13:15:55 +0200321
322 // Catch touch events
323 _touch : function (e) {
Akronc14cbfc2018-08-31 13:15:55 +0200324 if (e.type === 'touchstart') {
Akronda5bd3a2020-10-16 17:37:49 +0200325 this._lastTouch = e.touches[0].clientY;
Akronc14cbfc2018-08-31 13:15:55 +0200326 }
Akronda5bd3a2020-10-16 17:37:49 +0200327
Akronc14cbfc2018-08-31 13:15:55 +0200328 else if (e.type === 'touchend') {
329 this._lastTouch = undefined;
330 }
Akronda5bd3a2020-10-16 17:37:49 +0200331
Akronc14cbfc2018-08-31 13:15:55 +0200332 else if (e.type == 'touchmove') {
Akronda5bd3a2020-10-16 17:37:49 +0200333 if ((this._lastTouch + 10) < e.touches[0].clientY) {
Akronc14cbfc2018-08-31 13:15:55 +0200334 this.show();
335 this._lastTouch = undefined;
336 };
337 e.halt();
338 }
339 },
340
Akronda5bd3a2020-10-16 17:37:49 +0200341
342 // Unshow the hint menu
Akron1a5a5872016-09-05 20:17:14 +0200343 _unshow : function () {
Akron65c74352016-09-02 17:23:39 +0200344 if (this.active() !== null) {
Akronda5bd3a2020-10-16 17:37:49 +0200345
Akron65c74352016-09-02 17:23:39 +0200346 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200347 if (!this._alert.active) {
Akroncae907d2018-08-20 17:22:15 +0200348
Akronda5bd3a2020-10-16 17:37:49 +0200349 // TODO: This does not work for webkit?
350 this._inputField
351 .container()
352 .removeChild(this._active.element());
Akronc14cbfc2018-08-31 13:15:55 +0200353 }
Akronda5bd3a2020-10-16 17:37:49 +0200354
Akron1a5a5872016-09-05 20:17:14 +0200355 else {
356 this._unshowAlert();
357 };
358
Akron65c74352016-09-02 17:23:39 +0200359 this.active(null);
360 };
Akronda5bd3a2020-10-16 17:37:49 +0200361 },
362
363 // Unshow alert
364 _unshowAlert : function () {
365 this._alert.hide();
366 this.active(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000367 }
368 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000369});