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