blob: ed6b6c4b05e55c21af1a8d258921b48009c568d3 [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).
Akron97752a72016-05-25 14:43:07 +02009 * TODO: Optimize scrolling to active item.
Akron9c2f9382016-05-25 16:36:04 +020010 * TODO: Handle arrow keys correctly when list is empty.
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
79 var e = document.createElement("ul");
80 e.style.opacity = 0;
81 e.style.outline = 0;
82 e.setAttribute('tabindex', 0);
83 e.classList.add('menu');
84 e.classList.add('roll');
85 e.appendChild(this._prefix.element());
86 e.appendChild(this._lengthField.element());
87 e.appendChild(this._slider.element());
88
89 // This has to be cleaned up later on
90 e["menu"] = this;
91
92 // Arrow keys
93 e.addEventListener(
94 'keydown',
Akrona92fd8d2016-05-24 21:13:41 +020095 this._keydown.bind(this),
Akron5240b8c2016-05-20 09:17:41 +020096 false
97 );
98
99 // Strings
100 e.addEventListener(
101 'keypress',
Akrona92fd8d2016-05-24 21:13:41 +0200102 this._keypress.bind(this),
Akron5240b8c2016-05-20 09:17:41 +0200103 false
104 );
105
106 // Mousewheel
107 e.addEventListener(
108 'wheel',
Akrona92fd8d2016-05-24 21:13:41 +0200109 this._mousewheel.bind(this),
Akron5240b8c2016-05-20 09:17:41 +0200110 false
111 );
112
113 this._element = e;
114 this.active = false;
115 // this.selected = undefined;
116 this._items = new Array();
117 var i = 0;
118
119 // Initialize item list based on parameters
120 for (i in params) {
121 var obj = this._itemClass.create(params[i]);
122
123 // This may become circular
124 obj["_menu"] = this;
125 this._lengthField.add(params[i]);
126 this._items.push(obj);
127 };
128
129 this._limit = menuLimit;
130 this._slider.length(this.liveLength());
131 this._slider.limit(this._limit);
132
Akron5240b8c2016-05-20 09:17:41 +0200133 this._firstActive = false; // Show the first item active always?
134 this._reset();
135 return this;
136 },
137
138 // Initialize the item list
139 _initList : function () {
140
141 // Create a new list
142 if (this._list === undefined) {
143 this._list = [];
144 }
145 else if (this._list.length !== 0) {
146 this._boundary(false);
147 this._list.length = 0;
148 };
149
150 // Offset is initially zero
151 this._offset = 0;
152
153 // There is no prefix set
154 if (this.prefix().length <= 0) {
155
156 // add all items to the list and lowlight
Akron97752a72016-05-25 14:43:07 +0200157 var i = 0;
158 for (; i < this._items.length; i++) {
Akron5240b8c2016-05-20 09:17:41 +0200159 this._list.push(i);
160 this._items[i].lowlight();
161 };
162
Akron97752a72016-05-25 14:43:07 +0200163 this._slider.length(i);
164
Akron5240b8c2016-05-20 09:17:41 +0200165 return true;
166 };
167
168 /*
169 * There is a prefix set, so filter the list!
170 */
171 var pos;
Akron6ffad5d2016-05-24 17:16:58 +0200172 var prefix = " " + this.prefix().toLowerCase();
Akron5240b8c2016-05-20 09:17:41 +0200173
174 // Iterate over all items and choose preferred matching items
175 // i.e. the matching happens at the word start
176 for (pos = 0; pos < this._items.length; pos++) {
Akron6ffad5d2016-05-24 17:16:58 +0200177 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
Akron5240b8c2016-05-20 09:17:41 +0200178 this._list.push(pos);
179 };
180
181 // The list is empty - so lower your expectations
182 // Iterate over all items and choose matching items
183 // i.e. the matching happens anywhere in the word
Akron6ffad5d2016-05-24 17:16:58 +0200184 prefix = prefix.substring(1);
Akron5240b8c2016-05-20 09:17:41 +0200185 if (this._list.length == 0) {
186 for (pos = 0; pos < this._items.length; pos++) {
Akron6ffad5d2016-05-24 17:16:58 +0200187 if ((this.item(pos).lcField().indexOf(prefix)) >= 0)
Akron5240b8c2016-05-20 09:17:41 +0200188 this._list.push(pos);
189 };
190 };
191
Akron6ed13992016-05-23 18:06:05 +0200192 this._slider.length(this._list.length);
193
Akron5240b8c2016-05-20 09:17:41 +0200194 // Filter was successful - yeah!
195 return this._list.length > 0 ? true : false;
196 },
197
198
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000199 /**
200 * Destroy this menu
201 * (in case you don't trust the
202 * mark and sweep GC)!
203 */
204 destroy : function () {
Akron47c086c2016-05-18 21:22:06 +0200205
Akron5240b8c2016-05-20 09:17:41 +0200206 // Remove circular reference to "this" in menu
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000207 if (this._element != undefined)
208 delete this._element["menu"];
209
Akron5240b8c2016-05-20 09:17:41 +0200210 // Remove circular reference to "this" in items
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000211 for (var i = 0; i < this._items.length; i++) {
212 delete this._items[i]["_menu"];
213 };
Akron5240b8c2016-05-20 09:17:41 +0200214
215 // Remove circular reference to "this" in prefix
Nils Diewald5c5a7472015-04-02 22:13:38 +0000216 delete this._prefix['_menu'];
Akron5240b8c2016-05-20 09:17:41 +0200217 delete this._lengthField['_menu'];
218 delete this._slider['_menu'];
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000219 },
220
Nils Diewald7148c6f2015-05-04 15:07:53 +0000221
222 /**
223 * Focus on this menu.
224 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000225 focus : function () {
226 this._element.focus();
227 },
228
Nils Diewald7148c6f2015-05-04 15:07:53 +0000229
Nils Diewald59c02fc2015-03-07 01:29:09 +0000230 // mouse wheel treatment
231 _mousewheel : function (e) {
232 var delta = 0;
Nils Diewald5975d702015-03-09 17:45:42 +0000233
234 delta = e.deltaY / 120;
235 if (delta > 0)
Nils Diewald59c02fc2015-03-07 01:29:09 +0000236 this.next();
Nils Diewald5975d702015-03-09 17:45:42 +0000237 else if (delta < 0)
Nils Diewald59c02fc2015-03-07 01:29:09 +0000238 this.prev();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000239 e.halt();
240 },
241
Nils Diewald7148c6f2015-05-04 15:07:53 +0000242
Nils Diewald59c02fc2015-03-07 01:29:09 +0000243 // Arrow key and prefix treatment
Nils Diewald47f366b2015-04-15 20:06:35 +0000244 _keydown : function (e) {
Nils Diewald59c02fc2015-03-07 01:29:09 +0000245 var code = _codeFromEvent(e);
246
Nils Diewald59c02fc2015-03-07 01:29:09 +0000247 switch (code) {
248 case 27: // 'Esc'
249 e.halt();
250 this.hide();
251 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000252
Nils Diewald59c02fc2015-03-07 01:29:09 +0000253 case 38: // 'Up'
254 e.halt();
255 this.prev();
256 break;
Nils Diewald5975d702015-03-09 17:45:42 +0000257 case 33: // 'Page up'
Nils Diewald59c02fc2015-03-07 01:29:09 +0000258 e.halt();
Akron3c2730f2016-05-24 15:08:29 +0200259 this.pageUp();
Nils Diewald5975d702015-03-09 17:45:42 +0000260 break;
261 case 40: // 'Down'
262 e.halt();
263 this.next();
264 break;
265 case 34: // 'Page down'
266 e.halt();
Akron3c2730f2016-05-24 15:08:29 +0200267 this.pageDown();
Nils Diewald5975d702015-03-09 17:45:42 +0000268 break;
269 case 39: // 'Right'
Nils Diewalde8518f82015-03-18 22:41:49 +0000270 if (this._prefix.active())
271 break;
272
Akronf86eaea2016-05-13 18:02:27 +0200273 var item = this.liveItem(this.position);
Akron5ef4fa02015-06-02 16:25:14 +0200274
Nils Diewald5975d702015-03-09 17:45:42 +0000275 if (item["further"] !== undefined) {
276 item["further"].bind(item).apply();
Nils Diewald5975d702015-03-09 17:45:42 +0000277 };
Akron5ef4fa02015-06-02 16:25:14 +0200278
279 e.halt();
Nils Diewald5975d702015-03-09 17:45:42 +0000280 break;
281 case 13: // 'Enter'
282
283 // Click on prefix
284 if (this._prefix.active())
Nils Diewald6e43ffd2015-03-25 18:55:39 +0000285 this._prefix.onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000286
287 // Click on item
288 else
Akronf86eaea2016-05-13 18:02:27 +0200289 this.liveItem(this.position).onclick(e);
Nils Diewald5975d702015-03-09 17:45:42 +0000290 e.halt();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000291 break;
292 case 8: // 'Backspace'
Nils Diewald7148c6f2015-05-04 15:07:53 +0000293 this._prefix.chop();
Nils Diewald5975d702015-03-09 17:45:42 +0000294 this.show();
Nils Diewald59c02fc2015-03-07 01:29:09 +0000295 e.halt();
296 break;
Nils Diewald47f366b2015-04-15 20:06:35 +0000297 };
298 },
Nils Diewald59c02fc2015-03-07 01:29:09 +0000299
Nils Diewald47f366b2015-04-15 20:06:35 +0000300 // Add characters to prefix
301 _keypress : function (e) {
Akron9c2f9382016-05-25 16:36:04 +0200302 if (e.charCode !== 0) {
303 e.halt();
304 var c = String.fromCharCode(_codeFromEvent(e));
Nils Diewald5975d702015-03-09 17:45:42 +0000305
Akron9c2f9382016-05-25 16:36:04 +0200306 // Add prefix
307 this._prefix.add(c);
308 this.show();
309 };
Nils Diewald59c02fc2015-03-07 01:29:09 +0000310 },
311
Akron47c086c2016-05-18 21:22:06 +0200312 /**
Akron5240b8c2016-05-20 09:17:41 +0200313 * Show a screen with a given offset
314 * in the viewport.
Akron47c086c2016-05-18 21:22:06 +0200315 */
316 screen : function (nr) {
Akron3c2730f2016-05-24 15:08:29 +0200317 if (nr < 0) {
318 nr = 0
319 }
Akron6ac58442016-05-24 16:52:29 +0200320 else if (nr > (this.liveLength() - this.limit())) {
321 nr = (this.liveLength() - this.limit());
Akron3c2730f2016-05-24 15:08:29 +0200322 };
323
Akron47c086c2016-05-18 21:22:06 +0200324 if (this._offset === nr)
325 return;
Akron5a1f5bb2016-05-23 22:00:39 +0200326
Akron47c086c2016-05-18 21:22:06 +0200327 this._showItems(nr);
328 },
329
Nils Diewald2fe12e12015-03-06 16:47:06 +0000330 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000331 * Get the associated dom element.
Nils Diewald2fe12e12015-03-06 16:47:06 +0000332 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000333 element : function () {
334 return this._element;
335 },
336
Nils Diewald2fe12e12015-03-06 16:47:06 +0000337 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000338 * Get the creator class for items
Nils Diewald2fe12e12015-03-06 16:47:06 +0000339 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000340 itemClass : function () {
341 return this._itemClass;
342 },
343
344 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +0000345 * Get and set the numerical value
346 * for the maximum number of items visible.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000347 */
348 limit : function (limit) {
Nils Diewald5975d702015-03-09 17:45:42 +0000349 if (arguments.length === 1) {
Akron5240b8c2016-05-20 09:17:41 +0200350 if (this._limit !== limit) {
351 this._limit = limit;
352 this._slider.limit(limit);
353 };
Nils Diewald5975d702015-03-09 17:45:42 +0000354 return this;
355 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000356 return this._limit;
357 },
358
Nils Diewald7148c6f2015-05-04 15:07:53 +0000359
Nils Diewald86dad5b2015-01-28 15:09:07 +0000360 /**
361 * Upgrade this object to another object,
362 * while private data stays intact.
363 *
Nils Diewald2fe12e12015-03-06 16:47:06 +0000364 * @param {Object} An object with properties.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000365 */
366 upgradeTo : function (props) {
367 for (var prop in props) {
368 this[prop] = props[prop];
369 };
370 return this;
371 },
372
Nils Diewald7148c6f2015-05-04 15:07:53 +0000373
Nils Diewald86dad5b2015-01-28 15:09:07 +0000374 /**
Akron97752a72016-05-25 14:43:07 +0200375 * Filter the list and make it visible.
376 * This is always called once the prefix changes.
Nils Diewald86dad5b2015-01-28 15:09:07 +0000377 *
378 * @param {string} Prefix for filtering the list
379 */
Akron6ed13992016-05-23 18:06:05 +0200380 show : function (active) {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000381
Akron5240b8c2016-05-20 09:17:41 +0200382 // show menu based on initial offset
Akron6ac58442016-05-24 16:52:29 +0200383 this._unmark(); // Unmark everything that was marked before
Akrona92fd8d2016-05-24 21:13:41 +0200384 this.removeItems();
Akron6ed13992016-05-23 18:06:05 +0200385
386 // Initialize the list
387 if (!this._initList()) {
Akron6ac58442016-05-24 16:52:29 +0200388
Akron6ed13992016-05-23 18:06:05 +0200389 // The prefix is not active
390 this._prefix.active(true);
391
392 // finally show the element
393 this._element.style.opacity = 1;
394
395 return true;
396 };
397
398 var offset = 0;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000399
Akron9c2f9382016-05-25 16:36:04 +0200400
401 // Set a chosen value to active and move the viewport
Akron6ed13992016-05-23 18:06:05 +0200402 if (arguments.length === 1) {
403
404 // Normalize active value
Akron3c2730f2016-05-24 15:08:29 +0200405 if (active < 0) {
Akron6ed13992016-05-23 18:06:05 +0200406 active = 0;
Akron3c2730f2016-05-24 15:08:29 +0200407 }
Akron6ac58442016-05-24 16:52:29 +0200408 else if (active > this.liveLength()) {
409 active = this.liveLength() - 1;
Akron3c2730f2016-05-24 15:08:29 +0200410 };
Akron6ed13992016-05-23 18:06:05 +0200411
412 if (active > this._limit) {
413 offset = active;
Akron6ac58442016-05-24 16:52:29 +0200414 if (offset > (this.liveLength() - this._limit)) {
415 offset = this.liveLength() - this._limit;
Akron6ed13992016-05-23 18:06:05 +0200416 };
417 };
418
419 this.position = active;
Akron6ed13992016-05-23 18:06:05 +0200420 }
421
Akron9c2f9382016-05-25 16:36:04 +0200422 // Choose the first item
Akron6ed13992016-05-23 18:06:05 +0200423 else if (this._firstActive) {
Akron47c086c2016-05-18 21:22:06 +0200424 this.position = 0;
Akron47c086c2016-05-18 21:22:06 +0200425 }
Akroncb351d62016-05-19 23:10:33 +0200426
Akron9c2f9382016-05-25 16:36:04 +0200427 // Choose no item
Akron47c086c2016-05-18 21:22:06 +0200428 else {
429 this.position = -1;
Akron6ed13992016-05-23 18:06:05 +0200430 };
431
432 this._offset = offset;
433 this._showItems(offset); // Show new item list
434
435 // Make chosen value active
436 if (this.position !== -1) {
Akron3c2730f2016-05-24 15:08:29 +0200437 this.liveItem(this.position).active(true);
Akron6ed13992016-05-23 18:06:05 +0200438 };
Akron37513a62015-11-17 01:07:11 +0100439
Akron5240b8c2016-05-20 09:17:41 +0200440 // The prefix is not active
Nils Diewalde8518f82015-03-18 22:41:49 +0000441 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000442
Akron5240b8c2016-05-20 09:17:41 +0200443 // finally show the element
Nils Diewald2fe12e12015-03-06 16:47:06 +0000444 this._element.style.opacity = 1;
445
Nils Diewald86dad5b2015-01-28 15:09:07 +0000446 // Add classes for rolling menus
447 this._boundary(true);
Nils Diewald59c02fc2015-03-07 01:29:09 +0000448 return true;
Nils Diewald86dad5b2015-01-28 15:09:07 +0000449 },
450
Nils Diewald7148c6f2015-05-04 15:07:53 +0000451
452 /**
453 * Hide the menu and call the onHide callback.
454 */
Nils Diewald2fe12e12015-03-06 16:47:06 +0000455 hide : function () {
Nils Diewald59c02fc2015-03-07 01:29:09 +0000456 this.active = false;
Akron5240b8c2016-05-20 09:17:41 +0200457 this._unmark();
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;
549 return this.liveItem(this._offset + index);
550 },
551
552
Nils Diewald2fe12e12015-03-06 16:47:06 +0000553 /**
554 * Get the length of the full list
555 */
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
Nils Diewald86dad5b2015-01-28 15:09:07 +0000576 // No active element set
Nils Diewald5975d702015-03-09 17:45:42 +0000577 var newItem;
578
Akron47c086c2016-05-18 21:22:06 +0200579 if (this.position !== -1) {
580 // Set new live item
581 if (!this._prefix.active()) {
582 var oldItem = this.liveItem(this.position);
583 oldItem.active(false);
584 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000585 };
586
Akronf86eaea2016-05-13 18:02:27 +0200587 this.position++;
Nils Diewalde8518f82015-03-18 22:41:49 +0000588
Akronf86eaea2016-05-13 18:02:27 +0200589 newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000590
Nils Diewald5975d702015-03-09 17:45:42 +0000591 // The next element is undefined - roll to top or to prefix
Nils Diewald86dad5b2015-01-28 15:09:07 +0000592 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000593
594 // Activate prefix
595 var prefix = this._prefix;
596
597 // Mark prefix
598 if (prefix.isSet() && !prefix.active()) {
Akronf86eaea2016-05-13 18:02:27 +0200599 this.position--;
Nils Diewald5975d702015-03-09 17:45:42 +0000600 prefix.active(true);
601 return;
602 }
603 else {
Akronf86eaea2016-05-13 18:02:27 +0200604 this.position = 0;
Nils Diewald5975d702015-03-09 17:45:42 +0000605 newItem = this.liveItem(0);
606 this._showItems(0);
607 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000608 }
609
Akron5a1f5bb2016-05-23 22:00:39 +0200610 // The next element is after the viewport - roll down
Akronf86eaea2016-05-13 18:02:27 +0200611 else if (this.position >= (this.limit() + this._offset)) {
Akron5a1f5bb2016-05-23 22:00:39 +0200612 this.screen(this.position - this.limit() + 1);
613 }
614
615 // The next element is before the viewport - roll up
616 else if (this.position <= this._offset) {
617 this.screen(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000618 };
Nils Diewald5975d702015-03-09 17:45:42 +0000619
620 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000621 newItem.active(true);
622 },
623
Nils Diewalde8518f82015-03-18 22:41:49 +0000624 /*
Nils Diewald86dad5b2015-01-28 15:09:07 +0000625 * Make the previous item in the menu active
626 */
Nils Diewald86dad5b2015-01-28 15:09:07 +0000627 prev : function () {
Nils Diewald2d210752015-03-09 19:01:15 +0000628
Nils Diewald2fe12e12015-03-06 16:47:06 +0000629 // No active element set
Akronf86eaea2016-05-13 18:02:27 +0200630 if (this.position === -1) {
Nils Diewald86dad5b2015-01-28 15:09:07 +0000631 return;
Nils Diewalde8518f82015-03-18 22:41:49 +0000632 // TODO: Choose last item
633 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000634
Nils Diewald5975d702015-03-09 17:45:42 +0000635 var newItem;
636
Nils Diewald86dad5b2015-01-28 15:09:07 +0000637 // Set new live item
Nils Diewald2d210752015-03-09 19:01:15 +0000638 if (!this._prefix.active()) {
Akronf86eaea2016-05-13 18:02:27 +0200639 var oldItem = this.liveItem(this.position--);
Nils Diewald2d210752015-03-09 19:01:15 +0000640 oldItem.active(false);
641 };
642
Akronf86eaea2016-05-13 18:02:27 +0200643 newItem = this.liveItem(this.position);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000644
645 // The previous element is undefined - roll to bottom
646 if (newItem === undefined) {
Nils Diewald5975d702015-03-09 17:45:42 +0000647
648 // Activate prefix
649 var prefix = this._prefix;
Akrona92fd8d2016-05-24 21:13:41 +0200650 var offset = this.liveLength() - this.limit();
651 // this._offset = this.liveLength() - this.limit();
Nils Diewald2d210752015-03-09 19:01:15 +0000652
653 // Normalize offset
Akrona92fd8d2016-05-24 21:13:41 +0200654 // this._offset = this._offset < 0 ? 0 : this._offset;
655 offset = offset < 0 ? 0 : offset;
Nils Diewald2d210752015-03-09 19:01:15 +0000656
Akronf86eaea2016-05-13 18:02:27 +0200657 this.position = this.liveLength() - 1;
Nils Diewald5975d702015-03-09 17:45:42 +0000658
659 if (prefix.isSet() && !prefix.active()) {
Akronf86eaea2016-05-13 18:02:27 +0200660 this.position++;
Nils Diewald5975d702015-03-09 17:45:42 +0000661 prefix.active(true);
Akrona92fd8d2016-05-24 21:13:41 +0200662 this._offset = offset;
Nils Diewald5975d702015-03-09 17:45:42 +0000663 return;
664 }
665 else {
Akronf86eaea2016-05-13 18:02:27 +0200666 newItem = this.liveItem(this.position);
Akron5240b8c2016-05-20 09:17:41 +0200667 this._unmark();
Akrona92fd8d2016-05-24 21:13:41 +0200668 this._showItems(offset);
Nils Diewald5975d702015-03-09 17:45:42 +0000669 };
Nils Diewald86dad5b2015-01-28 15:09:07 +0000670 }
671
Akron5a1f5bb2016-05-23 22:00:39 +0200672 // The previous element is before the view - roll up
Akronf86eaea2016-05-13 18:02:27 +0200673 else if (this.position < this._offset) {
Akron5a1f5bb2016-05-23 22:00:39 +0200674 this.screen(this.position);
675 }
676
677 // The previous element is after the view - roll down
678 else if (this.position >= (this.limit() + this._offset)) {
679 this.screen(this.position - this.limit() + 2);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000680 };
Nils Diewald2fe12e12015-03-06 16:47:06 +0000681
Nils Diewald5975d702015-03-09 17:45:42 +0000682 this._prefix.active(false);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000683 newItem.active(true);
684 },
Nils Diewald86dad5b2015-01-28 15:09:07 +0000685
Akron3c2730f2016-05-24 15:08:29 +0200686 /**
687 * Move the page up by limit!
688 */
689 pageUp : function () {
690 this.screen(this._offset - this.limit());
691 },
692
693
694 /**
695 * Move the page down by limit!
696 */
697 pageDown : function () {
698 this.screen(this._offset + this.limit());
699 },
700
Nils Diewald86dad5b2015-01-28 15:09:07 +0000701
Akron5240b8c2016-05-20 09:17:41 +0200702 // Unmark all items
703 _unmark : function () {
704 for (var i in this._list) {
705 var item = this._items[this._list[i]];
706 item.lowlight();
707 item.active(false);
708 };
709 },
710
711
712 // Reset chosen item and prefix
713 _reset : function () {
714 this._offset = 0;
Akron97752a72016-05-25 14:43:07 +0200715 this.position = 0;
Akron6ffad5d2016-05-24 17:16:58 +0200716 this._prefix.clear();
Akron5240b8c2016-05-20 09:17:41 +0200717 },
718
719
720 // Set boundary for viewport
721 _boundary : function (bool) {
722 this.item(this._list[0]).noMore(bool);
723 this.item(this._list[this._list.length - 1]).noMore(bool);
724 },
725
726
727 // Append Items that should be shown
728 _showItems : function (off) {
729
Akrona92fd8d2016-05-24 21:13:41 +0200730 // optimization: scroll down one step
731 if (this._offset === (off - 1)) {
732 this._offset = off;
733 this._removeFirst();
734 var pos = this._offset + this.limit() - 1;
735 this._append(this._list[pos]);
736 }
Akron5240b8c2016-05-20 09:17:41 +0200737
Akrona92fd8d2016-05-24 21:13:41 +0200738 // optimization: scroll up one step
739 else if (this._offset === (off + 1)) {
740 this._offset = off;
741 this._removeLast();
742 this._prepend(this._list[this._offset]);
743 }
744 else {
745 this._offset = off;
Akron5240b8c2016-05-20 09:17:41 +0200746
Akrona92fd8d2016-05-24 21:13:41 +0200747 // Remove all items
748 this.removeItems();
Akron5240b8c2016-05-20 09:17:41 +0200749
Akrona92fd8d2016-05-24 21:13:41 +0200750 // Use list
751 var shown = 0;
752 var i;
Akron5240b8c2016-05-20 09:17:41 +0200753
Akrona92fd8d2016-05-24 21:13:41 +0200754 for (i in this._list) {
755
756 // Don't show - it's before offset
757 shown++;
758 if (shown <= off)
759 continue;
760
761 var itemNr = this._list[i];
762 var item = this.item(itemNr);
763 this._append(itemNr);
764
765 if (shown >= (this.limit() + off))
766 break;
767 };
Akron5240b8c2016-05-20 09:17:41 +0200768 };
769
770 // set the slider to the new offset
771 this._slider.offset(this._offset);
772 },
773
774
775 // Append item to the shown list based on index
776 _append : function (i) {
777 var item = this.item(i);
778
779 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200780 if (this.prefix().length > 0) {
Akron6ffad5d2016-05-24 17:16:58 +0200781 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200782 };
783
Akron5240b8c2016-05-20 09:17:41 +0200784
785 // Append element
786 this.element().appendChild(item.element());
787 },
788
789
790 // Prepend item to the shown list based on index
791 _prepend : function (i) {
792 var item = this.item(i);
793
794 // Highlight based on prefix
Akrona92fd8d2016-05-24 21:13:41 +0200795 if (this.prefix().length > 0) {
Akron6ffad5d2016-05-24 17:16:58 +0200796 item.highlight(this.prefix().toLowerCase());
Akrona92fd8d2016-05-24 21:13:41 +0200797 };
Akron5240b8c2016-05-20 09:17:41 +0200798
799 var e = this.element();
800 // Append element after lengthField/prefix/slider
801 e.insertBefore(
802 item.element(),
803 e.children[3]
804 );
Nils Diewald5975d702015-03-09 17:45:42 +0000805 },
806
807
Nils Diewald2fe12e12015-03-06 16:47:06 +0000808 // Remove the HTML node from the first item
Nils Diewald86dad5b2015-01-28 15:09:07 +0000809 _removeFirst : function () {
Akrona92fd8d2016-05-24 21:13:41 +0200810 // this.item(this._list[this._offset]).lowlight();
Akron9905e2a2016-05-10 16:06:44 +0200811 // leave lengthField/prefix/slider
812 this._element.removeChild(this._element.children[3]);
Nils Diewald86dad5b2015-01-28 15:09:07 +0000813 },
814
Nils Diewald2fe12e12015-03-06 16:47:06 +0000815
816 // Remove the HTML node from the last item
Nils Diewald86dad5b2015-01-28 15:09:07 +0000817 _removeLast : function () {
Akrona92fd8d2016-05-24 21:13:41 +0200818 // this.item(this._list[this._offset + this.limit() - 1]).lowlight();
Nils Diewald86dad5b2015-01-28 15:09:07 +0000819 this._element.removeChild(this._element.lastChild);
Nils Diewald2fe12e12015-03-06 16:47:06 +0000820 }
Nils Diewald86dad5b2015-01-28 15:09:07 +0000821 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000822});