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