blob: 519ed26ca04e1ac794d9ac739b59f955dce08d45 [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
Akroncae907d2018-08-20 17:22:15 +0200267 var c = this._inputField.container();
Akron8eaeb2e2016-08-29 18:26:28 +0200268 c.appendChild(menu.element());
Akroncae907d2018-08-20 17:22:15 +0200269
Akron8eaeb2e2016-08-29 18:26:28 +0200270 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();
Akrone39cc862018-04-25 15:16:11 +0200303
Akron65c74352016-09-02 17:23:39 +0200304 // This does not work for alert currently!
Akron1a5a5872016-09-05 20:17:14 +0200305 //if (act._type !== 'alert') {
306 if (!this._alert.active) {
Akroncae907d2018-08-20 17:22:15 +0200307
308 // This does not work for webkit!
Akron1a5a5872016-09-05 20:17:14 +0200309 var c = this._inputField.container();
Akrone39cc862018-04-25 15:16:11 +0200310 c.removeChild(this._active.element());
Akron1a5a5872016-09-05 20:17:14 +0200311 }
312 else {
313 this._unshowAlert();
314 };
315
Akron65c74352016-09-02 17:23:39 +0200316 // this._active.hide();
317 this.active(null);
318 };
Nils Diewald19ccee92014-12-08 11:30:08 +0000319 }
320 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000321});