blob: 9f447cad22798f210b98b8cff971a47e4d806da7 [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 = [];
15 var options = select.getElementsByTagName('option');
Akronad894832016-06-08 18:24:48 +020016
Akronaba7a5a2016-08-15 21:58:33 +020017 // Iterate through options list
18 for (var i = 0; i < options.length; i++) {
Akronad894832016-06-08 18:24:48 +020019
Akronaba7a5a2016-08-15 21:58:33 +020020 // Get option item and add to list
21 var item = options.item(i);
22 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);
32 };
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
53 for (i in obj._items) {
54 obj._items[i]._index = i;
55 };
Akron6bb71582016-06-10 20:41:08 +020056
Akronaba7a5a2016-08-15 21:58:33 +020057 // This is only domspecific
58 obj.element().addEventListener('blur', function (e) {
59 this.menu.hide();
60 this.menu.showTitle();
61 });
Akron6bb71582016-06-10 20:41:08 +020062
Akronaba7a5a2016-08-15 21:58:33 +020063 // In case another tool changes
64 // the option via JS - this needs
65 // to be reflected!
66 select.addEventListener('change', function (e) {
67 this.showTitle();
68 }.bind(obj));
Akron6bb71582016-06-10 20:41:08 +020069
Akronaba7a5a2016-08-15 21:58:33 +020070 obj.showTitle();
71 return obj;
Akron6bb71582016-06-10 20:41:08 +020072 },
73
Akronaba7a5a2016-08-15 21:58:33 +020074 /**
75 * Get or set the selection index
76 */
Akron6bb71582016-06-10 20:41:08 +020077 select : function (index) {
Akronaba7a5a2016-08-15 21:58:33 +020078 if (arguments.length > 0) {
79 this._selected = index;
80 this._select.selectedIndex = index;
81 };
Akron6bb71582016-06-10 20:41:08 +020082
Akron7f613e02016-11-07 02:50:44 +010083 return this._selected || this._select.selectedIndex || 0;
Akron6bb71582016-06-10 20:41:08 +020084 },
85
Akron086fe5d2017-11-13 14:01:45 +010086
87 /**
88 * Set the select value
89 */
90 selectValue : function (vParam) {
91 var qlf = this._select.options;
92 var i;
93 for (i in qlf) {
94 if (qlf[i].value == vParam) {
95 this.hide();
96 this.select(i);
97 this.showTitle();
98 break;
99 };
100 };
101 return this;
102 },
103
Akronaba7a5a2016-08-15 21:58:33 +0200104 /**
105 * Show the select menu
106 */
Akron6bb71582016-06-10 20:41:08 +0200107 showSelected : function () {
Akronaba7a5a2016-08-15 21:58:33 +0200108 this._title.style.display = 'none';
109 this._selected = this._select.selectedIndex;
110 this.show(this._selected);
111 this.focus();
Akron6bb71582016-06-10 20:41:08 +0200112 },
113
Akronaba7a5a2016-08-15 21:58:33 +0200114 /**
115 * Show the title
116 */
Akron6bb71582016-06-10 20:41:08 +0200117 showTitle : function () {
Akronaba7a5a2016-08-15 21:58:33 +0200118
119 // Get the selection context
120 var s = this.select();
121 this._title.textContent = this.item(
122 this.select()
123 ).title();
124 this._title.style.display = 'inline';
Akronad894832016-06-08 18:24:48 +0200125 }
126 }
127 }
128);