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