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