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