blob: dc2c8e6b76c4d35185590279ddf30f929ae21c89 [file] [log] [blame]
Akronad894832016-06-08 18:24:48 +02001define(
Akron0b489ad2018-02-02 16:49:32 +01002 ['menu', 'selectMenu/item', 'util'],
Akronc82513b2016-06-11 11:22:36 +02003 function (menuClass, selectMenuItemClass) {
Akronad894832016-06-08 18:24:48 +02004
5 return {
6 create : function (element) {
Akronad894832016-06-08 18:24:48 +02007
Akronaba7a5a2016-08-15 21:58:33 +02008 var select = element.getElementsByTagName('select')[0];
Akronad894832016-06-08 18:24:48 +02009
Akronaba7a5a2016-08-15 21:58:33 +020010 if (select === null)
11 return;
Akronad894832016-06-08 18:24:48 +020012
Akronaba7a5a2016-08-15 21:58:33 +020013 // Prepare list before object upgras
14 var list = [];
Akronad894832016-06-08 18:24:48 +020015
Akronaba7a5a2016-08-15 21:58:33 +020016 // Iterate through options list
Akronb50964a2020-10-12 11:44:37 +020017 Array.from(
18 select.getElementsByTagName('option')
Akronaba7a5a2016-08-15 21:58:33 +020019 // Get option item and add to list
Akronb50964a2020-10-12 11:44:37 +020020 ).forEach(function(item) {
21
Akronaba7a5a2016-08-15 21:58:33 +020022 var opt = [
23 item.textContent,
24 item.getAttribute('value')
25 ];
Akronad894832016-06-08 18:24:48 +020026
Akronaba7a5a2016-08-15 21:58:33 +020027 // If the item has an attribute - list
28 if (item.hasAttribute('desc'))
29 opt.push(item.getAttribute('desc'));
Akronad894832016-06-08 18:24:48 +020030
Akronaba7a5a2016-08-15 21:58:33 +020031 list.push(opt);
Akronb50964a2020-10-12 11:44:37 +020032 });
Akronad894832016-06-08 18:24:48 +020033
Akronaba7a5a2016-08-15 21:58:33 +020034 // Create object with list
35 var obj = Object.create(menuClass).upgradeTo(this)
36 ._init(list, {
37 itemClass : selectMenuItemClass
38 });
Akron7f613e02016-11-07 02:50:44 +010039
Akronaba7a5a2016-08-15 21:58:33 +020040 obj._container = element;
41 obj._select = select;
42 obj._select.style.display = 'none';
Akronad894832016-06-08 18:24:48 +020043
Akronaba7a5a2016-08-15 21:58:33 +020044 // Create title
Akron0b489ad2018-02-02 16:49:32 +010045 obj._title = obj._container.addE('span');
46 obj._title.addT('');
Akronaba7a5a2016-08-15 21:58:33 +020047 obj._container.appendChild(obj.element());
Akronad894832016-06-08 18:24:48 +020048
Akronaba7a5a2016-08-15 21:58:33 +020049 // Show the menu
50 obj._container.addEventListener('click', obj.showSelected.bind(obj));
Akron6bb71582016-06-10 20:41:08 +020051
Akronaba7a5a2016-08-15 21:58:33 +020052 // Add index information to each item
Akron678c26f2020-10-09 08:52:50 +020053 obj._items.forEach((e,i) => e._index = i);
Akron6bb71582016-06-10 20:41:08 +020054
Akronaba7a5a2016-08-15 21:58:33 +020055 // This is only domspecific
56 obj.element().addEventListener('blur', function (e) {
57 this.menu.hide();
58 this.menu.showTitle();
59 });
Akron6bb71582016-06-10 20:41:08 +020060
Akronaba7a5a2016-08-15 21:58:33 +020061 // In case another tool changes
62 // the option via JS - this needs
63 // to be reflected!
64 select.addEventListener('change', function (e) {
65 this.showTitle();
66 }.bind(obj));
Akron6bb71582016-06-10 20:41:08 +020067
Akronaba7a5a2016-08-15 21:58:33 +020068 obj.showTitle();
69 return obj;
Akron6bb71582016-06-10 20:41:08 +020070 },
71
Akronaba7a5a2016-08-15 21:58:33 +020072 /**
73 * Get or set the selection index
74 */
Akron6bb71582016-06-10 20:41:08 +020075 select : function (index) {
Akronaba7a5a2016-08-15 21:58:33 +020076 if (arguments.length > 0) {
77 this._selected = index;
78 this._select.selectedIndex = index;
79 };
Akron6bb71582016-06-10 20:41:08 +020080
Akron7f613e02016-11-07 02:50:44 +010081 return this._selected || this._select.selectedIndex || 0;
Akron6bb71582016-06-10 20:41:08 +020082 },
83
Akron086fe5d2017-11-13 14:01:45 +010084
85 /**
86 * Set the select value
87 */
88 selectValue : function (vParam) {
89 var qlf = this._select.options;
Akron678c26f2020-10-09 08:52:50 +020090 for (let i = 0; i < qlf.length; i++) {
Akron086fe5d2017-11-13 14:01:45 +010091 if (qlf[i].value == vParam) {
92 this.hide();
93 this.select(i);
94 this.showTitle();
95 break;
96 };
97 };
98 return this;
99 },
100
Akronaba7a5a2016-08-15 21:58:33 +0200101 /**
102 * Show the select menu
103 */
Akron6bb71582016-06-10 20:41:08 +0200104 showSelected : function () {
Akronaba7a5a2016-08-15 21:58:33 +0200105 this._title.style.display = 'none';
106 this._selected = this._select.selectedIndex;
107 this.show(this._selected);
108 this.focus();
Akron6bb71582016-06-10 20:41:08 +0200109 },
110
Akronaba7a5a2016-08-15 21:58:33 +0200111 /**
112 * Show the title
113 */
Akron6bb71582016-06-10 20:41:08 +0200114 showTitle : function () {
Akronaba7a5a2016-08-15 21:58:33 +0200115
116 // Get the selection context
117 var s = this.select();
118 this._title.textContent = this.item(
119 this.select()
120 ).title();
121 this._title.style.display = 'inline';
Akronad894832016-06-08 18:24:48 +0200122 }
123 }
124 }
125);