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