blob: c93d17861658ed862eaeb432cb238b33b5f01e35 [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 /**
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 * Return keycode based on event
33 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000034
35 // Initialize hint array
Nils Diewald5c5a7472015-04-02 22:13:38 +000036
37 /**
38 * KorAP.Hint.create({
39 * inputField : node,
40 * context : context regex
41 * });
42 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000043 return {
Nils Diewald5c5a7472015-04-02 22:13:38 +000044
45 // Some variables
46 // _firstTry : true,
Akron00cd4d12016-05-31 21:01:11 +020047 // active : false,
Nils Diewald5c5a7472015-04-02 22:13:38 +000048
Nils Diewald0e6992a2015-04-14 20:13:52 +000049 /**
50 * Create new hint helper.
51 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000052 create : function (param) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000053 return Object.create(this)._init(param);
Nils Diewald5c5a7472015-04-02 22:13:38 +000054 },
55
Nils Diewald0e6992a2015-04-14 20:13:52 +000056 // Initialize hint helper
Nils Diewald5c5a7472015-04-02 22:13:38 +000057 _init : function (param) {
58 param = param || {};
59
60 // Holds all menus per prefix context
Akron00cd4d12016-05-31 21:01:11 +020061 this._menu = {};
62 this._alert = alertClass.create();
Akron02360e42016-06-07 13:41:12 +020063 this._active = null;
Nils Diewald5c5a7472015-04-02 22:13:38 +000064
Akron80055992017-12-20 16:30:52 +010065 // No annotation helper available
66 if (!KorAP.annotationHelper) {
67 console.log("No annotationhelper defined");
68 return;
69 };
Akronfac16472018-07-26 16:47:21 +020070
71 /**
72 * @define {regex} Regular expression for context
73 */
74 KorAP.context = KorAP.context ||
75 "(?:^|[^-_a-zA-Z0-9])" + // Anchor
76 "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry
77 "(?:" +
78 "(?:[-_a-zA-Z0-9]+?)=" + // Layer
79 "(?:"+
80 "(?:[^:=\/ ]+?):|" + // Key
81 "(?:[^-=\/ ]+?)-" + // Node
82 ")?" +
83 ")?" +
84 ")$";
Akron80055992017-12-20 16:30:52 +010085
Nils Diewald5c5a7472015-04-02 22:13:38 +000086 // Get input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000087 var qfield = param["inputField"] || document.getElementById("q-field");
88 if (!qfield)
Akron8eaeb2e2016-08-29 18:26:28 +020089 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000090
Akron00cd4d12016-05-31 21:01:11 +020091 // Create input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000092 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000093
94 var inputFieldElement = this._inputField.element();
95
Akron00cd4d12016-05-31 21:01:11 +020096 var c = this._inputField.container();
Nils Diewald5c5a7472015-04-02 22:13:38 +000097
Akron00cd4d12016-05-31 21:01:11 +020098 // create alert
99 c.appendChild(this._alert.element());
100
Akron00cd4d12016-05-31 21:01:11 +0200101 var that = this;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000102
Nils Diewald47f366b2015-04-15 20:06:35 +0000103 this._inputField.container().addEventListener('click', function (e) {
Akron8eaeb2e2016-08-29 18:26:28 +0200104 if (!this.classList.contains('active')) {
105 that.show(false);
106 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000107 });
108
Akron00cd4d12016-05-31 21:01:11 +0200109 var _down = function (e) {
Akron8eaeb2e2016-08-29 18:26:28 +0200110 var code = _codeFromEvent(e);
111 if (code === 40) {
112 this.show(false);
113 e.halt();
114 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000115 };
Akron8eaeb2e2016-08-29 18:26:28 +0200116
Nils Diewald47f366b2015-04-15 20:06:35 +0000117 // Move infobox
Akron00cd4d12016-05-31 21:01:11 +0200118 inputFieldElement.addEventListener("keyup", this.update.bind(this));
119 inputFieldElement.addEventListener("click", this.update.bind(this));
120
121 // Add event listener for key pressed down
122 inputFieldElement.addEventListener(
Akron8eaeb2e2016-08-29 18:26:28 +0200123 "keydown", _down.bind(this), false
Akron00cd4d12016-05-31 21:01:11 +0200124 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000125
126 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000127 this._analyzer = analyzerClass.create(
Akron8eaeb2e2016-08-29 18:26:28 +0200128 param["context"] || KorAP.context
Nils Diewald5c5a7472015-04-02 22:13:38 +0000129 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000130 return this;
131 },
132
Akron308db382016-05-30 22:34:07 +0200133
134 /**
135 * Return the input field attached to the hint helper.
136 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000137 inputField : function () {
138 return this._inputField;
139 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000140
Akron308db382016-05-30 22:34:07 +0200141
142 /**
143 * Altert at a specific character position.
144 */
Akron00cd4d12016-05-31 21:01:11 +0200145 alert : function (charPos, msg) {
146
147 if (arguments.length === 0)
Akron8eaeb2e2016-08-29 18:26:28 +0200148 return this._alert;
Akron00cd4d12016-05-31 21:01:11 +0200149
150 // Do not alert if already alerted!
151 if (this._alert.active)
Akron8eaeb2e2016-08-29 18:26:28 +0200152 return false;
Akron00cd4d12016-05-31 21:01:11 +0200153
154 // Move to the correct position
Akron308db382016-05-30 22:34:07 +0200155 this._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200156
157 // Set container to active (aka hide the hint helper button)
158
159 this._alert.show(msg);
Akron02360e42016-06-07 13:41:12 +0200160 this.active(this._alert);
Akron00cd4d12016-05-31 21:01:11 +0200161 return true;
Akron308db382016-05-30 22:34:07 +0200162 },
Akron8eaeb2e2016-08-29 18:26:28 +0200163
Akron00cd4d12016-05-31 21:01:11 +0200164 _unshowAlert : function () {
Akron02360e42016-06-07 13:41:12 +0200165 this._alert.hide();
166 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200167 },
168
169 update : function () {
170 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200171 if (this._alert.hide())
Akron8eaeb2e2016-08-29 18:26:28 +0200172 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200173 },
174
175
Nils Diewald5c5a7472015-04-02 22:13:38 +0000176 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000177 * Return hint menu and probably init based on an action
178 */
179 menu : function (action) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000180 if (this._menu[action] === undefined) {
181
Akron8eaeb2e2016-08-29 18:26:28 +0200182 // No matching hint menu
Akrone91da782017-12-15 17:17:50 +0100183 if (KorAP.annotationHelper[action] === undefined)
Akron8eaeb2e2016-08-29 18:26:28 +0200184 return;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000185
Akron8eaeb2e2016-08-29 18:26:28 +0200186 // Create matching hint menu
187 this._menu[action] = menuClass.create(
Akrone91da782017-12-15 17:17:50 +0100188 this, action, KorAP.annotationHelper[action]
Akron8eaeb2e2016-08-29 18:26:28 +0200189 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000190 };
191
192 // Return matching hint menu
193 return this._menu[action];
194 },
195
196 /**
197 * Get the correct menu based on the context
198 */
199 contextMenu : function (ifContext) {
Akron1a5a5872016-09-05 20:17:14 +0200200
Akron02360e42016-06-07 13:41:12 +0200201 // Get context (aka left text)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000202 var context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200203
Akron65c74352016-09-02 17:23:39 +0200204 if (context === undefined || context.length === 0) {
Akron1a5a5872016-09-05 20:17:14 +0200205 return ifContext ? undefined : this.menu("-");
Akron65c74352016-09-02 17:23:39 +0200206 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000207
Akron02360e42016-06-07 13:41:12 +0200208 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000209 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200210
Akron1a5a5872016-09-05 20:17:14 +0200211 if (context === undefined || context.length == 0) {
Akron8eaeb2e2016-08-29 18:26:28 +0200212 return ifContext ? undefined : this.menu("-");
Akron1a5a5872016-09-05 20:17:14 +0200213 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000214
Akron1a5a5872016-09-05 20:17:14 +0200215 return this.menu(context) || (ifContext ? undefined : this.menu('-'));
Nils Diewald5c5a7472015-04-02 22:13:38 +0000216 },
217
Akron8eaeb2e2016-08-29 18:26:28 +0200218 /**
219 * Activate a certain menu.
220 * If a menu is passed, the menu will be activated.
221 * If null is passed, the active menu will be deactivated.
222 * If nothing is passed, returns the active menu.
223 */
Akron02360e42016-06-07 13:41:12 +0200224 active : function (obj) {
Akron8eaeb2e2016-08-29 18:26:28 +0200225
226 // A menu or null was passed
Akron00cd4d12016-05-31 21:01:11 +0200227 if (arguments.length === 1) {
Akron8eaeb2e2016-08-29 18:26:28 +0200228 var c = this._inputField.container();
229
230 // Make the menu active
231 if (obj !== null) {
232 c.classList.add('active');
233 this._active = obj;
234 }
235
236 // Make the menu inactive
237 else {
238 c.classList.remove('active');
239 this._active = null;
240 }
Akron00cd4d12016-05-31 21:01:11 +0200241 };
Akron8eaeb2e2016-08-29 18:26:28 +0200242
243 // Return
Akron00cd4d12016-05-31 21:01:11 +0200244 return this._active;
245 },
246
Nils Diewald5c5a7472015-04-02 22:13:38 +0000247
248 /**
Akron308db382016-05-30 22:34:07 +0200249 * Show the menu.
Akron65c74352016-09-02 17:23:39 +0200250 * Remove all old menus.
Akron02360e42016-06-07 13:41:12 +0200251 *
252 * @param {boolean} Boolean value to indicate if context
253 * is necessary (true) or if the main context should
254 * be shown if context fails.
255 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000256 */
257 show : function (ifContext) {
258
Akron1a5a5872016-09-05 20:17:14 +0200259 // Remove the active object
260 this._unshow();
Akrone39cc862018-04-25 15:16:11 +0200261
Nils Diewald5c5a7472015-04-02 22:13:38 +0000262 // Get the menu
263 var menu;
264 if (menu = this.contextMenu(ifContext)) {
Akron8eaeb2e2016-08-29 18:26:28 +0200265 this.active(menu);
Akron02360e42016-06-07 13:41:12 +0200266
Akron1a5a5872016-09-05 20:17:14 +0200267 var c = this._inputField.container();
Akron8eaeb2e2016-08-29 18:26:28 +0200268 c.appendChild(menu.element());
269 menu.show();
270 menu.focus();
271 // Focus on input field
272 // this.inputField.element.focus();
Akron65c74352016-09-02 17:23:39 +0200273 }
274 else {
275 this._inputField.element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000276 };
Akron00cd4d12016-05-31 21:01:11 +0200277 },
Akron8eaeb2e2016-08-29 18:26:28 +0200278
Akron02360e42016-06-07 13:41:12 +0200279 // This will get the context of the field
280 getContext : function () {},
281
Akron8eaeb2e2016-08-29 18:26:28 +0200282 /**
283 * Deactivate the current menu and focus on the input field.
284 */
Akron65c74352016-09-02 17:23:39 +0200285 unshow_old : function () {
Akron02360e42016-06-07 13:41:12 +0200286 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200287 this.inputField().element().focus();
Akron65c74352016-09-02 17:23:39 +0200288 },
289
290 /**
291 * Deactivate the current menu and focus on the input field.
292 */
293 unshow : function () {
Akron1a5a5872016-09-05 20:17:14 +0200294 this._unshow();
295 this._inputField.element().focus();
296 },
Akron65c74352016-09-02 17:23:39 +0200297
Akron1a5a5872016-09-05 20:17:14 +0200298
299 _unshow : function () {
Akron65c74352016-09-02 17:23:39 +0200300 if (this.active() !== null) {
Akron1a5a5872016-09-05 20:17:14 +0200301 // var act = this.active();
Akrone39cc862018-04-25 15:16:11 +0200302
Akron65c74352016-09-02 17:23:39 +0200303 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200304 //if (act._type !== 'alert') {
305 if (!this._alert.active) {
306 var c = this._inputField.container();
Akrone39cc862018-04-25 15:16:11 +0200307 c.removeChild(this._active.element());
Akron1a5a5872016-09-05 20:17:14 +0200308 }
309 else {
310 this._unshowAlert();
311 };
312
Akron65c74352016-09-02 17:23:39 +0200313 // this._active.hide();
314 this.active(null);
315 };
Nils Diewald19ccee92014-12-08 11:30:08 +0000316 }
317 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000318});