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