Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 1 | define( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 2 | ['menu', 'selectMenu/item', 'util'], |
Akron | c82513b | 2016-06-11 11:22:36 +0200 | [diff] [blame] | 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 |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 45 | obj._title = obj._container.addE('span'); |
| 46 | obj._title.addT(''); |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 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 |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame^] | 53 | obj._items.forEach((e,i) => e._index = i); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 54 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 55 | // This is only domspecific |
| 56 | obj.element().addEventListener('blur', function (e) { |
| 57 | this.menu.hide(); |
| 58 | this.menu.showTitle(); |
| 59 | }); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 60 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 61 | // In case another tool changes |
| 62 | // the option via JS - this needs |
| 63 | // to be reflected! |
| 64 | select.addEventListener('change', function (e) { |
| 65 | this.showTitle(); |
| 66 | }.bind(obj)); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 67 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 68 | obj.showTitle(); |
| 69 | return obj; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 70 | }, |
| 71 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 72 | /** |
| 73 | * Get or set the selection index |
| 74 | */ |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 75 | select : function (index) { |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 76 | if (arguments.length > 0) { |
| 77 | this._selected = index; |
| 78 | this._select.selectedIndex = index; |
| 79 | }; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 80 | |
Akron | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 81 | return this._selected || this._select.selectedIndex || 0; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 82 | }, |
| 83 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Set the select value |
| 87 | */ |
| 88 | selectValue : function (vParam) { |
| 89 | var qlf = this._select.options; |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame^] | 90 | for (let i = 0; i < qlf.length; i++) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 91 | if (qlf[i].value == vParam) { |
| 92 | this.hide(); |
| 93 | this.select(i); |
| 94 | this.showTitle(); |
| 95 | break; |
| 96 | }; |
| 97 | }; |
| 98 | return this; |
| 99 | }, |
| 100 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 101 | /** |
| 102 | * Show the select menu |
| 103 | */ |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 104 | showSelected : function () { |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 105 | this._title.style.display = 'none'; |
| 106 | this._selected = this._select.selectedIndex; |
| 107 | this.show(this._selected); |
| 108 | this.focus(); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 109 | }, |
| 110 | |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 111 | /** |
| 112 | * Show the title |
| 113 | */ |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 114 | showTitle : function () { |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 115 | |
| 116 | // Get the selection context |
| 117 | var s = this.select(); |
| 118 | this._title.textContent = this.item( |
| 119 | this.select() |
| 120 | ).title(); |
| 121 | this._title.style.display = 'inline'; |
Akron | ad89483 | 2016-06-08 18:24:48 +0200 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | ); |