blob: b2ec25bd68f3129c575a20cdc640fa3cecbf2bb9 [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/*
Akron3c2730f2016-05-24 15:08:29 +02007 * TODO: Show the slider briefly on move (whenever screen is called).
Akron9c4d1ae2016-05-25 21:43:22 +02008 * TODO: Ignore alt+ and strg+ key strokes.
Akron0b92f692016-05-25 22:37:13 +02009 * TODO: Should scroll to a chosen value after prefixing, if the chosen value is live
Akron201b72a2016-06-03 01:46:19 +020010 * TODO: Add a "title" to a menu that is not scrollable.
11 * TODO: Make the menu responsive by showing less items on smaller screens
12 * or anytime items would be outside the screen.
Akron02360e42016-06-07 13:41:12 +020013 * TODO: Add a .match() method to items for scrolling and probably for prefixing.
14 * TODO: Add static header (for title, sortation fields, but also for menu points like "fragments" and "history".
Akrone91da782017-12-15 17:17:50 +010015 * TODO: Support space separated list of prefixes so "co no" will highlight "common noun"
Nils Diewald2488d052015-04-09 21:46:02 +000016 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000017define([
18 'menu/item',
19 'menu/prefix',
Akronc7448732016-04-27 14:06:58 +020020 'menu/lengthField',
Akron9905e2a2016-05-10 16:06:44 +020021 'menu/slider',
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 'util'
23], function (defaultItemClass,
Akrone4961b12017-05-10 21:04:46 +020024 defaultPrefixClass,
25 defaultLengthFieldClass,
26 sliderClass) {
Nils Diewaldfda29d92015-01-22 17:28:01 +000027
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 // Default maximum number of menu items
29 var menuLimit = 8;
30
31 function _codeFromEvent (e) {
32 if (e.charCode && (e.keyCode == 0))
33 return e.charCode
34 return e.keyCode;
Nils Diewald59c02fc2015-03-07 01:29:09 +000035 };
36
Nils Diewald86dad5b2015-01-28 15:09:07 +000037
38 /**
39 * List of items for drop down menu (complete).
40 * Only a sublist of the menu is filtered (live).
41 * Only a sublist of the filtered menu is visible (shown).
42 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000043 return {
Nils Diewald86dad5b2015-01-28 15:09:07 +000044 /**
45 * Create new Menu based on the action prefix
46 * and a list of menu items.
47 *
Akron7524be12016-06-01 17:31:33 +020048 *
49 * Accepts an associative array containg the elements
50 * itemClass, prefixClass, lengthFieldClass
51 *
Nils Diewald86dad5b2015-01-28 15:09:07 +000052 * @this {Menu}
53 * @constructor
54 * @param {string} Context prefix
55 * @param {Array.<Array.<string>>} List of menu items
56 */
Akron7524be12016-06-01 17:31:33 +020057 create : function (list, params) {
58 return Object.create(this)._init(list, params);
Nils Diewald86dad5b2015-01-28 15:09:07 +000059 },
60
Akron5240b8c2016-05-20 09:17:41 +020061 // Initialize list
Akron7524be12016-06-01 17:31:33 +020062 _init : function (list, params) {
Akrona92fd8d2016-05-24 21:13:41 +020063
Akron7524be12016-06-01 17:31:33 +020064 if (params === undefined)
Akrone4961b12017-05-10 21:04:46 +020065 params = {};
Akron7524be12016-06-01 17:31:33 +020066
67 this._itemClass = params["itemClass"] || defaultItemClass;
Akron5240b8c2016-05-20 09:17:41 +020068
69 // Add prefix object
Akron7524be12016-06-01 17:31:33 +020070 if (params["prefixClass"] !== undefined) {
Akrone4961b12017-05-10 21:04:46 +020071 this._prefix = params["prefixClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020072 }
73 else {
Akrone4961b12017-05-10 21:04:46 +020074 this._prefix = defaultPrefixClass.create();
Akron5240b8c2016-05-20 09:17:41 +020075 };
76 this._prefix._menu = this;
77
78 // Add lengthField object
Akron7524be12016-06-01 17:31:33 +020079 if (params["lengthFieldClass"] !== undefined) {
Akrone4961b12017-05-10 21:04:46 +020080 this._lengthField = params["lengthFieldClass"].create();
Akron5240b8c2016-05-20 09:17:41 +020081 }
82 else {
Akrone4961b12017-05-10 21:04:46 +020083 this._lengthField = defaultLengthFieldClass.create();
Akron5240b8c2016-05-20 09:17:41 +020084 };
85 this._lengthField._menu = this;
86
87 // Initialize slider
88 this._slider = sliderClass.create(this);
89
90 // Create the element
Akron9c4d1ae2016-05-25 21:43:22 +020091 var el = document.createElement("ul");
92 with (el) {
Akrone4961b12017-05-10 21:04:46 +020093 style.outline = 0;
94 setAttribute('tabindex', 0);
95 classList.add('menu', 'roll');
96 appendChild(this._prefix.element());
97 appendChild(this._lengthField.element());
98 appendChild(this._slider.element());
Akron9c4d1ae2016-05-25 21:43:22 +020099 };
Akron5240b8c2016-05-20 09:17:41 +0200100
101 // This has to be cleaned up later on
Akron9c4d1ae2016-05-25 21:43:22 +0200102 el["menu"] = this;
Akron5240b8c2016-05-20 09:17:41 +0200103
104 // Arrow keys
Akron9c4d1ae2016-05-25 21:43:22 +0200105 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200106 'keydown',
107 this._keydown.bind(this),
108 false
Akron5240b8c2016-05-20 09:17:41 +0200109 );
110
111 // Strings
Akron9c4d1ae2016-05-25 21:43:22 +0200112 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200113 'keypress',
114 this._keypress.bind(this),
115 false
Akron5240b8c2016-05-20 09:17:41 +0200116 );
117
118 // Mousewheel
Akron9c4d1ae2016-05-25 21:43:22 +0200119 el.addEventListener(
Akrone4961b12017-05-10 21:04:46 +0200120 'wheel',
121 this._mousewheel.bind(this),
122 false
Akron5240b8c2016-05-20 09:17:41 +0200123 );
Akron9c4d1ae2016-05-25 21:43:22 +0200124 this._element = el;
Akroneaba63e2018-01-26 19:49:30 +0100125
126 this._limit = menuLimit;
Akrone4961b12017-05-10 21:04:46 +0200127
Akron5240b8c2016-05-20 09:17:41 +0200128 this._items = new Array();
Akron5240b8c2016-05-20 09:17:41 +0200129
Akroneaba63e2018-01-26 19:49:30 +0100130 // TODO:
131 // Make this separate from _init
132 this.readItems(list);
133
134 return this;
135 },
136
137 // Read items to add to list
138 readItems : function (list) {
139
140 this._list = undefined;
141
142 // Remove circular reference to "this" in items
143 for (var i = 0; i < this._items.length; i++) {
144 delete this._items[i]["_menu"];
145 delete this._items[i];
146 };
147
148 this._items = new Array();
149 this.removeItems();
150
151
152 // Initialize items
153 this._lengthField.reset();
154
Akron9c4d1ae2016-05-25 21:43:22 +0200155 var i = 0;
Akron5240b8c2016-05-20 09:17:41 +0200156 // Initialize item list based on parameters
Akron7524be12016-06-01 17:31:33 +0200157 for (i in list) {
Akrone4961b12017-05-10 21:04:46 +0200158 var obj = this._itemClass.create(list[i]);
Akron5240b8c2016-05-20 09:17:41 +0200159
Akrone4961b12017-05-10 21:04:46 +0200160 // This may become circular
161 obj["_menu"] = this;
162 this._lengthField.add(list[i]);
163 this._items.push(obj);
Akron5240b8c2016-05-20 09:17:41 +0200164 };
165
Akron9c4d1ae2016-05-25 21:43:22 +0200166 this._slider.length(this.liveLength())
Akrone4961b12017-05-10 21:04:46 +0200167 .limit(this._limit)
168 .reInit();
169
Akroneaba63e2018-01-26 19:49:30 +0100170 this._firstActive = false;
171 // Show the first item active always?
Akron9c4d1ae2016-05-25 21:43:22 +0200172 this.offset = 0;
173 this.position = 0;
Akron5240b8c2016-05-20 09:17:41 +0200174 },
Akroneaba63e2018-01-26 19:49:30 +0100175
Akron5240b8c2016-05-20 09:17:41 +0200176 // Initialize the item list
177 _initList : function () {
178
179 // Create a new list
180 if (this._list === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200181 this._list = [];
Akron5240b8c2016-05-20 09:17:41 +0200182 }
183 else if (this._list.length !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200184 this._boundary(false);
185 this._list.length = 0;
Akron5240b8c2016-05-20 09:17:41 +0200186 };
187
188 // Offset is initially zero
Akron9c4d1ae2016-05-25 21:43:22 +0200189 this.offset = 0;
Akron5240b8c2016-05-20 09:17:41 +0200190
191 // There is no prefix set
192 if (this.prefix().length <= 0) {
193
Akrone4961b12017-05-10 21:04:46 +0200194 // add all items to the list and lowlight
195 var i = 0;
196 for (; i < this._items.length; i++) {
197 this._list.push(i);
198 this._items[i].lowlight();
199 };
Akron5240b8c2016-05-20 09:17:41 +0200200
Akroneaba63e2018-01-26 19:49:30 +0100201 this._slider.length(i).reInit();
Akron97752a72016-05-25 14:43:07 +0200202
Akrone4961b12017-05-10 21:04:46 +0200203 return true;
Akron5240b8c2016-05-20 09:17:41 +0200204 };
205
206 /*
207 * There is a prefix set, so filter the list!
208 */
209 var pos;
Akronacffc652017-12-18 21:21:25 +0100210 var prefixList = this.prefix().toLowerCase().split(" ");
211
212 var items = [];
213 var maxPoints = 1; // minimum 1
Akron5240b8c2016-05-20 09:17:41 +0200214
215 // Iterate over all items and choose preferred matching items
216 // i.e. the matching happens at the word start
217 for (pos = 0; pos < this._items.length; pos++) {
Akronacffc652017-12-18 21:21:25 +0100218
219 var points = 0;
220
221 for (pref = 0; pref < prefixList.length; pref++) {
222 var prefix = " " + prefixList[pref];
223
224 // Check if it matches at the beginning
225 if ((this.item(pos).lcField().indexOf(prefix)) >= 0) {
226 points += 5;
227 }
228
229 // Check if it matches anywhere
230 else if ((this.item(pos).lcField().indexOf(prefix.substring(1))) >= 0) {
231 points += 1;
232 };
233 };
234
235 if (points > maxPoints) {
236 this._list = [pos];
237 maxPoints = points;
238 }
239 else if (points == maxPoints) {
Akrone4961b12017-05-10 21:04:46 +0200240 this._list.push(pos);
Akronacffc652017-12-18 21:21:25 +0100241 }
Akron5240b8c2016-05-20 09:17:41 +0200242 };
243
244 // The list is empty - so lower your expectations
245 // Iterate over all items and choose matching items
246 // i.e. the matching happens anywhere in the word
Akronacffc652017-12-18 21:21:25 +0100247 /*
Akron6ffad5d2016-05-24 17:16:58 +0200248 prefix = prefix.substring(1);
Akron5240b8c2016-05-20 09:17:41 +0200249 if (this._list.length == 0) {
Akrone4961b12017-05-10 21:04:46 +0200250 for (pos = 0; pos < this._items.length; pos++) {
251 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
252 this._list.push(pos);
253 };
Akron5240b8c2016-05-20 09:17:41 +0200254 };
Akronacffc652017-12-18 21:21:25 +0100255 */
Akron5240b8c2016-05-20 09:17:41 +0200256
Akron9c4d1ae2016-05-25 21:43:22 +0200257 this._slider.length(this._list.length).reInit();
Akron6ed13992016-05-23 18:06:05 +0200258
Akron5240b8c2016-05-20 09:17:41 +0200259 // Filter was successful - yeah!
260 return this._list.length > 0 ? true : false;
261 },
262
263
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000264 /**
265 * Destroy this menu
266 * (in case you don't trust the
267 * mark and sweep GC)!
268 */
269 destroy : function () {
Akron47c086c2016-05-18 21:22:06 +0200270
Akron5240b8c2016-05-20 09:17:41 +0200271 // Remove circular reference to "this" in menu
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000272 if (this._element != undefined)
Akrone4961b12017-05-10 21:04:46 +0200273 delete this._element["menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000274
Akron5240b8c2016-05-20 09:17:41 +0200275 // Remove circular reference to "this" in items
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000276 for (var i = 0; i < this._items.length; i++) {
Akrone4961b12017-05-10 21:04:46 +0200277 delete this._items[i]["_menu"];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000278 };
Akron5240b8c2016-05-20 09:17:41 +0200279
280 // Remove circular reference to "this" in prefix
Nils Diewald5c5a7472015-04-02 22:13:38 +0000281 delete this._prefix['_menu'];
Akron5240b8c2016-05-20 09:17:41 +0200282 delete this._lengthField['_menu'];
283 delete this._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000284 },
285
Nils Diewald7148c6f2015-05-04 15:07:53 +0000286
287 /**
288 * Focus on this menu.
289 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000290 focus : function () {
291 this._element.focus();
292 },
293
Nils Diewald7148c6f2015-05-04 15:07:53 +0000294
Nils Diewald59c02fc2015-03-07 01:29:09 +0000295 // mouse wheel treatment
296 _mousewheel : function (e) {
297 var delta = 0;
Nils Diewald5975d702015-03-09 17:45:42 +0000298
299 delta = e.deltaY / 120;
300 if (delta > 0)
Akrone4961b12017-05-10 21:04:46 +0200301 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000302 else if (delta < 0)
Akrone4961b12017-05-10 21:04:46 +0200303 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000304 e.halt();
305 },
306
Nils Diewald7148c6f2015-05-04 15:07:53 +0000307
Nils Diewald59c02fc2015-03-07 01:29:09 +0000308 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000309 _keydown : function (e) {
Nils Diewald59c02fc2015-03-07 01:29:09 +0000310 var code = _codeFromEvent(e);
311
Nils Diewald59c02fc2015-03-07 01:29:09 +0000312 switch (code) {
313 case 27: // 'Esc'
Akrone4961b12017-05-10 21:04:46 +0200314 e.halt();
315 this.hide();
316 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000317
Nils Diewald59c02fc2015-03-07 01:29:09 +0000318 case 38: // 'Up'
Akrone4961b12017-05-10 21:04:46 +0200319 e.halt();
320 this.prev();
321 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000322 case 33: // 'Page up'
Akrone4961b12017-05-10 21:04:46 +0200323 e.halt();
324 this.pageUp();
325 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000326 case 40: // 'Down'
Akrone4961b12017-05-10 21:04:46 +0200327 e.halt();
328 this.next();
329 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000330 case 34: // 'Page down'
Akrone4961b12017-05-10 21:04:46 +0200331 e.halt();
332 this.pageDown();
333 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000334 case 39: // 'Right'
Akrone4961b12017-05-10 21:04:46 +0200335 if (this._prefix.active())
336 break;
Nils Diewalde8518f82015-03-18 22:41:49 +0000337
Akrone4961b12017-05-10 21:04:46 +0200338 var item = this.liveItem(this.position);
339
340 if (item["further"] !== undefined) {
341 item["further"].bind(item).apply();
342 };
343
344 e.halt();
345 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000346 case 13: // 'Enter'
347
Akrone4961b12017-05-10 21:04:46 +0200348 // Click on prefix
349 if (this._prefix.active())
350 this._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000351
Akrone4961b12017-05-10 21:04:46 +0200352 // Click on item
353 else
354 this.liveItem(this.position).onclick(e);
355 e.halt();
356 break;
Nils Diewald59c02fc2015-03-07 01:29:09 +0000357 case 8: // 'Backspace'
Akrone4961b12017-05-10 21:04:46 +0200358 this._prefix.chop();
359 this.show();
360 e.halt();
361 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000362 };
363 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000364
Nils Diewald47f366b2015-04-15 20:06:35 +0000365 // Add characters to prefix
366 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200367 if (e.charCode !== 0) {
Akrone4961b12017-05-10 21:04:46 +0200368 e.halt();
369 var c = String.fromCharCode(_codeFromEvent(e));
370
371 // Add prefix
372 this._prefix.add(c);
373 this.show();
Akron9c2f9382016-05-25 16:36:04 +0200374 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000375 },
376
Akron47c086c2016-05-18 21:22:06 +0200377 /**
Akron5240b8c2016-05-20 09:17:41 +0200378 * Show a screen with a given offset
379 * in the viewport.
Akron47c086c2016-05-18 21:22:06 +0200380 */
381 screen : function (nr) {
Akron3c2730f2016-05-24 15:08:29 +0200382 if (nr < 0) {
Akrone4961b12017-05-10 21:04:46 +0200383 nr = 0
Akron3c2730f2016-05-24 15:08:29 +0200384 }
Akron6ac58442016-05-24 16:52:29 +0200385 else if (nr > (this.liveLength() - this.limit())) {
Akrone4961b12017-05-10 21:04:46 +0200386 nr = (this.liveLength() - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200387 };
388
Akron9c4d1ae2016-05-25 21:43:22 +0200389 if (this.offset === nr)
Akrone4961b12017-05-10 21:04:46 +0200390 return;
Akron5a1f5bb2016-05-23 22:00:39 +0200391
Akron47c086c2016-05-18 21:22:06 +0200392 this._showItems(nr);
393 },
394
Nils Diewald2fe12e12015-03-06 16:47:06 +0000395 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000396 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000397 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000398 element : function () {
399 return this._element;
400 },
401
Nils Diewald2fe12e12015-03-06 16:47:06 +0000402 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000403 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000404 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000405 itemClass : function () {
406 return this._itemClass;
407 },
408
409 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000410 * Get and set the numerical value
411 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000412 */
413 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000414 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200415 if (this._limit !== limit) {
416 this._limit = limit;
417 this._slider.limit(limit).reInit();
418 };
419 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000420 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000421 return this._limit;
422 },
423
Nils Diewald7148c6f2015-05-04 15:07:53 +0000424
Nils Diewald86dad5b2015-01-28 15:09:07 +0000425 /**
426 * Upgrade this object to another object,
427 * while private data stays intact.
428 *
Nils Diewald2fe12e12015-03-06 16:47:06 +0000429 * @param {Object} An object with properties.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000430 */
431 upgradeTo : function (props) {
432 for (var prop in props) {
Akrone4961b12017-05-10 21:04:46 +0200433 this[prop] = props[prop];
Nils Diewald86dad5b2015-01-28 15:09:07 +0000434 };
435 return this;
436 },
437
Nils Diewald7148c6f2015-05-04 15:07:53 +0000438
Nils Diewald86dad5b2015-01-28 15:09:07 +0000439 /**
Akron97752a72016-05-25 14:43:07 +0200440 * Filter the list and make it visible.
441 * This is always called once the prefix changes.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000442 *
443 * @param {string} Prefix for filtering the list
444 */
Akron6ed13992016-05-23 18:06:05 +0200445 show : function (active) {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000446
Akron5240b8c2016-05-20 09:17:41 +0200447 // show menu based on initial offset
Akron6ac58442016-05-24 16:52:29 +0200448 this._unmark(); // Unmark everything that was marked before
Akrona92fd8d2016-05-24 21:13:41 +0200449 this.removeItems();
Akron6ed13992016-05-23 18:06:05 +0200450
451 // Initialize the list
452 if (!this._initList()) {
Akron6ac58442016-05-24 16:52:29 +0200453
Akrone4961b12017-05-10 21:04:46 +0200454 // The prefix is not active
455 this._prefix.active(true);
Akron6ed13992016-05-23 18:06:05 +0200456
Akrone4961b12017-05-10 21:04:46 +0200457 // finally show the element
458 this._element.classList.add('visible');
459
460 return true;
Akron6ed13992016-05-23 18:06:05 +0200461 };
462
463 var offset = 0;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000464
Akron9c2f9382016-05-25 16:36:04 +0200465 // Set a chosen value to active and move the viewport
Akron6ed13992016-05-23 18:06:05 +0200466 if (arguments.length === 1) {
467
Akrone4961b12017-05-10 21:04:46 +0200468 // Normalize active value
469 if (active < 0) {
470 active = 0;
471 }
472 else if (active >= this.liveLength()) {
473 active = this.liveLength() - 1;
474 };
Akron6ed13992016-05-23 18:06:05 +0200475
Akrone4961b12017-05-10 21:04:46 +0200476 // Item is outside the first viewport
477 if (active >= this._limit) {
478 offset = active;
479 if (offset > (this.liveLength() - this._limit)) {
480 offset = this.liveLength() - this._limit;
481 };
482 };
483
484 this.position = active;
Akron6ed13992016-05-23 18:06:05 +0200485 }
486
Akron9c2f9382016-05-25 16:36:04 +0200487 // Choose the first item
Akron6ed13992016-05-23 18:06:05 +0200488 else if (this._firstActive) {
Akrone4961b12017-05-10 21:04:46 +0200489 this.position = 0;
Akron47c086c2016-05-18 21:22:06 +0200490 }
Akroncb351d62016-05-19 23:10:33 +0200491
Akron9c2f9382016-05-25 16:36:04 +0200492 // Choose no item
Akron47c086c2016-05-18 21:22:06 +0200493 else {
Akrone4961b12017-05-10 21:04:46 +0200494 this.position = -1;
Akron6ed13992016-05-23 18:06:05 +0200495 };
496
Akron9c4d1ae2016-05-25 21:43:22 +0200497 this.offset = offset;
Akron6ed13992016-05-23 18:06:05 +0200498 this._showItems(offset); // Show new item list
499
500 // Make chosen value active
501 if (this.position !== -1) {
Akrone4961b12017-05-10 21:04:46 +0200502 this.liveItem(this.position).active(true);
Akron6ed13992016-05-23 18:06:05 +0200503 };
Akron37513a62015-11-17 01:07:11 +0100504
Akron5240b8c2016-05-20 09:17:41 +0200505 // The prefix is not active
Nils Diewalde8518f82015-03-18 22:41:49 +0000506 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000507
Akron5240b8c2016-05-20 09:17:41 +0200508 // finally show the element
Akron6bb71582016-06-10 20:41:08 +0200509 this._element.classList.add('visible');
Nils Diewald2fe12e12015-03-06 16:47:06 +0000510
Nils Diewald86dad5b2015-01-28 15:09:07 +0000511 // Add classes for rolling menus
512 this._boundary(true);
Akron6bb71582016-06-10 20:41:08 +0200513
Nils Diewald59c02fc2015-03-07 01:29:09 +0000514 return true;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000515 },
516
Nils Diewald7148c6f2015-05-04 15:07:53 +0000517
518 /**
519 * Hide the menu and call the onHide callback.
520 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000521 hide : function () {
Akrona92fd8d2016-05-24 21:13:41 +0200522 this.removeItems();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000523 this._prefix.clear();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000524 this.onHide();
Akron6bb71582016-06-10 20:41:08 +0200525 this._element.classList.remove('visible');
526
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000527 /* this._element.blur(); */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000528 },
529
Nils Diewald7148c6f2015-05-04 15:07:53 +0000530 /**
531 * Function released when the menu hides.
532 * This method is expected to be overridden.
533 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000534 onHide : function () {},
535
Nils Diewald7148c6f2015-05-04 15:07:53 +0000536
Nils Diewald86dad5b2015-01-28 15:09:07 +0000537 /**
538 * Get the prefix for filtering,
Nils Diewald2fe12e12015-03-06 16:47:06 +0000539 * e.g. &quot;ve&quot; for &quot;verb&quot;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000540 */
Nils Diewald5975d702015-03-09 17:45:42 +0000541 prefix : function (pref) {
542 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200543 this._prefix.value(pref);
544 return this;
Nils Diewald5975d702015-03-09 17:45:42 +0000545 };
546 return this._prefix.value();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000547 },
548
Akronc7448732016-04-27 14:06:58 +0200549 /**
550 * Get the lengthField object.
551 */
552 lengthField : function () {
553 return this._lengthField;
554 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000555
Akron5240b8c2016-05-20 09:17:41 +0200556 /**
557 * Get the associated slider object.
558 */
559 slider : function () {
560 return this._slider;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000561 },
562
Akron5240b8c2016-05-20 09:17:41 +0200563
Nils Diewald86dad5b2015-01-28 15:09:07 +0000564 /**
565 * Delete all visible items from the menu element
566 */
Akrona92fd8d2016-05-24 21:13:41 +0200567 removeItems : function () {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000568 var child;
Nils Diewald2fe12e12015-03-06 16:47:06 +0000569
Nils Diewald2fe12e12015-03-06 16:47:06 +0000570 // Remove all children
Nils Diewald5975d702015-03-09 17:45:42 +0000571 var children = this._element.childNodes;
Akronc7448732016-04-27 14:06:58 +0200572 // Leave the prefix and lengthField
Akron9905e2a2016-05-10 16:06:44 +0200573 for (var i = children.length - 1; i >= 3; i--) {
Akrone4961b12017-05-10 21:04:46 +0200574 this._element.removeChild(
575 children[i]
576 );
Nils Diewald5975d702015-03-09 17:45:42 +0000577 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000578 },
579
Nils Diewald2fe12e12015-03-06 16:47:06 +0000580 /**
581 * Get a specific item from the complete list
582 *
583 * @param {number} index of the list item
584 */
585 item : function (index) {
586 return this._items[index]
587 },
588
589
Nils Diewald86dad5b2015-01-28 15:09:07 +0000590 /**
591 * Get a specific item from the filtered list
592 *
593 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000594 * in the filtered list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000595 */
596 liveItem : function (index) {
597 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200598 if (!this._initList())
599 return;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000600
601 return this._items[this._list[index]];
602 },
603
Nils Diewald86dad5b2015-01-28 15:09:07 +0000604
605 /**
Akron5240b8c2016-05-20 09:17:41 +0200606 * Get a specific item from the viewport list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000607 *
608 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000609 * in the visible list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000610 */
611 shownItem : function (index) {
612 if (index >= this.limit())
Akrone4961b12017-05-10 21:04:46 +0200613 return;
Akron9c4d1ae2016-05-25 21:43:22 +0200614 return this.liveItem(this.offset + index);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000615 },
616
617
Nils Diewald2fe12e12015-03-06 16:47:06 +0000618 /**
Akron9c4d1ae2016-05-25 21:43:22 +0200619 * Get the length of the full item list
Nils Diewald2fe12e12015-03-06 16:47:06 +0000620 */
621 length : function () {
622 return this._items.length;
623 },
624
625
626 /**
Akron5240b8c2016-05-20 09:17:41 +0200627 * Length of the filtered item list.
628 */
629 liveLength : function () {
630 if (this._list === undefined)
Akrone4961b12017-05-10 21:04:46 +0200631 this._initList();
Akron5240b8c2016-05-20 09:17:41 +0200632 return this._list.length;
633 },
634
635
636 /**
Nils Diewald2fe12e12015-03-06 16:47:06 +0000637 * Make the next item in the filtered menu active
Nils Diewald86dad5b2015-01-28 15:09:07 +0000638 */
639 next : function () {
Nils Diewald5975d702015-03-09 17:45:42 +0000640
Akronb38afb22016-05-25 19:30:01 +0200641 // No list
642 if (this.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200643 return;
Akronb38afb22016-05-25 19:30:01 +0200644
Akron9c4d1ae2016-05-25 21:43:22 +0200645 // Deactivate old item
646 if (this.position !== -1 && !this._prefix.active()) {
Akrone4961b12017-05-10 21:04:46 +0200647 this.liveItem(this.position).active(false);
Akron9c4d1ae2016-05-25 21:43:22 +0200648 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000649
Akron9c4d1ae2016-05-25 21:43:22 +0200650 // Get new active item
651 this.position++;
652 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000653
Nils Diewald5975d702015-03-09 17:45:42 +0000654 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000655 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000656
Akrone4961b12017-05-10 21:04:46 +0200657 // Activate prefix
658 var prefix = this._prefix;
Nils Diewald5975d702015-03-09 17:45:42 +0000659
Akrone4961b12017-05-10 21:04:46 +0200660 // Prefix is set and not active - choose!
661 if (prefix.isSet() && !prefix.active()) {
662 this.position--;
663 prefix.active(true);
664 return;
665 }
Akron9c4d1ae2016-05-25 21:43:22 +0200666
Akrone4961b12017-05-10 21:04:46 +0200667 // Choose first item
668 else {
669 newItem = this.liveItem(0);
670 // choose first item
671 this.position = 0;
672 this._showItems(0);
673 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000674 }
675
Akron5a1f5bb2016-05-23 22:00:39 +0200676 // The next element is after the viewport - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200677 else if (this.position >= (this.limit() + this.offset)) {
Akrone4961b12017-05-10 21:04:46 +0200678 this.screen(this.position - this.limit() + 1);
Akron5a1f5bb2016-05-23 22:00:39 +0200679 }
680
681 // The next element is before the viewport - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200682 else if (this.position <= this.offset) {
Akrone4961b12017-05-10 21:04:46 +0200683 this.screen(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000684 };
Nils Diewald5975d702015-03-09 17:45:42 +0000685
686 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000687 newItem.active(true);
688 },
689
Nils Diewalde8518f82015-03-18 22:41:49 +0000690 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000691 * Make the previous item in the menu active
692 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000693 prev : function () {
Nils Diewald2d210752015-03-09 19:01:15 +0000694
Akronb38afb22016-05-25 19:30:01 +0200695 // No list
696 if (this.liveLength() === 0)
Akrone4961b12017-05-10 21:04:46 +0200697 return;
Akronb38afb22016-05-25 19:30:01 +0200698
Akron9c4d1ae2016-05-25 21:43:22 +0200699 // Deactivate old item
Nils Diewald2d210752015-03-09 19:01:15 +0000700 if (!this._prefix.active()) {
Akron9c4d1ae2016-05-25 21:43:22 +0200701
Akrone4961b12017-05-10 21:04:46 +0200702 // No active element set
703 if (this.position === -1) {
704 this.position = this.liveLength();
705 }
Akron9c4d1ae2016-05-25 21:43:22 +0200706
Akrone4961b12017-05-10 21:04:46 +0200707 // No active element set
708 else {
709 this.liveItem(this.position--).active(false);
710 };
Nils Diewald2d210752015-03-09 19:01:15 +0000711 };
712
Akron9c4d1ae2016-05-25 21:43:22 +0200713 // Get new active item
714 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000715
716 // The previous element is undefined - roll to bottom
717 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000718
Akrone4961b12017-05-10 21:04:46 +0200719 // Activate prefix
720 var prefix = this._prefix;
721 var offset = this.liveLength() - this.limit();
722
723 // Normalize offset
724 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000725
Akrone4961b12017-05-10 21:04:46 +0200726 // Choose the last item
727 this.position = this.liveLength() - 1;
728
729 // Prefix is set and not active - choose!
730 if (prefix.isSet() && !prefix.active()) {
731 this.position++;
732 prefix.active(true);
733 this.offset = offset;
734 return;
735 }
Nils Diewald2d210752015-03-09 19:01:15 +0000736
Akrone4961b12017-05-10 21:04:46 +0200737 // Choose last item
738 else {
739 newItem = this.liveItem(this.position);
740 this._showItems(offset);
741 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000742 }
743
Akron5a1f5bb2016-05-23 22:00:39 +0200744 // The previous element is before the view - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200745 else if (this.position < this.offset) {
Akrone4961b12017-05-10 21:04:46 +0200746 this.screen(this.position);
Akron5a1f5bb2016-05-23 22:00:39 +0200747 }
748
749 // The previous element is after the view - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200750 else if (this.position >= (this.limit() + this.offset)) {
Akrone4961b12017-05-10 21:04:46 +0200751 this.screen(this.position - this.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000752 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000753
Nils Diewald5975d702015-03-09 17:45:42 +0000754 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000755 newItem.active(true);
756 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000757
Akron3c2730f2016-05-24 15:08:29 +0200758 /**
759 * Move the page up by limit!
760 */
761 pageUp : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200762 this.screen(this.offset - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200763 },
764
765
766 /**
767 * Move the page down by limit!
768 */
769 pageDown : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200770 this.screen(this.offset + this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200771 },
772
Nils Diewald86dad5b2015-01-28 15:09:07 +0000773
Akron5240b8c2016-05-20 09:17:41 +0200774 // Unmark all items
775 _unmark : function () {
776 for (var i in this._list) {
Akrone4961b12017-05-10 21:04:46 +0200777 var item = this._items[this._list[i]];
778 item.lowlight();
779 item.active(false);
Akron5240b8c2016-05-20 09:17:41 +0200780 };
781 },
782
Akron5240b8c2016-05-20 09:17:41 +0200783 // Set boundary for viewport
784 _boundary : function (bool) {
785 this.item(this._list[0]).noMore(bool);
786 this.item(this._list[this._list.length - 1]).noMore(bool);
787 },
788
789
790 // Append Items that should be shown
791 _showItems : function (off) {
792
Akrona92fd8d2016-05-24 21:13:41 +0200793 // optimization: scroll down one step
Akron9c4d1ae2016-05-25 21:43:22 +0200794 if (this.offset === (off - 1)) {
Akrone4961b12017-05-10 21:04:46 +0200795 this.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200796
Akrone4961b12017-05-10 21:04:46 +0200797 // Remove the HTML node from the first item
798 // leave lengthField/prefix/slider
799 this._element.removeChild(this._element.children[3]);
800 var pos = this.offset + this.limit() - 1;
801 this._append(this._list[pos]);
Akrona92fd8d2016-05-24 21:13:41 +0200802 }
Akron5240b8c2016-05-20 09:17:41 +0200803
Akrona92fd8d2016-05-24 21:13:41 +0200804 // optimization: scroll up one step
Akron9c4d1ae2016-05-25 21:43:22 +0200805 else if (this.offset === (off + 1)) {
Akrone4961b12017-05-10 21:04:46 +0200806 this.offset = off;
Akron9c4d1ae2016-05-25 21:43:22 +0200807
Akrone4961b12017-05-10 21:04:46 +0200808 // Remove the HTML node from the last item
809 this._element.removeChild(this._element.lastChild);
Akron9c4d1ae2016-05-25 21:43:22 +0200810
Akrone4961b12017-05-10 21:04:46 +0200811 this._prepend(this._list[this.offset]);
Akrona92fd8d2016-05-24 21:13:41 +0200812 }
813 else {
Akrone4961b12017-05-10 21:04:46 +0200814 this.offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200815
Akrone4961b12017-05-10 21:04:46 +0200816 // Remove all items
817 this.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200818
Akrone4961b12017-05-10 21:04:46 +0200819 // Use list
820 var shown = 0;
821 var i;
Akron5240b8c2016-05-20 09:17:41 +0200822
Akrone4961b12017-05-10 21:04:46 +0200823 for (i in this._list) {
Akrona92fd8d2016-05-24 21:13:41 +0200824
Akrone4961b12017-05-10 21:04:46 +0200825 // Don't show - it's before offset
826 shown++;
827 if (shown <= off)
828 continue;
Akrona92fd8d2016-05-24 21:13:41 +0200829
Akrone4961b12017-05-10 21:04:46 +0200830 var itemNr = this._list[i];
831 var item = this.item(itemNr);
832 this._append(itemNr);
833
834 if (shown >= (this.limit() + off))
835 break;
836 };
Akron5240b8c2016-05-20 09:17:41 +0200837 };
838
839 // set the slider to the new offset
Akron9c4d1ae2016-05-25 21:43:22 +0200840 this._slider.offset(this.offset);
Akron5240b8c2016-05-20 09:17:41 +0200841 },
842
843
844 // Append item to the shown list based on index
845 _append : function (i) {
846 var item = this.item(i);
847
848 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200849 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200850 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200851 };
852
Akron5240b8c2016-05-20 09:17:41 +0200853
854 // Append element
855 this.element().appendChild(item.element());
856 },
857
858
859 // Prepend item to the shown list based on index
860 _prepend : function (i) {
861 var item = this.item(i);
862
863 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200864 if (this.prefix().length > 0) {
Akrone4961b12017-05-10 21:04:46 +0200865 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200866 };
Akron5240b8c2016-05-20 09:17:41 +0200867
868 var e = this.element();
Akron9c4d1ae2016-05-25 21:43:22 +0200869
Akron5240b8c2016-05-20 09:17:41 +0200870 // Append element after lengthField/prefix/slider
871 e.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200872 item.element(),
873 e.children[3]
Akron5240b8c2016-05-20 09:17:41 +0200874 );
Nils Diewald2fe12e12015-03-06 16:47:06 +0000875 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000876 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000877});