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