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