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 | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 8 | var select = element.getElementsByTagName('select')[0]; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 9 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 10 | if (select === null) |
| 11 | return; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 12 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 13 | // Prepare list before object upgras |
| 14 | var list = []; |
| 15 | var options = select.getElementsByTagName('option'); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 16 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 17 | // Iterate through options list |
| 18 | for (var i = 0; i < options.length; i++) { |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 19 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 20 | // Get option item and add to list |
| 21 | var item = options.item(i); |
| 22 | var opt = [ |
| 23 | item.textContent, |
| 24 | item.getAttribute('value') |
| 25 | ]; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 26 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 27 | // If the item has an attribute - list |
| 28 | if (item.hasAttribute('desc')) |
| 29 | opt.push(item.getAttribute('desc')); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 30 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 31 | list.push(opt); |
| 32 | }; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 33 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 34 | // Create object with list |
| 35 | var obj = Object.create(menuClass).upgradeTo(this) |
| 36 | ._init(list, { |
| 37 | itemClass : selectMenuItemClass |
| 38 | }); |
Akron | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 39 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 40 | obj._container = element; |
| 41 | obj._select = select; |
| 42 | obj._select.style.display = 'none'; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 43 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 44 | // Create title |
| 45 | obj._title = obj._container.appendChild(document.createElement('span')); |
| 46 | obj._title.appendChild(document.createTextNode('')); |
| 47 | obj._container.appendChild(obj.element()); |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 48 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 49 | // Show the menu |
| 50 | obj._container.addEventListener('click', obj.showSelected.bind(obj)); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 51 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 52 | // Add index information to each item |
| 53 | for (i in obj._items) { |
| 54 | obj._items[i]._index = i; |
| 55 | }; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 56 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 57 | // This is only domspecific |
| 58 | obj.element().addEventListener('blur', function (e) { |
| 59 | this.menu.hide(); |
| 60 | this.menu.showTitle(); |
| 61 | }); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 62 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 63 | // 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)); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 69 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 70 | obj.showTitle(); |
| 71 | return obj; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 72 | }, |
| 73 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 74 | /** |
| 75 | * Get or set the selection index |
| 76 | */ |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 77 | select : function (index) { |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 78 | if (arguments.length > 0) { |
| 79 | this._selected = index; |
| 80 | this._select.selectedIndex = index; |
| 81 | }; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 82 | |
Akron | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 83 | return this._selected || this._select.selectedIndex || 0; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 84 | }, |
| 85 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 86 | |
| 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 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 104 | /** |
| 105 | * Show the select menu |
| 106 | */ |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 107 | showSelected : function () { |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 108 | this._title.style.display = 'none'; |
| 109 | this._selected = this._select.selectedIndex; |
| 110 | this.show(this._selected); |
| 111 | this.focus(); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 112 | }, |
| 113 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 114 | /** |
| 115 | * Show the title |
| 116 | */ |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 117 | showTitle : function () { |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 118 | |
| 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'; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | ); |