blob: dc998d1f746c1661bda9b0e0e46173ab18c47cec [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 */
Akrone51eaa32020-11-10 09:35:53 +010017
18"use strict";
Nils Diewald0e6992a2015-04-14 20:13:52 +000019define([
20 'menu/item',
21 'menu/prefix',
Akronc7448732016-04-27 14:06:58 +020022 'menu/lengthField',
Akron9905e2a2016-05-10 16:06:44 +020023 'menu/slider',
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 'util'
25], function (defaultItemClass,
Akrone4961b12017-05-10 21:04:46 +020026 defaultPrefixClass,
27 defaultLengthFieldClass,
28 sliderClass) {
Nils Diewaldfda29d92015-01-22 17:28:01 +000029
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 // Default maximum number of menu items
Akronc53cfc82020-10-19 11:00:58 +020031 const menuLimit = 8;
Nils Diewald0e6992a2015-04-14 20:13:52 +000032
Nils Diewald86dad5b2015-01-28 15:09:07 +000033 /**
34 * List of items for drop down menu (complete).
35 * Only a sublist of the menu is filtered (live).
36 * Only a sublist of the filtered menu is visible (shown).
37 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 return {
Nils Diewald86dad5b2015-01-28 15:09:07 +000039 /**
40 * Create new Menu based on the action prefix
41 * and a list of menu items.
42 *
Akron7524be12016-06-01 17:31:33 +020043 *
44 * Accepts an associative array containg the elements
45 * itemClass, prefixClass, lengthFieldClass
46 *
Nils Diewald86dad5b2015-01-28 15:09:07 +000047 * @this {Menu}
48 * @constructor
49 * @param {string} Context prefix
50 * @param {Array.<Array.<string>>} List of menu items
51 */
Akron7524be12016-06-01 17:31:33 +020052 create : function (list, params) {
53 return Object.create(this)._init(list, params);
Nils Diewald86dad5b2015-01-28 15:09:07 +000054 },
55
Akron5240b8c2016-05-20 09:17:41 +020056 // Initialize list
Akron7524be12016-06-01 17:31:33 +020057 _init : function (list, params) {
Akron7524be12016-06-01 17:31:33 +020058 if (params === undefined)
Akrone4961b12017-05-10 21:04:46 +020059 params = {};
Akron7524be12016-06-01 17:31:33 +020060
Akronc53cfc82020-10-19 11:00:58 +020061 const t = this;
Leo Repp56904d22021-04-26 15:53:22 +020062 t._notItemElements=3;
Akronc53cfc82020-10-19 11:00:58 +020063
64 t._itemClass = params["itemClass"] || defaultItemClass;
Akron5240b8c2016-05-20 09:17:41 +020065
66 // Add prefix object
Akron7524be12016-06-01 17:31:33 +020067 if (params["prefixClass"] !== undefined) {
Akronc53cfc82020-10-19 11:00:58 +020068 t._prefix = params["prefixClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020069 }
70 else {
Akronc53cfc82020-10-19 11:00:58 +020071 t._prefix = defaultPrefixClass.create();
Akron5240b8c2016-05-20 09:17:41 +020072 };
Akronc53cfc82020-10-19 11:00:58 +020073 t._prefix._menu = t;
Akron5240b8c2016-05-20 09:17:41 +020074
75 // Add lengthField object
Akron7524be12016-06-01 17:31:33 +020076 if (params["lengthFieldClass"] !== undefined) {
Akronc53cfc82020-10-19 11:00:58 +020077 t._lengthField = params["lengthFieldClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020078 }
79 else {
Akronc53cfc82020-10-19 11:00:58 +020080 t._lengthField = defaultLengthFieldClass.create();
Akron5240b8c2016-05-20 09:17:41 +020081 };
Akronc53cfc82020-10-19 11:00:58 +020082 t._lengthField._menu = t;
Akron5240b8c2016-05-20 09:17:41 +020083
84 // Initialize slider
Akronc53cfc82020-10-19 11:00:58 +020085 t._slider = sliderClass.create(t);
Akron5240b8c2016-05-20 09:17:41 +020086
87 // Create the element
Akron9c4d1ae2016-05-25 21:43:22 +020088 var el = document.createElement("ul");
Akronc53cfc82020-10-19 11:00:58 +020089 el.style.outline = 0;
90 el.setAttribute('tabindex', 0);
91 el.classList.add('menu', 'roll');
92 el.appendChild(t._prefix.element());
93 el.appendChild(t._lengthField.element());
94 el.appendChild(t._slider.element());
Akron5240b8c2016-05-20 09:17:41 +020095
96 // This has to be cleaned up later on
Akronc53cfc82020-10-19 11:00:58 +020097 el["menu"] = t;
Akron5240b8c2016-05-20 09:17:41 +020098
99 // Arrow keys
Akron9c4d1ae2016-05-25 21:43:22 +0200100 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200101 'keydown',
Akronc53cfc82020-10-19 11:00:58 +0200102 t._keydown.bind(t),
Akrone4961b12017-05-10 21:04:46 +0200103 false
Akron5240b8c2016-05-20 09:17:41 +0200104 );
105
106 // Strings
Akron9c4d1ae2016-05-25 21:43:22 +0200107 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200108 'keypress',
Akronc53cfc82020-10-19 11:00:58 +0200109 t._keypress.bind(t),
Akrone4961b12017-05-10 21:04:46 +0200110 false
Akron5240b8c2016-05-20 09:17:41 +0200111 );
112
113 // Mousewheel
Akron9c4d1ae2016-05-25 21:43:22 +0200114 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200115 'wheel',
Akronc53cfc82020-10-19 11:00:58 +0200116 t._mousewheel.bind(t),
Akrone4961b12017-05-10 21:04:46 +0200117 false
Akron5240b8c2016-05-20 09:17:41 +0200118 );
Akrona1159ff2018-07-22 13:28:31 +0200119
Akronc53cfc82020-10-19 11:00:58 +0200120 // Touch events
121 ['touchstart', 'touchend', 'touchmove'].forEach(
122 e => el.addEventListener(e, t._touch.bind(t), false)
Akrona1159ff2018-07-22 13:28:31 +0200123 );
124
125
Akron24aa0052020-11-10 11:00:34 +0100126 t._el = el;
Akroneaba63e2018-01-26 19:49:30 +0100127
Akronc53cfc82020-10-19 11:00:58 +0200128 t._limit = menuLimit;
Akrone4961b12017-05-10 21:04:46 +0200129
Leo Repp56904d22021-04-26 15:53:22 +0200130 t._items = new Array(); //all childNodes, i.e. ItemClass, prefixClass
Akron5240b8c2016-05-20 09:17:41 +0200131
Akronc53cfc82020-10-19 11:00:58 +0200132 t.readItems(list);
Akroneaba63e2018-01-26 19:49:30 +0100133
Akronc53cfc82020-10-19 11:00:58 +0200134 t.dontHide = false;
hebastaf95226b2019-09-19 11:37:00 +0200135
Akronc53cfc82020-10-19 11:00:58 +0200136 return t;
Akroneaba63e2018-01-26 19:49:30 +0100137 },
138
139 // Read items to add to list
140 readItems : function (list) {
Akronc53cfc82020-10-19 11:00:58 +0200141 const t = this;
Akroneaba63e2018-01-26 19:49:30 +0100142
Leo Repp56904d22021-04-26 15:53:22 +0200143 t._list = undefined; //filtered List containing all itemClass items
Akroneaba63e2018-01-26 19:49:30 +0100144
145 // Remove circular reference to "this" in items
Akronc53cfc82020-10-19 11:00:58 +0200146 for (let i = 0; i < t._items.length; i++) {
147 delete t._items[i]["_menu"];
148 delete t._items[i];
Akroneaba63e2018-01-26 19:49:30 +0100149 };
150
Akronc53cfc82020-10-19 11:00:58 +0200151 t._items = new Array();
152 t.removeItems();
Akroneaba63e2018-01-26 19:49:30 +0100153
154
155 // Initialize items
Akronc53cfc82020-10-19 11:00:58 +0200156 t._lengthField.reset();
Akroneaba63e2018-01-26 19:49:30 +0100157
Akron5240b8c2016-05-20 09:17:41 +0200158 // Initialize item list based on parameters
Akron678c26f2020-10-09 08:52:50 +0200159 list.forEach(function(i){
Akronc53cfc82020-10-19 11:00:58 +0200160 const obj = this._itemClass.create(i);
Akron5240b8c2016-05-20 09:17:41 +0200161
Akrone4961b12017-05-10 21:04:46 +0200162 // This may become circular
163 obj["_menu"] = this;
Akron678c26f2020-10-09 08:52:50 +0200164 this._lengthField.add(i);
Akrone4961b12017-05-10 21:04:46 +0200165 this._items.push(obj);
Akronc53cfc82020-10-19 11:00:58 +0200166 }, t);
Akron5240b8c2016-05-20 09:17:41 +0200167
Akronc53cfc82020-10-19 11:00:58 +0200168 t._slider.length(t.liveLength())
169 .limit(t._limit)
Akrone4961b12017-05-10 21:04:46 +0200170 .reInit();
171
Akronc53cfc82020-10-19 11:00:58 +0200172 t._firstActive = false;
Akroneaba63e2018-01-26 19:49:30 +0100173 // Show the first item active always?
Akronc53cfc82020-10-19 11:00:58 +0200174 t.offset = 0;
175 t.position = 0;
Akron5240b8c2016-05-20 09:17:41 +0200176 },
Akron8aa1c522021-07-23 11:33:49 +0200177
178 // Append item to list
179 append : function (item) {
180 const t = this;
181 // This is cyclic!
182 item["_menu"] = t;
183 t._list = undefined;
184 t.removeItems();
185 t._items.push(item);
186 t._lengthField.add([item.content().data]);
187 t._slider.length(t.liveLength()).reInit();
188 t._firstActive = false;
189 t.offset = 0;
190 t.position = 0;
191 },
Akroneaba63e2018-01-26 19:49:30 +0100192
Akron5240b8c2016-05-20 09:17:41 +0200193 // Initialize the item list
Leo Repp84539162021-10-25 12:06:07 +0200194 // returns true if the length of the resulting list is at least 1
195 // and there was a prefix value. Returns true if there was no prefix value set.
Akron5240b8c2016-05-20 09:17:41 +0200196 _initList : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200197 // Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200198 const t = this;
Akron5240b8c2016-05-20 09:17:41 +0200199
200 // Create a new list
Akronc53cfc82020-10-19 11:00:58 +0200201 if (t._list === undefined) {
202 t._list = [];
Akron5240b8c2016-05-20 09:17:41 +0200203 }
Akronc53cfc82020-10-19 11:00:58 +0200204 else if (t._list.length !== 0) {
205 t._boundary(false);
206 t._list.length = 0;
Akron5240b8c2016-05-20 09:17:41 +0200207 };
208
209 // Offset is initially zero
Akronc53cfc82020-10-19 11:00:58 +0200210 t.offset = 0;
Akron5240b8c2016-05-20 09:17:41 +0200211
212 // There is no prefix set
Akronc53cfc82020-10-19 11:00:58 +0200213 if (t.prefix().length <= 0) {
Akron5240b8c2016-05-20 09:17:41 +0200214
Akrone4961b12017-05-10 21:04:46 +0200215 // add all items to the list and lowlight
Akronb50964a2020-10-12 11:44:37 +0200216 let i = 0;
Akronc53cfc82020-10-19 11:00:58 +0200217 for (; i < t._items.length; i++) {
218 t._list.push(i);
219 t._items[i].lowlight();
Akrone4961b12017-05-10 21:04:46 +0200220 };
Akron5240b8c2016-05-20 09:17:41 +0200221
Akronc53cfc82020-10-19 11:00:58 +0200222 t._slider.length(i).reInit();
Akron97752a72016-05-25 14:43:07 +0200223
Akrone4961b12017-05-10 21:04:46 +0200224 return true;
Akron5240b8c2016-05-20 09:17:41 +0200225 };
226
227 /*
228 * There is a prefix set, so filter the list!
229 */
Akronc53cfc82020-10-19 11:00:58 +0200230 let pos;
231 const prefixList = t.prefix().toLowerCase().split(" ");
Akronacffc652017-12-18 21:21:25 +0100232
Akronc53cfc82020-10-19 11:00:58 +0200233 const items = [];
234 let maxPoints = 1; // minimum 1
Akron5240b8c2016-05-20 09:17:41 +0200235
236 // Iterate over all items and choose preferred matching items
237 // i.e. the matching happens at the word start
Akronc53cfc82020-10-19 11:00:58 +0200238 t._items.forEach(function(it, pos){
Akronacffc652017-12-18 21:21:25 +0100239
Akronb50964a2020-10-12 11:44:37 +0200240 let points = 0;
Akronacffc652017-12-18 21:21:25 +0100241
Akronb50964a2020-10-12 11:44:37 +0200242 prefixList.forEach(function(p) {
Akronacffc652017-12-18 21:21:25 +0100243
244 // Check if it matches at the beginning
Akronb50964a2020-10-12 11:44:37 +0200245 if ((it.lcField().includes(" " + p))) {
Akronacffc652017-12-18 21:21:25 +0100246 points += 5;
247 }
248
249 // Check if it matches anywhere
Akronb50964a2020-10-12 11:44:37 +0200250 else if (it.lcField().includes(p)) {
Akronacffc652017-12-18 21:21:25 +0100251 points += 1;
252 };
Akronb50964a2020-10-12 11:44:37 +0200253 });
Akronacffc652017-12-18 21:21:25 +0100254
255 if (points > maxPoints) {
256 this._list = [pos];
257 maxPoints = points;
258 }
259 else if (points == maxPoints) {
Akrone4961b12017-05-10 21:04:46 +0200260 this._list.push(pos);
Akronacffc652017-12-18 21:21:25 +0100261 }
Akronc53cfc82020-10-19 11:00:58 +0200262 }, t);
Akron5240b8c2016-05-20 09:17:41 +0200263
Akronc53cfc82020-10-19 11:00:58 +0200264 t._slider.length(t._list.length).reInit();
Akron6ed13992016-05-23 18:06:05 +0200265
Akron5240b8c2016-05-20 09:17:41 +0200266 // Filter was successful - yeah!
Akronc53cfc82020-10-19 11:00:58 +0200267 return t._list.length > 0 ? true : false;
Akron5240b8c2016-05-20 09:17:41 +0200268 },
269
270
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000271 /**
272 * Destroy this menu
273 * (in case you don't trust the
274 * mark and sweep GC)!
275 */
276 destroy : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200277 // Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200278 const t = this;
Akron47c086c2016-05-18 21:22:06 +0200279
Akron5240b8c2016-05-20 09:17:41 +0200280 // Remove circular reference to "this" in menu
Akron24aa0052020-11-10 11:00:34 +0100281 if (t._el != undefined)
282 delete t._el["menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000283
Akron5240b8c2016-05-20 09:17:41 +0200284 // Remove circular reference to "this" in items
Akronc53cfc82020-10-19 11:00:58 +0200285 t._items.forEach(function(i) {
Akron678c26f2020-10-09 08:52:50 +0200286 delete i["_menu"];
287 });
Akron5240b8c2016-05-20 09:17:41 +0200288
289 // Remove circular reference to "this" in prefix
Akronc53cfc82020-10-19 11:00:58 +0200290 delete t._prefix['_menu'];
291 delete t._lengthField['_menu'];
292 delete t._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000293 },
294
Nils Diewald7148c6f2015-05-04 15:07:53 +0000295
296 /**
297 * Focus on this menu.
298 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000299 focus : function () {
Akron24aa0052020-11-10 11:00:34 +0100300 this._el.focus();
Nils Diewald2fe12e12015-03-06 16:47:06 +0000301 },
302
Nils Diewald7148c6f2015-05-04 15:07:53 +0000303
Nils Diewald59c02fc2015-03-07 01:29:09 +0000304 // mouse wheel treatment
305 _mousewheel : function (e) {
Akronc53cfc82020-10-19 11:00:58 +0200306 const delta = e.deltaY / 120;
Nils Diewald5975d702015-03-09 17:45:42 +0000307 if (delta > 0)
Akrone4961b12017-05-10 21:04:46 +0200308 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000309 else if (delta < 0)
Akrone4961b12017-05-10 21:04:46 +0200310 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000311 e.halt();
312 },
313
Akronc53cfc82020-10-19 11:00:58 +0200314
Akrona1159ff2018-07-22 13:28:31 +0200315 // touchmove treatment
316 _touch : function (e) {
Akronc53cfc82020-10-19 11:00:58 +0200317 const s = this.slider();
318
Akrona1159ff2018-07-22 13:28:31 +0200319 if (e.type === "touchstart") {
Akronc53cfc82020-10-19 11:00:58 +0200320 this._lastTouch = e.touches[0].clientY;
Akrona1159ff2018-07-22 13:28:31 +0200321 }
322 else if (e.type === "touchend") {
Akrona1159ff2018-07-22 13:28:31 +0200323 this._lastTouch = undefined;
Akrona1159ff2018-07-22 13:28:31 +0200324 }
325 else if (e.type === "touchmove") {
Akronc53cfc82020-10-19 11:00:58 +0200326 const to = e.touches[0];
Akrone817b882018-08-31 14:09:17 +0200327
328 // TODO:
329 // Instead of using 26px, choose the item height
330 // or use the menu height // shownItems
331
Akrona1159ff2018-07-22 13:28:31 +0200332 // s.movetoRel(t.clientY - this._initTouch);
Akronc53cfc82020-10-19 11:00:58 +0200333 if ((this._lastTouch + 26) < to.clientY) {
Akrone817b882018-08-31 14:09:17 +0200334 this.viewDown();
Akronc53cfc82020-10-19 11:00:58 +0200335 this._lastTouch = to.clientY;
Akrona1159ff2018-07-22 13:28:31 +0200336 }
Akronc53cfc82020-10-19 11:00:58 +0200337 else if ((this._lastTouch - 26) > to.clientY) {
Akrone817b882018-08-31 14:09:17 +0200338 this.viewUp();
Akronc53cfc82020-10-19 11:00:58 +0200339 this._lastTouch = to.clientY;
Akrona1159ff2018-07-22 13:28:31 +0200340 }
341 e.halt();
342 };
343 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000344
Nils Diewald59c02fc2015-03-07 01:29:09 +0000345 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000346 _keydown : function (e) {
Leo Repp56904d22021-04-26 15:53:22 +0200347 //Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200348 const t = this;
Nils Diewald59c02fc2015-03-07 01:29:09 +0000349
Akronc53cfc82020-10-19 11:00:58 +0200350 switch (_codeFromEvent(e)) {
351
Nils Diewald59c02fc2015-03-07 01:29:09 +0000352 case 27: // 'Esc'
Akrone4961b12017-05-10 21:04:46 +0200353 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200354 t.hide();
Akrone4961b12017-05-10 21:04:46 +0200355 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000356
Nils Diewald59c02fc2015-03-07 01:29:09 +0000357 case 38: // 'Up'
Akrone4961b12017-05-10 21:04:46 +0200358 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200359 t.prev();
Akrone4961b12017-05-10 21:04:46 +0200360 break;
Akronc53cfc82020-10-19 11:00:58 +0200361
Nils Diewald5975d702015-03-09 17:45:42 +0000362 case 33: // 'Page up'
Akrone4961b12017-05-10 21:04:46 +0200363 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200364 t.pageUp();
Akrone4961b12017-05-10 21:04:46 +0200365 break;
Akronc53cfc82020-10-19 11:00:58 +0200366
Nils Diewald5975d702015-03-09 17:45:42 +0000367 case 40: // 'Down'
Akrone4961b12017-05-10 21:04:46 +0200368 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200369 t.next();
Akrone4961b12017-05-10 21:04:46 +0200370 break;
Akronc53cfc82020-10-19 11:00:58 +0200371
Nils Diewald5975d702015-03-09 17:45:42 +0000372 case 34: // 'Page down'
Akrone4961b12017-05-10 21:04:46 +0200373 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200374 t.pageDown();
Akrone4961b12017-05-10 21:04:46 +0200375 break;
Akronc53cfc82020-10-19 11:00:58 +0200376
Nils Diewald5975d702015-03-09 17:45:42 +0000377 case 39: // 'Right'
Akronc53cfc82020-10-19 11:00:58 +0200378 if (t._prefix.active())
Akrone4961b12017-05-10 21:04:46 +0200379 break;
Nils Diewalde8518f82015-03-18 22:41:49 +0000380
Akronc53cfc82020-10-19 11:00:58 +0200381 const item = t.liveItem(t.position);
Akrone4961b12017-05-10 21:04:46 +0200382
383 if (item["further"] !== undefined) {
384 item["further"].bind(item).apply();
385 };
386
387 e.halt();
388 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000389
Akronc53cfc82020-10-19 11:00:58 +0200390 case 13: // 'Enter'
Akrone4961b12017-05-10 21:04:46 +0200391 // Click on prefix
Akronc53cfc82020-10-19 11:00:58 +0200392 if (t._prefix.active())
393 t._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000394
Akrone4961b12017-05-10 21:04:46 +0200395 // Click on item
396 else
Akronc53cfc82020-10-19 11:00:58 +0200397 t.liveItem(t.position).onclick(e);
Akrone4961b12017-05-10 21:04:46 +0200398 e.halt();
399 break;
Akronc53cfc82020-10-19 11:00:58 +0200400
Nils Diewald59c02fc2015-03-07 01:29:09 +0000401 case 8: // 'Backspace'
Akronc53cfc82020-10-19 11:00:58 +0200402 t._prefix.chop();
403 t.show();
Akrone4961b12017-05-10 21:04:46 +0200404 e.halt();
405 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000406 };
407 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000408
Akronc53cfc82020-10-19 11:00:58 +0200409
Nils Diewald47f366b2015-04-15 20:06:35 +0000410 // Add characters to prefix
411 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200412 if (e.charCode !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200413 e.halt();
Akrone4961b12017-05-10 21:04:46 +0200414
415 // Add prefix
Akronc53cfc82020-10-19 11:00:58 +0200416 this._prefix.add(
417 String.fromCharCode(_codeFromEvent(e))
418 );
419
Akrone4961b12017-05-10 21:04:46 +0200420 this.show();
Akron9c2f9382016-05-25 16:36:04 +0200421 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000422 },
423
Akronc53cfc82020-10-19 11:00:58 +0200424
Akron47c086c2016-05-18 21:22:06 +0200425 /**
Akron5240b8c2016-05-20 09:17:41 +0200426 * Show a screen with a given offset
427 * in the viewport.
Akron47c086c2016-05-18 21:22:06 +0200428 */
429 screen : function (nr) {
Akronc53cfc82020-10-19 11:00:58 +0200430 const t = this;
Akrone817b882018-08-31 14:09:17 +0200431
432 // Normalize negative values
Akron3c2730f2016-05-24 15:08:29 +0200433 if (nr < 0) {
Akrone4961b12017-05-10 21:04:46 +0200434 nr = 0
Akron3c2730f2016-05-24 15:08:29 +0200435 }
Akrone817b882018-08-31 14:09:17 +0200436
437 // The shown list already shows everything
Akronc53cfc82020-10-19 11:00:58 +0200438 else if (t.liveLength() < t.limit()) {
Akrone817b882018-08-31 14:09:17 +0200439 return false;
440 }
441
442 // Move relatively to the next screen
Akronc53cfc82020-10-19 11:00:58 +0200443 else if (nr > (t.liveLength() - t.limit())) {
444 nr = (t.liveLength() - t.limit());
Akron3c2730f2016-05-24 15:08:29 +0200445 };
446
Akrone817b882018-08-31 14:09:17 +0200447 // no change
Akronc53cfc82020-10-19 11:00:58 +0200448 if (t.offset === nr)
Akrone817b882018-08-31 14:09:17 +0200449 return false;
Akron5a1f5bb2016-05-23 22:00:39 +0200450
Akronc53cfc82020-10-19 11:00:58 +0200451 t._showItems(nr);
Akrone817b882018-08-31 14:09:17 +0200452
453 return true;
Akron47c086c2016-05-18 21:22:06 +0200454 },
455
Akrone817b882018-08-31 14:09:17 +0200456
Nils Diewald2fe12e12015-03-06 16:47:06 +0000457 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000458 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000459 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000460 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100461 return this._el;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000462 },
463
Akronc53cfc82020-10-19 11:00:58 +0200464
Nils Diewald2fe12e12015-03-06 16:47:06 +0000465 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000466 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000467 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000468 itemClass : function () {
469 return this._itemClass;
470 },
471
Akronc53cfc82020-10-19 11:00:58 +0200472
Nils Diewald86dad5b2015-01-28 15:09:07 +0000473 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000474 * Get and set the numerical value
475 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000476 */
477 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000478 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200479 if (this._limit !== limit) {
480 this._limit = limit;
481 this._slider.limit(limit).reInit();
482 };
483 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000484 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000485 return this._limit;
486 },
487
Nils Diewald7148c6f2015-05-04 15:07:53 +0000488
Nils Diewald86dad5b2015-01-28 15:09:07 +0000489 /**
Akron97752a72016-05-25 14:43:07 +0200490 * Filter the list and make it visible.
491 * This is always called once the prefix changes.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000492 *
493 * @param {string} Prefix for filtering the list
494 */
Akron6ed13992016-05-23 18:06:05 +0200495 show : function (active) {
Leo Repp57997402021-08-18 16:37:52 +0200496 //Upon change please also update alwaysmenu.js and containermenu.js (only two lines new there)
Akronc53cfc82020-10-19 11:00:58 +0200497 const t = this;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000498
Akron5240b8c2016-05-20 09:17:41 +0200499 // show menu based on initial offset
Akronc53cfc82020-10-19 11:00:58 +0200500 t._unmark(); // Unmark everything that was marked before
501 t.removeItems();
Akron6ed13992016-05-23 18:06:05 +0200502
503 // Initialize the list
Akronc53cfc82020-10-19 11:00:58 +0200504 if (!t._initList()) {
Akron6ac58442016-05-24 16:52:29 +0200505
Akrone4961b12017-05-10 21:04:46 +0200506 // The prefix is not active
Akronc53cfc82020-10-19 11:00:58 +0200507 t._prefix.active(true);
Akron6ed13992016-05-23 18:06:05 +0200508
Akrone4961b12017-05-10 21:04:46 +0200509 // finally show the element
Akron24aa0052020-11-10 11:00:34 +0100510 t._el.classList.add('visible');
Akrone4961b12017-05-10 21:04:46 +0200511
512 return true;
Akron6ed13992016-05-23 18:06:05 +0200513 };
514
Akronc53cfc82020-10-19 11:00:58 +0200515 let offset = 0;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000516
Akron9c2f9382016-05-25 16:36:04 +0200517 // Set a chosen value to active and move the viewport
Akron6ed13992016-05-23 18:06:05 +0200518 if (arguments.length === 1) {
519
Akrone4961b12017-05-10 21:04:46 +0200520 // Normalize active value
521 if (active < 0) {
522 active = 0;
523 }
Akronc53cfc82020-10-19 11:00:58 +0200524 else if (active >= t.liveLength()) {
525 active = t.liveLength() - 1;
Akrone4961b12017-05-10 21:04:46 +0200526 };
Akron6ed13992016-05-23 18:06:05 +0200527
Akrone4961b12017-05-10 21:04:46 +0200528 // Item is outside the first viewport
Akronc53cfc82020-10-19 11:00:58 +0200529 if (active >= t._limit) {
Akrone4961b12017-05-10 21:04:46 +0200530 offset = active;
Akronc53cfc82020-10-19 11:00:58 +0200531 const newOffset = t.liveLength() - t._limit;
532 if (offset > newOffset) {
533 offset = newOffset;
Akrone4961b12017-05-10 21:04:46 +0200534 };
535 };
536
Akronc53cfc82020-10-19 11:00:58 +0200537 t.position = active;
Akron6ed13992016-05-23 18:06:05 +0200538 }
539
Akron9c2f9382016-05-25 16:36:04 +0200540 // Choose the first item
Akronc53cfc82020-10-19 11:00:58 +0200541 else if (t._firstActive) {
542 t.position = 0;
Akron47c086c2016-05-18 21:22:06 +0200543 }
Akroncb351d62016-05-19 23:10:33 +0200544
Akron9c2f9382016-05-25 16:36:04 +0200545 // Choose no item
Akron47c086c2016-05-18 21:22:06 +0200546 else {
Akronc53cfc82020-10-19 11:00:58 +0200547 t.position = -1;
Akron6ed13992016-05-23 18:06:05 +0200548 };
549
Akronc53cfc82020-10-19 11:00:58 +0200550 t.offset = offset;
551 t._showItems(offset); // Show new item list
Akron6ed13992016-05-23 18:06:05 +0200552
553 // Make chosen value active
Akronc53cfc82020-10-19 11:00:58 +0200554 if (t.position !== -1) {
555 t.liveItem(t.position).active(true);
Akron6ed13992016-05-23 18:06:05 +0200556 };
Akron37513a62015-11-17 01:07:11 +0100557
Akron5240b8c2016-05-20 09:17:41 +0200558 // The prefix is not active
Akronc53cfc82020-10-19 11:00:58 +0200559 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000560
Akron5240b8c2016-05-20 09:17:41 +0200561 // finally show the element
Akron24aa0052020-11-10 11:00:34 +0100562 t._el.classList.add('visible');
Nils Diewald2fe12e12015-03-06 16:47:06 +0000563
Nils Diewald86dad5b2015-01-28 15:09:07 +0000564 // Add classes for rolling menus
Akronc53cfc82020-10-19 11:00:58 +0200565 t._boundary(true);
Akron6bb71582016-06-10 20:41:08 +0200566
Nils Diewald59c02fc2015-03-07 01:29:09 +0000567 return true;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000568 },
569
Nils Diewald7148c6f2015-05-04 15:07:53 +0000570
571 /**
572 * Hide the menu and call the onHide callback.
573 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000574 hide : function () {
Akronc53cfc82020-10-19 11:00:58 +0200575 if (!this.dontHide) {
576 this.removeItems();
577 this._prefix.clear();
578 this.onHide();
Akron24aa0052020-11-10 11:00:34 +0100579 this._el.classList.remove('visible');
Akronc53cfc82020-10-19 11:00:58 +0200580 }
Akron24aa0052020-11-10 11:00:34 +0100581 // this._el.blur();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000582 },
583
Akronc53cfc82020-10-19 11:00:58 +0200584
Nils Diewald7148c6f2015-05-04 15:07:53 +0000585 /**
586 * Function released when the menu hides.
587 * This method is expected to be overridden.
588 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000589 onHide : function () {},
590
Akronc53cfc82020-10-19 11:00:58 +0200591
Nils Diewald86dad5b2015-01-28 15:09:07 +0000592 /**
593 * Get the prefix for filtering,
Nils Diewald2fe12e12015-03-06 16:47:06 +0000594 * e.g. &quot;ve&quot; for &quot;verb&quot;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000595 */
Nils Diewald5975d702015-03-09 17:45:42 +0000596 prefix : function (pref) {
597 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200598 this._prefix.value(pref);
599 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000600 };
601 return this._prefix.value();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000602 },
603
Akronc53cfc82020-10-19 11:00:58 +0200604
Akronc7448732016-04-27 14:06:58 +0200605 /**
606 * Get the lengthField object.
607 */
608 lengthField : function () {
609 return this._lengthField;
610 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000611
Akronc53cfc82020-10-19 11:00:58 +0200612
Akron5240b8c2016-05-20 09:17:41 +0200613 /**
614 * Get the associated slider object.
615 */
616 slider : function () {
617 return this._slider;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000618 },
619
Akron5240b8c2016-05-20 09:17:41 +0200620
Nils Diewald86dad5b2015-01-28 15:09:07 +0000621 /**
622 * Delete all visible items from the menu element
623 */
Leo Reppe5bd35a2021-05-05 15:28:26 +0200624
Leo Reppafcf9842021-06-30 13:01:09 +0200625 removeItems : function () {
Leo Reppe5bd35a2021-05-05 15:28:26 +0200626 const liElements=this._el.getElementsByTagName("LI");
Leo Reppd162b2e2021-06-30 13:51:07 +0200627 var ignoredCount = 0; //counts how many LI tag elements are not actually direct children
628 while (liElements.length>ignoredCount){
629 if (liElements[ignoredCount].parentNode === this._el){
630 this._el.removeChild(liElements[ignoredCount]);
631 } else {
632 ignoredCount++;
633 }
Nils Diewald5975d702015-03-09 17:45:42 +0000634 };
Leo Reppe5bd35a2021-05-05 15:28:26 +0200635 },
Leo Repp56904d22021-04-26 15:53:22 +0200636
Nils Diewald86dad5b2015-01-28 15:09:07 +0000637
Akronc53cfc82020-10-19 11:00:58 +0200638
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;
Akronc53cfc82020-10-19 11:00:58 +0200673
Akron9c4d1ae2016-05-25 21:43:22 +0200674 return this.liveItem(this.offset + index);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000675 },
676
677
Nils Diewald2fe12e12015-03-06 16:47:06 +0000678 /**
Akron9c4d1ae2016-05-25 21:43:22 +0200679 * Get the length of the full item list
Nils Diewald2fe12e12015-03-06 16:47:06 +0000680 */
681 length : function () {
682 return this._items.length;
683 },
684
685
686 /**
Akron5240b8c2016-05-20 09:17:41 +0200687 * Length of the filtered item list.
688 */
689 liveLength : function () {
690 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200691 this._initList();
Akron5240b8c2016-05-20 09:17:41 +0200692 return this._list.length;
693 },
694
Akrone817b882018-08-31 14:09:17 +0200695
Akron5240b8c2016-05-20 09:17:41 +0200696 /**
Nils Diewald2fe12e12015-03-06 16:47:06 +0000697 * Make the next item in the filtered menu active
Nils Diewald86dad5b2015-01-28 15:09:07 +0000698 */
699 next : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200700 //Upon change please update alwaysmenu.js next
Akronc53cfc82020-10-19 11:00:58 +0200701 const t = this;
Nils Diewald5975d702015-03-09 17:45:42 +0000702
Akronb38afb22016-05-25 19:30:01 +0200703 // No list
Akronc53cfc82020-10-19 11:00:58 +0200704 if (t.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200705 return;
Akronb38afb22016-05-25 19:30:01 +0200706
Akron9c4d1ae2016-05-25 21:43:22 +0200707 // Deactivate old item
Akronc53cfc82020-10-19 11:00:58 +0200708 if (t.position !== -1 && !t._prefix.active()) {
709 t.liveItem(t.position).active(false);
Akron9c4d1ae2016-05-25 21:43:22 +0200710 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000711
Akron9c4d1ae2016-05-25 21:43:22 +0200712 // Get new active item
Akronc53cfc82020-10-19 11:00:58 +0200713 t.position++;
714 let newItem = t.liveItem(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000715
Nils Diewald5975d702015-03-09 17:45:42 +0000716 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000717 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000718
Akrone4961b12017-05-10 21:04:46 +0200719 // Activate prefix
Akronc53cfc82020-10-19 11:00:58 +0200720 const prefix = this._prefix;
Nils Diewald5975d702015-03-09 17:45:42 +0000721
Akrone4961b12017-05-10 21:04:46 +0200722 // Prefix is set and not active - choose!
723 if (prefix.isSet() && !prefix.active()) {
Akronc53cfc82020-10-19 11:00:58 +0200724 t.position--;
Akrone4961b12017-05-10 21:04:46 +0200725 prefix.active(true);
726 return;
727 }
Akron9c4d1ae2016-05-25 21:43:22 +0200728
Akrone4961b12017-05-10 21:04:46 +0200729 // Choose first item
730 else {
Akronc53cfc82020-10-19 11:00:58 +0200731 newItem = t.liveItem(0);
Akrone4961b12017-05-10 21:04:46 +0200732 // choose first item
Akronc53cfc82020-10-19 11:00:58 +0200733 t.position = 0;
734 t._showItems(0);
Akrone4961b12017-05-10 21:04:46 +0200735 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000736 }
737
Akron5a1f5bb2016-05-23 22:00:39 +0200738 // The next element is after the viewport - roll down
Akronc53cfc82020-10-19 11:00:58 +0200739 else if (t.position >= (t.limit() + t.offset)) {
740 t.screen(t.position - t.limit() + 1);
Akron5a1f5bb2016-05-23 22:00:39 +0200741 }
742
743 // The next element is before the viewport - roll up
Akronc53cfc82020-10-19 11:00:58 +0200744 else if (t.position <= t.offset) {
745 t.screen(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000746 };
Nils Diewald5975d702015-03-09 17:45:42 +0000747
Akronc53cfc82020-10-19 11:00:58 +0200748 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000749 newItem.active(true);
750 },
751
Akronc53cfc82020-10-19 11:00:58 +0200752
Nils Diewalde8518f82015-03-18 22:41:49 +0000753 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000754 * Make the previous item in the menu active
755 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000756 prev : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200757 //Upon Change please update alwaysmenu.js prev
Akronc53cfc82020-10-19 11:00:58 +0200758 const t = this;
Nils Diewald2d210752015-03-09 19:01:15 +0000759
Akronb38afb22016-05-25 19:30:01 +0200760 // No list
Akronc53cfc82020-10-19 11:00:58 +0200761 if (t.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200762 return;
Akronb38afb22016-05-25 19:30:01 +0200763
Akron9c4d1ae2016-05-25 21:43:22 +0200764 // Deactivate old item
Akronc53cfc82020-10-19 11:00:58 +0200765 if (!t._prefix.active()) {
Akron9c4d1ae2016-05-25 21:43:22 +0200766
Akrone4961b12017-05-10 21:04:46 +0200767 // No active element set
Akronc53cfc82020-10-19 11:00:58 +0200768 if (t.position === -1) {
769 t.position = t.liveLength();
Akrone4961b12017-05-10 21:04:46 +0200770 }
Akron9c4d1ae2016-05-25 21:43:22 +0200771
Akrone4961b12017-05-10 21:04:46 +0200772 // No active element set
773 else {
Akronc53cfc82020-10-19 11:00:58 +0200774 t.liveItem(t.position--).active(false);
Akrone4961b12017-05-10 21:04:46 +0200775 };
Nils Diewald2d210752015-03-09 19:01:15 +0000776 };
777
Akron9c4d1ae2016-05-25 21:43:22 +0200778 // Get new active item
Akronc53cfc82020-10-19 11:00:58 +0200779 let newItem = t.liveItem(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000780
781 // The previous element is undefined - roll to bottom
782 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000783
Akrone4961b12017-05-10 21:04:46 +0200784 // Activate prefix
Akronc53cfc82020-10-19 11:00:58 +0200785 const prefix = t._prefix;
786 let offset = t.liveLength() - t.limit();
Akrone4961b12017-05-10 21:04:46 +0200787
788 // Normalize offset
789 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000790
Akrone4961b12017-05-10 21:04:46 +0200791 // Choose the last item
Akronc53cfc82020-10-19 11:00:58 +0200792 t.position = t.liveLength() - 1;
Akrone4961b12017-05-10 21:04:46 +0200793
794 // Prefix is set and not active - choose!
795 if (prefix.isSet() && !prefix.active()) {
Akronc53cfc82020-10-19 11:00:58 +0200796 t.position++;
Akrone4961b12017-05-10 21:04:46 +0200797 prefix.active(true);
Akronc53cfc82020-10-19 11:00:58 +0200798 t.offset = offset;
Akrone4961b12017-05-10 21:04:46 +0200799 return;
800 }
Nils Diewald2d210752015-03-09 19:01:15 +0000801
Akrone4961b12017-05-10 21:04:46 +0200802 // Choose last item
803 else {
Akronc53cfc82020-10-19 11:00:58 +0200804 newItem = t.liveItem(t.position);
805 t._showItems(offset);
Akrone4961b12017-05-10 21:04:46 +0200806 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000807 }
808
Akron5a1f5bb2016-05-23 22:00:39 +0200809 // The previous element is before the view - roll up
Akronc53cfc82020-10-19 11:00:58 +0200810 else if (t.position < t.offset) {
811 t.screen(t.position);
Akron5a1f5bb2016-05-23 22:00:39 +0200812 }
813
814 // The previous element is after the view - roll down
Akronc53cfc82020-10-19 11:00:58 +0200815 else if (t.position >= (t.limit() + t.offset)) {
816 t.screen(t.position - t.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000817 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000818
Akronc53cfc82020-10-19 11:00:58 +0200819 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000820 newItem.active(true);
821 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000822
Akronc53cfc82020-10-19 11:00:58 +0200823
Akron3c2730f2016-05-24 15:08:29 +0200824 /**
825 * Move the page up by limit!
826 */
827 pageUp : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200828 this.screen(this.offset - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200829 },
830
831
832 /**
833 * Move the page down by limit!
834 */
835 pageDown : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200836 this.screen(this.offset + this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200837 },
838
Nils Diewald86dad5b2015-01-28 15:09:07 +0000839
Akrone817b882018-08-31 14:09:17 +0200840 /**
841 * Move the view one item up
842 */
843 viewUp : function () {
844 this.screen(this.offset - 1);
845 },
846
847
848 /**
849 * Move the view one item down
850 */
851 viewDown : function () {
852 this.screen(this.offset + 1);
853 },
854
Leo Repp57997402021-08-18 16:37:52 +0200855 /**
856 * Reset the prefix. Currently not used in regular menu.
857 */
858 reset : function () {
859 this.prefix("");
860 },
Akronc53cfc82020-10-19 11:00:58 +0200861
Akron5240b8c2016-05-20 09:17:41 +0200862 // Unmark all items
863 _unmark : function () {
Akron678c26f2020-10-09 08:52:50 +0200864 this._list.forEach(function(it){
Akronc53cfc82020-10-19 11:00:58 +0200865 const item = this._items[it];
Akrone4961b12017-05-10 21:04:46 +0200866 item.lowlight();
Akron678c26f2020-10-09 08:52:50 +0200867 item.active(false);
868 }, this);
Akron5240b8c2016-05-20 09:17:41 +0200869 },
870
Akronc53cfc82020-10-19 11:00:58 +0200871
Akron5240b8c2016-05-20 09:17:41 +0200872 // Set boundary for viewport
873 _boundary : function (bool) {
Akron55a343b2018-04-06 19:57:36 +0200874 if (this._list.length === 0)
875 return;
Akronc53cfc82020-10-19 11:00:58 +0200876
Akron5240b8c2016-05-20 09:17:41 +0200877 this.item(this._list[0]).noMore(bool);
878 this.item(this._list[this._list.length - 1]).noMore(bool);
879 },
880
881
882 // Append Items that should be shown
883 _showItems : function (off) {
Akronc53cfc82020-10-19 11:00:58 +0200884 const t = this;
Akron5240b8c2016-05-20 09:17:41 +0200885
Akrona92fd8d2016-05-24 21:13:41 +0200886 // optimization: scroll down one step
Akronc53cfc82020-10-19 11:00:58 +0200887 if (t.offset === (off - 1)) {
888 t.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200889
Akrone4961b12017-05-10 21:04:46 +0200890 // Remove the HTML node from the first item
891 // leave lengthField/prefix/slider
Leo Reppd162b2e2021-06-30 13:51:07 +0200892 //console.log("_showItems, at _notItemElements is: ",t._el.children[this._notItemElements]);
Leo Repp56904d22021-04-26 15:53:22 +0200893 t._el.removeChild(t._el.children[this._notItemElements]);
Akronc53cfc82020-10-19 11:00:58 +0200894
895 t._append(
896 t._list[t.offset + t.limit() - 1]
897 );
Akrona92fd8d2016-05-24 21:13:41 +0200898 }
Akron5240b8c2016-05-20 09:17:41 +0200899
Akrona92fd8d2016-05-24 21:13:41 +0200900 // optimization: scroll up one step
Akronc53cfc82020-10-19 11:00:58 +0200901 else if (t.offset === (off + 1)) {
902 t.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200903
Akrone4961b12017-05-10 21:04:46 +0200904 // Remove the HTML node from the last item
Leo Reppd162b2e2021-06-30 13:51:07 +0200905 //console.log("_showItems, at lastChild is: ",t._el.lastChild);
Akron24aa0052020-11-10 11:00:34 +0100906 t._el.removeChild(t._el.lastChild);
Akron9c4d1ae2016-05-25 21:43:22 +0200907
Akronc53cfc82020-10-19 11:00:58 +0200908 t._prepend(t._list[t.offset]);
Akrona92fd8d2016-05-24 21:13:41 +0200909 }
Akronc53cfc82020-10-19 11:00:58 +0200910
Akrona92fd8d2016-05-24 21:13:41 +0200911 else {
Akronc53cfc82020-10-19 11:00:58 +0200912 t.offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200913
Akrone4961b12017-05-10 21:04:46 +0200914 // Remove all items
Akronc53cfc82020-10-19 11:00:58 +0200915 t.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200916
Akrone4961b12017-05-10 21:04:46 +0200917 // Use list
Akronc53cfc82020-10-19 11:00:58 +0200918 let shown = 0;
Akron5240b8c2016-05-20 09:17:41 +0200919
Akronc53cfc82020-10-19 11:00:58 +0200920 for (let i = 0; i < t._list.length; i++) {
Akrona92fd8d2016-05-24 21:13:41 +0200921
Akrone4961b12017-05-10 21:04:46 +0200922 // Don't show - it's before offset
923 shown++;
924 if (shown <= off)
925 continue;
Akrona92fd8d2016-05-24 21:13:41 +0200926
Akronc53cfc82020-10-19 11:00:58 +0200927 t._append(t._list[i]);
Akrone4961b12017-05-10 21:04:46 +0200928
Akronc53cfc82020-10-19 11:00:58 +0200929 if (shown >= (t.limit() + off))
Akrone4961b12017-05-10 21:04:46 +0200930 break;
931 };
Akron5240b8c2016-05-20 09:17:41 +0200932 };
933
934 // set the slider to the new offset
Akronc53cfc82020-10-19 11:00:58 +0200935 t._slider.offset(t.offset);
Akron5240b8c2016-05-20 09:17:41 +0200936 },
937
938
939 // Append item to the shown list based on index
940 _append : function (i) {
Akronc53cfc82020-10-19 11:00:58 +0200941 const item = this.item(i);
Akron5240b8c2016-05-20 09:17:41 +0200942
943 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200944 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200945 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200946 };
947
Akron5240b8c2016-05-20 09:17:41 +0200948 // Append element
949 this.element().appendChild(item.element());
950 },
951
952
953 // Prepend item to the shown list based on index
954 _prepend : function (i) {
Akronc53cfc82020-10-19 11:00:58 +0200955 const item = this.item(i);
Akron5240b8c2016-05-20 09:17:41 +0200956
957 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200958 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200959 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200960 };
Akron5240b8c2016-05-20 09:17:41 +0200961
Akronc53cfc82020-10-19 11:00:58 +0200962 const e = this.element();
Akron9c4d1ae2016-05-25 21:43:22 +0200963
Akron5240b8c2016-05-20 09:17:41 +0200964 // Append element after lengthField/prefix/slider
965 e.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200966 item.element(),
Leo Repp56904d22021-04-26 15:53:22 +0200967 e.children[this._notItemElements]
Akron5240b8c2016-05-20 09:17:41 +0200968 );
Nils Diewald2fe12e12015-03-06 16:47:06 +0000969 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000970 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000971});