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 | } |
| 329 | else if (nr > (this.length() - this.limit())) { |
| 330 | nr = (this.length() - this.limit()); |
| 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 |
| 414 | this._unmark(); // Unmark everything that was marked before |
| 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()) { |
| 419 | // The prefix is not active |
| 420 | this._prefix.active(true); |
| 421 | |
| 422 | // finally show the element |
| 423 | this._element.style.opacity = 1; |
| 424 | |
| 425 | return true; |
| 426 | }; |
| 427 | |
| 428 | var offset = 0; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 429 | |
| 430 | // Set the first element to active |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 431 | // Todo: Or the last element chosen |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 432 | if (arguments.length === 1) { |
| 433 | |
| 434 | // Normalize active value |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame^] | 435 | if (active < 0) { |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 436 | active = 0; |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame^] | 437 | } |
| 438 | else if (active > this.length()) { |
| 439 | active = this.length() - 1; |
| 440 | }; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 441 | |
| 442 | if (active > this._limit) { |
| 443 | offset = active; |
| 444 | if (offset > (this.length() - this._limit)) { |
| 445 | offset = this.length() - this._limit; |
| 446 | }; |
| 447 | }; |
| 448 | |
| 449 | this.position = active; |
| 450 | this._active = active; |
| 451 | } |
| 452 | |
| 453 | else if (this._firstActive) { |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 454 | this.position = 0; |
| 455 | this._active = 0; |
| 456 | } |
Akron | cb351d6 | 2016-05-19 23:10:33 +0200 | [diff] [blame] | 457 | |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 458 | else { |
| 459 | this.position = -1; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | this._offset = offset; |
| 463 | this._showItems(offset); // Show new item list |
| 464 | |
| 465 | // Make chosen value active |
| 466 | if (this.position !== -1) { |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame^] | 467 | this.liveItem(this.position).active(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 468 | }; |
Akron | 37513a6 | 2015-11-17 01:07:11 +0100 | [diff] [blame] | 469 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 470 | // The prefix is not active |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 471 | this._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 472 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 473 | // finally show the element |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 474 | this._element.style.opacity = 1; |
| 475 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 476 | // Show the slider |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 477 | //this._slider.show(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 478 | |
Akron | 37513a6 | 2015-11-17 01:07:11 +0100 | [diff] [blame] | 479 | // Iterate to the active item |
| 480 | if (this._active !== -1 && !this._prefix.isSet()) { |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 481 | while (this._list[this.position] < this._active) { |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 482 | |
| 483 | // TODO. Improve this by moving using screen! |
Akron | 37513a6 | 2015-11-17 01:07:11 +0100 | [diff] [blame] | 484 | this.next(); |
| 485 | }; |
| 486 | }; |
| 487 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 488 | // Add classes for rolling menus |
| 489 | this._boundary(true); |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 490 | return true; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 491 | }, |
| 492 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 493 | |
| 494 | /** |
| 495 | * Hide the menu and call the onHide callback. |
| 496 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 497 | hide : function () { |
Nils Diewald | 59c02fc | 2015-03-07 01:29:09 +0000 | [diff] [blame] | 498 | this.active = false; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 499 | this._unmark(); |
| 500 | this.unshow(); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 501 | this._element.style.opacity = 0; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 502 | this._prefix.clear(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 503 | this.onHide(); |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 504 | /* this._element.blur(); */ |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 505 | }, |
| 506 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 507 | /** |
| 508 | * Function released when the menu hides. |
| 509 | * This method is expected to be overridden. |
| 510 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 511 | onHide : function () {}, |
| 512 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 513 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 514 | /** |
| 515 | * Get the prefix for filtering, |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 516 | * e.g. "ve" for "verb" |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 517 | */ |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 518 | prefix : function (pref) { |
| 519 | if (arguments.length === 1) { |
| 520 | this._prefix.value(pref); |
| 521 | return this; |
| 522 | }; |
| 523 | return this._prefix.value(); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 524 | }, |
| 525 | |
Akron | c744873 | 2016-04-27 14:06:58 +0200 | [diff] [blame] | 526 | /** |
| 527 | * Get the lengthField object. |
| 528 | */ |
| 529 | lengthField : function () { |
| 530 | return this._lengthField; |
| 531 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 532 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 533 | /** |
| 534 | * Get the associated slider object. |
| 535 | */ |
| 536 | slider : function () { |
| 537 | return this._slider; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 538 | }, |
| 539 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 540 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 541 | /** |
| 542 | * Delete all visible items from the menu element |
| 543 | */ |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 544 | unshow : function () { |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 545 | var child; |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 546 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 547 | // Remove all children |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 548 | var children = this._element.childNodes; |
Akron | c744873 | 2016-04-27 14:06:58 +0200 | [diff] [blame] | 549 | // Leave the prefix and lengthField |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 550 | for (var i = children.length - 1; i >= 3; i--) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 551 | this._element.removeChild( |
| 552 | children[i] |
| 553 | ); |
| 554 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 555 | }, |
| 556 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 557 | /** |
| 558 | * Get a specific item from the complete list |
| 559 | * |
| 560 | * @param {number} index of the list item |
| 561 | */ |
| 562 | item : function (index) { |
| 563 | return this._items[index] |
| 564 | }, |
| 565 | |
| 566 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 567 | /** |
| 568 | * Get a specific item from the filtered list |
| 569 | * |
| 570 | * @param {number} index of the list item |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 571 | * in the filtered list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 572 | */ |
| 573 | liveItem : function (index) { |
| 574 | if (this._list === undefined) |
| 575 | if (!this._initList()) |
| 576 | return; |
| 577 | |
| 578 | return this._items[this._list[index]]; |
| 579 | }, |
| 580 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 581 | |
| 582 | /** |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 583 | * Get a specific item from the viewport list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 584 | * |
| 585 | * @param {number} index of the list item |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 586 | * in the visible list |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 587 | */ |
| 588 | shownItem : function (index) { |
| 589 | if (index >= this.limit()) |
| 590 | return; |
| 591 | return this.liveItem(this._offset + index); |
| 592 | }, |
| 593 | |
| 594 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 595 | /** |
| 596 | * Get the length of the full list |
| 597 | */ |
| 598 | length : function () { |
| 599 | return this._items.length; |
| 600 | }, |
| 601 | |
| 602 | |
| 603 | /** |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 604 | * Length of the filtered item list. |
| 605 | */ |
| 606 | liveLength : function () { |
| 607 | if (this._list === undefined) |
| 608 | this._initList(); |
| 609 | return this._list.length; |
| 610 | }, |
| 611 | |
| 612 | |
| 613 | /** |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 614 | * Make the next item in the filtered menu active |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 615 | */ |
| 616 | next : function () { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 617 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 618 | // No active element set |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 619 | var newItem; |
| 620 | |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 621 | if (this.position !== -1) { |
| 622 | // Set new live item |
| 623 | if (!this._prefix.active()) { |
| 624 | var oldItem = this.liveItem(this.position); |
| 625 | oldItem.active(false); |
| 626 | }; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 629 | this.position++; |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 630 | this._active = this.position; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 631 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 632 | newItem = this.liveItem(this.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 633 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 634 | // The next element is undefined - roll to top or to prefix |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 635 | if (newItem === undefined) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 636 | |
| 637 | // Activate prefix |
| 638 | var prefix = this._prefix; |
| 639 | |
| 640 | // Mark prefix |
| 641 | if (prefix.isSet() && !prefix.active()) { |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 642 | this.position--; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 643 | prefix.active(true); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 644 | this._active = -1; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | else { |
| 648 | this._offset = 0; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 649 | this.position = 0; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 650 | newItem = this.liveItem(0); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 651 | this._active = 0; |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 652 | this._unmark(); |
| 653 | this.unshow(); |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 654 | this._showItems(0); |
| 655 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 658 | // The next element is after the viewport - roll down |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 659 | else if (this.position >= (this.limit() + this._offset)) { |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 660 | |
| 661 | this.screen(this.position - this.limit() + 1); |
| 662 | } |
| 663 | |
| 664 | // The next element is before the viewport - roll up |
| 665 | else if (this.position <= this._offset) { |
| 666 | this.screen(this.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 667 | }; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 668 | |
| 669 | this._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 670 | newItem.active(true); |
| 671 | }, |
| 672 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 673 | /* |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 674 | * Make the previous item in the menu active |
| 675 | */ |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 676 | prev : function () { |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 677 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 678 | // No active element set |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 679 | if (this.position === -1) { |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 680 | return; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 681 | // TODO: Choose last item |
| 682 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 683 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 684 | var newItem; |
| 685 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 686 | // Set new live item |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 687 | if (!this._prefix.active()) { |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 688 | var oldItem = this.liveItem(this.position--); |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 689 | oldItem.active(false); |
| 690 | }; |
| 691 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 692 | newItem = this.liveItem(this.position); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 693 | |
| 694 | // The previous element is undefined - roll to bottom |
| 695 | if (newItem === undefined) { |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 696 | |
| 697 | // Activate prefix |
| 698 | var prefix = this._prefix; |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 699 | this._offset = this.liveLength() - this.limit(); |
Nils Diewald | 2d21075 | 2015-03-09 19:01:15 +0000 | [diff] [blame] | 700 | |
| 701 | // Normalize offset |
| 702 | this._offset = this._offset < 0 ? 0 : this._offset; |
| 703 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 704 | this.position = this.liveLength() - 1; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 705 | |
| 706 | if (prefix.isSet() && !prefix.active()) { |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 707 | this.position++; |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 708 | prefix.active(true); |
| 709 | return; |
| 710 | } |
| 711 | else { |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 712 | newItem = this.liveItem(this.position); |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 713 | this._unmark(); |
| 714 | this.unshow(); |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 715 | this._showItems(this._offset); |
| 716 | }; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 719 | // The previous element is before the view - roll up |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 720 | else if (this.position < this._offset) { |
Akron | 5a1f5bb | 2016-05-23 22:00:39 +0200 | [diff] [blame] | 721 | this.screen(this.position); |
| 722 | } |
| 723 | |
| 724 | // The previous element is after the view - roll down |
| 725 | else if (this.position >= (this.limit() + this._offset)) { |
| 726 | this.screen(this.position - this.limit() + 2); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 727 | }; |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 728 | |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 729 | this._prefix.active(false); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 730 | newItem.active(true); |
| 731 | }, |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 732 | |
Akron | 3c2730f | 2016-05-24 15:08:29 +0200 | [diff] [blame^] | 733 | /** |
| 734 | * Move the page up by limit! |
| 735 | */ |
| 736 | pageUp : function () { |
| 737 | this.screen(this._offset - this.limit()); |
| 738 | }, |
| 739 | |
| 740 | |
| 741 | /** |
| 742 | * Move the page down by limit! |
| 743 | */ |
| 744 | pageDown : function () { |
| 745 | this.screen(this._offset + this.limit()); |
| 746 | }, |
| 747 | |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 748 | |
Akron | 5240b8c | 2016-05-20 09:17:41 +0200 | [diff] [blame] | 749 | // Unmark all items |
| 750 | _unmark : function () { |
| 751 | for (var i in this._list) { |
| 752 | var item = this._items[this._list[i]]; |
| 753 | item.lowlight(); |
| 754 | item.active(false); |
| 755 | }; |
| 756 | }, |
| 757 | |
| 758 | |
| 759 | // Reset chosen item and prefix |
| 760 | _reset : function () { |
| 761 | this._offset = 0; |
| 762 | this._pos = 0; |
| 763 | this._prefix.value(''); |
| 764 | }, |
| 765 | |
| 766 | |
| 767 | // Set boundary for viewport |
| 768 | _boundary : function (bool) { |
| 769 | this.item(this._list[0]).noMore(bool); |
| 770 | this.item(this._list[this._list.length - 1]).noMore(bool); |
| 771 | }, |
| 772 | |
| 773 | |
| 774 | // Append Items that should be shown |
| 775 | _showItems : function (off) { |
| 776 | |
| 777 | // Use list |
| 778 | var shown = 0; |
| 779 | var i; |
| 780 | |
| 781 | for (i in this._list) { |
| 782 | |
| 783 | // Don't show - it's before offset |
| 784 | shown++; |
| 785 | if (shown <= off) |
| 786 | continue; |
| 787 | |
| 788 | var itemNr = this._list[i]; |
| 789 | var item = this.item(itemNr); |
| 790 | this._append(itemNr); |
| 791 | |
| 792 | // this._offset)) |
| 793 | if (shown >= (this.limit() + off)) |
| 794 | break; |
| 795 | }; |
| 796 | |
| 797 | // set the slider to the new offset |
| 798 | this._slider.offset(this._offset); |
| 799 | }, |
| 800 | |
| 801 | |
| 802 | // Append item to the shown list based on index |
| 803 | _append : function (i) { |
| 804 | var item = this.item(i); |
| 805 | |
| 806 | // Highlight based on prefix |
| 807 | if (this.prefix().length > 0) |
| 808 | item.highlight(this.prefix()); |
| 809 | |
| 810 | // Append element |
| 811 | this.element().appendChild(item.element()); |
| 812 | }, |
| 813 | |
| 814 | |
| 815 | // Prepend item to the shown list based on index |
| 816 | _prepend : function (i) { |
| 817 | var item = this.item(i); |
| 818 | |
| 819 | // Highlight based on prefix |
| 820 | if (this.prefix().length > 0) |
| 821 | item.highlight(this.prefix()); |
| 822 | |
| 823 | var e = this.element(); |
| 824 | // Append element after lengthField/prefix/slider |
| 825 | e.insertBefore( |
| 826 | item.element(), |
| 827 | e.children[3] |
| 828 | ); |
Nils Diewald | 5975d70 | 2015-03-09 17:45:42 +0000 | [diff] [blame] | 829 | }, |
| 830 | |
| 831 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 832 | // Remove the HTML node from the first item |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 833 | _removeFirst : function () { |
| 834 | this.item(this._list[this._offset]).lowlight(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 835 | // leave lengthField/prefix/slider |
| 836 | this._element.removeChild(this._element.children[3]); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 837 | }, |
| 838 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 839 | |
| 840 | // Remove the HTML node from the last item |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 841 | _removeLast : function () { |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 842 | this.item(this._list[this._offset + this.limit() - 1]).lowlight(); |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 843 | this._element.removeChild(this._element.lastChild); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 844 | } |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 845 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 846 | }); |