Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 1 | define( |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame^] | 2 | ['menu', 'selectMenu/item', 'menu/prefix', 'menu/lengthField'], |
| 3 | function (menuClass, selectMenuItemClass, prefixClass, lengthFieldClass) { |
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) |
| 30 | ._init(selectMenuItemClass, prefixClass, lengthFieldClass, list); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 31 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame^] | 32 | obj._container = element; |
| 33 | obj._select = select; |
| 34 | obj._select.style.display = 'none'; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 35 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame^] | 36 | // Create title |
| 37 | obj._title = obj._container.appendChild(document.createElement('span')); |
| 38 | obj._title.appendChild(document.createTextNode('')); |
| 39 | obj._container.appendChild(obj.element()); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 40 | |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 41 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame^] | 42 | obj._container.addEventListener('click', obj.showSelected.bind(obj)); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 43 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame^] | 44 | |
| 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(); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 65 | return obj; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame^] | 66 | }, |
| 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'; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | ); |