blob: 409bd6f84dbdd61a229335c410c9afeeea6631c0 [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
Akron00cd4d12016-05-31 21:01:11 +020056 this._menu = {};
57 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 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000100 });
101
Nils Diewald47f366b2015-04-15 20:06:35 +0000102 // Move infobox
Akronda5bd3a2020-10-16 17:37:49 +0200103 const inputFieldElement = this._inputField.element();
Akron00cd4d12016-05-31 21:01:11 +0200104 inputFieldElement.addEventListener("keyup", this.update.bind(this));
105 inputFieldElement.addEventListener("click", this.update.bind(this));
106
107 // Add event listener for key pressed down
Akronda5bd3a2020-10-16 17:37:49 +0200108 let _down = function (e) {
109 if (_codeFromEvent(e) === 40) {
110 this.show(false);
111 e.halt();
112 };
113 };
114
Akron00cd4d12016-05-31 21:01:11 +0200115 inputFieldElement.addEventListener(
Akronc14cbfc2018-08-31 13:15:55 +0200116 "keydown", _down.bind(this), false
117 );
118
119 // Add touch events
120 inputFieldElement.addEventListener(
121 'touchstart',
122 this._touch.bind(this),
123 false
124 );
125 inputFieldElement.addEventListener(
126 'touchend',
127 this._touch.bind(this),
128 false
129 );
130 inputFieldElement.addEventListener(
131 'touchmove',
132 this._touch.bind(this),
133 false
Akron00cd4d12016-05-31 21:01:11 +0200134 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000135
136 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000137 this._analyzer = analyzerClass.create(
Akronc14cbfc2018-08-31 13:15:55 +0200138 param["context"] || KorAP.context
Nils Diewald5c5a7472015-04-02 22:13:38 +0000139 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000140 return this;
141 },
142
Akron308db382016-05-30 22:34:07 +0200143
144 /**
145 * Return the input field attached to the hint helper.
146 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000147 inputField : function () {
148 return this._inputField;
149 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000150
Akron308db382016-05-30 22:34:07 +0200151
152 /**
153 * Altert at a specific character position.
154 */
Akron00cd4d12016-05-31 21:01:11 +0200155 alert : function (charPos, msg) {
Akronda5bd3a2020-10-16 17:37:49 +0200156 const t = this;
Akron00cd4d12016-05-31 21:01:11 +0200157 if (arguments.length === 0)
Akronda5bd3a2020-10-16 17:37:49 +0200158 return t._alert;
Akron00cd4d12016-05-31 21:01:11 +0200159
160 // Do not alert if already alerted!
Akronda5bd3a2020-10-16 17:37:49 +0200161 if (t._alert.active)
Akronc14cbfc2018-08-31 13:15:55 +0200162 return false;
Akron00cd4d12016-05-31 21:01:11 +0200163
164 // Move to the correct position
Akronda5bd3a2020-10-16 17:37:49 +0200165 t._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200166
167 // Set container to active (aka hide the hint helper button)
168
Akronda5bd3a2020-10-16 17:37:49 +0200169 t._alert.show(msg);
170 t.active(t._alert);
Akron00cd4d12016-05-31 21:01:11 +0200171 return true;
Akron308db382016-05-30 22:34:07 +0200172 },
Akron8eaeb2e2016-08-29 18:26:28 +0200173
Akron00cd4d12016-05-31 21:01:11 +0200174
Akronda5bd3a2020-10-16 17:37:49 +0200175 /**
176 * Update input field.
177 */
Akron00cd4d12016-05-31 21:01:11 +0200178 update : function () {
179 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200180 if (this._alert.hide())
Akronc14cbfc2018-08-31 13:15:55 +0200181 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200182 },
183
184
Nils Diewald5c5a7472015-04-02 22:13:38 +0000185 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000186 * Return hint menu and probably init based on an action
187 */
188 menu : function (action) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000189 if (this._menu[action] === undefined) {
Akronda5bd3a2020-10-16 17:37:49 +0200190
Akronc14cbfc2018-08-31 13:15:55 +0200191 // No matching hint menu
192 if (KorAP.annotationHelper[action] === undefined)
193 return;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000194
Akronc14cbfc2018-08-31 13:15:55 +0200195 // Create matching hint menu
196 this._menu[action] = menuClass.create(
197 this, action, KorAP.annotationHelper[action]
198 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000199 };
200
201 // Return matching hint menu
202 return this._menu[action];
203 },
204
Akronda5bd3a2020-10-16 17:37:49 +0200205
Nils Diewald5c5a7472015-04-02 22:13:38 +0000206 /**
207 * Get the correct menu based on the context
208 */
209 contextMenu : function (ifContext) {
Akronda5bd3a2020-10-16 17:37:49 +0200210 const noC = ifContext ? undefined : this.menu("-");
Akron1a5a5872016-09-05 20:17:14 +0200211
Akron02360e42016-06-07 13:41:12 +0200212 // Get context (aka left text)
Akronda5bd3a2020-10-16 17:37:49 +0200213 let context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200214
Akron65c74352016-09-02 17:23:39 +0200215 if (context === undefined || context.length === 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200216 return noC;
Akron65c74352016-09-02 17:23:39 +0200217 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000218
Akron02360e42016-06-07 13:41:12 +0200219 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000220 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200221
Akron1a5a5872016-09-05 20:17:14 +0200222 if (context === undefined || context.length == 0) {
Akronda5bd3a2020-10-16 17:37:49 +0200223 return noC;
Akron1a5a5872016-09-05 20:17:14 +0200224 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000225
Akronda5bd3a2020-10-16 17:37:49 +0200226 return this.menu(context) || noC;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000227 },
228
Akronda5bd3a2020-10-16 17:37:49 +0200229
Akron8eaeb2e2016-08-29 18:26:28 +0200230 /**
231 * Activate a certain menu.
232 * If a menu is passed, the menu will be activated.
233 * If null is passed, the active menu will be deactivated.
234 * If nothing is passed, returns the active menu.
235 */
Akron02360e42016-06-07 13:41:12 +0200236 active : function (obj) {
Akronda5bd3a2020-10-16 17:37:49 +0200237
Akron8eaeb2e2016-08-29 18:26:28 +0200238 // A menu or null was passed
Akronda5bd3a2020-10-16 17:37:49 +0200239 if (arguments.length === 1) {
240 const c = this._inputField.container();
241
Akron8eaeb2e2016-08-29 18:26:28 +0200242 // Make the menu active
Akronc14cbfc2018-08-31 13:15:55 +0200243 if (obj !== null) {
244 c.classList.add('active');
245 this._active = obj;
246 }
Akron8eaeb2e2016-08-29 18:26:28 +0200247
248 // Make the menu inactive
Akronc14cbfc2018-08-31 13:15:55 +0200249 else {
250 c.classList.remove('active');
251 this._active = null;
252 }
Akron00cd4d12016-05-31 21:01:11 +0200253 };
Akron8eaeb2e2016-08-29 18:26:28 +0200254
255 // Return
Akron00cd4d12016-05-31 21:01:11 +0200256 return this._active;
257 },
258
Nils Diewald5c5a7472015-04-02 22:13:38 +0000259
260 /**
Akron308db382016-05-30 22:34:07 +0200261 * Show the menu.
Akron65c74352016-09-02 17:23:39 +0200262 * Remove all old menus.
Akron02360e42016-06-07 13:41:12 +0200263 *
264 * @param {boolean} Boolean value to indicate if context
265 * is necessary (true) or if the main context should
266 * be shown if context fails.
267 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000268 */
269 show : function (ifContext) {
270
Akron1a5a5872016-09-05 20:17:14 +0200271 // Remove the active object
272 this._unshow();
Akronc14cbfc2018-08-31 13:15:55 +0200273
Nils Diewald5c5a7472015-04-02 22:13:38 +0000274 // Get the menu
Akronda5bd3a2020-10-16 17:37:49 +0200275 let menu;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000276 if (menu = this.contextMenu(ifContext)) {
Akronda5bd3a2020-10-16 17:37:49 +0200277
Akronc14cbfc2018-08-31 13:15:55 +0200278 this.active(menu);
Akron02360e42016-06-07 13:41:12 +0200279
Akronda5bd3a2020-10-16 17:37:49 +0200280 let e = menu.element();
281 // console.log("Chrome may send a blur on the old menu here");
282 this._active.element().blur();
283 this._inputField.container().appendChild(e);
Akroncae907d2018-08-20 17:22:15 +0200284
Akronc14cbfc2018-08-31 13:15:55 +0200285 menu.show();
286 menu.focus();
287 // Focus on input field
288 // this.inputField.element.focus();
Akron65c74352016-09-02 17:23:39 +0200289 }
Akronda5bd3a2020-10-16 17:37:49 +0200290
Akron65c74352016-09-02 17:23:39 +0200291 else {
292 this._inputField.element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000293 };
Akron00cd4d12016-05-31 21:01:11 +0200294 },
Akron8eaeb2e2016-08-29 18:26:28 +0200295
Akronda5bd3a2020-10-16 17:37:49 +0200296
Akron02360e42016-06-07 13:41:12 +0200297 // This will get the context of the field
298 getContext : function () {},
299
Akron65c74352016-09-02 17:23:39 +0200300
301 /**
302 * Deactivate the current menu and focus on the input field.
303 */
304 unshow : function () {
Akron1a5a5872016-09-05 20:17:14 +0200305 this._unshow();
306 this._inputField.element().focus();
307 },
Akron65c74352016-09-02 17:23:39 +0200308
Akronc14cbfc2018-08-31 13:15:55 +0200309
310 // Catch touch events
311 _touch : function (e) {
Akronc14cbfc2018-08-31 13:15:55 +0200312 if (e.type === 'touchstart') {
Akronda5bd3a2020-10-16 17:37:49 +0200313 this._lastTouch = e.touches[0].clientY;
Akronc14cbfc2018-08-31 13:15:55 +0200314 }
Akronda5bd3a2020-10-16 17:37:49 +0200315
Akronc14cbfc2018-08-31 13:15:55 +0200316 else if (e.type === 'touchend') {
317 this._lastTouch = undefined;
318 }
Akronda5bd3a2020-10-16 17:37:49 +0200319
Akronc14cbfc2018-08-31 13:15:55 +0200320 else if (e.type == 'touchmove') {
Akronda5bd3a2020-10-16 17:37:49 +0200321 if ((this._lastTouch + 10) < e.touches[0].clientY) {
Akronc14cbfc2018-08-31 13:15:55 +0200322 this.show();
323 this._lastTouch = undefined;
324 };
325 e.halt();
326 }
327 },
328
Akronda5bd3a2020-10-16 17:37:49 +0200329
330 // Unshow the hint menu
Akron1a5a5872016-09-05 20:17:14 +0200331 _unshow : function () {
Akron65c74352016-09-02 17:23:39 +0200332 if (this.active() !== null) {
Akronda5bd3a2020-10-16 17:37:49 +0200333
Akron65c74352016-09-02 17:23:39 +0200334 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200335 if (!this._alert.active) {
Akroncae907d2018-08-20 17:22:15 +0200336
Akronda5bd3a2020-10-16 17:37:49 +0200337 // TODO: This does not work for webkit?
338 this._inputField
339 .container()
340 .removeChild(this._active.element());
Akronc14cbfc2018-08-31 13:15:55 +0200341 }
Akronda5bd3a2020-10-16 17:37:49 +0200342
Akron1a5a5872016-09-05 20:17:14 +0200343 else {
344 this._unshowAlert();
345 };
346
Akron65c74352016-09-02 17:23:39 +0200347 this.active(null);
348 };
Akronda5bd3a2020-10-16 17:37:49 +0200349 },
350
351 // Unshow alert
352 _unshowAlert : function () {
353 this._alert.hide();
354 this.active(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000355 }
356 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000357});