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