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