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