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