blob: 69107d0c22d17dab5d9e78d5d0d5d012fad71abc [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
Nils Diewald2488d052015-04-09 21:46:02 +000011 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000012define([
13 'menu/item',
14 'menu/prefix',
Akronc7448732016-04-27 14:06:58 +020015 'menu/lengthField',
Akron9905e2a2016-05-10 16:06:44 +020016 'menu/slider',
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 'util'
18], function (defaultItemClass,
Akronc7448732016-04-27 14:06:58 +020019 defaultPrefixClass,
Akron9905e2a2016-05-10 16:06:44 +020020 defaultLengthFieldClass,
21 sliderClass) {
Nils Diewaldfda29d92015-01-22 17:28:01 +000022
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 // Default maximum number of menu items
24 var menuLimit = 8;
25
26 function _codeFromEvent (e) {
27 if (e.charCode && (e.keyCode == 0))
28 return e.charCode
29 return e.keyCode;
Nils Diewald59c02fc2015-03-07 01:29:09 +000030 };
31
Nils Diewald86dad5b2015-01-28 15:09:07 +000032
33 /**
34 * List of items for drop down menu (complete).
35 * Only a sublist of the menu is filtered (live).
36 * Only a sublist of the filtered menu is visible (shown).
37 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 return {
Nils Diewald86dad5b2015-01-28 15:09:07 +000039 /**
40 * Create new Menu based on the action prefix
41 * and a list of menu items.
42 *
43 * @this {Menu}
44 * @constructor
45 * @param {string} Context prefix
46 * @param {Array.<Array.<string>>} List of menu items
47 */
48 create : function (params) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000049 return Object.create(this)._init(params);
Nils Diewald86dad5b2015-01-28 15:09:07 +000050 },
51
Akron5240b8c2016-05-20 09:17:41 +020052 // Initialize list
53 _init : function (itemClass, prefixClass, lengthFieldClass, params) {
Akrona92fd8d2016-05-24 21:13:41 +020054
Akron5240b8c2016-05-20 09:17:41 +020055 this._itemClass = itemClass || defaultItemClass;
56
57 // Add prefix object
58 if (prefixClass !== undefined) {
59 this._prefix = prefixClass.create();
60 }
61 else {
62 this._prefix = defaultPrefixClass.create();
63 };
64 this._prefix._menu = this;
65
66 // Add lengthField object
67 if (lengthFieldClass !== undefined) {
68 this._lengthField = lengthFieldClass.create();
69 }
70 else {
71 this._lengthField = defaultLengthFieldClass.create();
72 };
73 this._lengthField._menu = this;
74
75 // Initialize slider
76 this._slider = sliderClass.create(this);
77
78 // Create the element
Akron9c4d1ae2016-05-25 21:43:22 +020079 var el = document.createElement("ul");
80 with (el) {
81 style.opacity = 0;
82 style.outline = 0;
83 setAttribute('tabindex', 0);
84 classList.add('menu', 'roll');
85 appendChild(this._prefix.element());
86 appendChild(this._lengthField.element());
87 appendChild(this._slider.element());
88 };
Akron5240b8c2016-05-20 09:17:41 +020089
90 // This has to be cleaned up later on
Akron9c4d1ae2016-05-25 21:43:22 +020091 el["menu"] = this;
Akron5240b8c2016-05-20 09:17:41 +020092
93 // Arrow keys
Akron9c4d1ae2016-05-25 21:43:22 +020094 el.addEventListener(
Akron5240b8c2016-05-20 09:17:41 +020095 'keydown',
Akrona92fd8d2016-05-24 21:13:41 +020096 this._keydown.bind(this),
Akron5240b8c2016-05-20 09:17:41 +020097 false
98 );
99
100 // Strings
Akron9c4d1ae2016-05-25 21:43:22 +0200101 el.addEventListener(
Akron5240b8c2016-05-20 09:17:41 +0200102 'keypress',
Akrona92fd8d2016-05-24 21:13:41 +0200103 this._keypress.bind(this),
Akron5240b8c2016-05-20 09:17:41 +0200104 false
105 );
106
107 // Mousewheel
Akron9c4d1ae2016-05-25 21:43:22 +0200108 el.addEventListener(
Akron5240b8c2016-05-20 09:17:41 +0200109 'wheel',
Akrona92fd8d2016-05-24 21:13:41 +0200110 this._mousewheel.bind(this),
Akron5240b8c2016-05-20 09:17:41 +0200111 false
112 );
Akron9c4d1ae2016-05-25 21:43:22 +0200113 this._element = el;
Akron5240b8c2016-05-20 09:17:41 +0200114
Akron5240b8c2016-05-20 09:17:41 +0200115 this._items = new Array();
Akron5240b8c2016-05-20 09:17:41 +0200116
Akron9c4d1ae2016-05-25 21:43:22 +0200117 var i = 0;
Akron5240b8c2016-05-20 09:17:41 +0200118 // Initialize item list based on parameters
119 for (i in params) {
120 var obj = this._itemClass.create(params[i]);
121
122 // This may become circular
123 obj["_menu"] = this;
124 this._lengthField.add(params[i]);
125 this._items.push(obj);
126 };
127
Akron9c4d1ae2016-05-25 21:43:22 +0200128 this._limit = menuLimit;
129 this._slider.length(this.liveLength())
130 .limit(this._limit)
131 .reInit();
Akron5240b8c2016-05-20 09:17:41 +0200132
Akron5240b8c2016-05-20 09:17:41 +0200133 this._firstActive = false; // Show the first item active always?
Akron9c4d1ae2016-05-25 21:43:22 +0200134 this.offset = 0;
135 this.position = 0;
Akron5240b8c2016-05-20 09:17:41 +0200136 return this;
137 },
138
139 // Initialize the item list
140 _initList : function () {
141
142 // Create a new list
143 if (this._list === undefined) {
144 this._list = [];
145 }
146 else if (this._list.length !== 0) {
147 this._boundary(false);
148 this._list.length = 0;
149 };
150
151 // Offset is initially zero
Akron9c4d1ae2016-05-25 21:43:22 +0200152 this.offset = 0;
Akron5240b8c2016-05-20 09:17:41 +0200153
154 // There is no prefix set
155 if (this.prefix().length <= 0) {
156
157 // add all items to the list and lowlight
Akron97752a72016-05-25 14:43:07 +0200158 var i = 0;
159 for (; i < this._items.length; i++) {
Akron5240b8c2016-05-20 09:17:41 +0200160 this._list.push(i);
161 this._items[i].lowlight();
162 };
163
Akron9c4d1ae2016-05-25 21:43:22 +0200164 this._slider.length(i).reInit();;
Akron97752a72016-05-25 14:43:07 +0200165
Akron5240b8c2016-05-20 09:17:41 +0200166 return true;
167 };
168
169 /*
170 * There is a prefix set, so filter the list!
171 */
172 var pos;
Akron6ffad5d2016-05-24 17:16:58 +0200173 var prefix = " " + this.prefix().toLowerCase();
Akron5240b8c2016-05-20 09:17:41 +0200174
175 // Iterate over all items and choose preferred matching items
176 // i.e. the matching happens at the word start
177 for (pos = 0; pos < this._items.length; pos++) {
Akron6ffad5d2016-05-24 17:16:58 +0200178 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
Akron5240b8c2016-05-20 09:17:41 +0200179 this._list.push(pos);
180 };
181
182 // The list is empty - so lower your expectations
183 // Iterate over all items and choose matching items
184 // i.e. the matching happens anywhere in the word
Akron6ffad5d2016-05-24 17:16:58 +0200185 prefix = prefix.substring(1);
Akron5240b8c2016-05-20 09:17:41 +0200186 if (this._list.length == 0) {
187 for (pos = 0; pos < this._items.length; pos++) {
Akron6ffad5d2016-05-24 17:16:58 +0200188 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
Akron5240b8c2016-05-20 09:17:41 +0200189 this._list.push(pos);
190 };
191 };
192
Akron9c4d1ae2016-05-25 21:43:22 +0200193 this._slider.length(this._list.length).reInit();
Akron6ed13992016-05-23 18:06:05 +0200194
Akron5240b8c2016-05-20 09:17:41 +0200195 // Filter was successful - yeah!
196 return this._list.length > 0 ? true : false;
197 },
198
199
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000200 /**
201 * Destroy this menu
202 * (in case you don't trust the
203 * mark and sweep GC)!
204 */
205 destroy : function () {
Akron47c086c2016-05-18 21:22:06 +0200206
Akron5240b8c2016-05-20 09:17:41 +0200207 // Remove circular reference to "this" in menu
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000208 if (this._element != undefined)
209 delete this._element["menu"];
210
Akron5240b8c2016-05-20 09:17:41 +0200211 // Remove circular reference to "this" in items
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000212 for (var i = 0; i < this._items.length; i++) {
213 delete this._items[i]["_menu"];
214 };
Akron5240b8c2016-05-20 09:17:41 +0200215
216 // Remove circular reference to "this" in prefix
Nils Diewald5c5a7472015-04-02 22:13:38 +0000217 delete this._prefix['_menu'];
Akron5240b8c2016-05-20 09:17:41 +0200218 delete this._lengthField['_menu'];
219 delete this._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000220 },
221
Nils Diewald7148c6f2015-05-04 15:07:53 +0000222
223 /**
224 * Focus on this menu.
225 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000226 focus : function () {
227 this._element.focus();
228 },
229
Nils Diewald7148c6f2015-05-04 15:07:53 +0000230
Nils Diewald59c02fc2015-03-07 01:29:09 +0000231 // mouse wheel treatment
232 _mousewheel : function (e) {
233 var delta = 0;
Nils Diewald5975d702015-03-09 17:45:42 +0000234
235 delta = e.deltaY / 120;
236 if (delta > 0)
Nils Diewald59c02fc2015-03-07 01:29:09 +0000237 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000238 else if (delta < 0)
Nils Diewald59c02fc2015-03-07 01:29:09 +0000239 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000240 e.halt();
241 },
242
Nils Diewald7148c6f2015-05-04 15:07:53 +0000243
Nils Diewald59c02fc2015-03-07 01:29:09 +0000244 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000245 _keydown : function (e) {
Nils Diewald59c02fc2015-03-07 01:29:09 +0000246 var code = _codeFromEvent(e);
247
Nils Diewald59c02fc2015-03-07 01:29:09 +0000248 switch (code) {
249 case 27: // 'Esc'
250 e.halt();
251 this.hide();
252 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000253
Nils Diewald59c02fc2015-03-07 01:29:09 +0000254 case 38: // 'Up'
255 e.halt();
256 this.prev();
257 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000258 case 33: // 'Page up'
Nils Diewald59c02fc2015-03-07 01:29:09 +0000259 e.halt();
Akron3c2730f2016-05-24 15:08:29 +0200260 this.pageUp();
Nils Diewald5975d702015-03-09 17:45:42 +0000261 break;
262 case 40: // 'Down'
263 e.halt();
264 this.next();
265 break;
266 case 34: // 'Page down'
267 e.halt();
Akron3c2730f2016-05-24 15:08:29 +0200268 this.pageDown();
Nils Diewald5975d702015-03-09 17:45:42 +0000269 break;
270 case 39: // 'Right'
Nils Diewalde8518f82015-03-18 22:41:49 +0000271 if (this._prefix.active())
272 break;
273
Akronf86eaea2016-05-13 18:02:27 +0200274 var item = this.liveItem(this.position);
Akron5ef4fa02015-06-02 16:25:14 +0200275
Nils Diewald5975d702015-03-09 17:45:42 +0000276 if (item["further"] !== undefined) {
277 item["further"].bind(item).apply();
Nils Diewald5975d702015-03-09 17:45:42 +0000278 };
Akron5ef4fa02015-06-02 16:25:14 +0200279
280 e.halt();
Nils Diewald5975d702015-03-09 17:45:42 +0000281 break;
282 case 13: // 'Enter'
283
284 // Click on prefix
285 if (this._prefix.active())
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000286 this._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000287
288 // Click on item
289 else
Akronf86eaea2016-05-13 18:02:27 +0200290 this.liveItem(this.position).onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000291 e.halt();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000292 break;
293 case 8: // 'Backspace'
Nils Diewald7148c6f2015-05-04 15:07:53 +0000294 this._prefix.chop();
Nils Diewald5975d702015-03-09 17:45:42 +0000295 this.show();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000296 e.halt();
297 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000298 };
299 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000300
Nils Diewald47f366b2015-04-15 20:06:35 +0000301 // Add characters to prefix
302 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200303 if (e.charCode !== 0) {
304 e.halt();
305 var c = String.fromCharCode(_codeFromEvent(e));
Nils Diewald5975d702015-03-09 17:45:42 +0000306
Akron9c2f9382016-05-25 16:36:04 +0200307 // Add prefix
308 this._prefix.add(c);
309 this.show();
310 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000311 },
312
Akron47c086c2016-05-18 21:22:06 +0200313 /**
Akron5240b8c2016-05-20 09:17:41 +0200314 * Show a screen with a given offset
315 * in the viewport.
Akron47c086c2016-05-18 21:22:06 +0200316 */
317 screen : function (nr) {
Akron3c2730f2016-05-24 15:08:29 +0200318 if (nr < 0) {
319 nr = 0
320 }
Akron6ac58442016-05-24 16:52:29 +0200321 else if (nr > (this.liveLength() - this.limit())) {
322 nr = (this.liveLength() - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200323 };
324
Akron9c4d1ae2016-05-25 21:43:22 +0200325 if (this.offset === nr)
Akron47c086c2016-05-18 21:22:06 +0200326 return;
Akron5a1f5bb2016-05-23 22:00:39 +0200327
Akron47c086c2016-05-18 21:22:06 +0200328 this._showItems(nr);
329 },
330
Nils Diewald2fe12e12015-03-06 16:47:06 +0000331 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000332 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000333 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000334 element : function () {
335 return this._element;
336 },
337
Nils Diewald2fe12e12015-03-06 16:47:06 +0000338 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000339 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000340 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000341 itemClass : function () {
342 return this._itemClass;
343 },
344
345 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000346 * Get and set the numerical value
347 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000348 */
349 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000350 if (arguments.length === 1) {
Akron5240b8c2016-05-20 09:17:41 +0200351 if (this._limit !== limit) {
352 this._limit = limit;
Akron9c4d1ae2016-05-25 21:43:22 +0200353 this._slider.limit(limit).reInit();
Akron5240b8c2016-05-20 09:17:41 +0200354 };
Nils Diewald5975d702015-03-09 17:45:42 +0000355 return this;
356 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000357 return this._limit;
358 },
359
Nils Diewald7148c6f2015-05-04 15:07:53 +0000360
Nils Diewald86dad5b2015-01-28 15:09:07 +0000361 /**
362 * Upgrade this object to another object,
363 * while private data stays intact.
364 *
Nils Diewald2fe12e12015-03-06 16:47:06 +0000365 * @param {Object} An object with properties.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000366 */
367 upgradeTo : function (props) {
368 for (var prop in props) {
369 this[prop] = props[prop];
370 };
371 return this;
372 },
373
Nils Diewald7148c6f2015-05-04 15:07:53 +0000374
Nils Diewald86dad5b2015-01-28 15:09:07 +0000375 /**
Akron97752a72016-05-25 14:43:07 +0200376 * Filter the list and make it visible.
377 * This is always called once the prefix changes.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000378 *
379 * @param {string} Prefix for filtering the list
380 */
Akron6ed13992016-05-23 18:06:05 +0200381 show : function (active) {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000382
Akron5240b8c2016-05-20 09:17:41 +0200383 // show menu based on initial offset
Akron6ac58442016-05-24 16:52:29 +0200384 this._unmark(); // Unmark everything that was marked before
Akrona92fd8d2016-05-24 21:13:41 +0200385 this.removeItems();
Akron6ed13992016-05-23 18:06:05 +0200386
387 // Initialize the list
388 if (!this._initList()) {
Akron6ac58442016-05-24 16:52:29 +0200389
Akron6ed13992016-05-23 18:06:05 +0200390 // The prefix is not active
391 this._prefix.active(true);
392
393 // finally show the element
394 this._element.style.opacity = 1;
395
396 return true;
397 };
398
399 var offset = 0;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000400
Akron9c2f9382016-05-25 16:36:04 +0200401
402 // Set a chosen value to active and move the viewport
Akron6ed13992016-05-23 18:06:05 +0200403 if (arguments.length === 1) {
404
405 // Normalize active value
Akron3c2730f2016-05-24 15:08:29 +0200406 if (active < 0) {
Akron6ed13992016-05-23 18:06:05 +0200407 active = 0;
Akron3c2730f2016-05-24 15:08:29 +0200408 }
Akron6ac58442016-05-24 16:52:29 +0200409 else if (active > this.liveLength()) {
410 active = this.liveLength() - 1;
Akron3c2730f2016-05-24 15:08:29 +0200411 };
Akron6ed13992016-05-23 18:06:05 +0200412
Akron0b92f692016-05-25 22:37:13 +0200413 // Item is outside the first viewport
414 if (active >= this._limit) {
Akron6ed13992016-05-23 18:06:05 +0200415 offset = active;
Akron6ac58442016-05-24 16:52:29 +0200416 if (offset > (this.liveLength() - this._limit)) {
417 offset = this.liveLength() - this._limit;
Akron6ed13992016-05-23 18:06:05 +0200418 };
419 };
420
421 this.position = active;
Akron6ed13992016-05-23 18:06:05 +0200422 }
423
Akron9c2f9382016-05-25 16:36:04 +0200424 // Choose the first item
Akron6ed13992016-05-23 18:06:05 +0200425 else if (this._firstActive) {
Akron47c086c2016-05-18 21:22:06 +0200426 this.position = 0;
Akron47c086c2016-05-18 21:22:06 +0200427 }
Akroncb351d62016-05-19 23:10:33 +0200428
Akron9c2f9382016-05-25 16:36:04 +0200429 // Choose no item
Akron47c086c2016-05-18 21:22:06 +0200430 else {
431 this.position = -1;
Akron6ed13992016-05-23 18:06:05 +0200432 };
433
Akron9c4d1ae2016-05-25 21:43:22 +0200434 this.offset = offset;
Akron6ed13992016-05-23 18:06:05 +0200435 this._showItems(offset); // Show new item list
436
437 // Make chosen value active
438 if (this.position !== -1) {
Akron3c2730f2016-05-24 15:08:29 +0200439 this.liveItem(this.position).active(true);
Akron6ed13992016-05-23 18:06:05 +0200440 };
Akron37513a62015-11-17 01:07:11 +0100441
Akron5240b8c2016-05-20 09:17:41 +0200442 // The prefix is not active
Nils Diewalde8518f82015-03-18 22:41:49 +0000443 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000444
Akron5240b8c2016-05-20 09:17:41 +0200445 // finally show the element
Nils Diewald2fe12e12015-03-06 16:47:06 +0000446 this._element.style.opacity = 1;
447
Nils Diewald86dad5b2015-01-28 15:09:07 +0000448 // Add classes for rolling menus
449 this._boundary(true);
Nils Diewald59c02fc2015-03-07 01:29:09 +0000450 return true;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000451 },
452
Nils Diewald7148c6f2015-05-04 15:07:53 +0000453
454 /**
455 * Hide the menu and call the onHide callback.
456 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000457 hide : function () {
Akrona92fd8d2016-05-24 21:13:41 +0200458 this.removeItems();
Nils Diewald2fe12e12015-03-06 16:47:06 +0000459 this._element.style.opacity = 0;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000460 this._prefix.clear();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000461 this.onHide();
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000462 /* this._element.blur(); */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000463 },
464
Nils Diewald7148c6f2015-05-04 15:07:53 +0000465 /**
466 * Function released when the menu hides.
467 * This method is expected to be overridden.
468 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000469 onHide : function () {},
470
Nils Diewald7148c6f2015-05-04 15:07:53 +0000471
Nils Diewald86dad5b2015-01-28 15:09:07 +0000472 /**
473 * Get the prefix for filtering,
Nils Diewald2fe12e12015-03-06 16:47:06 +0000474 * e.g. &quot;ve&quot; for &quot;verb&quot;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000475 */
Nils Diewald5975d702015-03-09 17:45:42 +0000476 prefix : function (pref) {
477 if (arguments.length === 1) {
478 this._prefix.value(pref);
479 return this;
480 };
481 return this._prefix.value();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000482 },
483
Akronc7448732016-04-27 14:06:58 +0200484 /**
485 * Get the lengthField object.
486 */
487 lengthField : function () {
488 return this._lengthField;
489 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000490
Akron5240b8c2016-05-20 09:17:41 +0200491 /**
492 * Get the associated slider object.
493 */
494 slider : function () {
495 return this._slider;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000496 },
497
Akron5240b8c2016-05-20 09:17:41 +0200498
Nils Diewald86dad5b2015-01-28 15:09:07 +0000499 /**
500 * Delete all visible items from the menu element
501 */
Akrona92fd8d2016-05-24 21:13:41 +0200502 removeItems : function () {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000503 var child;
Nils Diewald2fe12e12015-03-06 16:47:06 +0000504
Nils Diewald2fe12e12015-03-06 16:47:06 +0000505 // Remove all children
Nils Diewald5975d702015-03-09 17:45:42 +0000506 var children = this._element.childNodes;
Akronc7448732016-04-27 14:06:58 +0200507 // Leave the prefix and lengthField
Akron9905e2a2016-05-10 16:06:44 +0200508 for (var i = children.length - 1; i >= 3; i--) {
Nils Diewald5975d702015-03-09 17:45:42 +0000509 this._element.removeChild(
510 children[i]
511 );
512 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000513 },
514
Nils Diewald2fe12e12015-03-06 16:47:06 +0000515 /**
516 * Get a specific item from the complete list
517 *
518 * @param {number} index of the list item
519 */
520 item : function (index) {
521 return this._items[index]
522 },
523
524
Nils Diewald86dad5b2015-01-28 15:09:07 +0000525 /**
526 * Get a specific item from the filtered list
527 *
528 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000529 * in the filtered list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000530 */
531 liveItem : function (index) {
532 if (this._list === undefined)
533 if (!this._initList())
534 return;
535
536 return this._items[this._list[index]];
537 },
538
Nils Diewald86dad5b2015-01-28 15:09:07 +0000539
540 /**
Akron5240b8c2016-05-20 09:17:41 +0200541 * Get a specific item from the viewport list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000542 *
543 * @param {number} index of the list item
Nils Diewald2fe12e12015-03-06 16:47:06 +0000544 * in the visible list
Nils Diewald86dad5b2015-01-28 15:09:07 +0000545 */
546 shownItem : function (index) {
547 if (index >= this.limit())
548 return;
Akron9c4d1ae2016-05-25 21:43:22 +0200549 return this.liveItem(this.offset + index);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000550 },
551
552
Nils Diewald2fe12e12015-03-06 16:47:06 +0000553 /**
Akron9c4d1ae2016-05-25 21:43:22 +0200554 * Get the length of the full item list
Nils Diewald2fe12e12015-03-06 16:47:06 +0000555 */
556 length : function () {
557 return this._items.length;
558 },
559
560
561 /**
Akron5240b8c2016-05-20 09:17:41 +0200562 * Length of the filtered item list.
563 */
564 liveLength : function () {
565 if (this._list === undefined)
566 this._initList();
567 return this._list.length;
568 },
569
570
571 /**
Nils Diewald2fe12e12015-03-06 16:47:06 +0000572 * Make the next item in the filtered menu active
Nils Diewald86dad5b2015-01-28 15:09:07 +0000573 */
574 next : function () {
Nils Diewald5975d702015-03-09 17:45:42 +0000575
Akronb38afb22016-05-25 19:30:01 +0200576 // No list
577 if (this.liveLength() === 0)
578 return;
579
Akron9c4d1ae2016-05-25 21:43:22 +0200580 // Deactivate old item
581 if (this.position !== -1 && !this._prefix.active()) {
582 this.liveItem(this.position).active(false);
583 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000584
Akron9c4d1ae2016-05-25 21:43:22 +0200585 // Get new active item
586 this.position++;
587 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000588
Nils Diewald5975d702015-03-09 17:45:42 +0000589 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000590 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000591
592 // Activate prefix
593 var prefix = this._prefix;
594
Akron9c4d1ae2016-05-25 21:43:22 +0200595 // Prefix is set and not active - choose!
Nils Diewald5975d702015-03-09 17:45:42 +0000596 if (prefix.isSet() && !prefix.active()) {
Akronf86eaea2016-05-13 18:02:27 +0200597 this.position--;
Nils Diewald5975d702015-03-09 17:45:42 +0000598 prefix.active(true);
599 return;
600 }
Akron9c4d1ae2016-05-25 21:43:22 +0200601
602 // Choose first item
Nils Diewald5975d702015-03-09 17:45:42 +0000603 else {
Nils Diewald5975d702015-03-09 17:45:42 +0000604 newItem = this.liveItem(0);
Akron9c4d1ae2016-05-25 21:43:22 +0200605 // choose first item
606 this.position = 0;
Nils Diewald5975d702015-03-09 17:45:42 +0000607 this._showItems(0);
608 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000609 }
610
Akron5a1f5bb2016-05-23 22:00:39 +0200611 // The next element is after the viewport - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200612 else if (this.position >= (this.limit() + this.offset)) {
Akron5a1f5bb2016-05-23 22:00:39 +0200613 this.screen(this.position - this.limit() + 1);
614 }
615
616 // The next element is before the viewport - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200617 else if (this.position <= this.offset) {
Akron5a1f5bb2016-05-23 22:00:39 +0200618 this.screen(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000619 };
Nils Diewald5975d702015-03-09 17:45:42 +0000620
621 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000622 newItem.active(true);
623 },
624
Nils Diewalde8518f82015-03-18 22:41:49 +0000625 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000626 * Make the previous item in the menu active
627 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000628 prev : function () {
Nils Diewald2d210752015-03-09 19:01:15 +0000629
Akronb38afb22016-05-25 19:30:01 +0200630 // No list
631 if (this.liveLength() === 0)
632 return;
633
Akron9c4d1ae2016-05-25 21:43:22 +0200634 // Deactivate old item
Nils Diewald2d210752015-03-09 19:01:15 +0000635 if (!this._prefix.active()) {
Akron9c4d1ae2016-05-25 21:43:22 +0200636
637 // No active element set
638 if (this.position === -1) {
639 this.position = this.liveLength();
640 }
641
642 // No active element set
643 else {
644 this.liveItem(this.position--).active(false);
645 };
Nils Diewald2d210752015-03-09 19:01:15 +0000646 };
647
Akron9c4d1ae2016-05-25 21:43:22 +0200648 // Get new active item
649 var newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000650
651 // The previous element is undefined - roll to bottom
652 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000653
654 // Activate prefix
655 var prefix = this._prefix;
Akrona92fd8d2016-05-24 21:13:41 +0200656 var offset = this.liveLength() - this.limit();
Nils Diewald2d210752015-03-09 19:01:15 +0000657
658 // Normalize offset
Akrona92fd8d2016-05-24 21:13:41 +0200659 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000660
Akron9c4d1ae2016-05-25 21:43:22 +0200661 // Choose the last item
Akronf86eaea2016-05-13 18:02:27 +0200662 this.position = this.liveLength() - 1;
Nils Diewald5975d702015-03-09 17:45:42 +0000663
Akron9c4d1ae2016-05-25 21:43:22 +0200664 // Prefix is set and not active - choose!
Nils Diewald5975d702015-03-09 17:45:42 +0000665 if (prefix.isSet() && !prefix.active()) {
Akronf86eaea2016-05-13 18:02:27 +0200666 this.position++;
Nils Diewald5975d702015-03-09 17:45:42 +0000667 prefix.active(true);
Akron9c4d1ae2016-05-25 21:43:22 +0200668 this.offset = offset;
Nils Diewald5975d702015-03-09 17:45:42 +0000669 return;
670 }
Akron9c4d1ae2016-05-25 21:43:22 +0200671
672 // Choose last item
Nils Diewald5975d702015-03-09 17:45:42 +0000673 else {
Akronf86eaea2016-05-13 18:02:27 +0200674 newItem = this.liveItem(this.position);
Akrona92fd8d2016-05-24 21:13:41 +0200675 this._showItems(offset);
Nils Diewald5975d702015-03-09 17:45:42 +0000676 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000677 }
678
Akron5a1f5bb2016-05-23 22:00:39 +0200679 // The previous element is before the view - roll up
Akron9c4d1ae2016-05-25 21:43:22 +0200680 else if (this.position < this.offset) {
Akron5a1f5bb2016-05-23 22:00:39 +0200681 this.screen(this.position);
682 }
683
684 // The previous element is after the view - roll down
Akron9c4d1ae2016-05-25 21:43:22 +0200685 else if (this.position >= (this.limit() + this.offset)) {
Akron5a1f5bb2016-05-23 22:00:39 +0200686 this.screen(this.position - this.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000687 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000688
Nils Diewald5975d702015-03-09 17:45:42 +0000689 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000690 newItem.active(true);
691 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000692
Akron3c2730f2016-05-24 15:08:29 +0200693 /**
694 * Move the page up by limit!
695 */
696 pageUp : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200697 this.screen(this.offset - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200698 },
699
700
701 /**
702 * Move the page down by limit!
703 */
704 pageDown : function () {
Akron9c4d1ae2016-05-25 21:43:22 +0200705 this.screen(this.offset + this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200706 },
707
Nils Diewald86dad5b2015-01-28 15:09:07 +0000708
Akron5240b8c2016-05-20 09:17:41 +0200709 // Unmark all items
710 _unmark : function () {
711 for (var i in this._list) {
712 var item = this._items[this._list[i]];
713 item.lowlight();
714 item.active(false);
715 };
716 },
717
Akron5240b8c2016-05-20 09:17:41 +0200718 // Set boundary for viewport
719 _boundary : function (bool) {
720 this.item(this._list[0]).noMore(bool);
721 this.item(this._list[this._list.length - 1]).noMore(bool);
722 },
723
724
725 // Append Items that should be shown
726 _showItems : function (off) {
727
Akrona92fd8d2016-05-24 21:13:41 +0200728 // optimization: scroll down one step
Akron9c4d1ae2016-05-25 21:43:22 +0200729 if (this.offset === (off - 1)) {
730 this.offset = off;
731
732 // Remove the HTML node from the first item
733 // leave lengthField/prefix/slider
734 this._element.removeChild(this._element.children[3]);
735 var pos = this.offset + this.limit() - 1;
Akrona92fd8d2016-05-24 21:13:41 +0200736 this._append(this._list[pos]);
737 }
Akron5240b8c2016-05-20 09:17:41 +0200738
Akrona92fd8d2016-05-24 21:13:41 +0200739 // optimization: scroll up one step
Akron9c4d1ae2016-05-25 21:43:22 +0200740 else if (this.offset === (off + 1)) {
741 this.offset = off;
742
743 // Remove the HTML node from the last item
744 this._element.removeChild(this._element.lastChild);
745
746 this._prepend(this._list[this.offset]);
Akrona92fd8d2016-05-24 21:13:41 +0200747 }
748 else {
Akron9c4d1ae2016-05-25 21:43:22 +0200749 this.offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200750
Akrona92fd8d2016-05-24 21:13:41 +0200751 // Remove all items
752 this.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200753
Akrona92fd8d2016-05-24 21:13:41 +0200754 // Use list
755 var shown = 0;
756 var i;
Akron5240b8c2016-05-20 09:17:41 +0200757
Akrona92fd8d2016-05-24 21:13:41 +0200758 for (i in this._list) {
759
760 // Don't show - it's before offset
761 shown++;
762 if (shown <= off)
763 continue;
764
765 var itemNr = this._list[i];
766 var item = this.item(itemNr);
767 this._append(itemNr);
768
769 if (shown >= (this.limit() + off))
770 break;
771 };
Akron5240b8c2016-05-20 09:17:41 +0200772 };
773
774 // set the slider to the new offset
Akron9c4d1ae2016-05-25 21:43:22 +0200775 this._slider.offset(this.offset);
Akron5240b8c2016-05-20 09:17:41 +0200776 },
777
778
779 // Append item to the shown list based on index
780 _append : function (i) {
781 var item = this.item(i);
782
783 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200784 if (this.prefix().length > 0) {
Akron6ffad5d2016-05-24 17:16:58 +0200785 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200786 };
787
Akron5240b8c2016-05-20 09:17:41 +0200788
789 // Append element
790 this.element().appendChild(item.element());
791 },
792
793
794 // Prepend item to the shown list based on index
795 _prepend : function (i) {
796 var item = this.item(i);
797
798 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200799 if (this.prefix().length > 0) {
Akron6ffad5d2016-05-24 17:16:58 +0200800 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200801 };
Akron5240b8c2016-05-20 09:17:41 +0200802
803 var e = this.element();
Akron9c4d1ae2016-05-25 21:43:22 +0200804
Akron5240b8c2016-05-20 09:17:41 +0200805 // Append element after lengthField/prefix/slider
806 e.insertBefore(
807 item.element(),
808 e.children[3]
809 );
Nils Diewald2fe12e12015-03-06 16:47:06 +0000810 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000811 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000812});