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