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 | /** |
| 488 | * Upgrade this object to another object, |
| 489 | * while private data stays intact. |
| 490 | * |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 491 | * @param {Object} An object with properties. |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 492 | */ |
| 493 | upgradeTo : function (props) { |
| 494 | for (var prop in props) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 495 | this[prop] = props[prop]; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 496 | }; |
| 497 | return this; |
| 498 | }, |
| 499 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 500 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 501 | /** |
Akron | 97752a7 | 2016-05-25 14:43:07 +0200 | [diff] [blame] | 502 | * Filter the list and make it visible. |
| 503 | * This is always called once the prefix changes. |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 504 | * |
| 505 | * @param {string} Prefix for filtering the list |
| 506 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 507 | show : function (active) { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 508 | //Upon change please also update alwaysmenu.js (only two lines new there) |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 509 | const t = this; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 510 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 511 | // show menu based on initial offset |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 512 | t._unmark(); // Unmark everything that was marked before |
| 513 | t.removeItems(); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 514 | |
| 515 | // Initialize the list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 516 | if (!t._initList()) { |
Akron | 6ac5844 | 2016-05-24 16:52:29 +0200 | [diff] [blame] | 517 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 518 | // The prefix is not active |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 519 | t._prefix.active(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 520 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 521 | // finally show the element |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 522 | t._el.classList.add('visible'); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 523 | |
| 524 | return true; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 525 | }; |
| 526 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 527 | let offset = 0; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 528 | |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 529 | // Set a chosen value to active and move the viewport |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 530 | if (arguments.length === 1) { |
| 531 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 532 | // Normalize active value |
| 533 | if (active < 0) { |
| 534 | active = 0; |
| 535 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 536 | else if (active >= t.liveLength()) { |
| 537 | active = t.liveLength() - 1; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 538 | }; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 539 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 540 | // Item is outside the first viewport |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 541 | if (active >= t._limit) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 542 | offset = active; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 543 | const newOffset = t.liveLength() - t._limit; |
| 544 | if (offset > newOffset) { |
| 545 | offset = newOffset; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 546 | }; |
| 547 | }; |
| 548 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 549 | t.position = active; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 552 | // Choose the first item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 553 | else if (t._firstActive) { |
| 554 | t.position = 0; |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 555 | } |
Akron | cb351d6 | 2016-05-19 23:10:33 +0200 | [diff] [blame] | 556 | |
Akron | 9c2f938 | 2016-05-25 16:36:04 +0200 | [diff] [blame] | 557 | // Choose no item |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 558 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 559 | t.position = -1; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 560 | }; |
| 561 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 562 | t.offset = offset; |
| 563 | t._showItems(offset); // Show new item list |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 564 | |
| 565 | // Make chosen value active |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 566 | if (t.position !== -1) { |
| 567 | t.liveItem(t.position).active(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 568 | }; |
Akron | 37513a6 | 2015-11-17 01:07:11 +0100 | [diff] [blame] | 569 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 570 | // The prefix is not active |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 571 | t._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 572 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 573 | // finally show the element |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 574 | t._el.classList.add('visible'); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 575 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 576 | // Add classes for rolling menus |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 577 | t._boundary(true); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 578 | |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 579 | return true; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 580 | }, |
| 581 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 582 | |
| 583 | /** |
| 584 | * Hide the menu and call the onHide callback. |
| 585 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 586 | hide : function () { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 587 | if (!this.dontHide) { |
| 588 | this.removeItems(); |
| 589 | this._prefix.clear(); |
| 590 | this.onHide(); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 591 | this._el.classList.remove('visible'); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 592 | } |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 593 | // this._el.blur(); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 594 | }, |
| 595 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 596 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 597 | /** |
| 598 | * Function released when the menu hides. |
| 599 | * This method is expected to be overridden. |
| 600 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 601 | onHide : function () {}, |
| 602 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 603 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 604 | /** |
| 605 | * Get the prefix for filtering, |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 606 | * e.g. "ve" for "verb" |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 607 | */ |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 608 | prefix : function (pref) { |
| 609 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 610 | this._prefix.value(pref); |
| 611 | return this; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 612 | }; |
| 613 | return this._prefix.value(); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 614 | }, |
| 615 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 616 | |
Akron | c744873 | 2016-04-27 14:06:58 +0200 | [diff] [blame] | 617 | /** |
| 618 | * Get the lengthField object. |
| 619 | */ |
| 620 | lengthField : function () { |
| 621 | return this._lengthField; |
| 622 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 623 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 624 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 625 | /** |
| 626 | * Get the associated slider object. |
| 627 | */ |
| 628 | slider : function () { |
| 629 | return this._slider; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 630 | }, |
| 631 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 632 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 633 | /** |
| 634 | * Delete all visible items from the menu element |
| 635 | */ |
Leo Repp | e5bd35a | 2021-05-05 15:28:26 +0200 | [diff] [blame] | 636 | |
Leo Repp | afcf984 | 2021-06-30 13:01:09 +0200 | [diff] [blame] | 637 | removeItems : function () { |
Leo Repp | e5bd35a | 2021-05-05 15:28:26 +0200 | [diff] [blame] | 638 | const liElements=this._el.getElementsByTagName("LI"); |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 639 | var ignoredCount = 0; //counts how many LI tag elements are not actually direct children |
| 640 | while (liElements.length>ignoredCount){ |
| 641 | if (liElements[ignoredCount].parentNode === this._el){ |
| 642 | this._el.removeChild(liElements[ignoredCount]); |
| 643 | } else { |
| 644 | ignoredCount++; |
| 645 | } |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 646 | }; |
Leo Repp | e5bd35a | 2021-05-05 15:28:26 +0200 | [diff] [blame] | 647 | }, |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 648 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 649 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 650 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 651 | /** |
| 652 | * Get a specific item from the complete list |
| 653 | * |
| 654 | * @param {number} index of the list item |
| 655 | */ |
| 656 | item : function (index) { |
| 657 | return this._items[index] |
| 658 | }, |
| 659 | |
| 660 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 661 | /** |
| 662 | * Get a specific item from the filtered list |
| 663 | * |
| 664 | * @param {number} index of the list item |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 665 | * in the filtered list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 666 | */ |
| 667 | liveItem : function (index) { |
| 668 | if (this._list === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 669 | if (!this._initList()) |
| 670 | return; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 671 | |
| 672 | return this._items[this._list[index]]; |
| 673 | }, |
| 674 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 675 | |
| 676 | /** |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 677 | * Get a specific item from the viewport list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 678 | * |
| 679 | * @param {number} index of the list item |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 680 | * in the visible list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 681 | */ |
| 682 | shownItem : function (index) { |
| 683 | if (index >= this.limit()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 684 | return; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 685 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 686 | return this.liveItem(this.offset + index); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 687 | }, |
| 688 | |
| 689 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 690 | /** |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 691 | * Get the length of the full item list |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 692 | */ |
| 693 | length : function () { |
| 694 | return this._items.length; |
| 695 | }, |
| 696 | |
| 697 | |
| 698 | /** |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 699 | * Length of the filtered item list. |
| 700 | */ |
| 701 | liveLength : function () { |
| 702 | if (this._list === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 703 | this._initList(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 704 | return this._list.length; |
| 705 | }, |
| 706 | |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 707 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 708 | /** |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 709 | * Make the next item in the filtered menu active |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 710 | */ |
| 711 | next : function () { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 712 | //Upon change please update alwaysmenu.js next |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 713 | const t = this; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 714 | |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 715 | // No list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 716 | if (t.liveLength() === 0) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 717 | return; |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 718 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 719 | // Deactivate old item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 720 | if (t.position !== -1 && !t._prefix.active()) { |
| 721 | t.liveItem(t.position).active(false); |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 722 | }; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 723 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 724 | // Get new active item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 725 | t.position++; |
| 726 | let newItem = t.liveItem(t.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 727 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 728 | // The next element is undefined - roll to top or to prefix |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 729 | if (newItem === undefined) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 730 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 731 | // Activate prefix |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 732 | const prefix = this._prefix; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 733 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 734 | // Prefix is set and not active - choose! |
| 735 | if (prefix.isSet() && !prefix.active()) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 736 | t.position--; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 737 | prefix.active(true); |
| 738 | return; |
| 739 | } |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 740 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 741 | // Choose first item |
| 742 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 743 | newItem = t.liveItem(0); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 744 | // choose first item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 745 | t.position = 0; |
| 746 | t._showItems(0); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 747 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 750 | // The next element is after the viewport - roll down |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 751 | else if (t.position >= (t.limit() + t.offset)) { |
| 752 | t.screen(t.position - t.limit() + 1); |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // The next element is before the viewport - roll up |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 756 | else if (t.position <= t.offset) { |
| 757 | t.screen(t.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 758 | }; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 759 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 760 | t._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 761 | newItem.active(true); |
| 762 | }, |
| 763 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 764 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 765 | /* |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 766 | * Make the previous item in the menu active |
| 767 | */ |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 768 | prev : function () { |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 769 | //Upon Change please update alwaysmenu.js prev |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 770 | const t = this; |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 771 | |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 772 | // No list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 773 | if (t.liveLength() === 0) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 774 | return; |
Akron | b38afb2 | 2016-05-25 19:30:01 +0200 | [diff] [blame] | 775 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 776 | // Deactivate old item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 777 | if (!t._prefix.active()) { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 778 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 779 | // No active element set |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 780 | if (t.position === -1) { |
| 781 | t.position = t.liveLength(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 782 | } |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 783 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 784 | // No active element set |
| 785 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 786 | t.liveItem(t.position--).active(false); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 787 | }; |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 788 | }; |
| 789 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 790 | // Get new active item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 791 | let newItem = t.liveItem(t.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 792 | |
| 793 | // The previous element is undefined - roll to bottom |
| 794 | if (newItem === undefined) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 795 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 796 | // Activate prefix |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 797 | const prefix = t._prefix; |
| 798 | let offset = t.liveLength() - t.limit(); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 799 | |
| 800 | // Normalize offset |
| 801 | offset = offset < 0 ? 0 : offset; |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 802 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 803 | // Choose the last item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 804 | t.position = t.liveLength() - 1; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 805 | |
| 806 | // Prefix is set and not active - choose! |
| 807 | if (prefix.isSet() && !prefix.active()) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 808 | t.position++; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 809 | prefix.active(true); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 810 | t.offset = offset; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 811 | return; |
| 812 | } |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 813 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 814 | // Choose last item |
| 815 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 816 | newItem = t.liveItem(t.position); |
| 817 | t._showItems(offset); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 818 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 821 | // The previous element is before the view - roll up |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 822 | else if (t.position < t.offset) { |
| 823 | t.screen(t.position); |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // The previous element is after the view - roll down |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 827 | else if (t.position >= (t.limit() + t.offset)) { |
| 828 | t.screen(t.position - t.limit() + 2); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 829 | }; |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 830 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 831 | t._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 832 | newItem.active(true); |
| 833 | }, |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 834 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 835 | |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 836 | /** |
| 837 | * Move the page up by limit! |
| 838 | */ |
| 839 | pageUp : function () { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 840 | this.screen(this.offset - this.limit()); |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 841 | }, |
| 842 | |
| 843 | |
| 844 | /** |
| 845 | * Move the page down by limit! |
| 846 | */ |
| 847 | pageDown : function () { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 848 | this.screen(this.offset + this.limit()); |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame] | 849 | }, |
| 850 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 851 | |
Akron | e817b88 | 2018-08-31 14:09:17 +0200 | [diff] [blame] | 852 | /** |
| 853 | * Move the view one item up |
| 854 | */ |
| 855 | viewUp : function () { |
| 856 | this.screen(this.offset - 1); |
| 857 | }, |
| 858 | |
| 859 | |
| 860 | /** |
| 861 | * Move the view one item down |
| 862 | */ |
| 863 | viewDown : function () { |
| 864 | this.screen(this.offset + 1); |
| 865 | }, |
| 866 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 867 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 868 | // Unmark all items |
| 869 | _unmark : function () { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 870 | this._list.forEach(function(it){ |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 871 | const item = this._items[it]; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 872 | item.lowlight(); |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 873 | item.active(false); |
| 874 | }, this); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 875 | }, |
| 876 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 877 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 878 | // Set boundary for viewport |
| 879 | _boundary : function (bool) { |
Akron | 55a343b | 2018-04-06 19:57:36 +0200 | [diff] [blame] | 880 | if (this._list.length === 0) |
| 881 | return; |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 882 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 883 | this.item(this._list[0]).noMore(bool); |
| 884 | this.item(this._list[this._list.length - 1]).noMore(bool); |
| 885 | }, |
| 886 | |
| 887 | |
| 888 | // Append Items that should be shown |
| 889 | _showItems : function (off) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 890 | const t = this; |
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 down one step |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 893 | 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 first item |
| 897 | // leave lengthField/prefix/slider |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 898 | //console.log("_showItems, at _notItemElements is: ",t._el.children[this._notItemElements]); |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 899 | t._el.removeChild(t._el.children[this._notItemElements]); |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 900 | |
| 901 | t._append( |
| 902 | t._list[t.offset + t.limit() - 1] |
| 903 | ); |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 904 | } |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 905 | |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 906 | // optimization: scroll up one step |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 907 | else if (t.offset === (off + 1)) { |
| 908 | t.offset = off; |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 909 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 910 | // Remove the HTML node from the last item |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 911 | //console.log("_showItems, at lastChild is: ",t._el.lastChild); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 912 | t._el.removeChild(t._el.lastChild); |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 913 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 914 | t._prepend(t._list[t.offset]); |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 915 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 916 | |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 917 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 918 | t.offset = off; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 919 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 920 | // Remove all items |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 921 | t.removeItems(); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 922 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 923 | // Use list |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 924 | let shown = 0; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 925 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 926 | for (let i = 0; i < t._list.length; i++) { |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 927 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 928 | // Don't show - it's before offset |
| 929 | shown++; |
| 930 | if (shown <= off) |
| 931 | continue; |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 932 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 933 | t._append(t._list[i]); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 934 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 935 | if (shown >= (t.limit() + off)) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 936 | break; |
| 937 | }; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 938 | }; |
| 939 | |
| 940 | // set the slider to the new offset |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 941 | t._slider.offset(t.offset); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 942 | }, |
| 943 | |
| 944 | |
| 945 | // Append item to the shown list based on index |
| 946 | _append : 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 | }; |
| 953 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 954 | // Append element |
| 955 | this.element().appendChild(item.element()); |
| 956 | }, |
| 957 | |
| 958 | |
| 959 | // Prepend item to the shown list based on index |
| 960 | _prepend : function (i) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 961 | const item = this.item(i); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 962 | |
| 963 | // Highlight based on prefix |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 964 | if (this.prefix().length > 0) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 965 | item.highlight(this.prefix().toLowerCase()); |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 966 | }; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 967 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 968 | const e = this.element(); |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 969 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 970 | // Append element after lengthField/prefix/slider |
| 971 | e.insertBefore( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 972 | item.element(), |
Leo Repp | 56904d2 | 2021-04-26 15:53:22 +0200 | [diff] [blame] | 973 | e.children[this._notItemElements] |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 974 | ); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 975 | } |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 976 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 977 | }); |