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