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