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