Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 1 | define( |
Akron | c82513b | 2016-06-11 11:22:36 +0200 | [diff] [blame^] | 2 | ['menu', 'selectMenu/item'], |
| 3 | function (menuClass, selectMenuItemClass) { |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 4 | |
| 5 | return { |
| 6 | create : function (element) { |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 7 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 8 | var select = element.getElementsByTagName('select')[0]; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 9 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 10 | // Prepare list before object upgras |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 11 | var list = []; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 12 | var options = select.getElementsByTagName('option'); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 13 | |
| 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 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 28 | // Create object with list |
| 29 | var obj = Object.create(menuClass).upgradeTo(this) |
Akron | c82513b | 2016-06-11 11:22:36 +0200 | [diff] [blame^] | 30 | ._init(list, { |
| 31 | itemClass : selectMenuItemClass |
| 32 | }); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 33 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 34 | obj._container = element; |
| 35 | obj._select = select; |
| 36 | obj._select.style.display = 'none'; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 37 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 38 | // Create title |
| 39 | obj._title = obj._container.appendChild(document.createElement('span')); |
| 40 | obj._title.appendChild(document.createTextNode('')); |
| 41 | obj._container.appendChild(obj.element()); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 42 | |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 43 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 44 | obj._container.addEventListener('click', obj.showSelected.bind(obj)); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 45 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 46 | |
| 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(); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 67 | return obj; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 68 | }, |
| 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'; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | ); |