blob: 16b3f8b6498e9fe710aa5b39e0b19d0323d6081d [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/*
8 * TODO: List can be shown when prefix is like 'base/s=pcorenlp/'
9 * TODO: Sometimes the drop-down box down vanish when list is shown
10 * TODO: Create should expect an input text field
11 * TODO: Embed only one single menu (not multiple)
Akron02360e42016-06-07 13:41:12 +020012 * By holding the current menu in _active
13 * TODO: show() should accept a context field (especially for no-context fields,
14 * like fragments)
15 * TODO: Improve context analyzer from hint!
16 * TODO: Marked annotations should be addable to "fragments"
Akron308db382016-05-30 22:34:07 +020017 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000018define([
19 'hint/input',
20 'hint/menu',
21 'hint/contextanalyzer',
Akron00cd4d12016-05-31 21:01:11 +020022 'hint/alert',
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 'util'
24], function (inputClass,
Akron00cd4d12016-05-31 21:01:11 +020025 menuClass,
26 analyzerClass,
27 alertClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +000028 "use strict";
29
Nils Diewald19ccee92014-12-08 11:30:08 +000030 /**
31 * @define {regex} Regular expression for context
32 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 KorAP.context = KorAP.context ||
Nils Diewald19ccee92014-12-08 11:30:08 +000034 "(?:^|[^-_a-zA-Z0-9])" + // Anchor
35 "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry
36 "(?:" +
37 "(?:[-_a-zA-Z0-9]+?)=" + // Layer
Akron113cc1a2016-01-22 21:17:57 +010038 "(?:"+
39 "(?:[^:=\/ ]+?):|" + // Key
Akron308db382016-05-30 22:34:07 +020040 "(?:[^-=\/ ]+?)-" + // Node
Akron113cc1a2016-01-22 21:17:57 +010041 ")?" +
Nils Diewald19ccee92014-12-08 11:30:08 +000042 ")?" +
43 ")$";
Nils Diewald19ccee92014-12-08 11:30:08 +000044 KorAP.hintArray = KorAP.hintArray || {};
45
Nils Diewald0e6992a2015-04-14 20:13:52 +000046 /**
47 * Return keycode based on event
48 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000049
50 // Initialize hint array
Nils Diewald5c5a7472015-04-02 22:13:38 +000051
52 /**
53 * KorAP.Hint.create({
54 * inputField : node,
55 * context : context regex
56 * });
57 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000058 return {
Nils Diewald5c5a7472015-04-02 22:13:38 +000059
60 // Some variables
61 // _firstTry : true,
Akron00cd4d12016-05-31 21:01:11 +020062 // active : false,
Nils Diewald5c5a7472015-04-02 22:13:38 +000063
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 /**
65 * Create new hint helper.
66 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000067 create : function (param) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 return Object.create(this)._init(param);
Nils Diewald5c5a7472015-04-02 22:13:38 +000069 },
70
Nils Diewald0e6992a2015-04-14 20:13:52 +000071 // Initialize hint helper
Nils Diewald5c5a7472015-04-02 22:13:38 +000072 _init : function (param) {
73 param = param || {};
74
75 // Holds all menus per prefix context
Akron00cd4d12016-05-31 21:01:11 +020076 this._menu = {};
77 this._alert = alertClass.create();
Akron02360e42016-06-07 13:41:12 +020078 this._active = null;
Nils Diewald5c5a7472015-04-02 22:13:38 +000079
80 // Get input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000081 var qfield = param["inputField"] || document.getElementById("q-field");
82 if (!qfield)
83 return null;
84
Akron00cd4d12016-05-31 21:01:11 +020085 // Create input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000086 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000087
88 var inputFieldElement = this._inputField.element();
89
Akron00cd4d12016-05-31 21:01:11 +020090 var c = this._inputField.container();
Nils Diewald5c5a7472015-04-02 22:13:38 +000091
Akron00cd4d12016-05-31 21:01:11 +020092 // create alert
93 c.appendChild(this._alert.element());
94
Akron00cd4d12016-05-31 21:01:11 +020095 var that = this;
Nils Diewald5c5a7472015-04-02 22:13:38 +000096
Nils Diewald47f366b2015-04-15 20:06:35 +000097 this._inputField.container().addEventListener('click', function (e) {
98 if (!this.classList.contains('active')) {
99 that.show(false);
100 };
101 });
102
Akron00cd4d12016-05-31 21:01:11 +0200103 var _down = function (e) {
104 var code = _codeFromEvent(e);
105 if (code === 40) {
106 this.show(false);
107 e.halt();
108 };
Nils Diewald47f366b2015-04-15 20:06:35 +0000109 };
110
111 // Move infobox
Akron00cd4d12016-05-31 21:01:11 +0200112 inputFieldElement.addEventListener("keyup", this.update.bind(this));
113 inputFieldElement.addEventListener("click", this.update.bind(this));
114
115 // Add event listener for key pressed down
116 inputFieldElement.addEventListener(
117 "keydown", _down.bind(this), false
118 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000119
120 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000121 this._analyzer = analyzerClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000122 param["context"] || KorAP.context
123 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000124 return this;
125 },
126
Akron308db382016-05-30 22:34:07 +0200127
128 /**
129 * Return the input field attached to the hint helper.
130 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000131 inputField : function () {
132 return this._inputField;
133 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000134
Akron308db382016-05-30 22:34:07 +0200135
136 /**
137 * Altert at a specific character position.
138 */
Akron00cd4d12016-05-31 21:01:11 +0200139 alert : function (charPos, msg) {
140
141 if (arguments.length === 0)
142 return this._alert;
143
144 // Do not alert if already alerted!
145 if (this._alert.active)
146 return false;
147
148 // Move to the correct position
Akron308db382016-05-30 22:34:07 +0200149 this._inputField.moveto(charPos);
Akron00cd4d12016-05-31 21:01:11 +0200150
151 // Set container to active (aka hide the hint helper button)
152
153 this._alert.show(msg);
Akron02360e42016-06-07 13:41:12 +0200154 this.active(this._alert);
Akron00cd4d12016-05-31 21:01:11 +0200155 return true;
Akron308db382016-05-30 22:34:07 +0200156 },
157
Akron00cd4d12016-05-31 21:01:11 +0200158 _unshowAlert : function () {
Akron02360e42016-06-07 13:41:12 +0200159 this._alert.hide();
160 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200161 },
162
163 update : function () {
164 this._inputField.update();
Akron02360e42016-06-07 13:41:12 +0200165 if (this._alert.hide())
166 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200167 },
168
169
Nils Diewald5c5a7472015-04-02 22:13:38 +0000170 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000171 * Return hint menu and probably init based on an action
172 */
173 menu : function (action) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000174 if (this._menu[action] === undefined) {
175
176 // No matching hint menu
177 if (KorAP.hintArray[action] === undefined)
178 return;
179
180 // Create matching hint menu
Nils Diewald0e6992a2015-04-14 20:13:52 +0000181 this._menu[action] = menuClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000182 this, action, KorAP.hintArray[action]
Nils Diewald19ccee92014-12-08 11:30:08 +0000183 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000184 };
185
186 // Return matching hint menu
187 return this._menu[action];
188 },
189
190 /**
191 * Get the correct menu based on the context
192 */
193 contextMenu : function (ifContext) {
Akron02360e42016-06-07 13:41:12 +0200194
195 // Get context (aka left text)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000196 var context = this._inputField.context();
Akronee9ef4a2016-06-03 12:50:08 +0200197
Akron02360e42016-06-07 13:41:12 +0200198 if (context === undefined || context.length === 0)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000199 return ifContext ? undefined : this.menu("-");
200
Akron02360e42016-06-07 13:41:12 +0200201 // Get context (aka left text matching regex)
Nils Diewald5c5a7472015-04-02 22:13:38 +0000202 context = this._analyzer.test(context);
Akronee9ef4a2016-06-03 12:50:08 +0200203
Nils Diewald5c5a7472015-04-02 22:13:38 +0000204 if (context === undefined || context.length == 0)
205 return ifContext ? undefined : this.menu("-");
206
Akron02360e42016-06-07 13:41:12 +0200207 return this.menu(context) || this.menu('-');
Nils Diewald5c5a7472015-04-02 22:13:38 +0000208 },
209
Akron02360e42016-06-07 13:41:12 +0200210 active : function (obj) {
Akron00cd4d12016-05-31 21:01:11 +0200211 if (arguments.length === 1) {
212 var c = this._inputField.container();
Akron02360e42016-06-07 13:41:12 +0200213 if (obj !== null) {
Akron00cd4d12016-05-31 21:01:11 +0200214 c.classList.add('active');
Akron02360e42016-06-07 13:41:12 +0200215 this._active = obj;
Akron00cd4d12016-05-31 21:01:11 +0200216 }
217 else {
218 c.classList.remove('active');
Akron02360e42016-06-07 13:41:12 +0200219 this._active = null;
Akron00cd4d12016-05-31 21:01:11 +0200220 }
221 };
222 return this._active;
223 },
224
Nils Diewald5c5a7472015-04-02 22:13:38 +0000225
226 /**
Akron308db382016-05-30 22:34:07 +0200227 * Show the menu.
228 * Currently this means that multiple menus may be loaded
229 * but not shown.
Akron02360e42016-06-07 13:41:12 +0200230 *
231 * @param {boolean} Boolean value to indicate if context
232 * is necessary (true) or if the main context should
233 * be shown if context fails.
234 *
Nils Diewald5c5a7472015-04-02 22:13:38 +0000235 */
236 show : function (ifContext) {
237
Akron02360e42016-06-07 13:41:12 +0200238 var c = this._inputField.container();
239
Nils Diewald5c5a7472015-04-02 22:13:38 +0000240 // Menu is already active
Akron02360e42016-06-07 13:41:12 +0200241 if (this.active() !== null) {
242
243 // This does not work for alert currently!
244 if (this._active._type !== 'alert') {
245 c.removeChild(this._active.element());
246 };
247
248 // This may already be hidden!
249 this._active.hide();
250 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200251
252 // Alert is not active
Akron02360e42016-06-07 13:41:12 +0200253 /*
254 if (!this._alert.unshow())
Akron00cd4d12016-05-31 21:01:11 +0200255 return;
Akron02360e42016-06-07 13:41:12 +0200256 */
Akron00cd4d12016-05-31 21:01:11 +0200257 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000258
Nils Diewald5c5a7472015-04-02 22:13:38 +0000259 // Get the menu
260 var menu;
261 if (menu = this.contextMenu(ifContext)) {
Akron02360e42016-06-07 13:41:12 +0200262
263// TODO: Remove old element!
264
265 this.active(menu);
266
Nils Diewald2488d052015-04-09 21:46:02 +0000267 c.appendChild(menu.element());
Akron6ed13992016-05-23 18:06:05 +0200268 menu.show();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000269 menu.focus();
Akron308db382016-05-30 22:34:07 +0200270 // Focus on input field
271 // this.inputField.element.focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000272 };
Akron00cd4d12016-05-31 21:01:11 +0200273 },
274
Akron02360e42016-06-07 13:41:12 +0200275 // Show an object in the containerField
276 // This will hide all other objects
277 // Accepts menus as well as alerts
278 show2 : function (obj) {},
279
280 // This will get the context of the field
281 getContext : function () {},
282
Akron00cd4d12016-05-31 21:01:11 +0200283 unshow : function () {
Akron02360e42016-06-07 13:41:12 +0200284 this.active(null);
Akron00cd4d12016-05-31 21:01:11 +0200285 this.inputField().element().focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000286 }
287 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000288});