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