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