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