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