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