blob: 794d517f29cff29368d0585c2e96b1d584ff0766 [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
194 _initList : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200195 // Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200196 const t = this;
Akron5240b8c2016-05-20 09:17:41 +0200197
198 // Create a new list
Akronc53cfc82020-10-19 11:00:58 +0200199 if (t._list === undefined) {
200 t._list = [];
Akron5240b8c2016-05-20 09:17:41 +0200201 }
Akronc53cfc82020-10-19 11:00:58 +0200202 else if (t._list.length !== 0) {
203 t._boundary(false);
204 t._list.length = 0;
Akron5240b8c2016-05-20 09:17:41 +0200205 };
206
207 // Offset is initially zero
Akronc53cfc82020-10-19 11:00:58 +0200208 t.offset = 0;
Akron5240b8c2016-05-20 09:17:41 +0200209
210 // There is no prefix set
Akronc53cfc82020-10-19 11:00:58 +0200211 if (t.prefix().length <= 0) {
Akron5240b8c2016-05-20 09:17:41 +0200212
Akrone4961b12017-05-10 21:04:46 +0200213 // add all items to the list and lowlight
Akronb50964a2020-10-12 11:44:37 +0200214 let i = 0;
Akronc53cfc82020-10-19 11:00:58 +0200215 for (; i < t._items.length; i++) {
216 t._list.push(i);
217 t._items[i].lowlight();
Akrone4961b12017-05-10 21:04:46 +0200218 };
Akron5240b8c2016-05-20 09:17:41 +0200219
Akronc53cfc82020-10-19 11:00:58 +0200220 t._slider.length(i).reInit();
Akron97752a72016-05-25 14:43:07 +0200221
Akrone4961b12017-05-10 21:04:46 +0200222 return true;
Akron5240b8c2016-05-20 09:17:41 +0200223 };
224
225 /*
226 * There is a prefix set, so filter the list!
227 */
Akronc53cfc82020-10-19 11:00:58 +0200228 let pos;
229 const prefixList = t.prefix().toLowerCase().split(" ");
Akronacffc652017-12-18 21:21:25 +0100230
Akronc53cfc82020-10-19 11:00:58 +0200231 const items = [];
232 let maxPoints = 1; // minimum 1
Akron5240b8c2016-05-20 09:17:41 +0200233
234 // Iterate over all items and choose preferred matching items
235 // i.e. the matching happens at the word start
Akronc53cfc82020-10-19 11:00:58 +0200236 t._items.forEach(function(it, pos){
Akronacffc652017-12-18 21:21:25 +0100237
Akronb50964a2020-10-12 11:44:37 +0200238 let points = 0;
Akronacffc652017-12-18 21:21:25 +0100239
Akronb50964a2020-10-12 11:44:37 +0200240 prefixList.forEach(function(p) {
Akronacffc652017-12-18 21:21:25 +0100241
242 // Check if it matches at the beginning
Akronb50964a2020-10-12 11:44:37 +0200243 if ((it.lcField().includes(" " + p))) {
Akronacffc652017-12-18 21:21:25 +0100244 points += 5;
245 }
246
247 // Check if it matches anywhere
Akronb50964a2020-10-12 11:44:37 +0200248 else if (it.lcField().includes(p)) {
Akronacffc652017-12-18 21:21:25 +0100249 points += 1;
250 };
Akronb50964a2020-10-12 11:44:37 +0200251 });
Akronacffc652017-12-18 21:21:25 +0100252
253 if (points > maxPoints) {
254 this._list = [pos];
255 maxPoints = points;
256 }
257 else if (points == maxPoints) {
Akrone4961b12017-05-10 21:04:46 +0200258 this._list.push(pos);
Akronacffc652017-12-18 21:21:25 +0100259 }
Akronc53cfc82020-10-19 11:00:58 +0200260 }, t);
Akron5240b8c2016-05-20 09:17:41 +0200261
Akronc53cfc82020-10-19 11:00:58 +0200262 t._slider.length(t._list.length).reInit();
Akron6ed13992016-05-23 18:06:05 +0200263
Akron5240b8c2016-05-20 09:17:41 +0200264 // Filter was successful - yeah!
Akronc53cfc82020-10-19 11:00:58 +0200265 return t._list.length > 0 ? true : false;
Akron5240b8c2016-05-20 09:17:41 +0200266 },
267
268
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000269 /**
270 * Destroy this menu
271 * (in case you don't trust the
272 * mark and sweep GC)!
273 */
274 destroy : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200275 // Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200276 const t = this;
Akron47c086c2016-05-18 21:22:06 +0200277
Akron5240b8c2016-05-20 09:17:41 +0200278 // Remove circular reference to "this" in menu
Akron24aa0052020-11-10 11:00:34 +0100279 if (t._el != undefined)
280 delete t._el["menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000281
Akron5240b8c2016-05-20 09:17:41 +0200282 // Remove circular reference to "this" in items
Akronc53cfc82020-10-19 11:00:58 +0200283 t._items.forEach(function(i) {
Akron678c26f2020-10-09 08:52:50 +0200284 delete i["_menu"];
285 });
Akron5240b8c2016-05-20 09:17:41 +0200286
287 // Remove circular reference to "this" in prefix
Akronc53cfc82020-10-19 11:00:58 +0200288 delete t._prefix['_menu'];
289 delete t._lengthField['_menu'];
290 delete t._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000291 },
292
Nils Diewald7148c6f2015-05-04 15:07:53 +0000293
294 /**
295 * Focus on this menu.
296 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000297 focus : function () {
Akron24aa0052020-11-10 11:00:34 +0100298 this._el.focus();
Nils Diewald2fe12e12015-03-06 16:47:06 +0000299 },
300
Nils Diewald7148c6f2015-05-04 15:07:53 +0000301
Nils Diewald59c02fc2015-03-07 01:29:09 +0000302 // mouse wheel treatment
303 _mousewheel : function (e) {
Akronc53cfc82020-10-19 11:00:58 +0200304 const delta = e.deltaY / 120;
Nils Diewald5975d702015-03-09 17:45:42 +0000305 if (delta > 0)
Akrone4961b12017-05-10 21:04:46 +0200306 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000307 else if (delta < 0)
Akrone4961b12017-05-10 21:04:46 +0200308 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000309 e.halt();
310 },
311
Akronc53cfc82020-10-19 11:00:58 +0200312
Akrona1159ff2018-07-22 13:28:31 +0200313 // touchmove treatment
314 _touch : function (e) {
Akronc53cfc82020-10-19 11:00:58 +0200315 const s = this.slider();
316
Akrona1159ff2018-07-22 13:28:31 +0200317 if (e.type === "touchstart") {
Akronc53cfc82020-10-19 11:00:58 +0200318 this._lastTouch = e.touches[0].clientY;
Akrona1159ff2018-07-22 13:28:31 +0200319 }
320 else if (e.type === "touchend") {
Akrona1159ff2018-07-22 13:28:31 +0200321 this._lastTouch = undefined;
Akrona1159ff2018-07-22 13:28:31 +0200322 }
323 else if (e.type === "touchmove") {
Akronc53cfc82020-10-19 11:00:58 +0200324 const to = e.touches[0];
Akrone817b882018-08-31 14:09:17 +0200325
326 // TODO:
327 // Instead of using 26px, choose the item height
328 // or use the menu height // shownItems
329
Akrona1159ff2018-07-22 13:28:31 +0200330 // s.movetoRel(t.clientY - this._initTouch);
Akronc53cfc82020-10-19 11:00:58 +0200331 if ((this._lastTouch + 26) < to.clientY) {
Akrone817b882018-08-31 14:09:17 +0200332 this.viewDown();
Akronc53cfc82020-10-19 11:00:58 +0200333 this._lastTouch = to.clientY;
Akrona1159ff2018-07-22 13:28:31 +0200334 }
Akronc53cfc82020-10-19 11:00:58 +0200335 else if ((this._lastTouch - 26) > to.clientY) {
Akrone817b882018-08-31 14:09:17 +0200336 this.viewUp();
Akronc53cfc82020-10-19 11:00:58 +0200337 this._lastTouch = to.clientY;
Akrona1159ff2018-07-22 13:28:31 +0200338 }
339 e.halt();
340 };
341 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000342
Nils Diewald59c02fc2015-03-07 01:29:09 +0000343 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000344 _keydown : function (e) {
Leo Repp56904d22021-04-26 15:53:22 +0200345 //Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200346 const t = this;
Nils Diewald59c02fc2015-03-07 01:29:09 +0000347
Akronc53cfc82020-10-19 11:00:58 +0200348 switch (_codeFromEvent(e)) {
349
Nils Diewald59c02fc2015-03-07 01:29:09 +0000350 case 27: // 'Esc'
Akrone4961b12017-05-10 21:04:46 +0200351 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200352 t.hide();
Akrone4961b12017-05-10 21:04:46 +0200353 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000354
Nils Diewald59c02fc2015-03-07 01:29:09 +0000355 case 38: // 'Up'
Akrone4961b12017-05-10 21:04:46 +0200356 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200357 t.prev();
Akrone4961b12017-05-10 21:04:46 +0200358 break;
Akronc53cfc82020-10-19 11:00:58 +0200359
Nils Diewald5975d702015-03-09 17:45:42 +0000360 case 33: // 'Page up'
Akrone4961b12017-05-10 21:04:46 +0200361 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200362 t.pageUp();
Akrone4961b12017-05-10 21:04:46 +0200363 break;
Akronc53cfc82020-10-19 11:00:58 +0200364
Nils Diewald5975d702015-03-09 17:45:42 +0000365 case 40: // 'Down'
Akrone4961b12017-05-10 21:04:46 +0200366 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200367 t.next();
Akrone4961b12017-05-10 21:04:46 +0200368 break;
Akronc53cfc82020-10-19 11:00:58 +0200369
Nils Diewald5975d702015-03-09 17:45:42 +0000370 case 34: // 'Page down'
Akrone4961b12017-05-10 21:04:46 +0200371 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200372 t.pageDown();
Akrone4961b12017-05-10 21:04:46 +0200373 break;
Akronc53cfc82020-10-19 11:00:58 +0200374
Nils Diewald5975d702015-03-09 17:45:42 +0000375 case 39: // 'Right'
Akronc53cfc82020-10-19 11:00:58 +0200376 if (t._prefix.active())
Akrone4961b12017-05-10 21:04:46 +0200377 break;
Nils Diewalde8518f82015-03-18 22:41:49 +0000378
Akronc53cfc82020-10-19 11:00:58 +0200379 const item = t.liveItem(t.position);
Akrone4961b12017-05-10 21:04:46 +0200380
381 if (item["further"] !== undefined) {
382 item["further"].bind(item).apply();
383 };
384
385 e.halt();
386 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000387
Akronc53cfc82020-10-19 11:00:58 +0200388 case 13: // 'Enter'
Akrone4961b12017-05-10 21:04:46 +0200389 // Click on prefix
Akronc53cfc82020-10-19 11:00:58 +0200390 if (t._prefix.active())
391 t._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000392
Akrone4961b12017-05-10 21:04:46 +0200393 // Click on item
394 else
Akronc53cfc82020-10-19 11:00:58 +0200395 t.liveItem(t.position).onclick(e);
Akrone4961b12017-05-10 21:04:46 +0200396 e.halt();
397 break;
Akronc53cfc82020-10-19 11:00:58 +0200398
Nils Diewald59c02fc2015-03-07 01:29:09 +0000399 case 8: // 'Backspace'
Akronc53cfc82020-10-19 11:00:58 +0200400 t._prefix.chop();
401 t.show();
Akrone4961b12017-05-10 21:04:46 +0200402 e.halt();
403 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000404 };
405 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000406
Akronc53cfc82020-10-19 11:00:58 +0200407
Nils Diewald47f366b2015-04-15 20:06:35 +0000408 // Add characters to prefix
409 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200410 if (e.charCode !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200411 e.halt();
Akrone4961b12017-05-10 21:04:46 +0200412
413 // Add prefix
Akronc53cfc82020-10-19 11:00:58 +0200414 this._prefix.add(
415 String.fromCharCode(_codeFromEvent(e))
416 );
417
Akrone4961b12017-05-10 21:04:46 +0200418 this.show();
Akron9c2f9382016-05-25 16:36:04 +0200419 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000420 },
421
Akronc53cfc82020-10-19 11:00:58 +0200422
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) {
Akronc53cfc82020-10-19 11:00:58 +0200428 const t = this;
Akrone817b882018-08-31 14:09:17 +0200429
430 // Normalize negative values
Akron3c2730f2016-05-24 15:08:29 +0200431 if (nr < 0) {
Akrone4961b12017-05-10 21:04:46 +0200432 nr = 0
Akron3c2730f2016-05-24 15:08:29 +0200433 }
Akrone817b882018-08-31 14:09:17 +0200434
435 // The shown list already shows everything
Akronc53cfc82020-10-19 11:00:58 +0200436 else if (t.liveLength() < t.limit()) {
Akrone817b882018-08-31 14:09:17 +0200437 return false;
438 }
439
440 // Move relatively to the next screen
Akronc53cfc82020-10-19 11:00:58 +0200441 else if (nr > (t.liveLength() - t.limit())) {
442 nr = (t.liveLength() - t.limit());
Akron3c2730f2016-05-24 15:08:29 +0200443 };
444
Akrone817b882018-08-31 14:09:17 +0200445 // no change
Akronc53cfc82020-10-19 11:00:58 +0200446 if (t.offset === nr)
Akrone817b882018-08-31 14:09:17 +0200447 return false;
Akron5a1f5bb2016-05-23 22:00:39 +0200448
Akronc53cfc82020-10-19 11:00:58 +0200449 t._showItems(nr);
Akrone817b882018-08-31 14:09:17 +0200450
451 return true;
Akron47c086c2016-05-18 21:22:06 +0200452 },
453
Akrone817b882018-08-31 14:09:17 +0200454
Nils Diewald2fe12e12015-03-06 16:47:06 +0000455 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000456 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000457 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000458 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100459 return this._el;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000460 },
461
Akronc53cfc82020-10-19 11:00:58 +0200462
Nils Diewald2fe12e12015-03-06 16:47:06 +0000463 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000464 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000465 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000466 itemClass : function () {
467 return this._itemClass;
468 },
469
Akronc53cfc82020-10-19 11:00:58 +0200470
Nils Diewald86dad5b2015-01-28 15:09:07 +0000471 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000472 * Get and set the numerical value
473 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000474 */
475 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000476 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200477 if (this._limit !== limit) {
478 this._limit = limit;
479 this._slider.limit(limit).reInit();
480 };
481 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000482 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000483 return this._limit;
484 },
485
Nils Diewald7148c6f2015-05-04 15:07:53 +0000486
Nils Diewald86dad5b2015-01-28 15:09:07 +0000487 /**
488 * Upgrade this object to another object,
489 * while private data stays intact.
490 *
Nils Diewald2fe12e12015-03-06 16:47:06 +0000491 * @param {Object} An object with properties.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000492 */
493 upgradeTo : function (props) {
494 for (var prop in props) {
Akrone4961b12017-05-10 21:04:46 +0200495 this[prop] = props[prop];
Nils Diewald86dad5b2015-01-28 15:09:07 +0000496 };
497 return this;
498 },
499
Nils Diewald7148c6f2015-05-04 15:07:53 +0000500
Nils Diewald86dad5b2015-01-28 15:09:07 +0000501 /**
Akron97752a72016-05-25 14:43:07 +0200502 * Filter the list and make it visible.
503 * This is always called once the prefix changes.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000504 *
505 * @param {string} Prefix for filtering the list
506 */
Akron6ed13992016-05-23 18:06:05 +0200507 show : function (active) {
Leo Repp56904d22021-04-26 15:53:22 +0200508 //Upon change please also update alwaysmenu.js (only two lines new there)
Akronc53cfc82020-10-19 11:00:58 +0200509 const t = this;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000510
Akron5240b8c2016-05-20 09:17:41 +0200511 // show menu based on initial offset
Akronc53cfc82020-10-19 11:00:58 +0200512 t._unmark(); // Unmark everything that was marked before
513 t.removeItems();
Akron6ed13992016-05-23 18:06:05 +0200514
515 // Initialize the list
Akronc53cfc82020-10-19 11:00:58 +0200516 if (!t._initList()) {
Akron6ac58442016-05-24 16:52:29 +0200517
Akrone4961b12017-05-10 21:04:46 +0200518 // The prefix is not active
Akronc53cfc82020-10-19 11:00:58 +0200519 t._prefix.active(true);
Akron6ed13992016-05-23 18:06:05 +0200520
Akrone4961b12017-05-10 21:04:46 +0200521 // finally show the element
Akron24aa0052020-11-10 11:00:34 +0100522 t._el.classList.add('visible');
Akrone4961b12017-05-10 21:04:46 +0200523
524 return true;
Akron6ed13992016-05-23 18:06:05 +0200525 };
526
Akronc53cfc82020-10-19 11:00:58 +0200527 let offset = 0;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000528
Akron9c2f9382016-05-25 16:36:04 +0200529 // Set a chosen value to active and move the viewport
Akron6ed13992016-05-23 18:06:05 +0200530 if (arguments.length === 1) {
531
Akrone4961b12017-05-10 21:04:46 +0200532 // Normalize active value
533 if (active < 0) {
534 active = 0;
535 }
Akronc53cfc82020-10-19 11:00:58 +0200536 else if (active >= t.liveLength()) {
537 active = t.liveLength() - 1;
Akrone4961b12017-05-10 21:04:46 +0200538 };
Akron6ed13992016-05-23 18:06:05 +0200539
Akrone4961b12017-05-10 21:04:46 +0200540 // Item is outside the first viewport
Akronc53cfc82020-10-19 11:00:58 +0200541 if (active >= t._limit) {
Akrone4961b12017-05-10 21:04:46 +0200542 offset = active;
Akronc53cfc82020-10-19 11:00:58 +0200543 const newOffset = t.liveLength() - t._limit;
544 if (offset > newOffset) {
545 offset = newOffset;
Akrone4961b12017-05-10 21:04:46 +0200546 };
547 };
548
Akronc53cfc82020-10-19 11:00:58 +0200549 t.position = active;
Akron6ed13992016-05-23 18:06:05 +0200550 }
551
Akron9c2f9382016-05-25 16:36:04 +0200552 // Choose the first item
Akronc53cfc82020-10-19 11:00:58 +0200553 else if (t._firstActive) {
554 t.position = 0;
Akron47c086c2016-05-18 21:22:06 +0200555 }
Akroncb351d62016-05-19 23:10:33 +0200556
Akron9c2f9382016-05-25 16:36:04 +0200557 // Choose no item
Akron47c086c2016-05-18 21:22:06 +0200558 else {
Akronc53cfc82020-10-19 11:00:58 +0200559 t.position = -1;
Akron6ed13992016-05-23 18:06:05 +0200560 };
561
Akronc53cfc82020-10-19 11:00:58 +0200562 t.offset = offset;
563 t._showItems(offset); // Show new item list
Akron6ed13992016-05-23 18:06:05 +0200564
565 // Make chosen value active
Akronc53cfc82020-10-19 11:00:58 +0200566 if (t.position !== -1) {
567 t.liveItem(t.position).active(true);
Akron6ed13992016-05-23 18:06:05 +0200568 };
Akron37513a62015-11-17 01:07:11 +0100569
Akron5240b8c2016-05-20 09:17:41 +0200570 // The prefix is not active
Akronc53cfc82020-10-19 11:00:58 +0200571 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000572
Akron5240b8c2016-05-20 09:17:41 +0200573 // finally show the element
Akron24aa0052020-11-10 11:00:34 +0100574 t._el.classList.add('visible');
Nils Diewald2fe12e12015-03-06 16:47:06 +0000575
Nils Diewald86dad5b2015-01-28 15:09:07 +0000576 // Add classes for rolling menus
Akronc53cfc82020-10-19 11:00:58 +0200577 t._boundary(true);
Akron6bb71582016-06-10 20:41:08 +0200578
Nils Diewald59c02fc2015-03-07 01:29:09 +0000579 return true;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000580 },
581
Nils Diewald7148c6f2015-05-04 15:07:53 +0000582
583 /**
584 * Hide the menu and call the onHide callback.
585 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000586 hide : function () {
Akronc53cfc82020-10-19 11:00:58 +0200587 if (!this.dontHide) {
588 this.removeItems();
589 this._prefix.clear();
590 this.onHide();
Akron24aa0052020-11-10 11:00:34 +0100591 this._el.classList.remove('visible');
Akronc53cfc82020-10-19 11:00:58 +0200592 }
Akron24aa0052020-11-10 11:00:34 +0100593 // this._el.blur();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000594 },
595
Akronc53cfc82020-10-19 11:00:58 +0200596
Nils Diewald7148c6f2015-05-04 15:07:53 +0000597 /**
598 * Function released when the menu hides.
599 * This method is expected to be overridden.
600 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000601 onHide : function () {},
602
Akronc53cfc82020-10-19 11:00:58 +0200603
Nils Diewald86dad5b2015-01-28 15:09:07 +0000604 /**
605 * Get the prefix for filtering,
Nils Diewald2fe12e12015-03-06 16:47:06 +0000606 * e.g. &quot;ve&quot; for &quot;verb&quot;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000607 */
Nils Diewald5975d702015-03-09 17:45:42 +0000608 prefix : function (pref) {
609 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200610 this._prefix.value(pref);
611 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000612 };
613 return this._prefix.value();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000614 },
615
Akronc53cfc82020-10-19 11:00:58 +0200616
Akronc7448732016-04-27 14:06:58 +0200617 /**
618 * Get the lengthField object.
619 */
620 lengthField : function () {
621 return this._lengthField;
622 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000623
Akronc53cfc82020-10-19 11:00:58 +0200624
Akron5240b8c2016-05-20 09:17:41 +0200625 /**
626 * Get the associated slider object.
627 */
628 slider : function () {
629 return this._slider;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000630 },
631
Akron5240b8c2016-05-20 09:17:41 +0200632
Nils Diewald86dad5b2015-01-28 15:09:07 +0000633 /**
634 * Delete all visible items from the menu element
635 */
Leo Reppe5bd35a2021-05-05 15:28:26 +0200636
Leo Reppafcf9842021-06-30 13:01:09 +0200637 removeItems : function () {
Leo Reppe5bd35a2021-05-05 15:28:26 +0200638 const liElements=this._el.getElementsByTagName("LI");
Leo Reppd162b2e2021-06-30 13:51:07 +0200639 var ignoredCount = 0; //counts how many LI tag elements are not actually direct children
640 while (liElements.length>ignoredCount){
641 if (liElements[ignoredCount].parentNode === this._el){
642 this._el.removeChild(liElements[ignoredCount]);
643 } else {
644 ignoredCount++;
645 }
Nils Diewald5975d702015-03-09 17:45:42 +0000646 };
Leo Reppe5bd35a2021-05-05 15:28:26 +0200647 },
Leo Repp56904d22021-04-26 15:53:22 +0200648
Nils Diewald86dad5b2015-01-28 15:09:07 +0000649
Akronc53cfc82020-10-19 11:00:58 +0200650
Nils Diewald2fe12e12015-03-06 16:47:06 +0000651 /**
652 * Get a specific item from the complete list
653 *
654 * @param {number} index of the list item
655 */
656 item : function (index) {
657 return this._items[index]
658 },
659
660
Nils Diewald86dad5b2015-01-28 15:09:07 +0000661 /**
662 * Get a specific item from the filtered list
663 *
664 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000665 * in the filtered list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000666 */
667 liveItem : function (index) {
668 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200669 if (!this._initList())
670 return;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000671
672 return this._items[this._list[index]];
673 },
674
Nils Diewald86dad5b2015-01-28 15:09:07 +0000675
676 /**
Akron5240b8c2016-05-20 09:17:41 +0200677 * Get a specific item from the viewport list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000678 *
679 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000680 * in the visible list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000681 */
682 shownItem : function (index) {
683 if (index >= this.limit())
Akrone4961b12017-05-10 21:04:46 +0200684 return;
Akronc53cfc82020-10-19 11:00:58 +0200685
Akron9c4d1ae2016-05-25 21:43:22 +0200686 return this.liveItem(this.offset + index);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000687 },
688
689
Nils Diewald2fe12e12015-03-06 16:47:06 +0000690 /**
Akron9c4d1ae2016-05-25 21:43:22 +0200691 * Get the length of the full item list
Nils Diewald2fe12e12015-03-06 16:47:06 +0000692 */
693 length : function () {
694 return this._items.length;
695 },
696
697
698 /**
Akron5240b8c2016-05-20 09:17:41 +0200699 * Length of the filtered item list.
700 */
701 liveLength : function () {
702 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200703 this._initList();
Akron5240b8c2016-05-20 09:17:41 +0200704 return this._list.length;
705 },
706
Akrone817b882018-08-31 14:09:17 +0200707
Akron5240b8c2016-05-20 09:17:41 +0200708 /**
Nils Diewald2fe12e12015-03-06 16:47:06 +0000709 * Make the next item in the filtered menu active
Nils Diewald86dad5b2015-01-28 15:09:07 +0000710 */
711 next : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200712 //Upon change please update alwaysmenu.js next
Akronc53cfc82020-10-19 11:00:58 +0200713 const t = this;
Nils Diewald5975d702015-03-09 17:45:42 +0000714
Akronb38afb22016-05-25 19:30:01 +0200715 // No list
Akronc53cfc82020-10-19 11:00:58 +0200716 if (t.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200717 return;
Akronb38afb22016-05-25 19:30:01 +0200718
Akron9c4d1ae2016-05-25 21:43:22 +0200719 // Deactivate old item
Akronc53cfc82020-10-19 11:00:58 +0200720 if (t.position !== -1 && !t._prefix.active()) {
721 t.liveItem(t.position).active(false);
Akron9c4d1ae2016-05-25 21:43:22 +0200722 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000723
Akron9c4d1ae2016-05-25 21:43:22 +0200724 // Get new active item
Akronc53cfc82020-10-19 11:00:58 +0200725 t.position++;
726 let newItem = t.liveItem(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000727
Nils Diewald5975d702015-03-09 17:45:42 +0000728 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000729 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000730
Akrone4961b12017-05-10 21:04:46 +0200731 // Activate prefix
Akronc53cfc82020-10-19 11:00:58 +0200732 const prefix = this._prefix;
Nils Diewald5975d702015-03-09 17:45:42 +0000733
Akrone4961b12017-05-10 21:04:46 +0200734 // Prefix is set and not active - choose!
735 if (prefix.isSet() && !prefix.active()) {
Akronc53cfc82020-10-19 11:00:58 +0200736 t.position--;
Akrone4961b12017-05-10 21:04:46 +0200737 prefix.active(true);
738 return;
739 }
Akron9c4d1ae2016-05-25 21:43:22 +0200740
Akrone4961b12017-05-10 21:04:46 +0200741 // Choose first item
742 else {
Akronc53cfc82020-10-19 11:00:58 +0200743 newItem = t.liveItem(0);
Akrone4961b12017-05-10 21:04:46 +0200744 // choose first item
Akronc53cfc82020-10-19 11:00:58 +0200745 t.position = 0;
746 t._showItems(0);
Akrone4961b12017-05-10 21:04:46 +0200747 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000748 }
749
Akron5a1f5bb2016-05-23 22:00:39 +0200750 // The next element is after the viewport - roll down
Akronc53cfc82020-10-19 11:00:58 +0200751 else if (t.position >= (t.limit() + t.offset)) {
752 t.screen(t.position - t.limit() + 1);
Akron5a1f5bb2016-05-23 22:00:39 +0200753 }
754
755 // The next element is before the viewport - roll up
Akronc53cfc82020-10-19 11:00:58 +0200756 else if (t.position <= t.offset) {
757 t.screen(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000758 };
Nils Diewald5975d702015-03-09 17:45:42 +0000759
Akronc53cfc82020-10-19 11:00:58 +0200760 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000761 newItem.active(true);
762 },
763
Akronc53cfc82020-10-19 11:00:58 +0200764
Nils Diewalde8518f82015-03-18 22:41:49 +0000765 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000766 * Make the previous item in the menu active
767 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000768 prev : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200769 //Upon Change please update alwaysmenu.js prev
Akronc53cfc82020-10-19 11:00:58 +0200770 const t = this;
Nils Diewald2d210752015-03-09 19:01:15 +0000771
Akronb38afb22016-05-25 19:30:01 +0200772 // No list
Akronc53cfc82020-10-19 11:00:58 +0200773 if (t.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200774 return;
Akronb38afb22016-05-25 19:30:01 +0200775
Akron9c4d1ae2016-05-25 21:43:22 +0200776 // Deactivate old item
Akronc53cfc82020-10-19 11:00:58 +0200777 if (!t._prefix.active()) {
Akron9c4d1ae2016-05-25 21:43:22 +0200778
Akrone4961b12017-05-10 21:04:46 +0200779 // No active element set
Akronc53cfc82020-10-19 11:00:58 +0200780 if (t.position === -1) {
781 t.position = t.liveLength();
Akrone4961b12017-05-10 21:04:46 +0200782 }
Akron9c4d1ae2016-05-25 21:43:22 +0200783
Akrone4961b12017-05-10 21:04:46 +0200784 // No active element set
785 else {
Akronc53cfc82020-10-19 11:00:58 +0200786 t.liveItem(t.position--).active(false);
Akrone4961b12017-05-10 21:04:46 +0200787 };
Nils Diewald2d210752015-03-09 19:01:15 +0000788 };
789
Akron9c4d1ae2016-05-25 21:43:22 +0200790 // Get new active item
Akronc53cfc82020-10-19 11:00:58 +0200791 let newItem = t.liveItem(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000792
793 // The previous element is undefined - roll to bottom
794 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000795
Akrone4961b12017-05-10 21:04:46 +0200796 // Activate prefix
Akronc53cfc82020-10-19 11:00:58 +0200797 const prefix = t._prefix;
798 let offset = t.liveLength() - t.limit();
Akrone4961b12017-05-10 21:04:46 +0200799
800 // Normalize offset
801 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000802
Akrone4961b12017-05-10 21:04:46 +0200803 // Choose the last item
Akronc53cfc82020-10-19 11:00:58 +0200804 t.position = t.liveLength() - 1;
Akrone4961b12017-05-10 21:04:46 +0200805
806 // Prefix is set and not active - choose!
807 if (prefix.isSet() && !prefix.active()) {
Akronc53cfc82020-10-19 11:00:58 +0200808 t.position++;
Akrone4961b12017-05-10 21:04:46 +0200809 prefix.active(true);
Akronc53cfc82020-10-19 11:00:58 +0200810 t.offset = offset;
Akrone4961b12017-05-10 21:04:46 +0200811 return;
812 }
Nils Diewald2d210752015-03-09 19:01:15 +0000813
Akrone4961b12017-05-10 21:04:46 +0200814 // Choose last item
815 else {
Akronc53cfc82020-10-19 11:00:58 +0200816 newItem = t.liveItem(t.position);
817 t._showItems(offset);
Akrone4961b12017-05-10 21:04:46 +0200818 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000819 }
820
Akron5a1f5bb2016-05-23 22:00:39 +0200821 // The previous element is before the view - roll up
Akronc53cfc82020-10-19 11:00:58 +0200822 else if (t.position < t.offset) {
823 t.screen(t.position);
Akron5a1f5bb2016-05-23 22:00:39 +0200824 }
825
826 // The previous element is after the view - roll down
Akronc53cfc82020-10-19 11:00:58 +0200827 else if (t.position >= (t.limit() + t.offset)) {
828 t.screen(t.position - t.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000829 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000830
Akronc53cfc82020-10-19 11:00:58 +0200831 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000832 newItem.active(true);
833 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000834
Akronc53cfc82020-10-19 11:00:58 +0200835
Akron3c2730f2016-05-24 15:08:29 +0200836 /**
837 * Move the page up by limit!
838 */
839 pageUp : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200840 this.screen(this.offset - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200841 },
842
843
844 /**
845 * Move the page down by limit!
846 */
847 pageDown : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200848 this.screen(this.offset + this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200849 },
850
Nils Diewald86dad5b2015-01-28 15:09:07 +0000851
Akrone817b882018-08-31 14:09:17 +0200852 /**
853 * Move the view one item up
854 */
855 viewUp : function () {
856 this.screen(this.offset - 1);
857 },
858
859
860 /**
861 * Move the view one item down
862 */
863 viewDown : function () {
864 this.screen(this.offset + 1);
865 },
866
Akronc53cfc82020-10-19 11:00:58 +0200867
Akron5240b8c2016-05-20 09:17:41 +0200868 // Unmark all items
869 _unmark : function () {
Akron678c26f2020-10-09 08:52:50 +0200870 this._list.forEach(function(it){
Akronc53cfc82020-10-19 11:00:58 +0200871 const item = this._items[it];
Akrone4961b12017-05-10 21:04:46 +0200872 item.lowlight();
Akron678c26f2020-10-09 08:52:50 +0200873 item.active(false);
874 }, this);
Akron5240b8c2016-05-20 09:17:41 +0200875 },
876
Akronc53cfc82020-10-19 11:00:58 +0200877
Akron5240b8c2016-05-20 09:17:41 +0200878 // Set boundary for viewport
879 _boundary : function (bool) {
Akron55a343b2018-04-06 19:57:36 +0200880 if (this._list.length === 0)
881 return;
Akronc53cfc82020-10-19 11:00:58 +0200882
Akron5240b8c2016-05-20 09:17:41 +0200883 this.item(this._list[0]).noMore(bool);
884 this.item(this._list[this._list.length - 1]).noMore(bool);
885 },
886
887
888 // Append Items that should be shown
889 _showItems : function (off) {
Akronc53cfc82020-10-19 11:00:58 +0200890 const t = this;
Akron5240b8c2016-05-20 09:17:41 +0200891
Akrona92fd8d2016-05-24 21:13:41 +0200892 // optimization: scroll down one step
Akronc53cfc82020-10-19 11:00:58 +0200893 if (t.offset === (off - 1)) {
894 t.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200895
Akrone4961b12017-05-10 21:04:46 +0200896 // Remove the HTML node from the first item
897 // leave lengthField/prefix/slider
Leo Reppd162b2e2021-06-30 13:51:07 +0200898 //console.log("_showItems, at _notItemElements is: ",t._el.children[this._notItemElements]);
Leo Repp56904d22021-04-26 15:53:22 +0200899 t._el.removeChild(t._el.children[this._notItemElements]);
Akronc53cfc82020-10-19 11:00:58 +0200900
901 t._append(
902 t._list[t.offset + t.limit() - 1]
903 );
Akrona92fd8d2016-05-24 21:13:41 +0200904 }
Akron5240b8c2016-05-20 09:17:41 +0200905
Akrona92fd8d2016-05-24 21:13:41 +0200906 // optimization: scroll up one step
Akronc53cfc82020-10-19 11:00:58 +0200907 else if (t.offset === (off + 1)) {
908 t.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200909
Akrone4961b12017-05-10 21:04:46 +0200910 // Remove the HTML node from the last item
Leo Reppd162b2e2021-06-30 13:51:07 +0200911 //console.log("_showItems, at lastChild is: ",t._el.lastChild);
Akron24aa0052020-11-10 11:00:34 +0100912 t._el.removeChild(t._el.lastChild);
Akron9c4d1ae2016-05-25 21:43:22 +0200913
Akronc53cfc82020-10-19 11:00:58 +0200914 t._prepend(t._list[t.offset]);
Akrona92fd8d2016-05-24 21:13:41 +0200915 }
Akronc53cfc82020-10-19 11:00:58 +0200916
Akrona92fd8d2016-05-24 21:13:41 +0200917 else {
Akronc53cfc82020-10-19 11:00:58 +0200918 t.offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200919
Akrone4961b12017-05-10 21:04:46 +0200920 // Remove all items
Akronc53cfc82020-10-19 11:00:58 +0200921 t.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200922
Akrone4961b12017-05-10 21:04:46 +0200923 // Use list
Akronc53cfc82020-10-19 11:00:58 +0200924 let shown = 0;
Akron5240b8c2016-05-20 09:17:41 +0200925
Akronc53cfc82020-10-19 11:00:58 +0200926 for (let i = 0; i < t._list.length; i++) {
Akrona92fd8d2016-05-24 21:13:41 +0200927
Akrone4961b12017-05-10 21:04:46 +0200928 // Don't show - it's before offset
929 shown++;
930 if (shown <= off)
931 continue;
Akrona92fd8d2016-05-24 21:13:41 +0200932
Akronc53cfc82020-10-19 11:00:58 +0200933 t._append(t._list[i]);
Akrone4961b12017-05-10 21:04:46 +0200934
Akronc53cfc82020-10-19 11:00:58 +0200935 if (shown >= (t.limit() + off))
Akrone4961b12017-05-10 21:04:46 +0200936 break;
937 };
Akron5240b8c2016-05-20 09:17:41 +0200938 };
939
940 // set the slider to the new offset
Akronc53cfc82020-10-19 11:00:58 +0200941 t._slider.offset(t.offset);
Akron5240b8c2016-05-20 09:17:41 +0200942 },
943
944
945 // Append item to the shown list based on index
946 _append : function (i) {
Akronc53cfc82020-10-19 11:00:58 +0200947 const item = this.item(i);
Akron5240b8c2016-05-20 09:17:41 +0200948
949 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200950 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200951 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200952 };
953
Akron5240b8c2016-05-20 09:17:41 +0200954 // Append element
955 this.element().appendChild(item.element());
956 },
957
958
959 // Prepend item to the shown list based on index
960 _prepend : function (i) {
Akronc53cfc82020-10-19 11:00:58 +0200961 const item = this.item(i);
Akron5240b8c2016-05-20 09:17:41 +0200962
963 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200964 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200965 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200966 };
Akron5240b8c2016-05-20 09:17:41 +0200967
Akronc53cfc82020-10-19 11:00:58 +0200968 const e = this.element();
Akron9c4d1ae2016-05-25 21:43:22 +0200969
Akron5240b8c2016-05-20 09:17:41 +0200970 // Append element after lengthField/prefix/slider
971 e.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200972 item.element(),
Leo Repp56904d22021-04-26 15:53:22 +0200973 e.children[this._notItemElements]
Akron5240b8c2016-05-20 09:17:41 +0200974 );
Nils Diewald2fe12e12015-03-06 16:47:06 +0000975 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000976 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000977});