blob: 9ef698607740ead98e2f65fd1c0fe524cdf5e94c [file] [log] [blame]
Akronad894832016-06-08 18:24:48 +02001define(
Akron6bb71582016-06-10 20:41:08 +02002 ['menu', 'selectMenu/item', 'menu/prefix', 'menu/lengthField'],
3 function (menuClass, selectMenuItemClass, prefixClass, lengthFieldClass) {
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)
30 ._init(selectMenuItemClass, prefixClass, lengthFieldClass, list);
Akronad894832016-06-08 18:24:48 +020031
Akron6bb71582016-06-10 20:41:08 +020032 obj._container = element;
33 obj._select = select;
34 obj._select.style.display = 'none';
Akronad894832016-06-08 18:24:48 +020035
Akron6bb71582016-06-10 20:41:08 +020036 // Create title
37 obj._title = obj._container.appendChild(document.createElement('span'));
38 obj._title.appendChild(document.createTextNode(''));
39 obj._container.appendChild(obj.element());
Akronad894832016-06-08 18:24:48 +020040
Akronad894832016-06-08 18:24:48 +020041
Akron6bb71582016-06-10 20:41:08 +020042 obj._container.addEventListener('click', obj.showSelected.bind(obj));
Akronad894832016-06-08 18:24:48 +020043
Akron6bb71582016-06-10 20:41:08 +020044
45 // Add index information to each item
46 for (i in obj._items) {
47 obj._items[i]._index = i;
48 };
49
50 // This is only domspecific
51 obj.element().addEventListener('blur', function (e) {
52 this.menu.hide();
53 this.menu.showTitle();
54 });
55
56
57 // In case another tool changes
58 // the option via JS - this needs
59 // to be reflected!
60 select.addEventListener('change', function (e) {
61 this.showTitle();
62 }.bind(obj));
63
64 obj.showTitle();
Akronad894832016-06-08 18:24:48 +020065 return obj;
Akron6bb71582016-06-10 20:41:08 +020066 },
67
68 select : function (index) {
69 if (arguments.length > 0) {
70 this._selected = index;
71 this._select.selectedIndex = index;
72 };
73
74 return this._selected || 0;
75 },
76
77 showSelected : function () {
78 this._title.style.display = 'none';
79 this._selected = this._select.selectedIndex;
80 this.show(this._selected);
81 this.focus();
82 },
83
84 showTitle : function () {
85 var s = this.select();
86 this._title.textContent = this.item(
87 this.select()
88 ).title();
89 this._title.style.display = 'inline';
Akronad894832016-06-08 18:24:48 +020090 }
91 }
92 }
93);