blob: 3e5c60536969a147235c546d4c8621c2eaf03e0f [file] [log] [blame]
Akronad894832016-06-08 18:24:48 +02001define(
Akronc82513b2016-06-11 11:22:36 +02002 ['menu', 'selectMenu/item'],
3 function (menuClass, selectMenuItemClass) {
Akronad894832016-06-08 18:24:48 +02004
5 return {
6 create : function (element) {
Akronad894832016-06-08 18:24:48 +02007
Akron6bb71582016-06-10 20:41:08 +02008 var select = element.getElementsByTagName('select')[0];
Akronad894832016-06-08 18:24:48 +02009
Akron6bb71582016-06-10 20:41:08 +020010 // Prepare list before object upgras
Akronad894832016-06-08 18:24:48 +020011 var list = [];
Akron6bb71582016-06-10 20:41:08 +020012 var options = select.getElementsByTagName('option');
Akronad894832016-06-08 18:24:48 +020013
14 for (var i = 0; i < options.length; i++) {
15
16 var item = options.item(i);
17 var opt = [
18 item.textContent,
19 item.getAttribute('value')
20 ];
21
22 if (item.hasAttribute('desc'))
23 opt.push(item.getAttribute('desc'));
24
25 list.push(opt);
26 };
27
Akron6bb71582016-06-10 20:41:08 +020028 // Create object with list
29 var obj = Object.create(menuClass).upgradeTo(this)
Akronc82513b2016-06-11 11:22:36 +020030 ._init(list, {
31 itemClass : selectMenuItemClass
32 });
Akronad894832016-06-08 18:24:48 +020033
Akron6bb71582016-06-10 20:41:08 +020034 obj._container = element;
35 obj._select = select;
36 obj._select.style.display = 'none';
Akronad894832016-06-08 18:24:48 +020037
Akron6bb71582016-06-10 20:41:08 +020038 // Create title
39 obj._title = obj._container.appendChild(document.createElement('span'));
40 obj._title.appendChild(document.createTextNode(''));
41 obj._container.appendChild(obj.element());
Akronad894832016-06-08 18:24:48 +020042
Akronad894832016-06-08 18:24:48 +020043
Akron6bb71582016-06-10 20:41:08 +020044 obj._container.addEventListener('click', obj.showSelected.bind(obj));
Akronad894832016-06-08 18:24:48 +020045
Akron6bb71582016-06-10 20:41:08 +020046
47 // Add index information to each item
48 for (i in obj._items) {
49 obj._items[i]._index = i;
50 };
51
52 // This is only domspecific
53 obj.element().addEventListener('blur', function (e) {
54 this.menu.hide();
55 this.menu.showTitle();
56 });
57
58
59 // In case another tool changes
60 // the option via JS - this needs
61 // to be reflected!
62 select.addEventListener('change', function (e) {
63 this.showTitle();
64 }.bind(obj));
65
66 obj.showTitle();
Akronad894832016-06-08 18:24:48 +020067 return obj;
Akron6bb71582016-06-10 20:41:08 +020068 },
69
70 select : function (index) {
71 if (arguments.length > 0) {
72 this._selected = index;
73 this._select.selectedIndex = index;
74 };
75
76 return this._selected || 0;
77 },
78
79 showSelected : function () {
80 this._title.style.display = 'none';
81 this._selected = this._select.selectedIndex;
82 this.show(this._selected);
83 this.focus();
84 },
85
86 showTitle : function () {
87 var s = this.select();
88 this._title.textContent = this.item(
89 this.select()
90 ).title();
91 this._title.style.display = 'inline';
Akronad894832016-06-08 18:24:48 +020092 }
93 }
94 }
95);