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