blob: e3a8c401d6acf1b7e81dc399c077d1761b3d03ab [file] [log] [blame]
Nils Diewald2fe12e12015-03-06 16:47:06 +00001/**
Nils Diewald7148c6f2015-05-04 15:07:53 +00002 * Scrollable drop-down menus with view filter.
Nils Diewald2fe12e12015-03-06 16:47:06 +00003 *
4 * @author Nils Diewald
5 */
Nils Diewald2488d052015-04-09 21:46:02 +00006/*
Nils Diewald0e6992a2015-04-14 20:13:52 +00007 * TODO: space is not a valid prefix!
Akron3c2730f2016-05-24 15:08:29 +02008 * TODO: Show the slider briefly on move (whenever screen is called).
Akron9c4d1ae2016-05-25 21:43:22 +02009 * TODO: Ignore alt+ and strg+ key strokes.
Akron0b92f692016-05-25 22:37:13 +020010 * TODO: Should scroll to a chosen value after prefixing, if the chosen value is live
Akron201b72a2016-06-03 01:46:19 +020011 * TODO: Add a "title" to a menu that is not scrollable.
12 * TODO: Make the menu responsive by showing less items on smaller screens
13 * or anytime items would be outside the screen.
Akron02360e42016-06-07 13:41:12 +020014 * TODO: Add a .match() method to items for scrolling and probably for prefixing.
15 * TODO: Add static header (for title, sortation fields, but also for menu points like "fragments" and "history".
Akrone91da782017-12-15 17:17:50 +010016 * TODO: Support space separated list of prefixes so "co no" will highlight "common noun"
Nils Diewald2488d052015-04-09 21:46:02 +000017 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000018define([
19 'menu/item',
20 'menu/prefix',
Akronc7448732016-04-27 14:06:58 +020021 'menu/lengthField',
Akron9905e2a2016-05-10 16:06:44 +020022 'menu/slider',
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 'util'
24], function (defaultItemClass,
Akrone4961b12017-05-10 21:04:46 +020025 defaultPrefixClass,
26 defaultLengthFieldClass,
27 sliderClass) {
Nils Diewaldfda29d92015-01-22 17:28:01 +000028
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 // Default maximum number of menu items
30 var menuLimit = 8;
31
32 function _codeFromEvent (e) {
33 if (e.charCode && (e.keyCode == 0))
34 return e.charCode
35 return e.keyCode;
Nils Diewald59c02fc2015-03-07 01:29:09 +000036 };
37
Nils Diewald86dad5b2015-01-28 15:09:07 +000038
39 /**
40 * List of items for drop down menu (complete).
41 * Only a sublist of the menu is filtered (live).
42 * Only a sublist of the filtered menu is visible (shown).
43 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000044 return {
Nils Diewald86dad5b2015-01-28 15:09:07 +000045 /**
46 * Create new Menu based on the action prefix
47 * and a list of menu items.
48 *
Akron7524be12016-06-01 17:31:33 +020049 *
50 * Accepts an associative array containg the elements
51 * itemClass, prefixClass, lengthFieldClass
52 *
Nils Diewald86dad5b2015-01-28 15:09:07 +000053 * @this {Menu}
54 * @constructor
55 * @param {string} Context prefix
56 * @param {Array.<Array.<string>>} List of menu items
57 */
Akron7524be12016-06-01 17:31:33 +020058 create : function (list, params) {
59 return Object.create(this)._init(list, params);
Nils Diewald86dad5b2015-01-28 15:09:07 +000060 },
61
Akron5240b8c2016-05-20 09:17:41 +020062 // Initialize list
Akron7524be12016-06-01 17:31:33 +020063 _init : function (list, params) {
Akrona92fd8d2016-05-24 21:13:41 +020064
Akron7524be12016-06-01 17:31:33 +020065 if (params === undefined)
Akrone4961b12017-05-10 21:04:46 +020066 params = {};
Akron7524be12016-06-01 17:31:33 +020067
68 this._itemClass = params["itemClass"] || defaultItemClass;
Akron5240b8c2016-05-20 09:17:41 +020069
70 // Add prefix object
Akron7524be12016-06-01 17:31:33 +020071 if (params["prefixClass"] !== undefined) {
Akrone4961b12017-05-10 21:04:46 +020072 this._prefix = params["prefixClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020073 }
74 else {
Akrone4961b12017-05-10 21:04:46 +020075 this._prefix = defaultPrefixClass.create();
Akron5240b8c2016-05-20 09:17:41 +020076 };
77 this._prefix._menu = this;
78
79 // Add lengthField object
Akron7524be12016-06-01 17:31:33 +020080 if (params["lengthFieldClass"] !== undefined) {
Akrone4961b12017-05-10 21:04:46 +020081 this._lengthField = params["lengthFieldClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020082 }
83 else {
Akrone4961b12017-05-10 21:04:46 +020084 this._lengthField = defaultLengthFieldClass.create();
Akron5240b8c2016-05-20 09:17:41 +020085 };
86 this._lengthField._menu = this;
87
88 // Initialize slider
89 this._slider = sliderClass.create(this);
90
91 // Create the element
Akron9c4d1ae2016-05-25 21:43:22 +020092 var el = document.createElement("ul");
93 with (el) {
Akrone4961b12017-05-10 21:04:46 +020094 style.outline = 0;
95 setAttribute('tabindex', 0);
96 classList.add('menu', 'roll');
97 appendChild(this._prefix.element());
98 appendChild(this._lengthField.element());
99 appendChild(this._slider.element());
Akron9c4d1ae2016-05-25 21:43:22 +0200100 };
Akron5240b8c2016-05-20 09:17:41 +0200101
102 // This has to be cleaned up later on
Akron9c4d1ae2016-05-25 21:43:22 +0200103 el["menu"] = this;
Akron5240b8c2016-05-20 09:17:41 +0200104
105 // Arrow keys
Akron9c4d1ae2016-05-25 21:43:22 +0200106 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200107 'keydown',
108 this._keydown.bind(this),
109 false
Akron5240b8c2016-05-20 09:17:41 +0200110 );
111
112 // Strings
Akron9c4d1ae2016-05-25 21:43:22 +0200113 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200114 'keypress',
115 this._keypress.bind(this),
116 false
Akron5240b8c2016-05-20 09:17:41 +0200117 );
118
119 // Mousewheel
Akron9c4d1ae2016-05-25 21:43:22 +0200120 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200121 'wheel',
122 this._mousewheel.bind(this),
123 false
Akron5240b8c2016-05-20 09:17:41 +0200124 );
Akron9c4d1ae2016-05-25 21:43:22 +0200125 this._element = el;
Akrone4961b12017-05-10 21:04:46 +0200126
Akron5240b8c2016-05-20 09:17:41 +0200127 this._items = new Array();
Akron5240b8c2016-05-20 09:17:41 +0200128
Akron9c4d1ae2016-05-25 21:43:22 +0200129 var i = 0;
Akron5240b8c2016-05-20 09:17:41 +0200130 // Initialize item list based on parameters
Akron7524be12016-06-01 17:31:33 +0200131 for (i in list) {
Akrone4961b12017-05-10 21:04:46 +0200132 var obj = this._itemClass.create(list[i]);
Akron5240b8c2016-05-20 09:17:41 +0200133
Akrone4961b12017-05-10 21:04:46 +0200134 // This may become circular
135 obj["_menu"] = this;
136 this._lengthField.add(list[i]);
137 this._items.push(obj);
Akron5240b8c2016-05-20 09:17:41 +0200138 };
139
Akron9c4d1ae2016-05-25 21:43:22 +0200140 this._limit = menuLimit;
141 this._slider.length(this.liveLength())
Akrone4961b12017-05-10 21:04:46 +0200142 .limit(this._limit)
143 .reInit();
144
Akron5240b8c2016-05-20 09:17:41 +0200145 this._firstActive = false; // Show the first item active always?
Akron9c4d1ae2016-05-25 21:43:22 +0200146 this.offset = 0;
147 this.position = 0;
Akron5240b8c2016-05-20 09:17:41 +0200148 return this;
149 },
150
151 // Initialize the item list
152 _initList : function () {
153
154 // Create a new list
155 if (this._list === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200156 this._list = [];
Akron5240b8c2016-05-20 09:17:41 +0200157 }
158 else if (this._list.length !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200159 this._boundary(false);
160 this._list.length = 0;
Akron5240b8c2016-05-20 09:17:41 +0200161 };
162
163 // Offset is initially zero
Akron9c4d1ae2016-05-25 21:43:22 +0200164 this.offset = 0;
Akron5240b8c2016-05-20 09:17:41 +0200165
166 // There is no prefix set
167 if (this.prefix().length <= 0) {
168
Akrone4961b12017-05-10 21:04:46 +0200169 // add all items to the list and lowlight
170 var i = 0;
171 for (; i < this._items.length; i++) {
172 this._list.push(i);
173 this._items[i].lowlight();
174 };
Akron5240b8c2016-05-20 09:17:41 +0200175
Akrone4961b12017-05-10 21:04:46 +0200176 this._slider.length(i).reInit();;
Akron97752a72016-05-25 14:43:07 +0200177
Akrone4961b12017-05-10 21:04:46 +0200178 return true;
Akron5240b8c2016-05-20 09:17:41 +0200179 };
180
181 /*
182 * There is a prefix set, so filter the list!
183 */
184 var pos;
Akron6ffad5d2016-05-24 17:16:58 +0200185 var prefix = " " + this.prefix().toLowerCase();
Akron5240b8c2016-05-20 09:17:41 +0200186
187 // Iterate over all items and choose preferred matching items
188 // i.e. the matching happens at the word start
189 for (pos = 0; pos < this._items.length; pos++) {
Akrone4961b12017-05-10 21:04:46 +0200190 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
191 this._list.push(pos);
Akron5240b8c2016-05-20 09:17:41 +0200192 };
193
194 // The list is empty - so lower your expectations
195 // Iterate over all items and choose matching items
196 // i.e. the matching happens anywhere in the word
Akron6ffad5d2016-05-24 17:16:58 +0200197 prefix = prefix.substring(1);
Akron5240b8c2016-05-20 09:17:41 +0200198 if (this._list.length == 0) {
Akrone4961b12017-05-10 21:04:46 +0200199 for (pos = 0; pos < this._items.length; pos++) {
200 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
201 this._list.push(pos);
202 };
Akron5240b8c2016-05-20 09:17:41 +0200203 };
204
Akron9c4d1ae2016-05-25 21:43:22 +0200205 this._slider.length(this._list.length).reInit();
Akron6ed13992016-05-23 18:06:05 +0200206
Akron5240b8c2016-05-20 09:17:41 +0200207 // Filter was successful - yeah!
208 return this._list.length > 0 ? true : false;
209 },
210
211
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000212 /**
213 * Destroy this menu
214 * (in case you don't trust the
215 * mark and sweep GC)!
216 */
217 destroy : function () {
Akron47c086c2016-05-18 21:22:06 +0200218
Akron5240b8c2016-05-20 09:17:41 +0200219 // Remove circular reference to "this" in menu
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000220 if (this._element != undefined)
Akrone4961b12017-05-10 21:04:46 +0200221 delete this._element["menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000222
Akron5240b8c2016-05-20 09:17:41 +0200223 // Remove circular reference to "this" in items
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000224 for (var i = 0; i < this._items.length; i++) {
Akrone4961b12017-05-10 21:04:46 +0200225 delete this._items[i]["_menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000226 };
Akron5240b8c2016-05-20 09:17:41 +0200227
228 // Remove circular reference to "this" in prefix
Nils Diewald5c5a7472015-04-02 22:13:38 +0000229 delete this._prefix['_menu'];
Akron5240b8c2016-05-20 09:17:41 +0200230 delete this._lengthField['_menu'];
231 delete this._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000232 },
233
Nils Diewald7148c6f2015-05-04 15:07:53 +0000234
235 /**
236 * Focus on this menu.
237 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000238 focus : function () {
239 this._element.focus();
240 },
241
Nils Diewald7148c6f2015-05-04 15:07:53 +0000242
Nils Diewald59c02fc2015-03-07 01:29:09 +0000243 // mouse wheel treatment
244 _mousewheel : function (e) {
245 var delta = 0;
Nils Diewald5975d702015-03-09 17:45:42 +0000246
247 delta = e.deltaY / 120;
248 if (delta > 0)
Akrone4961b12017-05-10 21:04:46 +0200249 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000250 else if (delta < 0)
Akrone4961b12017-05-10 21:04:46 +0200251 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000252 e.halt();
253 },
254
Nils Diewald7148c6f2015-05-04 15:07:53 +0000255
Nils Diewald59c02fc2015-03-07 01:29:09 +0000256 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000257 _keydown : function (e) {
Nils Diewald59c02fc2015-03-07 01:29:09 +0000258 var code = _codeFromEvent(e);
259
Nils Diewald59c02fc2015-03-07 01:29:09 +0000260 switch (code) {
261 case 27: // 'Esc'
Akrone4961b12017-05-10 21:04:46 +0200262 e.halt();
263 this.hide();
264 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000265
Nils Diewald59c02fc2015-03-07 01:29:09 +0000266 case 38: // 'Up'
Akrone4961b12017-05-10 21:04:46 +0200267 e.halt();
268 this.prev();
269 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000270 case 33: // 'Page up'
Akrone4961b12017-05-10 21:04:46 +0200271 e.halt();
272 this.pageUp();
273 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000274 case 40: // 'Down'
Akrone4961b12017-05-10 21:04:46 +0200275 e.halt();
276 this.next();
277 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000278 case 34: // 'Page down'
Akrone4961b12017-05-10 21:04:46 +0200279 e.halt();
280 this.pageDown();
281 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000282 case 39: // 'Right'
Akrone4961b12017-05-10 21:04:46 +0200283 if (this._prefix.active())
284 break;
Nils Diewalde8518f82015-03-18 22:41:49 +0000285
Akrone4961b12017-05-10 21:04:46 +0200286 var item = this.liveItem(this.position);
287
288 if (item["further"] !== undefined) {
289 item["further"].bind(item).apply();
290 };
291
292 e.halt();
293 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000294 case 13: // 'Enter'
295
Akrone4961b12017-05-10 21:04:46 +0200296 // Click on prefix
297 if (this._prefix.active())
298 this._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000299
Akrone4961b12017-05-10 21:04:46 +0200300 // Click on item
301 else
302 this.liveItem(this.position).onclick(e);
303 e.halt();
304 break;
Nils Diewald59c02fc2015-03-07 01:29:09 +0000305 case 8: // 'Backspace'
Akrone4961b12017-05-10 21:04:46 +0200306 this._prefix.chop();
307 this.show();
308 e.halt();
309 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000310 };
311 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000312
Nils Diewald47f366b2015-04-15 20:06:35 +0000313 // Add characters to prefix
314 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200315 if (e.charCode !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200316 e.halt();
317 var c = String.fromCharCode(_codeFromEvent(e));
318
319 // Add prefix
320 this._prefix.add(c);
321 this.show();
Akron9c2f9382016-05-25 16:36:04 +0200322 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000323 },
324
Akron47c086c2016-05-18 21:22:06 +0200325 /**
Akron5240b8c2016-05-20 09:17:41 +0200326 * Show a screen with a given offset
327 * in the viewport.
Akron47c086c2016-05-18 21:22:06 +0200328 */
329 screen : function (nr) {
Akron3c2730f2016-05-24 15:08:29 +0200330 if (nr < 0) {
Akrone4961b12017-05-10 21:04:46 +0200331 nr = 0
Akron3c2730f2016-05-24 15:08:29 +0200332 }
Akron6ac58442016-05-24 16:52:29 +0200333 else if (nr > (this.liveLength() - this.limit())) {
Akrone4961b12017-05-10 21:04:46 +0200334 nr = (this.liveLength() - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200335 };
336
Akron9c4d1ae2016-05-25 21:43:22 +0200337 if (this.offset === nr)
Akrone4961b12017-05-10 21:04:46 +0200338 return;
Akron5a1f5bb2016-05-23 22:00:39 +0200339
Akron47c086c2016-05-18 21:22:06 +0200340 this._showItems(nr);
341 },
342
Nils Diewald2fe12e12015-03-06 16:47:06 +0000343 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000344 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000345 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000346 element : function () {
347 return this._element;
348 },
349
Nils Diewald2fe12e12015-03-06 16:47:06 +0000350 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000351 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000352 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000353 itemClass : function () {
354 return this._itemClass;
355 },
356
357 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000358 * Get and set the numerical value
359 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000360 */
361 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000362 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200363 if (this._limit !== limit) {
364 this._limit = limit;
365 this._slider.limit(limit).reInit();
366 };
367 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000368 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000369 return this._limit;
370 },
371
Nils Diewald7148c6f2015-05-04 15:07:53 +0000372
Nils Diewald86dad5b2015-01-28 15:09:07 +0000373 /**
374 * Upgrade this object to another object,
375 * while private data stays intact.
376 *
Nils Diewald2fe12e12015-03-06 16:47:06 +0000377 * @param {Object} An object with properties.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000378 */
379 upgradeTo : function (props) {
380 for (var prop in props) {
Akrone4961b12017-05-10 21:04:46 +0200381 this[prop] = props[prop];
Nils Diewald86dad5b2015-01-28 15:09:07 +0000382 };
383 return this;
384 },
385
Nils Diewald7148c6f2015-05-04 15:07:53 +0000386
Nils Diewald86dad5b2015-01-28 15:09:07 +0000387 /**
Akron97752a72016-05-25 14:43:07 +0200388 * Filter the list and make it visible.
389 * This is always called once the prefix changes.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000390 *
391 * @param {string} Prefix for filtering the list
392 */
Akron6ed13992016-05-23 18:06:05 +0200393 show : function (active) {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000394
Akron5240b8c2016-05-20 09:17:41 +0200395 // show menu based on initial offset
Akron6ac58442016-05-24 16:52:29 +0200396 this._unmark(); // Unmark everything that was marked before
Akrona92fd8d2016-05-24 21:13:41 +0200397 this.removeItems();
Akron6ed13992016-05-23 18:06:05 +0200398
399 // Initialize the list
400 if (!this._initList()) {
Akron6ac58442016-05-24 16:52:29 +0200401
Akrone4961b12017-05-10 21:04:46 +0200402 // The prefix is not active
403 this._prefix.active(true);
Akron6ed13992016-05-23 18:06:05 +0200404
Akrone4961b12017-05-10 21:04:46 +0200405 // finally show the element
406 this._element.classList.add('visible');
407
408 return true;
Akron6ed13992016-05-23 18:06:05 +0200409 };
410
411 var offset = 0;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000412
Akron9c2f9382016-05-25 16:36:04 +0200413 // Set a chosen value to active and move the viewport
Akron6ed13992016-05-23 18:06:05 +0200414 if (arguments.length === 1) {
415
Akrone4961b12017-05-10 21:04:46 +0200416 // Normalize active value
417 if (active < 0) {
418 active = 0;
419 }
420 else if (active >= this.liveLength()) {
421 active = this.liveLength() - 1;
422 };
Akron6ed13992016-05-23 18:06:05 +0200423
Akrone4961b12017-05-10 21:04:46 +0200424 // Item is outside the first viewport
425 if (active >= this._limit) {
426 offset = active;
427 if (offset > (this.liveLength() - this._limit)) {
428 offset = this.liveLength() - this._limit;
429 };
430 };
431
432 this.position = active;
Akron6ed13992016-05-23 18:06:05 +0200433 }
434
Akron9c2f9382016-05-25 16:36:04 +0200435 // Choose the first item
Akron6ed13992016-05-23 18:06:05 +0200436 else if (this._firstActive) {
Akrone4961b12017-05-10 21:04:46 +0200437 this.position = 0;
Akron47c086c2016-05-18 21:22:06 +0200438 }
Akroncb351d62016-05-19 23:10:33 +0200439
Akron9c2f9382016-05-25 16:36:04 +0200440 // Choose no item
Akron47c086c2016-05-18 21:22:06 +0200441 else {
Akrone4961b12017-05-10 21:04:46 +0200442 this.position = -1;
Akron6ed13992016-05-23 18:06:05 +0200443 };
444
Akron9c4d1ae2016-05-25 21:43:22 +0200445 this.offset = offset;
Akron6ed13992016-05-23 18:06:05 +0200446 this._showItems(offset); // Show new item list
447
448 // Make chosen value active
449 if (this.position !== -1) {
Akrone4961b12017-05-10 21:04:46 +0200450 this.liveItem(this.position).active(true);
Akron6ed13992016-05-23 18:06:05 +0200451 };
Akron37513a62015-11-17 01:07:11 +0100452
Akron5240b8c2016-05-20 09:17:41 +0200453 // The prefix is not active
Nils Diewalde8518f82015-03-18 22:41:49 +0000454 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000455
Akron5240b8c2016-05-20 09:17:41 +0200456 // finally show the element
Akron6bb71582016-06-10 20:41:08 +0200457 this._element.classList.add('visible');
Nils Diewald2fe12e12015-03-06 16:47:06 +0000458
Nils Diewald86dad5b2015-01-28 15:09:07 +0000459 // Add classes for rolling menus
460 this._boundary(true);
Akron6bb71582016-06-10 20:41:08 +0200461
Nils Diewald59c02fc2015-03-07 01:29:09 +0000462 return true;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000463 },
464
Nils Diewald7148c6f2015-05-04 15:07:53 +0000465
466 /**
467 * Hide the menu and call the onHide callback.
468 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000469 hide : function () {
Akrona92fd8d2016-05-24 21:13:41 +0200470 this.removeItems();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000471 this._prefix.clear();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000472 this.onHide();
Akron6bb71582016-06-10 20:41:08 +0200473 this._element.classList.remove('visible');
474
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000475 /* this._element.blur(); */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000476 },
477
Nils Diewald7148c6f2015-05-04 15:07:53 +0000478 /**
479 * Function released when the menu hides.
480 * This method is expected to be overridden.
481 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000482 onHide : function () {},
483
Nils Diewald7148c6f2015-05-04 15:07:53 +0000484
Nils Diewald86dad5b2015-01-28 15:09:07 +0000485 /**
486 * Get the prefix for filtering,
Nils Diewald2fe12e12015-03-06 16:47:06 +0000487 * e.g. &quot;ve&quot; for &quot;verb&quot;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000488 */
Nils Diewald5975d702015-03-09 17:45:42 +0000489 prefix : function (pref) {
490 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200491 this._prefix.value(pref);
492 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000493 };
494 return this._prefix.value();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000495 },
496
Akronc7448732016-04-27 14:06:58 +0200497 /**
498 * Get the lengthField object.
499 */
500 lengthField : function () {
501 return this._lengthField;
502 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000503
Akron5240b8c2016-05-20 09:17:41 +0200504 /**
505 * Get the associated slider object.
506 */
507 slider : function () {
508 return this._slider;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000509 },
510
Akron5240b8c2016-05-20 09:17:41 +0200511
Nils Diewald86dad5b2015-01-28 15:09:07 +0000512 /**
513 * Delete all visible items from the menu element
514 */
Akrona92fd8d2016-05-24 21:13:41 +0200515 removeItems : function () {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000516 var child;
Nils Diewald2fe12e12015-03-06 16:47:06 +0000517
Nils Diewald2fe12e12015-03-06 16:47:06 +0000518 // Remove all children
Nils Diewald5975d702015-03-09 17:45:42 +0000519 var children = this._element.childNodes;
Akronc7448732016-04-27 14:06:58 +0200520 // Leave the prefix and lengthField
Akron9905e2a2016-05-10 16:06:44 +0200521 for (var i = children.length - 1; i >= 3; i--) {
Akrone4961b12017-05-10 21:04:46 +0200522 this._element.removeChild(
523 children[i]
524 );
Nils Diewald5975d702015-03-09 17:45:42 +0000525 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000526 },
527
Nils Diewald2fe12e12015-03-06 16:47:06 +0000528 /**
529 * Get a specific item from the complete list
530 *
531 * @param {number} index of the list item
532 */
533 item : function (index) {
534 return this._items[index]
535 },
536
537
Nils Diewald86dad5b2015-01-28 15:09:07 +0000538 /**
539 * Get a specific item from the filtered list
540 *
541 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000542 * in the filtered list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000543 */
544 liveItem : function (index) {
545 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200546 if (!this._initList())
547 return;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000548
549 return this._items[this._list[index]];
550 },
551
Nils Diewald86dad5b2015-01-28 15:09:07 +0000552
553 /**
Akron5240b8c2016-05-20 09:17:41 +0200554 * Get a specific item from the viewport list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000555 *
556 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000557 * in the visible list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000558 */
559 shownItem : function (index) {
560 if (index >= this.limit())
Akrone4961b12017-05-10 21:04:46 +0200561 return;
Akron9c4d1ae2016-05-25 21:43:22 +0200562 return this.liveItem(this.offset + index);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000563 },
564
565
Nils Diewald2fe12e12015-03-06 16:47:06 +0000566 /**
Akron9c4d1ae2016-05-25 21:43:22 +0200567 * Get the length of the full item list
Nils Diewald2fe12e12015-03-06 16:47:06 +0000568 */
569 length : function () {
570 return this._items.length;
571 },
572
573
574 /**
Akron5240b8c2016-05-20 09:17:41 +0200575 * Length of the filtered item list.
576 */
577 liveLength : function () {
578 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200579 this._initList();
Akron5240b8c2016-05-20 09:17:41 +0200580 return this._list.length;
581 },
582
583
584 /**
Nils Diewald2fe12e12015-03-06 16:47:06 +0000585 * Make the next item in the filtered menu active
Nils Diewald86dad5b2015-01-28 15:09:07 +0000586 */
587 next : function () {
Nils Diewald5975d702015-03-09 17:45:42 +0000588
Akronb38afb22016-05-25 19:30:01 +0200589 // No list
590 if (this.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200591 return;
Akronb38afb22016-05-25 19:30:01 +0200592
Akron9c4d1ae2016-05-25 21:43:22 +0200593 // Deactivate old item
594 if (this.position !== -1 && !this._prefix.active()) {
Akrone4961b12017-05-10 21:04:46 +0200595 this.liveItem(this.position).active(false);
Akron9c4d1ae2016-05-25 21:43:22 +0200596 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000597
Akron9c4d1ae2016-05-25 21:43:22 +0200598 // Get new active item
599 this.position++;
600 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000601
Nils Diewald5975d702015-03-09 17:45:42 +0000602 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000603 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000604
Akrone4961b12017-05-10 21:04:46 +0200605 // Activate prefix
606 var prefix = this._prefix;
Nils Diewald5975d702015-03-09 17:45:42 +0000607
Akrone4961b12017-05-10 21:04:46 +0200608 // Prefix is set and not active - choose!
609 if (prefix.isSet() && !prefix.active()) {
610 this.position--;
611 prefix.active(true);
612 return;
613 }
Akron9c4d1ae2016-05-25 21:43:22 +0200614
Akrone4961b12017-05-10 21:04:46 +0200615 // Choose first item
616 else {
617 newItem = this.liveItem(0);
618 // choose first item
619 this.position = 0;
620 this._showItems(0);
621 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000622 }
623
Akron5a1f5bb2016-05-23 22:00:39 +0200624 // The next element is after the viewport - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200625 else if (this.position >= (this.limit() + this.offset)) {
Akrone4961b12017-05-10 21:04:46 +0200626 this.screen(this.position - this.limit() + 1);
Akron5a1f5bb2016-05-23 22:00:39 +0200627 }
628
629 // The next element is before the viewport - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200630 else if (this.position <= this.offset) {
Akrone4961b12017-05-10 21:04:46 +0200631 this.screen(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000632 };
Nils Diewald5975d702015-03-09 17:45:42 +0000633
634 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000635 newItem.active(true);
636 },
637
Nils Diewalde8518f82015-03-18 22:41:49 +0000638 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000639 * Make the previous item in the menu active
640 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000641 prev : function () {
Nils Diewald2d210752015-03-09 19:01:15 +0000642
Akronb38afb22016-05-25 19:30:01 +0200643 // No list
644 if (this.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200645 return;
Akronb38afb22016-05-25 19:30:01 +0200646
Akron9c4d1ae2016-05-25 21:43:22 +0200647 // Deactivate old item
Nils Diewald2d210752015-03-09 19:01:15 +0000648 if (!this._prefix.active()) {
Akron9c4d1ae2016-05-25 21:43:22 +0200649
Akrone4961b12017-05-10 21:04:46 +0200650 // No active element set
651 if (this.position === -1) {
652 this.position = this.liveLength();
653 }
Akron9c4d1ae2016-05-25 21:43:22 +0200654
Akrone4961b12017-05-10 21:04:46 +0200655 // No active element set
656 else {
657 this.liveItem(this.position--).active(false);
658 };
Nils Diewald2d210752015-03-09 19:01:15 +0000659 };
660
Akron9c4d1ae2016-05-25 21:43:22 +0200661 // Get new active item
662 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000663
664 // The previous element is undefined - roll to bottom
665 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000666
Akrone4961b12017-05-10 21:04:46 +0200667 // Activate prefix
668 var prefix = this._prefix;
669 var offset = this.liveLength() - this.limit();
670
671 // Normalize offset
672 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000673
Akrone4961b12017-05-10 21:04:46 +0200674 // Choose the last item
675 this.position = this.liveLength() - 1;
676
677 // Prefix is set and not active - choose!
678 if (prefix.isSet() && !prefix.active()) {
679 this.position++;
680 prefix.active(true);
681 this.offset = offset;
682 return;
683 }
Nils Diewald2d210752015-03-09 19:01:15 +0000684
Akrone4961b12017-05-10 21:04:46 +0200685 // Choose last item
686 else {
687 newItem = this.liveItem(this.position);
688 this._showItems(offset);
689 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000690 }
691
Akron5a1f5bb2016-05-23 22:00:39 +0200692 // The previous element is before the view - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200693 else if (this.position < this.offset) {
Akrone4961b12017-05-10 21:04:46 +0200694 this.screen(this.position);
Akron5a1f5bb2016-05-23 22:00:39 +0200695 }
696
697 // The previous element is after the view - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200698 else if (this.position >= (this.limit() + this.offset)) {
Akrone4961b12017-05-10 21:04:46 +0200699 this.screen(this.position - this.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000700 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000701
Nils Diewald5975d702015-03-09 17:45:42 +0000702 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000703 newItem.active(true);
704 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000705
Akron3c2730f2016-05-24 15:08:29 +0200706 /**
707 * Move the page up by limit!
708 */
709 pageUp : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200710 this.screen(this.offset - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200711 },
712
713
714 /**
715 * Move the page down by limit!
716 */
717 pageDown : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200718 this.screen(this.offset + this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200719 },
720
Nils Diewald86dad5b2015-01-28 15:09:07 +0000721
Akron5240b8c2016-05-20 09:17:41 +0200722 // Unmark all items
723 _unmark : function () {
724 for (var i in this._list) {
Akrone4961b12017-05-10 21:04:46 +0200725 var item = this._items[this._list[i]];
726 item.lowlight();
727 item.active(false);
Akron5240b8c2016-05-20 09:17:41 +0200728 };
729 },
730
Akron5240b8c2016-05-20 09:17:41 +0200731 // Set boundary for viewport
732 _boundary : function (bool) {
733 this.item(this._list[0]).noMore(bool);
734 this.item(this._list[this._list.length - 1]).noMore(bool);
735 },
736
737
738 // Append Items that should be shown
739 _showItems : function (off) {
740
Akrona92fd8d2016-05-24 21:13:41 +0200741 // optimization: scroll down one step
Akron9c4d1ae2016-05-25 21:43:22 +0200742 if (this.offset === (off - 1)) {
Akrone4961b12017-05-10 21:04:46 +0200743 this.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200744
Akrone4961b12017-05-10 21:04:46 +0200745 // Remove the HTML node from the first item
746 // leave lengthField/prefix/slider
747 this._element.removeChild(this._element.children[3]);
748 var pos = this.offset + this.limit() - 1;
749 this._append(this._list[pos]);
Akrona92fd8d2016-05-24 21:13:41 +0200750 }
Akron5240b8c2016-05-20 09:17:41 +0200751
Akrona92fd8d2016-05-24 21:13:41 +0200752 // optimization: scroll up one step
Akron9c4d1ae2016-05-25 21:43:22 +0200753 else if (this.offset === (off + 1)) {
Akrone4961b12017-05-10 21:04:46 +0200754 this.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200755
Akrone4961b12017-05-10 21:04:46 +0200756 // Remove the HTML node from the last item
757 this._element.removeChild(this._element.lastChild);
Akron9c4d1ae2016-05-25 21:43:22 +0200758
Akrone4961b12017-05-10 21:04:46 +0200759 this._prepend(this._list[this.offset]);
Akrona92fd8d2016-05-24 21:13:41 +0200760 }
761 else {
Akrone4961b12017-05-10 21:04:46 +0200762 this.offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200763
Akrone4961b12017-05-10 21:04:46 +0200764 // Remove all items
765 this.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200766
Akrone4961b12017-05-10 21:04:46 +0200767 // Use list
768 var shown = 0;
769 var i;
Akron5240b8c2016-05-20 09:17:41 +0200770
Akrone4961b12017-05-10 21:04:46 +0200771 for (i in this._list) {
Akrona92fd8d2016-05-24 21:13:41 +0200772
Akrone4961b12017-05-10 21:04:46 +0200773 // Don't show - it's before offset
774 shown++;
775 if (shown <= off)
776 continue;
Akrona92fd8d2016-05-24 21:13:41 +0200777
Akrone4961b12017-05-10 21:04:46 +0200778 var itemNr = this._list[i];
779 var item = this.item(itemNr);
780 this._append(itemNr);
781
782 if (shown >= (this.limit() + off))
783 break;
784 };
Akron5240b8c2016-05-20 09:17:41 +0200785 };
786
787 // set the slider to the new offset
Akron9c4d1ae2016-05-25 21:43:22 +0200788 this._slider.offset(this.offset);
Akron5240b8c2016-05-20 09:17:41 +0200789 },
790
791
792 // Append item to the shown list based on index
793 _append : function (i) {
794 var item = this.item(i);
795
796 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200797 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200798 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200799 };
800
Akron5240b8c2016-05-20 09:17:41 +0200801
802 // Append element
803 this.element().appendChild(item.element());
804 },
805
806
807 // Prepend item to the shown list based on index
808 _prepend : function (i) {
809 var item = this.item(i);
810
811 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200812 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200813 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200814 };
Akron5240b8c2016-05-20 09:17:41 +0200815
816 var e = this.element();
Akron9c4d1ae2016-05-25 21:43:22 +0200817
Akron5240b8c2016-05-20 09:17:41 +0200818 // Append element after lengthField/prefix/slider
819 e.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200820 item.element(),
821 e.children[3]
Akron5240b8c2016-05-20 09:17:41 +0200822 );
Nils Diewald2fe12e12015-03-06 16:47:06 +0000823 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000824 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000825});