blob: 19c360ff80713f216f6e0e314747c79ce4ac937f [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 ")$";
Akron80055992017-12-20 16:30:52 +010045 // KorAP.annotationHelper = KorAP.annotationHelper || { "-" : [] };
Nils Diewald19ccee92014-12-08 11:30:08 +000046
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
Akron80055992017-12-20 16:30:52 +010081 // No annotation helper available
82 if (!KorAP.annotationHelper) {
83 console.log("No annotationhelper defined");
84 return;
85 };
86
Nils Diewald5c5a7472015-04-02 22:13:38 +000087 // Get input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000088 var qfield = param["inputField"] || document.getElementById("q-field");
89 if (!qfield)
Akron8eaeb2e2016-08-29 18:26:28 +020090 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000091
Akron00cd4d12016-05-31 21:01:11 +020092 // Create input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000094
95 var inputFieldElement = this._inputField.element();
96
Akron00cd4d12016-05-31 21:01:11 +020097 var c = this._inputField.container();
Nils Diewald5c5a7472015-04-02 22:13:38 +000098
Akron00cd4d12016-05-31 21:01:11 +020099 // create alert
100 c.appendChild(this._alert.element());
101
Akron00cd4d12016-05-31 21:01:11 +0200102 var that = this;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000103
Nils Diewald47f366b2015-04-15 20:06:35 +0000104 this._inputField.container().addEventListener('click', function (e) {
Akron8eaeb2e2016-08-29 18:26:28 +0200105 if (!this.classList.contains('active')) {
106 that.show(false);
107 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000108 });
109
Akron00cd4d12016-05-31 21:01:11 +0200110 var _down = function (e) {
Akron8eaeb2e2016-08-29 18:26:28 +0200111 var code = _codeFromEvent(e);
112 if (code === 40) {
113 this.show(false);
114 e.halt();
115 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000116 };
Akron8eaeb2e2016-08-29 18:26:28 +0200117
Nils Diewald47f366b2015-04-15 20:06:35 +0000118 // Move infobox
Akron00cd4d12016-05-31 21:01:11 +0200119 inputFieldElement.addEventListener("keyup", this.update.bind(this));
120 inputFieldElement.addEventListener("click", this.update.bind(this));
121
122 // Add event listener for key pressed down
123 inputFieldElement.addEventListener(
Akron8eaeb2e2016-08-29 18:26:28 +0200124 "keydown", _down.bind(this), false
Akron00cd4d12016-05-31 21:01:11 +0200125 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000126
127 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000128 this._analyzer = analyzerClass.create(
Akron8eaeb2e2016-08-29 18:26:28 +0200129 param["context"] || KorAP.context
Nils Diewald5c5a7472015-04-02 22:13:38 +0000130 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000131 return this;
132 },
133
Akron308db382016-05-30 22:34:07 +0200134
135 /**
136 * Return the input field attached to the hint helper.
137 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000138 inputField : function () {
139 return this._inputField;
140 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000141
Akron308db382016-05-30 22:34:07 +0200142
143 /**
144 * Altert at a specific character position.
145 */
Akron00cd4d12016-05-31 21:01:11 +0200146 alert : function (charPos, msg) {
147
148 if (arguments.length === 0)
Akron8eaeb2e2016-08-29 18:26:28 +0200149 return this._alert;
Akron00cd4d12016-05-31 21:01:11 +0200150
151 // Do not alert if already alerted!
152 if (this._alert.active)
Akron8eaeb2e2016-08-29 18:26:28 +0200153 return false;
Akron00cd4d12016-05-31 21:01:11 +0200154
155 // Move to the correct position
Akron308db382016-05-30 22:34:07 +0200156 this._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200157
158 // Set container to active (aka hide the hint helper button)
159
160 this._alert.show(msg);
Akron02360e42016-06-07 13:41:12 +0200161 this.active(this._alert);
Akron00cd4d12016-05-31 21:01:11 +0200162 return true;
Akron308db382016-05-30 22:34:07 +0200163 },
Akron8eaeb2e2016-08-29 18:26:28 +0200164
Akron00cd4d12016-05-31 21:01:11 +0200165 _unshowAlert : function () {
Akron02360e42016-06-07 13:41:12 +0200166 this._alert.hide();
167 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200168 },
169
170 update : function () {
171 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200172 if (this._alert.hide())
Akron8eaeb2e2016-08-29 18:26:28 +0200173 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200174 },
175
176
Nils Diewald5c5a7472015-04-02 22:13:38 +0000177 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000178 * Return hint menu and probably init based on an action
179 */
180 menu : function (action) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000181 if (this._menu[action] === undefined) {
182
Akron8eaeb2e2016-08-29 18:26:28 +0200183 // No matching hint menu
Akrone91da782017-12-15 17:17:50 +0100184 if (KorAP.annotationHelper[action] === undefined)
Akron8eaeb2e2016-08-29 18:26:28 +0200185 return;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000186
Akron8eaeb2e2016-08-29 18:26:28 +0200187 // Create matching hint menu
188 this._menu[action] = menuClass.create(
Akrone91da782017-12-15 17:17:50 +0100189 this, action, KorAP.annotationHelper[action]
Akron8eaeb2e2016-08-29 18:26:28 +0200190 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000191 };
192
193 // Return matching hint menu
194 return this._menu[action];
195 },
196
197 /**
198 * Get the correct menu based on the context
199 */
200 contextMenu : function (ifContext) {
Akron1a5a5872016-09-05 20:17:14 +0200201
Akron02360e42016-06-07 13:41:12 +0200202 // Get context (aka left text)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000203 var context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200204
Akron65c74352016-09-02 17:23:39 +0200205 if (context === undefined || context.length === 0) {
Akron1a5a5872016-09-05 20:17:14 +0200206 return ifContext ? undefined : this.menu("-");
Akron65c74352016-09-02 17:23:39 +0200207 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000208
Akron02360e42016-06-07 13:41:12 +0200209 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000210 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200211
Akron1a5a5872016-09-05 20:17:14 +0200212 if (context === undefined || context.length == 0) {
Akron8eaeb2e2016-08-29 18:26:28 +0200213 return ifContext ? undefined : this.menu("-");
Akron1a5a5872016-09-05 20:17:14 +0200214 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000215
Akron1a5a5872016-09-05 20:17:14 +0200216 return this.menu(context) || (ifContext ? undefined : this.menu('-'));
Nils Diewald5c5a7472015-04-02 22:13:38 +0000217 },
218
Akron8eaeb2e2016-08-29 18:26:28 +0200219 /**
220 * Activate a certain menu.
221 * If a menu is passed, the menu will be activated.
222 * If null is passed, the active menu will be deactivated.
223 * If nothing is passed, returns the active menu.
224 */
Akron02360e42016-06-07 13:41:12 +0200225 active : function (obj) {
Akron8eaeb2e2016-08-29 18:26:28 +0200226
227 // A menu or null was passed
Akron00cd4d12016-05-31 21:01:11 +0200228 if (arguments.length === 1) {
Akron8eaeb2e2016-08-29 18:26:28 +0200229 var c = this._inputField.container();
230
231 // Make the menu active
232 if (obj !== null) {
233 c.classList.add('active');
234 this._active = obj;
235 }
236
237 // Make the menu inactive
238 else {
239 c.classList.remove('active');
240 this._active = null;
241 }
Akron00cd4d12016-05-31 21:01:11 +0200242 };
Akron8eaeb2e2016-08-29 18:26:28 +0200243
244 // Return
Akron00cd4d12016-05-31 21:01:11 +0200245 return this._active;
246 },
247
Nils Diewald5c5a7472015-04-02 22:13:38 +0000248
249 /**
Akron308db382016-05-30 22:34:07 +0200250 * Show the menu.
Akron65c74352016-09-02 17:23:39 +0200251 * Remove all old menus.
Akron02360e42016-06-07 13:41:12 +0200252 *
253 * @param {boolean} Boolean value to indicate if context
254 * is necessary (true) or if the main context should
255 * be shown if context fails.
256 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000257 */
258 show : function (ifContext) {
259
Akron1a5a5872016-09-05 20:17:14 +0200260 // Remove the active object
261 this._unshow();
262
Nils Diewald5c5a7472015-04-02 22:13:38 +0000263 // Get the menu
264 var menu;
265 if (menu = this.contextMenu(ifContext)) {
Akron8eaeb2e2016-08-29 18:26:28 +0200266 this.active(menu);
Akron02360e42016-06-07 13:41:12 +0200267
Akron1a5a5872016-09-05 20:17:14 +0200268 var c = this._inputField.container();
Akron8eaeb2e2016-08-29 18:26:28 +0200269 c.appendChild(menu.element());
270 menu.show();
271 menu.focus();
272 // Focus on input field
273 // this.inputField.element.focus();
Akron65c74352016-09-02 17:23:39 +0200274 }
275 else {
276 this._inputField.element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000277 };
Akron00cd4d12016-05-31 21:01:11 +0200278 },
Akron8eaeb2e2016-08-29 18:26:28 +0200279
Akron02360e42016-06-07 13:41:12 +0200280 // This will get the context of the field
281 getContext : function () {},
282
Akron8eaeb2e2016-08-29 18:26:28 +0200283 /**
284 * Deactivate the current menu and focus on the input field.
285 */
Akron65c74352016-09-02 17:23:39 +0200286 unshow_old : function () {
Akron02360e42016-06-07 13:41:12 +0200287 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200288 this.inputField().element().focus();
Akron65c74352016-09-02 17:23:39 +0200289 },
290
291 /**
292 * Deactivate the current menu and focus on the input field.
293 */
294 unshow : function () {
Akron1a5a5872016-09-05 20:17:14 +0200295 this._unshow();
296 this._inputField.element().focus();
297 },
Akron65c74352016-09-02 17:23:39 +0200298
Akron1a5a5872016-09-05 20:17:14 +0200299
300 _unshow : function () {
Akron65c74352016-09-02 17:23:39 +0200301 if (this.active() !== null) {
Akron1a5a5872016-09-05 20:17:14 +0200302 // var act = this.active();
Akron65c74352016-09-02 17:23:39 +0200303
304 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200305 //if (act._type !== 'alert') {
306 if (!this._alert.active) {
307 var c = this._inputField.container();
Akron65c74352016-09-02 17:23:39 +0200308 c.removeChild(this._active.element());
Akron1a5a5872016-09-05 20:17:14 +0200309 }
310 else {
311 this._unshowAlert();
312 };
313
Akron65c74352016-09-02 17:23:39 +0200314 // this._active.hide();
315 this.active(null);
316 };
Nils Diewald19ccee92014-12-08 11:30:08 +0000317 }
318 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000319});