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