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