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