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