blob: 088618bf1850dd9a30cc7d84f1a727e088596ecb [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) {
Akronc53cfc82020-10-19 11:00:58 +020058
Akron7524be12016-06-01 17:31:33 +020059 if (params === undefined)
Akrone4961b12017-05-10 21:04:46 +020060 params = {};
Akron7524be12016-06-01 17:31:33 +020061
Akronc53cfc82020-10-19 11:00:58 +020062 const t = this;
Leo Repp56904d22021-04-26 15:53:22 +020063 t._notItemElements=3;
Akronc53cfc82020-10-19 11:00:58 +020064
65 t._itemClass = params["itemClass"] || defaultItemClass;
Akron5240b8c2016-05-20 09:17:41 +020066
67 // Add prefix object
Akron7524be12016-06-01 17:31:33 +020068 if (params["prefixClass"] !== undefined) {
Akronc53cfc82020-10-19 11:00:58 +020069 t._prefix = params["prefixClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020070 }
71 else {
Akronc53cfc82020-10-19 11:00:58 +020072 t._prefix = defaultPrefixClass.create();
Akron5240b8c2016-05-20 09:17:41 +020073 };
Akronc53cfc82020-10-19 11:00:58 +020074 t._prefix._menu = t;
Akron5240b8c2016-05-20 09:17:41 +020075
76 // Add lengthField object
Akron7524be12016-06-01 17:31:33 +020077 if (params["lengthFieldClass"] !== undefined) {
Akronc53cfc82020-10-19 11:00:58 +020078 t._lengthField = params["lengthFieldClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020079 }
80 else {
Akronc53cfc82020-10-19 11:00:58 +020081 t._lengthField = defaultLengthFieldClass.create();
Akron5240b8c2016-05-20 09:17:41 +020082 };
Akronc53cfc82020-10-19 11:00:58 +020083 t._lengthField._menu = t;
Akron5240b8c2016-05-20 09:17:41 +020084
85 // Initialize slider
Akronc53cfc82020-10-19 11:00:58 +020086 t._slider = sliderClass.create(t);
Akron5240b8c2016-05-20 09:17:41 +020087
88 // Create the element
Akron9c4d1ae2016-05-25 21:43:22 +020089 var el = document.createElement("ul");
Akronc53cfc82020-10-19 11:00:58 +020090 el.style.outline = 0;
91 el.setAttribute('tabindex', 0);
92 el.classList.add('menu', 'roll');
93 el.appendChild(t._prefix.element());
94 el.appendChild(t._lengthField.element());
95 el.appendChild(t._slider.element());
Akron5240b8c2016-05-20 09:17:41 +020096
97 // This has to be cleaned up later on
Akronc53cfc82020-10-19 11:00:58 +020098 el["menu"] = t;
Akron5240b8c2016-05-20 09:17:41 +020099
100 // Arrow keys
Akron9c4d1ae2016-05-25 21:43:22 +0200101 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200102 'keydown',
Akronc53cfc82020-10-19 11:00:58 +0200103 t._keydown.bind(t),
Akrone4961b12017-05-10 21:04:46 +0200104 false
Akron5240b8c2016-05-20 09:17:41 +0200105 );
106
107 // Strings
Akron9c4d1ae2016-05-25 21:43:22 +0200108 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200109 'keypress',
Akronc53cfc82020-10-19 11:00:58 +0200110 t._keypress.bind(t),
Akrone4961b12017-05-10 21:04:46 +0200111 false
Akron5240b8c2016-05-20 09:17:41 +0200112 );
113
114 // Mousewheel
Akron9c4d1ae2016-05-25 21:43:22 +0200115 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200116 'wheel',
Akronc53cfc82020-10-19 11:00:58 +0200117 t._mousewheel.bind(t),
Akrone4961b12017-05-10 21:04:46 +0200118 false
Akron5240b8c2016-05-20 09:17:41 +0200119 );
Akrona1159ff2018-07-22 13:28:31 +0200120
Akronc53cfc82020-10-19 11:00:58 +0200121 // Touch events
122 ['touchstart', 'touchend', 'touchmove'].forEach(
123 e => el.addEventListener(e, t._touch.bind(t), false)
Akrona1159ff2018-07-22 13:28:31 +0200124 );
125
126
Akron24aa0052020-11-10 11:00:34 +0100127 t._el = el;
Akroneaba63e2018-01-26 19:49:30 +0100128
Akronc53cfc82020-10-19 11:00:58 +0200129 t._limit = menuLimit;
Akrone4961b12017-05-10 21:04:46 +0200130
Leo Repp56904d22021-04-26 15:53:22 +0200131 t._items = new Array(); //all childNodes, i.e. ItemClass, prefixClass
Akron5240b8c2016-05-20 09:17:41 +0200132
Akroneaba63e2018-01-26 19:49:30 +0100133 // TODO:
134 // Make this separate from _init
Akronc53cfc82020-10-19 11:00:58 +0200135 t.readItems(list);
Akroneaba63e2018-01-26 19:49:30 +0100136
Akronc53cfc82020-10-19 11:00:58 +0200137 t.dontHide = false;
hebastaf95226b2019-09-19 11:37:00 +0200138
Akronc53cfc82020-10-19 11:00:58 +0200139 return t;
Akroneaba63e2018-01-26 19:49:30 +0100140 },
141
142 // Read items to add to list
143 readItems : function (list) {
Akronc53cfc82020-10-19 11:00:58 +0200144 const t = this;
Akroneaba63e2018-01-26 19:49:30 +0100145
Leo Repp56904d22021-04-26 15:53:22 +0200146 t._list = undefined; //filtered List containing all itemClass items
Akroneaba63e2018-01-26 19:49:30 +0100147
148 // Remove circular reference to "this" in items
Akronc53cfc82020-10-19 11:00:58 +0200149 for (let i = 0; i < t._items.length; i++) {
150 delete t._items[i]["_menu"];
151 delete t._items[i];
Akroneaba63e2018-01-26 19:49:30 +0100152 };
153
Akronc53cfc82020-10-19 11:00:58 +0200154 t._items = new Array();
155 t.removeItems();
Akroneaba63e2018-01-26 19:49:30 +0100156
157
158 // Initialize items
Akronc53cfc82020-10-19 11:00:58 +0200159 t._lengthField.reset();
Akroneaba63e2018-01-26 19:49:30 +0100160
Akron5240b8c2016-05-20 09:17:41 +0200161 // Initialize item list based on parameters
Akron678c26f2020-10-09 08:52:50 +0200162 list.forEach(function(i){
Akronc53cfc82020-10-19 11:00:58 +0200163 const obj = this._itemClass.create(i);
Akron5240b8c2016-05-20 09:17:41 +0200164
Akrone4961b12017-05-10 21:04:46 +0200165 // This may become circular
166 obj["_menu"] = this;
Akron678c26f2020-10-09 08:52:50 +0200167 this._lengthField.add(i);
Akrone4961b12017-05-10 21:04:46 +0200168 this._items.push(obj);
Akronc53cfc82020-10-19 11:00:58 +0200169 }, t);
Akron5240b8c2016-05-20 09:17:41 +0200170
Akronc53cfc82020-10-19 11:00:58 +0200171 t._slider.length(t.liveLength())
172 .limit(t._limit)
Akrone4961b12017-05-10 21:04:46 +0200173 .reInit();
174
Akronc53cfc82020-10-19 11:00:58 +0200175 t._firstActive = false;
Akroneaba63e2018-01-26 19:49:30 +0100176 // Show the first item active always?
Akronc53cfc82020-10-19 11:00:58 +0200177 t.offset = 0;
178 t.position = 0;
Akron5240b8c2016-05-20 09:17:41 +0200179 },
Akroneaba63e2018-01-26 19:49:30 +0100180
Akron5240b8c2016-05-20 09:17:41 +0200181 // Initialize the item list
182 _initList : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200183 // Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200184 const t = this;
Akron5240b8c2016-05-20 09:17:41 +0200185
186 // Create a new list
Akronc53cfc82020-10-19 11:00:58 +0200187 if (t._list === undefined) {
188 t._list = [];
Akron5240b8c2016-05-20 09:17:41 +0200189 }
Akronc53cfc82020-10-19 11:00:58 +0200190 else if (t._list.length !== 0) {
191 t._boundary(false);
192 t._list.length = 0;
Akron5240b8c2016-05-20 09:17:41 +0200193 };
194
195 // Offset is initially zero
Akronc53cfc82020-10-19 11:00:58 +0200196 t.offset = 0;
Akron5240b8c2016-05-20 09:17:41 +0200197
198 // There is no prefix set
Akronc53cfc82020-10-19 11:00:58 +0200199 if (t.prefix().length <= 0) {
Akron5240b8c2016-05-20 09:17:41 +0200200
Akrone4961b12017-05-10 21:04:46 +0200201 // add all items to the list and lowlight
Akronb50964a2020-10-12 11:44:37 +0200202 let i = 0;
Akronc53cfc82020-10-19 11:00:58 +0200203 for (; i < t._items.length; i++) {
204 t._list.push(i);
205 t._items[i].lowlight();
Akrone4961b12017-05-10 21:04:46 +0200206 };
Akron5240b8c2016-05-20 09:17:41 +0200207
Akronc53cfc82020-10-19 11:00:58 +0200208 t._slider.length(i).reInit();
Akron97752a72016-05-25 14:43:07 +0200209
Akrone4961b12017-05-10 21:04:46 +0200210 return true;
Akron5240b8c2016-05-20 09:17:41 +0200211 };
212
213 /*
214 * There is a prefix set, so filter the list!
215 */
Akronc53cfc82020-10-19 11:00:58 +0200216 let pos;
217 const prefixList = t.prefix().toLowerCase().split(" ");
Akronacffc652017-12-18 21:21:25 +0100218
Akronc53cfc82020-10-19 11:00:58 +0200219 const items = [];
220 let maxPoints = 1; // minimum 1
Akron5240b8c2016-05-20 09:17:41 +0200221
222 // Iterate over all items and choose preferred matching items
223 // i.e. the matching happens at the word start
Akronc53cfc82020-10-19 11:00:58 +0200224 t._items.forEach(function(it, pos){
Akronacffc652017-12-18 21:21:25 +0100225
Akronb50964a2020-10-12 11:44:37 +0200226 let points = 0;
Akronacffc652017-12-18 21:21:25 +0100227
Akronb50964a2020-10-12 11:44:37 +0200228 prefixList.forEach(function(p) {
Akronacffc652017-12-18 21:21:25 +0100229
230 // Check if it matches at the beginning
Akronb50964a2020-10-12 11:44:37 +0200231 if ((it.lcField().includes(" " + p))) {
Akronacffc652017-12-18 21:21:25 +0100232 points += 5;
233 }
234
235 // Check if it matches anywhere
Akronb50964a2020-10-12 11:44:37 +0200236 else if (it.lcField().includes(p)) {
Akronacffc652017-12-18 21:21:25 +0100237 points += 1;
238 };
Akronb50964a2020-10-12 11:44:37 +0200239 });
Akronacffc652017-12-18 21:21:25 +0100240
241 if (points > maxPoints) {
242 this._list = [pos];
243 maxPoints = points;
244 }
245 else if (points == maxPoints) {
Akrone4961b12017-05-10 21:04:46 +0200246 this._list.push(pos);
Akronacffc652017-12-18 21:21:25 +0100247 }
Akronc53cfc82020-10-19 11:00:58 +0200248 }, t);
Akron5240b8c2016-05-20 09:17:41 +0200249
Akronc53cfc82020-10-19 11:00:58 +0200250 t._slider.length(t._list.length).reInit();
Akron6ed13992016-05-23 18:06:05 +0200251
Akron5240b8c2016-05-20 09:17:41 +0200252 // Filter was successful - yeah!
Akronc53cfc82020-10-19 11:00:58 +0200253 return t._list.length > 0 ? true : false;
Akron5240b8c2016-05-20 09:17:41 +0200254 },
255
256
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000257 /**
258 * Destroy this menu
259 * (in case you don't trust the
260 * mark and sweep GC)!
261 */
262 destroy : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200263 // Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200264 const t = this;
Akron47c086c2016-05-18 21:22:06 +0200265
Akron5240b8c2016-05-20 09:17:41 +0200266 // Remove circular reference to "this" in menu
Akron24aa0052020-11-10 11:00:34 +0100267 if (t._el != undefined)
268 delete t._el["menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000269
Akron5240b8c2016-05-20 09:17:41 +0200270 // Remove circular reference to "this" in items
Akronc53cfc82020-10-19 11:00:58 +0200271 t._items.forEach(function(i) {
Akron678c26f2020-10-09 08:52:50 +0200272 delete i["_menu"];
273 });
Akron5240b8c2016-05-20 09:17:41 +0200274
275 // Remove circular reference to "this" in prefix
Akronc53cfc82020-10-19 11:00:58 +0200276 delete t._prefix['_menu'];
277 delete t._lengthField['_menu'];
278 delete t._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000279 },
280
Nils Diewald7148c6f2015-05-04 15:07:53 +0000281
282 /**
283 * Focus on this menu.
284 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000285 focus : function () {
Akron24aa0052020-11-10 11:00:34 +0100286 this._el.focus();
Nils Diewald2fe12e12015-03-06 16:47:06 +0000287 },
288
Nils Diewald7148c6f2015-05-04 15:07:53 +0000289
Nils Diewald59c02fc2015-03-07 01:29:09 +0000290 // mouse wheel treatment
291 _mousewheel : function (e) {
Akronc53cfc82020-10-19 11:00:58 +0200292 const delta = e.deltaY / 120;
Nils Diewald5975d702015-03-09 17:45:42 +0000293 if (delta > 0)
Akrone4961b12017-05-10 21:04:46 +0200294 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000295 else if (delta < 0)
Akrone4961b12017-05-10 21:04:46 +0200296 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000297 e.halt();
298 },
299
Akronc53cfc82020-10-19 11:00:58 +0200300
Akrona1159ff2018-07-22 13:28:31 +0200301 // touchmove treatment
302 _touch : function (e) {
Akronc53cfc82020-10-19 11:00:58 +0200303 const s = this.slider();
304
Akrona1159ff2018-07-22 13:28:31 +0200305 if (e.type === "touchstart") {
Akronc53cfc82020-10-19 11:00:58 +0200306 this._lastTouch = e.touches[0].clientY;
Akrona1159ff2018-07-22 13:28:31 +0200307 }
308 else if (e.type === "touchend") {
Akrona1159ff2018-07-22 13:28:31 +0200309 this._lastTouch = undefined;
Akrona1159ff2018-07-22 13:28:31 +0200310 }
311 else if (e.type === "touchmove") {
Akronc53cfc82020-10-19 11:00:58 +0200312 const to = e.touches[0];
Akrone817b882018-08-31 14:09:17 +0200313
314 // TODO:
315 // Instead of using 26px, choose the item height
316 // or use the menu height // shownItems
317
Akrona1159ff2018-07-22 13:28:31 +0200318 // s.movetoRel(t.clientY - this._initTouch);
Akronc53cfc82020-10-19 11:00:58 +0200319 if ((this._lastTouch + 26) < to.clientY) {
Akrone817b882018-08-31 14:09:17 +0200320 this.viewDown();
Akronc53cfc82020-10-19 11:00:58 +0200321 this._lastTouch = to.clientY;
Akrona1159ff2018-07-22 13:28:31 +0200322 }
Akronc53cfc82020-10-19 11:00:58 +0200323 else if ((this._lastTouch - 26) > to.clientY) {
Akrone817b882018-08-31 14:09:17 +0200324 this.viewUp();
Akronc53cfc82020-10-19 11:00:58 +0200325 this._lastTouch = to.clientY;
Akrona1159ff2018-07-22 13:28:31 +0200326 }
327 e.halt();
328 };
329 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000330
Nils Diewald59c02fc2015-03-07 01:29:09 +0000331 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000332 _keydown : function (e) {
Leo Repp56904d22021-04-26 15:53:22 +0200333 //Upon change also update alwaysmenu.js please
Akronc53cfc82020-10-19 11:00:58 +0200334 const t = this;
Nils Diewald59c02fc2015-03-07 01:29:09 +0000335
Akronc53cfc82020-10-19 11:00:58 +0200336 switch (_codeFromEvent(e)) {
337
Nils Diewald59c02fc2015-03-07 01:29:09 +0000338 case 27: // 'Esc'
Akrone4961b12017-05-10 21:04:46 +0200339 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200340 t.hide();
Akrone4961b12017-05-10 21:04:46 +0200341 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000342
Nils Diewald59c02fc2015-03-07 01:29:09 +0000343 case 38: // 'Up'
Akrone4961b12017-05-10 21:04:46 +0200344 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200345 t.prev();
Akrone4961b12017-05-10 21:04:46 +0200346 break;
Akronc53cfc82020-10-19 11:00:58 +0200347
Nils Diewald5975d702015-03-09 17:45:42 +0000348 case 33: // 'Page up'
Akrone4961b12017-05-10 21:04:46 +0200349 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200350 t.pageUp();
Akrone4961b12017-05-10 21:04:46 +0200351 break;
Akronc53cfc82020-10-19 11:00:58 +0200352
Nils Diewald5975d702015-03-09 17:45:42 +0000353 case 40: // 'Down'
Akrone4961b12017-05-10 21:04:46 +0200354 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200355 t.next();
Akrone4961b12017-05-10 21:04:46 +0200356 break;
Akronc53cfc82020-10-19 11:00:58 +0200357
Nils Diewald5975d702015-03-09 17:45:42 +0000358 case 34: // 'Page down'
Akrone4961b12017-05-10 21:04:46 +0200359 e.halt();
Akronc53cfc82020-10-19 11:00:58 +0200360 t.pageDown();
Akrone4961b12017-05-10 21:04:46 +0200361 break;
Akronc53cfc82020-10-19 11:00:58 +0200362
Nils Diewald5975d702015-03-09 17:45:42 +0000363 case 39: // 'Right'
Akronc53cfc82020-10-19 11:00:58 +0200364 if (t._prefix.active())
Akrone4961b12017-05-10 21:04:46 +0200365 break;
Nils Diewalde8518f82015-03-18 22:41:49 +0000366
Akronc53cfc82020-10-19 11:00:58 +0200367 const item = t.liveItem(t.position);
Akrone4961b12017-05-10 21:04:46 +0200368
369 if (item["further"] !== undefined) {
370 item["further"].bind(item).apply();
371 };
372
373 e.halt();
374 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000375
Akronc53cfc82020-10-19 11:00:58 +0200376 case 13: // 'Enter'
Akrone4961b12017-05-10 21:04:46 +0200377 // Click on prefix
Akronc53cfc82020-10-19 11:00:58 +0200378 if (t._prefix.active())
379 t._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000380
Akrone4961b12017-05-10 21:04:46 +0200381 // Click on item
382 else
Akronc53cfc82020-10-19 11:00:58 +0200383 t.liveItem(t.position).onclick(e);
Akrone4961b12017-05-10 21:04:46 +0200384 e.halt();
385 break;
Akronc53cfc82020-10-19 11:00:58 +0200386
Nils Diewald59c02fc2015-03-07 01:29:09 +0000387 case 8: // 'Backspace'
Akronc53cfc82020-10-19 11:00:58 +0200388 t._prefix.chop();
389 t.show();
Akrone4961b12017-05-10 21:04:46 +0200390 e.halt();
391 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000392 };
393 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000394
Akronc53cfc82020-10-19 11:00:58 +0200395
Nils Diewald47f366b2015-04-15 20:06:35 +0000396 // Add characters to prefix
397 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200398 if (e.charCode !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200399 e.halt();
Akrone4961b12017-05-10 21:04:46 +0200400
401 // Add prefix
Akronc53cfc82020-10-19 11:00:58 +0200402 this._prefix.add(
403 String.fromCharCode(_codeFromEvent(e))
404 );
405
Akrone4961b12017-05-10 21:04:46 +0200406 this.show();
Akron9c2f9382016-05-25 16:36:04 +0200407 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000408 },
409
Akronc53cfc82020-10-19 11:00:58 +0200410
Akron47c086c2016-05-18 21:22:06 +0200411 /**
Akron5240b8c2016-05-20 09:17:41 +0200412 * Show a screen with a given offset
413 * in the viewport.
Akron47c086c2016-05-18 21:22:06 +0200414 */
415 screen : function (nr) {
Akronc53cfc82020-10-19 11:00:58 +0200416 const t = this;
Akrone817b882018-08-31 14:09:17 +0200417
418 // Normalize negative values
Akron3c2730f2016-05-24 15:08:29 +0200419 if (nr < 0) {
Akrone4961b12017-05-10 21:04:46 +0200420 nr = 0
Akron3c2730f2016-05-24 15:08:29 +0200421 }
Akrone817b882018-08-31 14:09:17 +0200422
423 // The shown list already shows everything
Akronc53cfc82020-10-19 11:00:58 +0200424 else if (t.liveLength() < t.limit()) {
Akrone817b882018-08-31 14:09:17 +0200425 return false;
426 }
427
428 // Move relatively to the next screen
Akronc53cfc82020-10-19 11:00:58 +0200429 else if (nr > (t.liveLength() - t.limit())) {
430 nr = (t.liveLength() - t.limit());
Akron3c2730f2016-05-24 15:08:29 +0200431 };
432
Akrone817b882018-08-31 14:09:17 +0200433 // no change
Akronc53cfc82020-10-19 11:00:58 +0200434 if (t.offset === nr)
Akrone817b882018-08-31 14:09:17 +0200435 return false;
Akron5a1f5bb2016-05-23 22:00:39 +0200436
Akronc53cfc82020-10-19 11:00:58 +0200437 t._showItems(nr);
Akrone817b882018-08-31 14:09:17 +0200438
439 return true;
Akron47c086c2016-05-18 21:22:06 +0200440 },
441
Akrone817b882018-08-31 14:09:17 +0200442
Nils Diewald2fe12e12015-03-06 16:47:06 +0000443 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000444 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000445 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000446 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100447 return this._el;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000448 },
449
Akronc53cfc82020-10-19 11:00:58 +0200450
Nils Diewald2fe12e12015-03-06 16:47:06 +0000451 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000452 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000453 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000454 itemClass : function () {
455 return this._itemClass;
456 },
457
Akronc53cfc82020-10-19 11:00:58 +0200458
Nils Diewald86dad5b2015-01-28 15:09:07 +0000459 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000460 * Get and set the numerical value
461 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000462 */
463 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000464 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200465 if (this._limit !== limit) {
466 this._limit = limit;
467 this._slider.limit(limit).reInit();
468 };
469 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000470 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000471 return this._limit;
472 },
473
Nils Diewald7148c6f2015-05-04 15:07:53 +0000474
Nils Diewald86dad5b2015-01-28 15:09:07 +0000475 /**
476 * Upgrade this object to another object,
477 * while private data stays intact.
478 *
Nils Diewald2fe12e12015-03-06 16:47:06 +0000479 * @param {Object} An object with properties.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000480 */
481 upgradeTo : function (props) {
482 for (var prop in props) {
Akrone4961b12017-05-10 21:04:46 +0200483 this[prop] = props[prop];
Nils Diewald86dad5b2015-01-28 15:09:07 +0000484 };
485 return this;
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 Repp56904d22021-04-26 15:53:22 +0200496 //Upon change please also update alwaysmenu.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
625 removeItems : function () {
626 const liElements=this._el.getElementsByTagName("LI");
627 while (liElements.length>0){
628 this._el.removeChild(liElements[0]);
Nils Diewald5975d702015-03-09 17:45:42 +0000629 };
Leo Reppe5bd35a2021-05-05 15:28:26 +0200630 },
Leo Repp56904d22021-04-26 15:53:22 +0200631
Nils Diewald86dad5b2015-01-28 15:09:07 +0000632
Akronc53cfc82020-10-19 11:00:58 +0200633
Nils Diewald2fe12e12015-03-06 16:47:06 +0000634 /**
635 * Get a specific item from the complete list
636 *
637 * @param {number} index of the list item
638 */
639 item : function (index) {
640 return this._items[index]
641 },
642
643
Nils Diewald86dad5b2015-01-28 15:09:07 +0000644 /**
645 * Get a specific item from the filtered list
646 *
647 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000648 * in the filtered list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000649 */
650 liveItem : function (index) {
651 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200652 if (!this._initList())
653 return;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000654
655 return this._items[this._list[index]];
656 },
657
Nils Diewald86dad5b2015-01-28 15:09:07 +0000658
659 /**
Akron5240b8c2016-05-20 09:17:41 +0200660 * Get a specific item from the viewport list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000661 *
662 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000663 * in the visible list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000664 */
665 shownItem : function (index) {
666 if (index >= this.limit())
Akrone4961b12017-05-10 21:04:46 +0200667 return;
Akronc53cfc82020-10-19 11:00:58 +0200668
Akron9c4d1ae2016-05-25 21:43:22 +0200669 return this.liveItem(this.offset + index);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000670 },
671
672
Nils Diewald2fe12e12015-03-06 16:47:06 +0000673 /**
Akron9c4d1ae2016-05-25 21:43:22 +0200674 * Get the length of the full item list
Nils Diewald2fe12e12015-03-06 16:47:06 +0000675 */
676 length : function () {
677 return this._items.length;
678 },
679
680
681 /**
Akron5240b8c2016-05-20 09:17:41 +0200682 * Length of the filtered item list.
683 */
684 liveLength : function () {
685 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200686 this._initList();
Akron5240b8c2016-05-20 09:17:41 +0200687 return this._list.length;
688 },
689
Akrone817b882018-08-31 14:09:17 +0200690
Akron5240b8c2016-05-20 09:17:41 +0200691 /**
Nils Diewald2fe12e12015-03-06 16:47:06 +0000692 * Make the next item in the filtered menu active
Nils Diewald86dad5b2015-01-28 15:09:07 +0000693 */
694 next : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200695 //Upon change please update alwaysmenu.js next
Akronc53cfc82020-10-19 11:00:58 +0200696 const t = this;
Nils Diewald5975d702015-03-09 17:45:42 +0000697
Akronb38afb22016-05-25 19:30:01 +0200698 // No list
Akronc53cfc82020-10-19 11:00:58 +0200699 if (t.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200700 return;
Akronb38afb22016-05-25 19:30:01 +0200701
Akron9c4d1ae2016-05-25 21:43:22 +0200702 // Deactivate old item
Akronc53cfc82020-10-19 11:00:58 +0200703 if (t.position !== -1 && !t._prefix.active()) {
704 t.liveItem(t.position).active(false);
Akron9c4d1ae2016-05-25 21:43:22 +0200705 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000706
Akron9c4d1ae2016-05-25 21:43:22 +0200707 // Get new active item
Akronc53cfc82020-10-19 11:00:58 +0200708 t.position++;
709 let newItem = t.liveItem(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000710
Nils Diewald5975d702015-03-09 17:45:42 +0000711 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000712 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000713
Akrone4961b12017-05-10 21:04:46 +0200714 // Activate prefix
Akronc53cfc82020-10-19 11:00:58 +0200715 const prefix = this._prefix;
Nils Diewald5975d702015-03-09 17:45:42 +0000716
Akrone4961b12017-05-10 21:04:46 +0200717 // Prefix is set and not active - choose!
718 if (prefix.isSet() && !prefix.active()) {
Akronc53cfc82020-10-19 11:00:58 +0200719 t.position--;
Akrone4961b12017-05-10 21:04:46 +0200720 prefix.active(true);
721 return;
722 }
Akron9c4d1ae2016-05-25 21:43:22 +0200723
Akrone4961b12017-05-10 21:04:46 +0200724 // Choose first item
725 else {
Akronc53cfc82020-10-19 11:00:58 +0200726 newItem = t.liveItem(0);
Akrone4961b12017-05-10 21:04:46 +0200727 // choose first item
Akronc53cfc82020-10-19 11:00:58 +0200728 t.position = 0;
729 t._showItems(0);
Akrone4961b12017-05-10 21:04:46 +0200730 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000731 }
732
Akron5a1f5bb2016-05-23 22:00:39 +0200733 // The next element is after the viewport - roll down
Akronc53cfc82020-10-19 11:00:58 +0200734 else if (t.position >= (t.limit() + t.offset)) {
735 t.screen(t.position - t.limit() + 1);
Akron5a1f5bb2016-05-23 22:00:39 +0200736 }
737
738 // The next element is before the viewport - roll up
Akronc53cfc82020-10-19 11:00:58 +0200739 else if (t.position <= t.offset) {
740 t.screen(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000741 };
Nils Diewald5975d702015-03-09 17:45:42 +0000742
Akronc53cfc82020-10-19 11:00:58 +0200743 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000744 newItem.active(true);
745 },
746
Akronc53cfc82020-10-19 11:00:58 +0200747
Nils Diewalde8518f82015-03-18 22:41:49 +0000748 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000749 * Make the previous item in the menu active
750 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000751 prev : function () {
Leo Repp56904d22021-04-26 15:53:22 +0200752 //Upon Change please update alwaysmenu.js prev
Akronc53cfc82020-10-19 11:00:58 +0200753 const t = this;
Nils Diewald2d210752015-03-09 19:01:15 +0000754
Akronb38afb22016-05-25 19:30:01 +0200755 // No list
Akronc53cfc82020-10-19 11:00:58 +0200756 if (t.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200757 return;
Akronb38afb22016-05-25 19:30:01 +0200758
Akron9c4d1ae2016-05-25 21:43:22 +0200759 // Deactivate old item
Akronc53cfc82020-10-19 11:00:58 +0200760 if (!t._prefix.active()) {
Akron9c4d1ae2016-05-25 21:43:22 +0200761
Akrone4961b12017-05-10 21:04:46 +0200762 // No active element set
Akronc53cfc82020-10-19 11:00:58 +0200763 if (t.position === -1) {
764 t.position = t.liveLength();
Akrone4961b12017-05-10 21:04:46 +0200765 }
Akron9c4d1ae2016-05-25 21:43:22 +0200766
Akrone4961b12017-05-10 21:04:46 +0200767 // No active element set
768 else {
Akronc53cfc82020-10-19 11:00:58 +0200769 t.liveItem(t.position--).active(false);
Akrone4961b12017-05-10 21:04:46 +0200770 };
Nils Diewald2d210752015-03-09 19:01:15 +0000771 };
772
Akron9c4d1ae2016-05-25 21:43:22 +0200773 // Get new active item
Akronc53cfc82020-10-19 11:00:58 +0200774 let newItem = t.liveItem(t.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000775
776 // The previous element is undefined - roll to bottom
777 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000778
Akrone4961b12017-05-10 21:04:46 +0200779 // Activate prefix
Akronc53cfc82020-10-19 11:00:58 +0200780 const prefix = t._prefix;
781 let offset = t.liveLength() - t.limit();
Akrone4961b12017-05-10 21:04:46 +0200782
783 // Normalize offset
784 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000785
Akrone4961b12017-05-10 21:04:46 +0200786 // Choose the last item
Akronc53cfc82020-10-19 11:00:58 +0200787 t.position = t.liveLength() - 1;
Akrone4961b12017-05-10 21:04:46 +0200788
789 // Prefix is set and not active - choose!
790 if (prefix.isSet() && !prefix.active()) {
Akronc53cfc82020-10-19 11:00:58 +0200791 t.position++;
Akrone4961b12017-05-10 21:04:46 +0200792 prefix.active(true);
Akronc53cfc82020-10-19 11:00:58 +0200793 t.offset = offset;
Akrone4961b12017-05-10 21:04:46 +0200794 return;
795 }
Nils Diewald2d210752015-03-09 19:01:15 +0000796
Akrone4961b12017-05-10 21:04:46 +0200797 // Choose last item
798 else {
Akronc53cfc82020-10-19 11:00:58 +0200799 newItem = t.liveItem(t.position);
800 t._showItems(offset);
Akrone4961b12017-05-10 21:04:46 +0200801 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000802 }
803
Akron5a1f5bb2016-05-23 22:00:39 +0200804 // The previous element is before the view - roll up
Akronc53cfc82020-10-19 11:00:58 +0200805 else if (t.position < t.offset) {
806 t.screen(t.position);
Akron5a1f5bb2016-05-23 22:00:39 +0200807 }
808
809 // The previous element is after the view - roll down
Akronc53cfc82020-10-19 11:00:58 +0200810 else if (t.position >= (t.limit() + t.offset)) {
811 t.screen(t.position - t.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000812 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000813
Akronc53cfc82020-10-19 11:00:58 +0200814 t._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000815 newItem.active(true);
816 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000817
Akronc53cfc82020-10-19 11:00:58 +0200818
Akron3c2730f2016-05-24 15:08:29 +0200819 /**
820 * Move the page up by limit!
821 */
822 pageUp : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200823 this.screen(this.offset - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200824 },
825
826
827 /**
828 * Move the page down by limit!
829 */
830 pageDown : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200831 this.screen(this.offset + this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200832 },
833
Nils Diewald86dad5b2015-01-28 15:09:07 +0000834
Akrone817b882018-08-31 14:09:17 +0200835 /**
836 * Move the view one item up
837 */
838 viewUp : function () {
839 this.screen(this.offset - 1);
840 },
841
842
843 /**
844 * Move the view one item down
845 */
846 viewDown : function () {
847 this.screen(this.offset + 1);
848 },
849
Akronc53cfc82020-10-19 11:00:58 +0200850
Akron5240b8c2016-05-20 09:17:41 +0200851 // Unmark all items
852 _unmark : function () {
Akron678c26f2020-10-09 08:52:50 +0200853 this._list.forEach(function(it){
Akronc53cfc82020-10-19 11:00:58 +0200854 const item = this._items[it];
Akrone4961b12017-05-10 21:04:46 +0200855 item.lowlight();
Akron678c26f2020-10-09 08:52:50 +0200856 item.active(false);
857 }, this);
Akron5240b8c2016-05-20 09:17:41 +0200858 },
859
Akronc53cfc82020-10-19 11:00:58 +0200860
Akron5240b8c2016-05-20 09:17:41 +0200861 // Set boundary for viewport
862 _boundary : function (bool) {
Akron55a343b2018-04-06 19:57:36 +0200863 if (this._list.length === 0)
864 return;
Akronc53cfc82020-10-19 11:00:58 +0200865
Akron5240b8c2016-05-20 09:17:41 +0200866 this.item(this._list[0]).noMore(bool);
867 this.item(this._list[this._list.length - 1]).noMore(bool);
868 },
869
870
871 // Append Items that should be shown
872 _showItems : function (off) {
Akronc53cfc82020-10-19 11:00:58 +0200873 const t = this;
Akron5240b8c2016-05-20 09:17:41 +0200874
Akrona92fd8d2016-05-24 21:13:41 +0200875 // optimization: scroll down one step
Akronc53cfc82020-10-19 11:00:58 +0200876 if (t.offset === (off - 1)) {
877 t.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200878
Akrone4961b12017-05-10 21:04:46 +0200879 // Remove the HTML node from the first item
880 // leave lengthField/prefix/slider
Leo Repp56904d22021-04-26 15:53:22 +0200881 t._el.removeChild(t._el.children[this._notItemElements]);
Akronc53cfc82020-10-19 11:00:58 +0200882
883 t._append(
884 t._list[t.offset + t.limit() - 1]
885 );
Akrona92fd8d2016-05-24 21:13:41 +0200886 }
Akron5240b8c2016-05-20 09:17:41 +0200887
Akrona92fd8d2016-05-24 21:13:41 +0200888 // optimization: scroll up one step
Akronc53cfc82020-10-19 11:00:58 +0200889 else if (t.offset === (off + 1)) {
890 t.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200891
Akrone4961b12017-05-10 21:04:46 +0200892 // Remove the HTML node from the last item
Akron24aa0052020-11-10 11:00:34 +0100893 t._el.removeChild(t._el.lastChild);
Akron9c4d1ae2016-05-25 21:43:22 +0200894
Akronc53cfc82020-10-19 11:00:58 +0200895 t._prepend(t._list[t.offset]);
Akrona92fd8d2016-05-24 21:13:41 +0200896 }
Akronc53cfc82020-10-19 11:00:58 +0200897
Akrona92fd8d2016-05-24 21:13:41 +0200898 else {
Akronc53cfc82020-10-19 11:00:58 +0200899 t.offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200900
Akrone4961b12017-05-10 21:04:46 +0200901 // Remove all items
Akronc53cfc82020-10-19 11:00:58 +0200902 t.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200903
Akrone4961b12017-05-10 21:04:46 +0200904 // Use list
Akronc53cfc82020-10-19 11:00:58 +0200905 let shown = 0;
Akron5240b8c2016-05-20 09:17:41 +0200906
Akronc53cfc82020-10-19 11:00:58 +0200907 for (let i = 0; i < t._list.length; i++) {
Akrona92fd8d2016-05-24 21:13:41 +0200908
Akrone4961b12017-05-10 21:04:46 +0200909 // Don't show - it's before offset
910 shown++;
911 if (shown <= off)
912 continue;
Akrona92fd8d2016-05-24 21:13:41 +0200913
Akronc53cfc82020-10-19 11:00:58 +0200914 t._append(t._list[i]);
Akrone4961b12017-05-10 21:04:46 +0200915
Akronc53cfc82020-10-19 11:00:58 +0200916 if (shown >= (t.limit() + off))
Akrone4961b12017-05-10 21:04:46 +0200917 break;
918 };
Akron5240b8c2016-05-20 09:17:41 +0200919 };
920
921 // set the slider to the new offset
Akronc53cfc82020-10-19 11:00:58 +0200922 t._slider.offset(t.offset);
Akron5240b8c2016-05-20 09:17:41 +0200923 },
924
925
926 // Append item to the shown list based on index
927 _append : function (i) {
Akronc53cfc82020-10-19 11:00:58 +0200928 const item = this.item(i);
Akron5240b8c2016-05-20 09:17:41 +0200929
930 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200931 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200932 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200933 };
934
Akron5240b8c2016-05-20 09:17:41 +0200935 // Append element
936 this.element().appendChild(item.element());
937 },
938
939
940 // Prepend item to the shown list based on index
941 _prepend : function (i) {
Akronc53cfc82020-10-19 11:00:58 +0200942 const item = this.item(i);
Akron5240b8c2016-05-20 09:17:41 +0200943
944 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200945 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200946 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200947 };
Akron5240b8c2016-05-20 09:17:41 +0200948
Akronc53cfc82020-10-19 11:00:58 +0200949 const e = this.element();
Akron9c4d1ae2016-05-25 21:43:22 +0200950
Akron5240b8c2016-05-20 09:17:41 +0200951 // Append element after lengthField/prefix/slider
952 e.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200953 item.element(),
Leo Repp56904d22021-04-26 15:53:22 +0200954 e.children[this._notItemElements]
Akron5240b8c2016-05-20 09:17:41 +0200955 );
Nils Diewald2fe12e12015-03-06 16:47:06 +0000956 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000957 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000958});