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