blob: 98204ea3852cf5f904915fabbcaca82de18cbd2c [file] [log] [blame]
Nils Diewald2fe12e12015-03-06 16:47:06 +00001/**
Nils Diewald7148c6f2015-05-04 15:07:53 +00002 * Scrollable drop-down menus with view filter.
Nils Diewald2fe12e12015-03-06 16:47:06 +00003 *
4 * @author Nils Diewald
5 */
Nils Diewald2488d052015-04-09 21:46:02 +00006/*
Akron3c2730f2016-05-24 15:08:29 +02007 * TODO: Show the slider briefly on move (whenever screen is called).
Akron9c4d1ae2016-05-25 21:43:22 +02008 * TODO: Ignore alt+ and strg+ key strokes.
Akron0b92f692016-05-25 22:37:13 +02009 * TODO: Should scroll to a chosen value after prefixing, if the chosen value is live
Akron201b72a2016-06-03 01:46:19 +020010 * TODO: Add a "title" to a menu that is not scrollable.
11 * TODO: Make the menu responsive by showing less items on smaller screens
12 * or anytime items would be outside the screen.
Akron02360e42016-06-07 13:41:12 +020013 * TODO: Add a .match() method to items for scrolling and probably for prefixing.
14 * TODO: Add static header (for title, sortation fields, but also for menu points like "fragments" and "history".
Akrone91da782017-12-15 17:17:50 +010015 * TODO: Support space separated list of prefixes so "co no" will highlight "common noun"
Nils Diewald2488d052015-04-09 21:46:02 +000016 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000017define([
18 'menu/item',
19 'menu/prefix',
Akronc7448732016-04-27 14:06:58 +020020 'menu/lengthField',
Akron9905e2a2016-05-10 16:06:44 +020021 'menu/slider',
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 'util'
23], function (defaultItemClass,
Akrone4961b12017-05-10 21:04:46 +020024 defaultPrefixClass,
25 defaultLengthFieldClass,
26 sliderClass) {
Nils Diewaldfda29d92015-01-22 17:28:01 +000027
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 // Default maximum number of menu items
29 var menuLimit = 8;
30
Nils Diewald86dad5b2015-01-28 15:09:07 +000031 /**
32 * List of items for drop down menu (complete).
33 * Only a sublist of the menu is filtered (live).
34 * Only a sublist of the filtered menu is visible (shown).
35 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 return {
Nils Diewald86dad5b2015-01-28 15:09:07 +000037 /**
38 * Create new Menu based on the action prefix
39 * and a list of menu items.
40 *
Akron7524be12016-06-01 17:31:33 +020041 *
42 * Accepts an associative array containg the elements
43 * itemClass, prefixClass, lengthFieldClass
44 *
Nils Diewald86dad5b2015-01-28 15:09:07 +000045 * @this {Menu}
46 * @constructor
47 * @param {string} Context prefix
48 * @param {Array.<Array.<string>>} List of menu items
49 */
Akron7524be12016-06-01 17:31:33 +020050 create : function (list, params) {
51 return Object.create(this)._init(list, params);
Nils Diewald86dad5b2015-01-28 15:09:07 +000052 },
53
Akron5240b8c2016-05-20 09:17:41 +020054 // Initialize list
Akron7524be12016-06-01 17:31:33 +020055 _init : function (list, params) {
Akrona92fd8d2016-05-24 21:13:41 +020056
Akron7524be12016-06-01 17:31:33 +020057 if (params === undefined)
Akrone4961b12017-05-10 21:04:46 +020058 params = {};
Akron7524be12016-06-01 17:31:33 +020059
60 this._itemClass = params["itemClass"] || defaultItemClass;
Akron5240b8c2016-05-20 09:17:41 +020061
62 // Add prefix object
Akron7524be12016-06-01 17:31:33 +020063 if (params["prefixClass"] !== undefined) {
Akrone4961b12017-05-10 21:04:46 +020064 this._prefix = params["prefixClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020065 }
66 else {
Akrone4961b12017-05-10 21:04:46 +020067 this._prefix = defaultPrefixClass.create();
Akron5240b8c2016-05-20 09:17:41 +020068 };
69 this._prefix._menu = this;
70
71 // Add lengthField object
Akron7524be12016-06-01 17:31:33 +020072 if (params["lengthFieldClass"] !== undefined) {
Akrone4961b12017-05-10 21:04:46 +020073 this._lengthField = params["lengthFieldClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020074 }
75 else {
Akrone4961b12017-05-10 21:04:46 +020076 this._lengthField = defaultLengthFieldClass.create();
Akron5240b8c2016-05-20 09:17:41 +020077 };
78 this._lengthField._menu = this;
79
80 // Initialize slider
81 this._slider = sliderClass.create(this);
82
83 // Create the element
Akron9c4d1ae2016-05-25 21:43:22 +020084 var el = document.createElement("ul");
85 with (el) {
Akrone4961b12017-05-10 21:04:46 +020086 style.outline = 0;
87 setAttribute('tabindex', 0);
88 classList.add('menu', 'roll');
89 appendChild(this._prefix.element());
90 appendChild(this._lengthField.element());
91 appendChild(this._slider.element());
Akron9c4d1ae2016-05-25 21:43:22 +020092 };
Akron5240b8c2016-05-20 09:17:41 +020093
94 // This has to be cleaned up later on
Akron9c4d1ae2016-05-25 21:43:22 +020095 el["menu"] = this;
Akron5240b8c2016-05-20 09:17:41 +020096
97 // Arrow keys
Akron9c4d1ae2016-05-25 21:43:22 +020098 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +020099 'keydown',
100 this._keydown.bind(this),
101 false
Akron5240b8c2016-05-20 09:17:41 +0200102 );
103
104 // Strings
Akron9c4d1ae2016-05-25 21:43:22 +0200105 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200106 'keypress',
107 this._keypress.bind(this),
108 false
Akron5240b8c2016-05-20 09:17:41 +0200109 );
110
111 // Mousewheel
Akron9c4d1ae2016-05-25 21:43:22 +0200112 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200113 'wheel',
114 this._mousewheel.bind(this),
115 false
Akron5240b8c2016-05-20 09:17:41 +0200116 );
Akrona1159ff2018-07-22 13:28:31 +0200117
118 // Touch
119 el.addEventListener(
120 'touchstart',
121 this._touch.bind(this),
122 false
123 );
124 el.addEventListener(
125 'touchend',
126 this._touch.bind(this),
127 false
128 );
129 el.addEventListener(
130 'touchmove',
131 this._touch.bind(this),
132 false
133 );
134
135
Akron9c4d1ae2016-05-25 21:43:22 +0200136 this._element = el;
Akroneaba63e2018-01-26 19:49:30 +0100137
138 this._limit = menuLimit;
Akrone4961b12017-05-10 21:04:46 +0200139
Akron5240b8c2016-05-20 09:17:41 +0200140 this._items = new Array();
Akron5240b8c2016-05-20 09:17:41 +0200141
Akroneaba63e2018-01-26 19:49:30 +0100142 // TODO:
143 // Make this separate from _init
144 this.readItems(list);
145
hebastaf95226b2019-09-19 11:37:00 +0200146 this.dontHide = false;
147
Akroneaba63e2018-01-26 19:49:30 +0100148 return this;
149 },
150
151 // Read items to add to list
152 readItems : function (list) {
153
154 this._list = undefined;
155
156 // Remove circular reference to "this" in items
157 for (var i = 0; i < this._items.length; i++) {
158 delete this._items[i]["_menu"];
159 delete this._items[i];
160 };
161
162 this._items = new Array();
163 this.removeItems();
164
165
166 // Initialize items
167 this._lengthField.reset();
168
Akron9c4d1ae2016-05-25 21:43:22 +0200169 var i = 0;
Akron5240b8c2016-05-20 09:17:41 +0200170 // Initialize item list based on parameters
Akron678c26f2020-10-09 08:52:50 +0200171 list.forEach(function(i){
172 var obj = this._itemClass.create(i);
Akron5240b8c2016-05-20 09:17:41 +0200173
Akrone4961b12017-05-10 21:04:46 +0200174 // This may become circular
175 obj["_menu"] = this;
Akron678c26f2020-10-09 08:52:50 +0200176 this._lengthField.add(i);
Akrone4961b12017-05-10 21:04:46 +0200177 this._items.push(obj);
Akron678c26f2020-10-09 08:52:50 +0200178 }, this);
Akron5240b8c2016-05-20 09:17:41 +0200179
Akron9c4d1ae2016-05-25 21:43:22 +0200180 this._slider.length(this.liveLength())
Akrone4961b12017-05-10 21:04:46 +0200181 .limit(this._limit)
182 .reInit();
183
Akroneaba63e2018-01-26 19:49:30 +0100184 this._firstActive = false;
185 // Show the first item active always?
Akron9c4d1ae2016-05-25 21:43:22 +0200186 this.offset = 0;
187 this.position = 0;
Akron5240b8c2016-05-20 09:17:41 +0200188 },
Akroneaba63e2018-01-26 19:49:30 +0100189
Akron5240b8c2016-05-20 09:17:41 +0200190 // Initialize the item list
191 _initList : function () {
192
193 // Create a new list
194 if (this._list === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200195 this._list = [];
Akron5240b8c2016-05-20 09:17:41 +0200196 }
197 else if (this._list.length !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200198 this._boundary(false);
199 this._list.length = 0;
Akron5240b8c2016-05-20 09:17:41 +0200200 };
201
202 // Offset is initially zero
Akron9c4d1ae2016-05-25 21:43:22 +0200203 this.offset = 0;
Akron5240b8c2016-05-20 09:17:41 +0200204
205 // There is no prefix set
206 if (this.prefix().length <= 0) {
207
Akrone4961b12017-05-10 21:04:46 +0200208 // add all items to the list and lowlight
209 var i = 0;
210 for (; i < this._items.length; i++) {
211 this._list.push(i);
212 this._items[i].lowlight();
213 };
Akron5240b8c2016-05-20 09:17:41 +0200214
Akroneaba63e2018-01-26 19:49:30 +0100215 this._slider.length(i).reInit();
Akron97752a72016-05-25 14:43:07 +0200216
Akrone4961b12017-05-10 21:04:46 +0200217 return true;
Akron5240b8c2016-05-20 09:17:41 +0200218 };
219
220 /*
221 * There is a prefix set, so filter the list!
222 */
223 var pos;
Akronacffc652017-12-18 21:21:25 +0100224 var prefixList = this.prefix().toLowerCase().split(" ");
225
226 var items = [];
227 var maxPoints = 1; // minimum 1
Akron5240b8c2016-05-20 09:17:41 +0200228
229 // Iterate over all items and choose preferred matching items
230 // i.e. the matching happens at the word start
231 for (pos = 0; pos < this._items.length; pos++) {
Akronacffc652017-12-18 21:21:25 +0100232
233 var points = 0;
234
235 for (pref = 0; pref < prefixList.length; pref++) {
236 var prefix = " " + prefixList[pref];
237
238 // Check if it matches at the beginning
Akron3b253d32018-07-15 10:16:06 +0200239 // if ((this.item(pos).lcField().indexOf(prefix)) >= 0) {
240 if ((this.item(pos).lcField().includes(prefix))) {
Akronacffc652017-12-18 21:21:25 +0100241 points += 5;
242 }
243
244 // Check if it matches anywhere
Akron3b253d32018-07-15 10:16:06 +0200245 // else if ((this.item(pos).lcField().indexOf(prefix.substring(1))) >= 0) {
246 else if ((this.item(pos).lcField().includes(prefix.substring(1)))) {
Akronacffc652017-12-18 21:21:25 +0100247 points += 1;
248 };
249 };
250
251 if (points > maxPoints) {
252 this._list = [pos];
253 maxPoints = points;
254 }
255 else if (points == maxPoints) {
Akrone4961b12017-05-10 21:04:46 +0200256 this._list.push(pos);
Akronacffc652017-12-18 21:21:25 +0100257 }
Akron5240b8c2016-05-20 09:17:41 +0200258 };
259
260 // The list is empty - so lower your expectations
261 // Iterate over all items and choose matching items
262 // i.e. the matching happens anywhere in the word
Akronacffc652017-12-18 21:21:25 +0100263 /*
Akron6ffad5d2016-05-24 17:16:58 +0200264 prefix = prefix.substring(1);
Akron5240b8c2016-05-20 09:17:41 +0200265 if (this._list.length == 0) {
Akrone4961b12017-05-10 21:04:46 +0200266 for (pos = 0; pos < this._items.length; pos++) {
267 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
268 this._list.push(pos);
269 };
Akron5240b8c2016-05-20 09:17:41 +0200270 };
Akronacffc652017-12-18 21:21:25 +0100271 */
Akron5240b8c2016-05-20 09:17:41 +0200272
Akron9c4d1ae2016-05-25 21:43:22 +0200273 this._slider.length(this._list.length).reInit();
Akron6ed13992016-05-23 18:06:05 +0200274
Akron5240b8c2016-05-20 09:17:41 +0200275 // Filter was successful - yeah!
276 return this._list.length > 0 ? true : false;
277 },
278
279
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000280 /**
281 * Destroy this menu
282 * (in case you don't trust the
283 * mark and sweep GC)!
284 */
285 destroy : function () {
Akron47c086c2016-05-18 21:22:06 +0200286
Akron5240b8c2016-05-20 09:17:41 +0200287 // Remove circular reference to "this" in menu
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000288 if (this._element != undefined)
Akrone4961b12017-05-10 21:04:46 +0200289 delete this._element["menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000290
Akron5240b8c2016-05-20 09:17:41 +0200291 // Remove circular reference to "this" in items
Akron678c26f2020-10-09 08:52:50 +0200292 this._items.forEach(function(i) {
293 delete i["_menu"];
294 });
Akron5240b8c2016-05-20 09:17:41 +0200295
296 // Remove circular reference to "this" in prefix
Nils Diewald5c5a7472015-04-02 22:13:38 +0000297 delete this._prefix['_menu'];
Akron5240b8c2016-05-20 09:17:41 +0200298 delete this._lengthField['_menu'];
299 delete this._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000300 },
301
Nils Diewald7148c6f2015-05-04 15:07:53 +0000302
303 /**
304 * Focus on this menu.
305 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000306 focus : function () {
307 this._element.focus();
308 },
309
Nils Diewald7148c6f2015-05-04 15:07:53 +0000310
Nils Diewald59c02fc2015-03-07 01:29:09 +0000311 // mouse wheel treatment
312 _mousewheel : function (e) {
313 var delta = 0;
Nils Diewald5975d702015-03-09 17:45:42 +0000314 delta = e.deltaY / 120;
315 if (delta > 0)
Akrone4961b12017-05-10 21:04:46 +0200316 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000317 else if (delta < 0)
Akrone4961b12017-05-10 21:04:46 +0200318 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000319 e.halt();
320 },
321
Akrona1159ff2018-07-22 13:28:31 +0200322 // touchmove treatment
323 _touch : function (e) {
324 var s = this.slider();
325 if (e.type === "touchstart") {
326 // s.active(true);
327 var t = e.touches[0];
328 this._lastTouch = t.clientY;
329 }
330 else if (e.type === "touchend") {
331 // s.active(false);
332 this._lastTouch = undefined;
Akrona1159ff2018-07-22 13:28:31 +0200333 }
334 else if (e.type === "touchmove") {
335 var t = e.touches[0];
Akrone817b882018-08-31 14:09:17 +0200336
337 // TODO:
338 // Instead of using 26px, choose the item height
339 // or use the menu height // shownItems
340
Akrona1159ff2018-07-22 13:28:31 +0200341 // s.movetoRel(t.clientY - this._initTouch);
342 if ((this._lastTouch + 26) < t.clientY) {
Akrone817b882018-08-31 14:09:17 +0200343 this.viewDown();
Akrona1159ff2018-07-22 13:28:31 +0200344 this._lastTouch = t.clientY;
345 }
346 else if ((this._lastTouch - 26) > t.clientY) {
Akrone817b882018-08-31 14:09:17 +0200347 this.viewUp();
Akrona1159ff2018-07-22 13:28:31 +0200348 this._lastTouch = t.clientY;
349 }
350 e.halt();
351 };
352 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000353
Nils Diewald59c02fc2015-03-07 01:29:09 +0000354 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000355 _keydown : function (e) {
Nils Diewald59c02fc2015-03-07 01:29:09 +0000356 var code = _codeFromEvent(e);
357
Nils Diewald59c02fc2015-03-07 01:29:09 +0000358 switch (code) {
359 case 27: // 'Esc'
Akrone4961b12017-05-10 21:04:46 +0200360 e.halt();
361 this.hide();
362 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000363
Nils Diewald59c02fc2015-03-07 01:29:09 +0000364 case 38: // 'Up'
Akrone4961b12017-05-10 21:04:46 +0200365 e.halt();
366 this.prev();
367 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000368 case 33: // 'Page up'
Akrone4961b12017-05-10 21:04:46 +0200369 e.halt();
370 this.pageUp();
371 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000372 case 40: // 'Down'
Akrone4961b12017-05-10 21:04:46 +0200373 e.halt();
374 this.next();
375 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000376 case 34: // 'Page down'
Akrone4961b12017-05-10 21:04:46 +0200377 e.halt();
378 this.pageDown();
379 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000380 case 39: // 'Right'
Akrone4961b12017-05-10 21:04:46 +0200381 if (this._prefix.active())
382 break;
Nils Diewalde8518f82015-03-18 22:41:49 +0000383
Akrone4961b12017-05-10 21:04:46 +0200384 var item = this.liveItem(this.position);
385
386 if (item["further"] !== undefined) {
387 item["further"].bind(item).apply();
388 };
389
390 e.halt();
391 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000392 case 13: // 'Enter'
393
Akrone4961b12017-05-10 21:04:46 +0200394 // Click on prefix
395 if (this._prefix.active())
396 this._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000397
Akrone4961b12017-05-10 21:04:46 +0200398 // Click on item
399 else
400 this.liveItem(this.position).onclick(e);
401 e.halt();
402 break;
Nils Diewald59c02fc2015-03-07 01:29:09 +0000403 case 8: // 'Backspace'
Akrone4961b12017-05-10 21:04:46 +0200404 this._prefix.chop();
405 this.show();
406 e.halt();
407 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000408 };
409 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000410
Nils Diewald47f366b2015-04-15 20:06:35 +0000411 // Add characters to prefix
412 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200413 if (e.charCode !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200414 e.halt();
415 var c = String.fromCharCode(_codeFromEvent(e));
416
417 // Add prefix
418 this._prefix.add(c);
419 this.show();
Akron9c2f9382016-05-25 16:36:04 +0200420 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000421 },
422
Akron47c086c2016-05-18 21:22:06 +0200423 /**
Akron5240b8c2016-05-20 09:17:41 +0200424 * Show a screen with a given offset
425 * in the viewport.
Akron47c086c2016-05-18 21:22:06 +0200426 */
427 screen : function (nr) {
Akrone817b882018-08-31 14:09:17 +0200428
429 // Normalize negative values
Akron3c2730f2016-05-24 15:08:29 +0200430 if (nr < 0) {
Akrone4961b12017-05-10 21:04:46 +0200431 nr = 0
Akron3c2730f2016-05-24 15:08:29 +0200432 }
Akrone817b882018-08-31 14:09:17 +0200433
434 // The shown list already shows everything
435 else if (this.liveLength() < this.limit()) {
436 return false;
437 }
438
439 // Move relatively to the next screen
Akron6ac58442016-05-24 16:52:29 +0200440 else if (nr > (this.liveLength() - this.limit())) {
Akrone4961b12017-05-10 21:04:46 +0200441 nr = (this.liveLength() - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200442 };
443
Akrone817b882018-08-31 14:09:17 +0200444 // no change
Akron9c4d1ae2016-05-25 21:43:22 +0200445 if (this.offset === nr)
Akrone817b882018-08-31 14:09:17 +0200446 return false;
Akron5a1f5bb2016-05-23 22:00:39 +0200447
Akron47c086c2016-05-18 21:22:06 +0200448 this._showItems(nr);
Akrone817b882018-08-31 14:09:17 +0200449
450 return true;
Akron47c086c2016-05-18 21:22:06 +0200451 },
452
Akrone817b882018-08-31 14:09:17 +0200453
Nils Diewald2fe12e12015-03-06 16:47:06 +0000454 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000455 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000456 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000457 element : function () {
458 return this._element;
459 },
460
Nils Diewald2fe12e12015-03-06 16:47:06 +0000461 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000462 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000463 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000464 itemClass : function () {
465 return this._itemClass;
466 },
467
468 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000469 * Get and set the numerical value
470 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000471 */
472 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000473 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200474 if (this._limit !== limit) {
475 this._limit = limit;
476 this._slider.limit(limit).reInit();
477 };
478 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000479 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000480 return this._limit;
481 },
482
Nils Diewald7148c6f2015-05-04 15:07:53 +0000483
Nils Diewald86dad5b2015-01-28 15:09:07 +0000484 /**
485 * Upgrade this object to another object,
486 * while private data stays intact.
487 *
Nils Diewald2fe12e12015-03-06 16:47:06 +0000488 * @param {Object} An object with properties.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000489 */
490 upgradeTo : function (props) {
491 for (var prop in props) {
Akrone4961b12017-05-10 21:04:46 +0200492 this[prop] = props[prop];
Nils Diewald86dad5b2015-01-28 15:09:07 +0000493 };
494 return this;
495 },
496
Nils Diewald7148c6f2015-05-04 15:07:53 +0000497
Nils Diewald86dad5b2015-01-28 15:09:07 +0000498 /**
Akron97752a72016-05-25 14:43:07 +0200499 * Filter the list and make it visible.
500 * This is always called once the prefix changes.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000501 *
502 * @param {string} Prefix for filtering the list
503 */
Akron6ed13992016-05-23 18:06:05 +0200504 show : function (active) {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000505
Akron5240b8c2016-05-20 09:17:41 +0200506 // show menu based on initial offset
Akron6ac58442016-05-24 16:52:29 +0200507 this._unmark(); // Unmark everything that was marked before
Akrona92fd8d2016-05-24 21:13:41 +0200508 this.removeItems();
Akron6ed13992016-05-23 18:06:05 +0200509
510 // Initialize the list
511 if (!this._initList()) {
Akron6ac58442016-05-24 16:52:29 +0200512
Akrone4961b12017-05-10 21:04:46 +0200513 // The prefix is not active
514 this._prefix.active(true);
Akron6ed13992016-05-23 18:06:05 +0200515
Akrone4961b12017-05-10 21:04:46 +0200516 // finally show the element
517 this._element.classList.add('visible');
518
519 return true;
Akron6ed13992016-05-23 18:06:05 +0200520 };
521
522 var offset = 0;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000523
Akron9c2f9382016-05-25 16:36:04 +0200524 // Set a chosen value to active and move the viewport
Akron6ed13992016-05-23 18:06:05 +0200525 if (arguments.length === 1) {
526
Akrone4961b12017-05-10 21:04:46 +0200527 // Normalize active value
528 if (active < 0) {
529 active = 0;
530 }
531 else if (active >= this.liveLength()) {
532 active = this.liveLength() - 1;
533 };
Akron6ed13992016-05-23 18:06:05 +0200534
Akrone4961b12017-05-10 21:04:46 +0200535 // Item is outside the first viewport
536 if (active >= this._limit) {
537 offset = active;
538 if (offset > (this.liveLength() - this._limit)) {
539 offset = this.liveLength() - this._limit;
540 };
541 };
542
543 this.position = active;
Akron6ed13992016-05-23 18:06:05 +0200544 }
545
Akron9c2f9382016-05-25 16:36:04 +0200546 // Choose the first item
Akron6ed13992016-05-23 18:06:05 +0200547 else if (this._firstActive) {
Akrone4961b12017-05-10 21:04:46 +0200548 this.position = 0;
Akron47c086c2016-05-18 21:22:06 +0200549 }
Akroncb351d62016-05-19 23:10:33 +0200550
Akron9c2f9382016-05-25 16:36:04 +0200551 // Choose no item
Akron47c086c2016-05-18 21:22:06 +0200552 else {
Akrone4961b12017-05-10 21:04:46 +0200553 this.position = -1;
Akron6ed13992016-05-23 18:06:05 +0200554 };
555
Akron9c4d1ae2016-05-25 21:43:22 +0200556 this.offset = offset;
Akron6ed13992016-05-23 18:06:05 +0200557 this._showItems(offset); // Show new item list
558
559 // Make chosen value active
560 if (this.position !== -1) {
Akrone4961b12017-05-10 21:04:46 +0200561 this.liveItem(this.position).active(true);
Akron6ed13992016-05-23 18:06:05 +0200562 };
Akron37513a62015-11-17 01:07:11 +0100563
Akron5240b8c2016-05-20 09:17:41 +0200564 // The prefix is not active
Nils Diewalde8518f82015-03-18 22:41:49 +0000565 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000566
Akron5240b8c2016-05-20 09:17:41 +0200567 // finally show the element
Akron6bb71582016-06-10 20:41:08 +0200568 this._element.classList.add('visible');
Nils Diewald2fe12e12015-03-06 16:47:06 +0000569
Nils Diewald86dad5b2015-01-28 15:09:07 +0000570 // Add classes for rolling menus
571 this._boundary(true);
Akron6bb71582016-06-10 20:41:08 +0200572
Nils Diewald59c02fc2015-03-07 01:29:09 +0000573 return true;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000574 },
575
Nils Diewald7148c6f2015-05-04 15:07:53 +0000576
577 /**
578 * Hide the menu and call the onHide callback.
579 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000580 hide : function () {
hebastaf95226b2019-09-19 11:37:00 +0200581 if(!this.dontHide){
582 this.removeItems();
583 this._prefix.clear();
584 this.onHide();
585 this._element.classList.remove('visible');
586 }
587 /* this._element.blur(); */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000588 },
589
Nils Diewald7148c6f2015-05-04 15:07:53 +0000590 /**
591 * Function released when the menu hides.
592 * This method is expected to be overridden.
593 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000594 onHide : function () {},
595
Nils Diewald86dad5b2015-01-28 15:09:07 +0000596 /**
597 * Get the prefix for filtering,
Nils Diewald2fe12e12015-03-06 16:47:06 +0000598 * e.g. &quot;ve&quot; for &quot;verb&quot;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000599 */
Nils Diewald5975d702015-03-09 17:45:42 +0000600 prefix : function (pref) {
601 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200602 this._prefix.value(pref);
603 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000604 };
605 return this._prefix.value();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000606 },
607
Akronc7448732016-04-27 14:06:58 +0200608 /**
609 * Get the lengthField object.
610 */
611 lengthField : function () {
612 return this._lengthField;
613 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000614
Akron5240b8c2016-05-20 09:17:41 +0200615 /**
616 * Get the associated slider object.
617 */
618 slider : function () {
619 return this._slider;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000620 },
621
Akron5240b8c2016-05-20 09:17:41 +0200622
Nils Diewald86dad5b2015-01-28 15:09:07 +0000623 /**
624 * Delete all visible items from the menu element
625 */
Akrona92fd8d2016-05-24 21:13:41 +0200626 removeItems : function () {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000627 var child;
Nils Diewald2fe12e12015-03-06 16:47:06 +0000628
Nils Diewald2fe12e12015-03-06 16:47:06 +0000629 // Remove all children
Nils Diewald5975d702015-03-09 17:45:42 +0000630 var children = this._element.childNodes;
Akronc7448732016-04-27 14:06:58 +0200631 // Leave the prefix and lengthField
Akron9905e2a2016-05-10 16:06:44 +0200632 for (var i = children.length - 1; i >= 3; i--) {
Akrone4961b12017-05-10 21:04:46 +0200633 this._element.removeChild(
634 children[i]
635 );
Nils Diewald5975d702015-03-09 17:45:42 +0000636 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000637 },
638
Nils Diewald2fe12e12015-03-06 16:47:06 +0000639 /**
640 * Get a specific item from the complete list
641 *
642 * @param {number} index of the list item
643 */
644 item : function (index) {
645 return this._items[index]
646 },
647
648
Nils Diewald86dad5b2015-01-28 15:09:07 +0000649 /**
650 * Get a specific item from the filtered list
651 *
652 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000653 * in the filtered list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000654 */
655 liveItem : function (index) {
656 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200657 if (!this._initList())
658 return;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000659
660 return this._items[this._list[index]];
661 },
662
Nils Diewald86dad5b2015-01-28 15:09:07 +0000663
664 /**
Akron5240b8c2016-05-20 09:17:41 +0200665 * Get a specific item from the viewport list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000666 *
667 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000668 * in the visible list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000669 */
670 shownItem : function (index) {
671 if (index >= this.limit())
Akrone4961b12017-05-10 21:04:46 +0200672 return;
Akron9c4d1ae2016-05-25 21:43:22 +0200673 return this.liveItem(this.offset + index);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000674 },
675
676
Nils Diewald2fe12e12015-03-06 16:47:06 +0000677 /**
Akron9c4d1ae2016-05-25 21:43:22 +0200678 * Get the length of the full item list
Nils Diewald2fe12e12015-03-06 16:47:06 +0000679 */
680 length : function () {
681 return this._items.length;
682 },
683
684
685 /**
Akron5240b8c2016-05-20 09:17:41 +0200686 * Length of the filtered item list.
687 */
688 liveLength : function () {
689 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200690 this._initList();
Akron5240b8c2016-05-20 09:17:41 +0200691 return this._list.length;
692 },
693
Akrone817b882018-08-31 14:09:17 +0200694
Akron5240b8c2016-05-20 09:17:41 +0200695 /**
Nils Diewald2fe12e12015-03-06 16:47:06 +0000696 * Make the next item in the filtered menu active
Nils Diewald86dad5b2015-01-28 15:09:07 +0000697 */
698 next : function () {
Nils Diewald5975d702015-03-09 17:45:42 +0000699
Akronb38afb22016-05-25 19:30:01 +0200700 // No list
701 if (this.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200702 return;
Akronb38afb22016-05-25 19:30:01 +0200703
Akron9c4d1ae2016-05-25 21:43:22 +0200704 // Deactivate old item
705 if (this.position !== -1 && !this._prefix.active()) {
Akrone4961b12017-05-10 21:04:46 +0200706 this.liveItem(this.position).active(false);
Akron9c4d1ae2016-05-25 21:43:22 +0200707 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000708
Akron9c4d1ae2016-05-25 21:43:22 +0200709 // Get new active item
710 this.position++;
711 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000712
Nils Diewald5975d702015-03-09 17:45:42 +0000713 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000714 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000715
Akrone4961b12017-05-10 21:04:46 +0200716 // Activate prefix
717 var prefix = this._prefix;
Nils Diewald5975d702015-03-09 17:45:42 +0000718
Akrone4961b12017-05-10 21:04:46 +0200719 // Prefix is set and not active - choose!
720 if (prefix.isSet() && !prefix.active()) {
721 this.position--;
722 prefix.active(true);
723 return;
724 }
Akron9c4d1ae2016-05-25 21:43:22 +0200725
Akrone4961b12017-05-10 21:04:46 +0200726 // Choose first item
727 else {
728 newItem = this.liveItem(0);
729 // choose first item
730 this.position = 0;
731 this._showItems(0);
732 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000733 }
734
Akron5a1f5bb2016-05-23 22:00:39 +0200735 // The next element is after the viewport - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200736 else if (this.position >= (this.limit() + this.offset)) {
Akrone4961b12017-05-10 21:04:46 +0200737 this.screen(this.position - this.limit() + 1);
Akron5a1f5bb2016-05-23 22:00:39 +0200738 }
739
740 // The next element is before the viewport - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200741 else if (this.position <= this.offset) {
Akrone4961b12017-05-10 21:04:46 +0200742 this.screen(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000743 };
Nils Diewald5975d702015-03-09 17:45:42 +0000744
745 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000746 newItem.active(true);
747 },
748
Nils Diewalde8518f82015-03-18 22:41:49 +0000749 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000750 * Make the previous item in the menu active
751 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000752 prev : function () {
Nils Diewald2d210752015-03-09 19:01:15 +0000753
Akronb38afb22016-05-25 19:30:01 +0200754 // No list
755 if (this.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200756 return;
Akronb38afb22016-05-25 19:30:01 +0200757
Akron9c4d1ae2016-05-25 21:43:22 +0200758 // Deactivate old item
Nils Diewald2d210752015-03-09 19:01:15 +0000759 if (!this._prefix.active()) {
Akron9c4d1ae2016-05-25 21:43:22 +0200760
Akrone4961b12017-05-10 21:04:46 +0200761 // No active element set
762 if (this.position === -1) {
763 this.position = this.liveLength();
764 }
Akron9c4d1ae2016-05-25 21:43:22 +0200765
Akrone4961b12017-05-10 21:04:46 +0200766 // No active element set
767 else {
768 this.liveItem(this.position--).active(false);
769 };
Nils Diewald2d210752015-03-09 19:01:15 +0000770 };
771
Akron9c4d1ae2016-05-25 21:43:22 +0200772 // Get new active item
773 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000774
775 // The previous element is undefined - roll to bottom
776 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000777
Akrone4961b12017-05-10 21:04:46 +0200778 // Activate prefix
779 var prefix = this._prefix;
780 var offset = this.liveLength() - this.limit();
781
782 // Normalize offset
783 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000784
Akrone4961b12017-05-10 21:04:46 +0200785 // Choose the last item
786 this.position = this.liveLength() - 1;
787
788 // Prefix is set and not active - choose!
789 if (prefix.isSet() && !prefix.active()) {
790 this.position++;
791 prefix.active(true);
792 this.offset = offset;
793 return;
794 }
Nils Diewald2d210752015-03-09 19:01:15 +0000795
Akrone4961b12017-05-10 21:04:46 +0200796 // Choose last item
797 else {
798 newItem = this.liveItem(this.position);
799 this._showItems(offset);
800 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000801 }
802
Akron5a1f5bb2016-05-23 22:00:39 +0200803 // The previous element is before the view - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200804 else if (this.position < this.offset) {
Akrone4961b12017-05-10 21:04:46 +0200805 this.screen(this.position);
Akron5a1f5bb2016-05-23 22:00:39 +0200806 }
807
808 // The previous element is after the view - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200809 else if (this.position >= (this.limit() + this.offset)) {
Akrone4961b12017-05-10 21:04:46 +0200810 this.screen(this.position - this.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000811 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000812
Nils Diewald5975d702015-03-09 17:45:42 +0000813 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000814 newItem.active(true);
815 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000816
Akron3c2730f2016-05-24 15:08:29 +0200817 /**
818 * Move the page up by limit!
819 */
820 pageUp : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200821 this.screen(this.offset - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200822 },
823
824
825 /**
826 * Move the page down by limit!
827 */
828 pageDown : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200829 this.screen(this.offset + this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200830 },
831
Nils Diewald86dad5b2015-01-28 15:09:07 +0000832
Akrone817b882018-08-31 14:09:17 +0200833 /**
834 * Move the view one item up
835 */
836 viewUp : function () {
837 this.screen(this.offset - 1);
838 },
839
840
841 /**
842 * Move the view one item down
843 */
844 viewDown : function () {
845 this.screen(this.offset + 1);
846 },
847
Akron5240b8c2016-05-20 09:17:41 +0200848 // Unmark all items
849 _unmark : function () {
Akron678c26f2020-10-09 08:52:50 +0200850 this._list.forEach(function(it){
851 var item = this._items[it];
Akrone4961b12017-05-10 21:04:46 +0200852 item.lowlight();
Akron678c26f2020-10-09 08:52:50 +0200853 item.active(false);
854 }, this);
Akron5240b8c2016-05-20 09:17:41 +0200855 },
856
Akron5240b8c2016-05-20 09:17:41 +0200857 // Set boundary for viewport
858 _boundary : function (bool) {
Akron55a343b2018-04-06 19:57:36 +0200859 if (this._list.length === 0)
860 return;
Akron5240b8c2016-05-20 09:17:41 +0200861 this.item(this._list[0]).noMore(bool);
862 this.item(this._list[this._list.length - 1]).noMore(bool);
863 },
864
865
866 // Append Items that should be shown
867 _showItems : function (off) {
868
Akrona92fd8d2016-05-24 21:13:41 +0200869 // optimization: scroll down one step
Akron9c4d1ae2016-05-25 21:43:22 +0200870 if (this.offset === (off - 1)) {
Akrone4961b12017-05-10 21:04:46 +0200871 this.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200872
Akrone4961b12017-05-10 21:04:46 +0200873 // Remove the HTML node from the first item
874 // leave lengthField/prefix/slider
875 this._element.removeChild(this._element.children[3]);
876 var pos = this.offset + this.limit() - 1;
877 this._append(this._list[pos]);
Akrona92fd8d2016-05-24 21:13:41 +0200878 }
Akron5240b8c2016-05-20 09:17:41 +0200879
Akrona92fd8d2016-05-24 21:13:41 +0200880 // optimization: scroll up one step
Akron9c4d1ae2016-05-25 21:43:22 +0200881 else if (this.offset === (off + 1)) {
Akrone4961b12017-05-10 21:04:46 +0200882 this.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200883
Akrone4961b12017-05-10 21:04:46 +0200884 // Remove the HTML node from the last item
885 this._element.removeChild(this._element.lastChild);
Akron9c4d1ae2016-05-25 21:43:22 +0200886
Akrone4961b12017-05-10 21:04:46 +0200887 this._prepend(this._list[this.offset]);
Akrona92fd8d2016-05-24 21:13:41 +0200888 }
889 else {
Akrone4961b12017-05-10 21:04:46 +0200890 this.offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200891
Akrone4961b12017-05-10 21:04:46 +0200892 // Remove all items
893 this.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200894
Akrone4961b12017-05-10 21:04:46 +0200895 // Use list
896 var shown = 0;
897 var i;
Akron5240b8c2016-05-20 09:17:41 +0200898
Akron678c26f2020-10-09 08:52:50 +0200899 for (let i = 0; i < this._list.length; i++) {
Akrona92fd8d2016-05-24 21:13:41 +0200900
Akrone4961b12017-05-10 21:04:46 +0200901 // Don't show - it's before offset
902 shown++;
903 if (shown <= off)
904 continue;
Akrona92fd8d2016-05-24 21:13:41 +0200905
Akrone4961b12017-05-10 21:04:46 +0200906 var itemNr = this._list[i];
907 var item = this.item(itemNr);
908 this._append(itemNr);
909
910 if (shown >= (this.limit() + off))
911 break;
912 };
Akron5240b8c2016-05-20 09:17:41 +0200913 };
914
915 // set the slider to the new offset
Akron9c4d1ae2016-05-25 21:43:22 +0200916 this._slider.offset(this.offset);
Akron5240b8c2016-05-20 09:17:41 +0200917 },
918
919
920 // Append item to the shown list based on index
921 _append : function (i) {
922 var item = this.item(i);
923
924 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200925 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200926 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200927 };
928
Akron5240b8c2016-05-20 09:17:41 +0200929
930 // Append element
931 this.element().appendChild(item.element());
932 },
933
934
935 // Prepend item to the shown list based on index
936 _prepend : function (i) {
937 var item = this.item(i);
938
939 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200940 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200941 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200942 };
Akron5240b8c2016-05-20 09:17:41 +0200943
944 var e = this.element();
Akron9c4d1ae2016-05-25 21:43:22 +0200945
Akron5240b8c2016-05-20 09:17:41 +0200946 // Append element after lengthField/prefix/slider
947 e.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200948 item.element(),
949 e.children[3]
Akron5240b8c2016-05-20 09:17:41 +0200950 );
Nils Diewald2fe12e12015-03-06 16:47:06 +0000951 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000952 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000953});