blob: 359d816f0cdebe8c5d8dbfb6c884c8e4af6920f2 [file] [log] [blame]
Akron5de8c7d2020-10-19 14:15:11 +02001"use strict";
2
Akronad894832016-06-08 18:24:48 +02003define(
Akron0b489ad2018-02-02 16:49:32 +01004 ['menu', 'selectMenu/item', 'util'],
Akronc82513b2016-06-11 11:22:36 +02005 function (menuClass, selectMenuItemClass) {
Akronad894832016-06-08 18:24:48 +02006
7 return {
8 create : function (element) {
Akronad894832016-06-08 18:24:48 +02009
Akron5de8c7d2020-10-19 14:15:11 +020010 const select = element.getElementsByTagName('select')[0];
Akronad894832016-06-08 18:24:48 +020011
Akronaba7a5a2016-08-15 21:58:33 +020012 if (select === null)
13 return;
Akronad894832016-06-08 18:24:48 +020014
Akronaba7a5a2016-08-15 21:58:33 +020015 // Prepare list before object upgras
Akron5de8c7d2020-10-19 14:15:11 +020016 const list = [];
Akronad894832016-06-08 18:24:48 +020017
Akronaba7a5a2016-08-15 21:58:33 +020018 // Iterate through options list
Akron5de8c7d2020-10-19 14:15:11 +020019 // Get option item and add to list
Akronb50964a2020-10-12 11:44:37 +020020 Array.from(
21 select.getElementsByTagName('option')
Akronb50964a2020-10-12 11:44:37 +020022 ).forEach(function(item) {
23
Akron5de8c7d2020-10-19 14:15:11 +020024 const opt = [
Akronaba7a5a2016-08-15 21:58:33 +020025 item.textContent,
26 item.getAttribute('value')
27 ];
Akronad894832016-06-08 18:24:48 +020028
Akronaba7a5a2016-08-15 21:58:33 +020029 // If the item has an attribute - list
30 if (item.hasAttribute('desc'))
31 opt.push(item.getAttribute('desc'));
Akronad894832016-06-08 18:24:48 +020032
Akronaba7a5a2016-08-15 21:58:33 +020033 list.push(opt);
Akronb50964a2020-10-12 11:44:37 +020034 });
Akronad894832016-06-08 18:24:48 +020035
Akronaba7a5a2016-08-15 21:58:33 +020036 // Create object with list
Akron5de8c7d2020-10-19 14:15:11 +020037 const obj = Object.create(menuClass).upgradeTo(this)
38 ._init(
39 list, {
40 itemClass : selectMenuItemClass
41 }
42 );
Akron7f613e02016-11-07 02:50:44 +010043
Akronaba7a5a2016-08-15 21:58:33 +020044 obj._container = element;
45 obj._select = select;
Akron5de8c7d2020-10-19 14:15:11 +020046 select.style.display = 'none';
Akronad894832016-06-08 18:24:48 +020047
Akronaba7a5a2016-08-15 21:58:33 +020048 // Create title
Akron0b489ad2018-02-02 16:49:32 +010049 obj._title = obj._container.addE('span');
50 obj._title.addT('');
Akron5de8c7d2020-10-19 14:15:11 +020051
52 obj._container.appendChild(obj.element());
Akronad894832016-06-08 18:24:48 +020053
Akronaba7a5a2016-08-15 21:58:33 +020054 // Show the menu
55 obj._container.addEventListener('click', obj.showSelected.bind(obj));
Akron6bb71582016-06-10 20:41:08 +020056
Akronaba7a5a2016-08-15 21:58:33 +020057 // Add index information to each item
Akron678c26f2020-10-09 08:52:50 +020058 obj._items.forEach((e,i) => e._index = i);
Akron6bb71582016-06-10 20:41:08 +020059
Akronaba7a5a2016-08-15 21:58:33 +020060 // This is only domspecific
61 obj.element().addEventListener('blur', function (e) {
62 this.menu.hide();
63 this.menu.showTitle();
64 });
Akron6bb71582016-06-10 20:41:08 +020065
Akronaba7a5a2016-08-15 21:58:33 +020066 // In case another tool changes
67 // the option via JS - this needs
68 // to be reflected!
69 select.addEventListener('change', function (e) {
70 this.showTitle();
71 }.bind(obj));
Akron6bb71582016-06-10 20:41:08 +020072
Akronaba7a5a2016-08-15 21:58:33 +020073 obj.showTitle();
74 return obj;
Akron6bb71582016-06-10 20:41:08 +020075 },
76
Akronaba7a5a2016-08-15 21:58:33 +020077 /**
78 * Get or set the selection index
79 */
Akron6bb71582016-06-10 20:41:08 +020080 select : function (index) {
Akron5de8c7d2020-10-19 14:15:11 +020081 const t = this;
Akronaba7a5a2016-08-15 21:58:33 +020082 if (arguments.length > 0) {
Akron5de8c7d2020-10-19 14:15:11 +020083 t._selected = index;
84 t._select.selectedIndex = index;
Akronaba7a5a2016-08-15 21:58:33 +020085 };
Akron6bb71582016-06-10 20:41:08 +020086
Akron5de8c7d2020-10-19 14:15:11 +020087 return t._selected || t._select.selectedIndex || 0;
Akron6bb71582016-06-10 20:41:08 +020088 },
89
Akron086fe5d2017-11-13 14:01:45 +010090
91 /**
92 * Set the select value
93 */
94 selectValue : function (vParam) {
Akron5de8c7d2020-10-19 14:15:11 +020095 const t = this;
96 const qlf = t._select.options;
Akron678c26f2020-10-09 08:52:50 +020097 for (let i = 0; i < qlf.length; i++) {
Akron086fe5d2017-11-13 14:01:45 +010098 if (qlf[i].value == vParam) {
Akron5de8c7d2020-10-19 14:15:11 +020099 t.hide();
100 t.select(i);
101 t.showTitle();
Akron086fe5d2017-11-13 14:01:45 +0100102 break;
103 };
104 };
Akron5de8c7d2020-10-19 14:15:11 +0200105 return t;
Akron086fe5d2017-11-13 14:01:45 +0100106 },
107
Akron5de8c7d2020-10-19 14:15:11 +0200108
Akronaba7a5a2016-08-15 21:58:33 +0200109 /**
110 * Show the select menu
111 */
Akron6bb71582016-06-10 20:41:08 +0200112 showSelected : function () {
Akron5de8c7d2020-10-19 14:15:11 +0200113 const t = this;
114 t._title.style.display = 'none';
115 t.show(t._selected = t._select.selectedIndex);
116 t.focus();
Akron6bb71582016-06-10 20:41:08 +0200117 },
118
Akron5de8c7d2020-10-19 14:15:11 +0200119
Akronaba7a5a2016-08-15 21:58:33 +0200120 /**
121 * Show the title
122 */
Akron6bb71582016-06-10 20:41:08 +0200123 showTitle : function () {
Akronaba7a5a2016-08-15 21:58:33 +0200124
125 // Get the selection context
Akron5de8c7d2020-10-19 14:15:11 +0200126 const t = this;
127 const s = t.select();
128 t._title.textContent = t.item(
129 t.select()
Akronaba7a5a2016-08-15 21:58:33 +0200130 ).title();
Akron5de8c7d2020-10-19 14:15:11 +0200131 t._title.style.display = 'inline';
Akronad894832016-06-08 18:24:48 +0200132 }
133 }
134 }
135);