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