Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 1 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 2 | * Scrollable drop-down menus with view filter. |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * @author Nils Diewald |
| 5 | */ |
Nils Diewald | 2488d05 | 2015-04-09 21:46:02 +0000 | [diff] [blame] | 6 | /* |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 7 | * TODO: Show the slider briefly on move (whenever screen is called). |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 8 | * TODO: Ignore alt+ and strg+ key strokes. |
Akron | 0b92f69 | 2016-05-25 22:37:13 +0200 | [diff] [blame] | 9 | * TODO: Should scroll to a chosen value after prefixing, if the chosen value is live |
Akron | 201b72a | 2016-06-03 01:46:19 +0200 | [diff] [blame] | 10 | * 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. |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 13 | * 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". |
Akron | e91da78 | 2017-12-15 17:17:50 +0100 | [diff] [blame] | 15 | * TODO: Support space separated list of prefixes so "co no" will highlight "common noun" |
Nils Diewald | 2488d05 | 2015-04-09 21:46:02 +0000 | [diff] [blame] | 16 | */ |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 17 | |
| 18 | "use strict"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | define([ |
| 20 | 'menu/item', |
| 21 | 'menu/prefix', |
Akron | c744873 | 2016-04-27 14:06:58 +0200 | [diff] [blame] | 22 | 'menu/lengthField', |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 23 | 'menu/slider', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 24 | 'util' |
| 25 | ], function (defaultItemClass, |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 26 | defaultPrefixClass, |
| 27 | defaultLengthFieldClass, |
| 28 | sliderClass) { |
Nils Diewald | fda29d9 | 2015-01-22 17:28:01 +0000 | [diff] [blame] | 29 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 30 | // Default maximum number of menu items |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 31 | const menuLimit = 8; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 32 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 33 | /** |
| 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 Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 38 | return { |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Create new Menu based on the action prefix |
| 41 | * and a list of menu items. |
| 42 | * |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 43 | * |
| 44 | * Accepts an associative array containg the elements |
| 45 | * itemClass, prefixClass, lengthFieldClass |
| 46 | * |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 47 | * @this {Menu} |
| 48 | * @constructor |
| 49 | * @param {string} Context prefix |
| 50 | * @param {Array.<Array.<string>>} List of menu items |
| 51 | */ |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 52 | create : function (list, params) { |
| 53 | return Object.create(this)._init(list, params); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 54 | }, |
| 55 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 56 | // Initialize list |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 57 | _init : function (list, params) { |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 58 | if (params === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 59 | params = {}; |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 60 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 61 | const t = this; |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 62 | t._notItemElements=3; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 63 | |
| 64 | t._itemClass = params["itemClass"] || defaultItemClass; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 65 | |
| 66 | // Add prefix object |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 67 | if (params["prefixClass"] !== undefined) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 68 | t._prefix = params["prefixClass"].create(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 69 | } |
| 70 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 71 | t._prefix = defaultPrefixClass.create(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 72 | }; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 73 | t._prefix._menu = t; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 74 | |
| 75 | // Add lengthField object |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 76 | if (params["lengthFieldClass"] !== undefined) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 77 | t._lengthField = params["lengthFieldClass"].create(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 78 | } |
| 79 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 80 | t._lengthField = defaultLengthFieldClass.create(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 81 | }; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 82 | t._lengthField._menu = t; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 83 | |
| 84 | // Initialize slider |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 85 | t._slider = sliderClass.create(t); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 86 | |
| 87 | // Create the element |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 88 | var el = document.createElement("ul"); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 89 | 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()); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 95 | |
| 96 | // This has to be cleaned up later on |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 97 | el["menu"] = t; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 98 | |
| 99 | // Arrow keys |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 100 | el.addEventListener( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 101 | 'keydown', |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 102 | t._keydown.bind(t), |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 103 | false |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 104 | ); |
| 105 | |
| 106 | // Strings |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 107 | el.addEventListener( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 108 | 'keypress', |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 109 | t._keypress.bind(t), |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 110 | false |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 111 | ); |
| 112 | |
| 113 | // Mousewheel |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 114 | el.addEventListener( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 115 | 'wheel', |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 116 | t._mousewheel.bind(t), |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 117 | false |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 118 | ); |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 119 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 120 | // Touch events |
| 121 | ['touchstart', 'touchend', 'touchmove'].forEach( |
| 122 | e => el.addEventListener(e, t._touch.bind(t), false) |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 123 | ); |
| 124 | |
| 125 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 126 | t._el = el; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 127 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 128 | t._limit = menuLimit; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 129 | |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 130 | t._items = new Array(); //all childNodes, i.e. ItemClass, prefixClass |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 131 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 132 | t.readItems(list); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 133 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 134 | t.dontHide = false; |
hebasta | f95226b | 2019-09-19 11:37:00 +0200 | [diff] [blame] | 135 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 136 | return t; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 137 | }, |
| 138 | |
| 139 | // Read items to add to list |
| 140 | readItems : function (list) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 141 | const t = this; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 142 | |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 143 | t._list = undefined; //filtered List containing all itemClass items |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 144 | |
| 145 | // Remove circular reference to "this" in items |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 146 | for (let i = 0; i < t._items.length; i++) { |
| 147 | delete t._items[i]["_menu"]; |
| 148 | delete t._items[i]; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 151 | t._items = new Array(); |
| 152 | t.removeItems(); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 153 | |
| 154 | |
| 155 | // Initialize items |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 156 | t._lengthField.reset(); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 157 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 158 | // Initialize item list based on parameters |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 159 | list.forEach(function(i){ |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 160 | const obj = this._itemClass.create(i); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 161 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 162 | // This may become circular |
| 163 | obj["_menu"] = this; |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 164 | this._lengthField.add(i); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 165 | this._items.push(obj); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 166 | }, t); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 167 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 168 | t._slider.length(t.liveLength()) |
| 169 | .limit(t._limit) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 170 | .reInit(); |
| 171 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 172 | t._firstActive = false; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 173 | // Show the first item active always? |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 174 | t.offset = 0; |
| 175 | t.position = 0; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 176 | }, |
Akron | 8aa1c52 | 2021-07-23 11:33:49 +0200 | [diff] [blame] | 177 | |
| 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 | }, |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 192 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 193 | // Initialize the item list |
Leo Repp | 8453916 | 2021-10-25 12:06:07 +0200 | [diff] [blame] | 194 | // 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. |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 196 | _initList : function () { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 197 | // Upon change also update alwaysmenu.js please |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 198 | const t = this; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 199 | |
| 200 | // Create a new list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 201 | if (t._list === undefined) { |
| 202 | t._list = []; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 203 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 204 | else if (t._list.length !== 0) { |
| 205 | t._boundary(false); |
| 206 | t._list.length = 0; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | // Offset is initially zero |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 210 | t.offset = 0; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 211 | |
| 212 | // There is no prefix set |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 213 | if (t.prefix().length <= 0) { |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 214 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 215 | // add all items to the list and lowlight |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 216 | let i = 0; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 217 | for (; i < t._items.length; i++) { |
| 218 | t._list.push(i); |
| 219 | t._items[i].lowlight(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 220 | }; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 221 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 222 | t._slider.length(i).reInit(); |
Akron | 97752a7 | 2016-05-25 14:43:07 +0200 | [diff] [blame] | 223 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 224 | return true; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | /* |
| 228 | * There is a prefix set, so filter the list! |
| 229 | */ |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 230 | let pos; |
| 231 | const prefixList = t.prefix().toLowerCase().split(" "); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 232 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 233 | const items = []; |
| 234 | let maxPoints = 1; // minimum 1 |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 235 | |
| 236 | // Iterate over all items and choose preferred matching items |
| 237 | // i.e. the matching happens at the word start |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 238 | t._items.forEach(function(it, pos){ |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 239 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 240 | let points = 0; |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 241 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 242 | prefixList.forEach(function(p) { |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 243 | |
| 244 | // Check if it matches at the beginning |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 245 | if ((it.lcField().includes(" " + p))) { |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 246 | points += 5; |
| 247 | } |
| 248 | |
| 249 | // Check if it matches anywhere |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 250 | else if (it.lcField().includes(p)) { |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 251 | points += 1; |
| 252 | }; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 253 | }); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 254 | |
| 255 | if (points > maxPoints) { |
| 256 | this._list = [pos]; |
| 257 | maxPoints = points; |
| 258 | } |
| 259 | else if (points == maxPoints) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 260 | this._list.push(pos); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 261 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 262 | }, t); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 263 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 264 | t._slider.length(t._list.length).reInit(); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 265 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 266 | // Filter was successful - yeah! |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 267 | return t._list.length > 0 ? true : false; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 268 | }, |
| 269 | |
| 270 | |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 271 | /** |
| 272 | * Destroy this menu |
| 273 | * (in case you don't trust the |
| 274 | * mark and sweep GC)! |
| 275 | */ |
| 276 | destroy : function () { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 277 | // Upon change also update alwaysmenu.js please |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 278 | const t = this; |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 279 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 280 | // Remove circular reference to "this" in menu |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 281 | if (t._el != undefined) |
| 282 | delete t._el["menu"]; |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 283 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 284 | // Remove circular reference to "this" in items |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 285 | t._items.forEach(function(i) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 286 | delete i["_menu"]; |
| 287 | }); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 288 | |
| 289 | // Remove circular reference to "this" in prefix |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 290 | delete t._prefix['_menu']; |
| 291 | delete t._lengthField['_menu']; |
| 292 | delete t._slider['_menu']; |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 293 | }, |
| 294 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * Focus on this menu. |
| 298 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 299 | focus : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 300 | this._el.focus(); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 301 | }, |
| 302 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 303 | |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 304 | // mouse wheel treatment |
| 305 | _mousewheel : function (e) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 306 | const delta = e.deltaY / 120; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 307 | if (delta > 0) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 308 | this.next(); |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 309 | else if (delta < 0) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 310 | this.prev(); |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 311 | e.halt(); |
| 312 | }, |
| 313 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 314 | |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 315 | // touchmove treatment |
| 316 | _touch : function (e) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 317 | const s = this.slider(); |
| 318 | |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 319 | if (e.type === "touchstart") { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 320 | this._lastTouch = e.touches[0].clientY; |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 321 | } |
| 322 | else if (e.type === "touchend") { |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 323 | this._lastTouch = undefined; |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 324 | } |
| 325 | else if (e.type === "touchmove") { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 326 | const to = e.touches[0]; |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 327 | |
| 328 | // TODO: |
| 329 | // Instead of using 26px, choose the item height |
| 330 | // or use the menu height // shownItems |
| 331 | |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 332 | // s.movetoRel(t.clientY - this._initTouch); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 333 | if ((this._lastTouch + 26) < to.clientY) { |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 334 | this.viewDown(); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 335 | this._lastTouch = to.clientY; |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 336 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 337 | else if ((this._lastTouch - 26) > to.clientY) { |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 338 | this.viewUp(); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 339 | this._lastTouch = to.clientY; |
Akron | a1159ff | 2018-07-22 13:28:31 +0200 | [diff] [blame] | 340 | } |
| 341 | e.halt(); |
| 342 | }; |
| 343 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 344 | |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 345 | // Arrow key and prefix treatment |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 346 | _keydown : function (e) { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 347 | //Upon change also update alwaysmenu.js please |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 348 | const t = this; |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 349 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 350 | switch (_codeFromEvent(e)) { |
| 351 | |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 352 | case 27: // 'Esc' |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 353 | e.halt(); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 354 | t.hide(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 355 | break; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 356 | |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 357 | case 38: // 'Up' |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 358 | e.halt(); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 359 | t.prev(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 360 | break; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 361 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 362 | case 33: // 'Page up' |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 363 | e.halt(); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 364 | t.pageUp(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 365 | break; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 366 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 367 | case 40: // 'Down' |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 368 | e.halt(); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 369 | t.next(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 370 | break; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 371 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 372 | case 34: // 'Page down' |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 373 | e.halt(); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 374 | t.pageDown(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 375 | break; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 376 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 377 | case 39: // 'Right' |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 378 | if (t._prefix.active()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 379 | break; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 380 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 381 | const item = t.liveItem(t.position); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 382 | |
| 383 | if (item["further"] !== undefined) { |
| 384 | item["further"].bind(item).apply(); |
| 385 | }; |
| 386 | |
| 387 | e.halt(); |
| 388 | break; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 389 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 390 | case 13: // 'Enter' |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 391 | // Click on prefix |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 392 | if (t._prefix.active()) |
| 393 | t._prefix.onclick(e); |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 394 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 395 | // Click on item |
| 396 | else |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 397 | t.liveItem(t.position).onclick(e); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 398 | e.halt(); |
| 399 | break; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 400 | |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 401 | case 8: // 'Backspace' |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 402 | t._prefix.chop(); |
| 403 | t.show(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 404 | e.halt(); |
| 405 | break; |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 406 | }; |
| 407 | }, |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 408 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 409 | |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 410 | // Add characters to prefix |
| 411 | _keypress : function (e) { |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 412 | if (e.charCode !== 0) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 413 | e.halt(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 414 | |
| 415 | // Add prefix |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 416 | this._prefix.add( |
| 417 | String.fromCharCode(_codeFromEvent(e)) |
| 418 | ); |
| 419 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 420 | this.show(); |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 421 | }; |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 422 | }, |
| 423 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 424 | |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 425 | /** |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 426 | * Show a screen with a given offset |
| 427 | * in the viewport. |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 428 | */ |
| 429 | screen : function (nr) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 430 | const t = this; |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 431 | |
| 432 | // Normalize negative values |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 433 | if (nr < 0) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 434 | nr = 0 |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 435 | } |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 436 | |
| 437 | // The shown list already shows everything |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 438 | else if (t.liveLength() < t.limit()) { |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
| 442 | // Move relatively to the next screen |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 443 | else if (nr > (t.liveLength() - t.limit())) { |
| 444 | nr = (t.liveLength() - t.limit()); |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 445 | }; |
| 446 | |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 447 | // no change |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 448 | if (t.offset === nr) |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 449 | return false; |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 450 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 451 | t._showItems(nr); |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 452 | |
| 453 | return true; |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 454 | }, |
| 455 | |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 456 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 457 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 458 | * Get the associated dom element. |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 459 | */ |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 460 | element : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 461 | return this._el; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 462 | }, |
| 463 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 464 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 465 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 466 | * Get the creator class for items |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 467 | */ |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 468 | itemClass : function () { |
| 469 | return this._itemClass; |
| 470 | }, |
| 471 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 472 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 473 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 474 | * Get and set the numerical value |
| 475 | * for the maximum number of items visible. |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 476 | */ |
| 477 | limit : function (limit) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 478 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 479 | if (this._limit !== limit) { |
| 480 | this._limit = limit; |
| 481 | this._slider.limit(limit).reInit(); |
| 482 | }; |
| 483 | return this; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 484 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 485 | return this._limit; |
| 486 | }, |
| 487 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 488 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 489 | /** |
Akron | 97752a7 | 2016-05-25 14:43:07 +0200 | [diff] [blame] | 490 | * Filter the list and make it visible. |
| 491 | * This is always called once the prefix changes. |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 492 | * |
| 493 | * @param {string} Prefix for filtering the list |
| 494 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 495 | show : function (active) { |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 496 | //Upon change please also update alwaysmenu.js and containermenu.js (only two lines new there) |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 497 | const t = this; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 498 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 499 | // show menu based on initial offset |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 500 | t._unmark(); // Unmark everything that was marked before |
| 501 | t.removeItems(); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 502 | |
| 503 | // Initialize the list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 504 | if (!t._initList()) { |
Akron | 6ac5844 | 2016-05-24 16:52:29 +0200 | [diff] [blame] | 505 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 506 | // The prefix is not active |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 507 | t._prefix.active(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 508 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 509 | // finally show the element |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 510 | t._el.classList.add('visible'); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 511 | |
| 512 | return true; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 513 | }; |
| 514 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 515 | let offset = 0; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 516 | |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 517 | // Set a chosen value to active and move the viewport |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 518 | if (arguments.length === 1) { |
| 519 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 520 | // Normalize active value |
| 521 | if (active < 0) { |
| 522 | active = 0; |
| 523 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 524 | else if (active >= t.liveLength()) { |
| 525 | active = t.liveLength() - 1; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 526 | }; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 527 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 528 | // Item is outside the first viewport |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 529 | if (active >= t._limit) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 530 | offset = active; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 531 | const newOffset = t.liveLength() - t._limit; |
| 532 | if (offset > newOffset) { |
| 533 | offset = newOffset; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 534 | }; |
| 535 | }; |
| 536 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 537 | t.position = active; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 540 | // Choose the first item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 541 | else if (t._firstActive) { |
| 542 | t.position = 0; |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 543 | } |
Akron | cb351d6 | 2016-05-19 23:10:33 +0200 | [diff] [blame] | 544 | |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 545 | // Choose no item |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 546 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 547 | t.position = -1; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 548 | }; |
| 549 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 550 | t.offset = offset; |
| 551 | t._showItems(offset); // Show new item list |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 552 | |
| 553 | // Make chosen value active |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 554 | if (t.position !== -1) { |
| 555 | t.liveItem(t.position).active(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 556 | }; |
Akron | 37513a6 | 2015-11-17 01:07:11 +0100 | [diff] [blame] | 557 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 558 | // The prefix is not active |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 559 | t._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 560 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 561 | // finally show the element |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 562 | t._el.classList.add('visible'); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 563 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 564 | // Add classes for rolling menus |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 565 | t._boundary(true); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 566 | |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 567 | return true; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 568 | }, |
| 569 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 570 | |
| 571 | /** |
| 572 | * Hide the menu and call the onHide callback. |
| 573 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 574 | hide : function () { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 575 | if (!this.dontHide) { |
| 576 | this.removeItems(); |
| 577 | this._prefix.clear(); |
| 578 | this.onHide(); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 579 | this._el.classList.remove('visible'); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 580 | } |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 581 | // this._el.blur(); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 582 | }, |
| 583 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 584 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 585 | /** |
| 586 | * Function released when the menu hides. |
| 587 | * This method is expected to be overridden. |
| 588 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 589 | onHide : function () {}, |
| 590 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 591 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 592 | /** |
| 593 | * Get the prefix for filtering, |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 594 | * e.g. "ve" for "verb" |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 595 | */ |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 596 | prefix : function (pref) { |
| 597 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 598 | this._prefix.value(pref); |
| 599 | return this; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 600 | }; |
| 601 | return this._prefix.value(); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 602 | }, |
| 603 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 604 | |
Akron | c744873 | 2016-04-27 14:06:58 +0200 | [diff] [blame] | 605 | /** |
| 606 | * Get the lengthField object. |
| 607 | */ |
| 608 | lengthField : function () { |
| 609 | return this._lengthField; |
| 610 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 611 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 612 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 613 | /** |
| 614 | * Get the associated slider object. |
| 615 | */ |
| 616 | slider : function () { |
| 617 | return this._slider; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 618 | }, |
| 619 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 620 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 621 | /** |
| 622 | * Delete all visible items from the menu element |
| 623 | */ |
Leo Repp | e5bd35a | 2021-05-05 15:28:26 +0200 | [diff] [blame] | 624 | |
Leo Repp | afcf984 | 2021-06-30 13:01:09 +0200 | [diff] [blame] | 625 | removeItems : function () { |
Leo Repp | e5bd35a | 2021-05-05 15:28:26 +0200 | [diff] [blame] | 626 | const liElements=this._el.getElementsByTagName("LI"); |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 627 | 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 Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 634 | }; |
Leo Repp | e5bd35a | 2021-05-05 15:28:26 +0200 | [diff] [blame] | 635 | }, |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 636 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 637 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 638 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 639 | /** |
| 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 Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 649 | /** |
| 650 | * Get a specific item from the filtered list |
| 651 | * |
| 652 | * @param {number} index of the list item |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 653 | * in the filtered list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 654 | */ |
| 655 | liveItem : function (index) { |
| 656 | if (this._list === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 657 | if (!this._initList()) |
| 658 | return; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 659 | |
| 660 | return this._items[this._list[index]]; |
| 661 | }, |
| 662 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 663 | |
| 664 | /** |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 665 | * Get a specific item from the viewport list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 666 | * |
| 667 | * @param {number} index of the list item |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 668 | * in the visible list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 669 | */ |
| 670 | shownItem : function (index) { |
| 671 | if (index >= this.limit()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 672 | return; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 673 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 674 | return this.liveItem(this.offset + index); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 675 | }, |
| 676 | |
| 677 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 678 | /** |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 679 | * Get the length of the full item list |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 680 | */ |
| 681 | length : function () { |
| 682 | return this._items.length; |
| 683 | }, |
| 684 | |
| 685 | |
| 686 | /** |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 687 | * Length of the filtered item list. |
| 688 | */ |
| 689 | liveLength : function () { |
| 690 | if (this._list === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 691 | this._initList(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 692 | return this._list.length; |
| 693 | }, |
| 694 | |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 695 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 696 | /** |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 697 | * Make the next item in the filtered menu active |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 698 | */ |
| 699 | next : function () { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 700 | //Upon change please update alwaysmenu.js next |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 701 | const t = this; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 702 | |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 703 | // No list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 704 | if (t.liveLength() === 0) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 705 | return; |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 706 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 707 | // Deactivate old item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 708 | if (t.position !== -1 && !t._prefix.active()) { |
| 709 | t.liveItem(t.position).active(false); |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 710 | }; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 711 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 712 | // Get new active item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 713 | t.position++; |
| 714 | let newItem = t.liveItem(t.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 715 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 716 | // The next element is undefined - roll to top or to prefix |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 717 | if (newItem === undefined) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 718 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 719 | // Activate prefix |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 720 | const prefix = this._prefix; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 721 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 722 | // Prefix is set and not active - choose! |
| 723 | if (prefix.isSet() && !prefix.active()) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 724 | t.position--; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 725 | prefix.active(true); |
| 726 | return; |
| 727 | } |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 728 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 729 | // Choose first item |
| 730 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 731 | newItem = t.liveItem(0); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 732 | // choose first item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 733 | t.position = 0; |
| 734 | t._showItems(0); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 735 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 738 | // The next element is after the viewport - roll down |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 739 | else if (t.position >= (t.limit() + t.offset)) { |
| 740 | t.screen(t.position - t.limit() + 1); |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // The next element is before the viewport - roll up |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 744 | else if (t.position <= t.offset) { |
| 745 | t.screen(t.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 746 | }; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 747 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 748 | t._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 749 | newItem.active(true); |
| 750 | }, |
| 751 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 752 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 753 | /* |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 754 | * Make the previous item in the menu active |
| 755 | */ |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 756 | prev : function () { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 757 | //Upon Change please update alwaysmenu.js prev |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 758 | const t = this; |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 759 | |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 760 | // No list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 761 | if (t.liveLength() === 0) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 762 | return; |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 763 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 764 | // Deactivate old item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 765 | if (!t._prefix.active()) { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 766 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 767 | // No active element set |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 768 | if (t.position === -1) { |
| 769 | t.position = t.liveLength(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 770 | } |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 771 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 772 | // No active element set |
| 773 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 774 | t.liveItem(t.position--).active(false); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 775 | }; |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 776 | }; |
| 777 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 778 | // Get new active item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 779 | let newItem = t.liveItem(t.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 780 | |
| 781 | // The previous element is undefined - roll to bottom |
| 782 | if (newItem === undefined) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 783 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 784 | // Activate prefix |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 785 | const prefix = t._prefix; |
| 786 | let offset = t.liveLength() - t.limit(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 787 | |
| 788 | // Normalize offset |
| 789 | offset = offset < 0 ? 0 : offset; |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 790 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 791 | // Choose the last item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 792 | t.position = t.liveLength() - 1; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 793 | |
| 794 | // Prefix is set and not active - choose! |
| 795 | if (prefix.isSet() && !prefix.active()) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 796 | t.position++; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 797 | prefix.active(true); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 798 | t.offset = offset; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 799 | return; |
| 800 | } |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 801 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 802 | // Choose last item |
| 803 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 804 | newItem = t.liveItem(t.position); |
| 805 | t._showItems(offset); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 806 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 809 | // The previous element is before the view - roll up |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 810 | else if (t.position < t.offset) { |
| 811 | t.screen(t.position); |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | // The previous element is after the view - roll down |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 815 | else if (t.position >= (t.limit() + t.offset)) { |
| 816 | t.screen(t.position - t.limit() + 2); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 817 | }; |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 818 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 819 | t._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 820 | newItem.active(true); |
| 821 | }, |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 822 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 823 | |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 824 | /** |
| 825 | * Move the page up by limit! |
| 826 | */ |
| 827 | pageUp : function () { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 828 | this.screen(this.offset - this.limit()); |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 829 | }, |
| 830 | |
| 831 | |
| 832 | /** |
| 833 | * Move the page down by limit! |
| 834 | */ |
| 835 | pageDown : function () { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 836 | this.screen(this.offset + this.limit()); |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 837 | }, |
| 838 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 839 | |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 840 | /** |
| 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 Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 855 | /** |
| 856 | * Reset the prefix. Currently not used in regular menu. |
| 857 | */ |
| 858 | reset : function () { |
| 859 | this.prefix(""); |
| 860 | }, |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 861 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 862 | // Unmark all items |
| 863 | _unmark : function () { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 864 | this._list.forEach(function(it){ |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 865 | const item = this._items[it]; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 866 | item.lowlight(); |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 867 | item.active(false); |
| 868 | }, this); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 869 | }, |
| 870 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 871 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 872 | // Set boundary for viewport |
| 873 | _boundary : function (bool) { |
Akron | 55a343b | 2018-04-06 19:57:36 +0200 | [diff] [blame] | 874 | if (this._list.length === 0) |
| 875 | return; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 876 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 877 | 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) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 884 | const t = this; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 885 | |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 886 | // optimization: scroll down one step |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 887 | if (t.offset === (off - 1)) { |
| 888 | t.offset = off; |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 889 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 890 | // Remove the HTML node from the first item |
| 891 | // leave lengthField/prefix/slider |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 892 | //console.log("_showItems, at _notItemElements is: ",t._el.children[this._notItemElements]); |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 893 | t._el.removeChild(t._el.children[this._notItemElements]); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 894 | |
| 895 | t._append( |
| 896 | t._list[t.offset + t.limit() - 1] |
| 897 | ); |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 898 | } |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 899 | |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 900 | // optimization: scroll up one step |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 901 | else if (t.offset === (off + 1)) { |
| 902 | t.offset = off; |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 903 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 904 | // Remove the HTML node from the last item |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 905 | //console.log("_showItems, at lastChild is: ",t._el.lastChild); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 906 | t._el.removeChild(t._el.lastChild); |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 907 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 908 | t._prepend(t._list[t.offset]); |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 909 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 910 | |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 911 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 912 | t.offset = off; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 913 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 914 | // Remove all items |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 915 | t.removeItems(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 916 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 917 | // Use list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 918 | let shown = 0; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 919 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 920 | for (let i = 0; i < t._list.length; i++) { |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 921 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 922 | // Don't show - it's before offset |
| 923 | shown++; |
| 924 | if (shown <= off) |
| 925 | continue; |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 926 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 927 | t._append(t._list[i]); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 928 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 929 | if (shown >= (t.limit() + off)) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 930 | break; |
| 931 | }; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 932 | }; |
| 933 | |
| 934 | // set the slider to the new offset |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 935 | t._slider.offset(t.offset); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 936 | }, |
| 937 | |
| 938 | |
| 939 | // Append item to the shown list based on index |
| 940 | _append : function (i) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 941 | const item = this.item(i); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 942 | |
| 943 | // Highlight based on prefix |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 944 | if (this.prefix().length > 0) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 945 | item.highlight(this.prefix().toLowerCase()); |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 946 | }; |
| 947 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 948 | // 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) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 955 | const item = this.item(i); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 956 | |
| 957 | // Highlight based on prefix |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 958 | if (this.prefix().length > 0) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 959 | item.highlight(this.prefix().toLowerCase()); |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 960 | }; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 961 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 962 | const e = this.element(); |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 963 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 964 | // Append element after lengthField/prefix/slider |
| 965 | e.insertBefore( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 966 | item.element(), |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 967 | e.children[this._notItemElements] |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 968 | ); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 969 | } |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 970 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 971 | }); |