Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Menu to choose from in a button group. |
| 3 | */ |
| 4 | "use strict"; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 5 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 6 | define(['menu'], function (menuClass) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 7 | |
| 8 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Create new menu object. |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 12 | * Pass the panel object |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 13 | * and the item parameters. |
| 14 | * |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 15 | * @param panel The panel object |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 16 | * @param params The match menu items |
| 17 | * as an array of arrays. |
| 18 | */ |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 19 | create : function (list, itemClass) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 20 | const obj = Object.create(menuClass) |
| 21 | .upgradeTo(this) |
| 22 | ._init(list, {itemClass : itemClass}); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 23 | obj.limit(6); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 24 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 25 | const e = obj.element(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 26 | |
| 27 | // This is only domspecific |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 28 | e.addEventListener('blur', function (e) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 29 | this.menu.hide(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 30 | }); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 31 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 32 | e.classList.add('button-group-list'); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 33 | |
| 34 | // Add menu to body |
| 35 | document.getElementsByTagName('body')[0].appendChild(e); |
| 36 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 37 | return obj; |
| 38 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 39 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 40 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 41 | /** |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 42 | * The panel object of the menu. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 43 | */ |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 44 | panel :function (panelVar) { |
| 45 | if (panelVar !== undefined) |
| 46 | this._panel = panelVar; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 47 | |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 48 | return this._panel; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 49 | }, |
| 50 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 51 | |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 52 | // Attach menu to button |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 53 | button : function (btn) { |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 54 | |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 55 | this._button = btn; |
| 56 | |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 57 | this._repos(this._button); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 58 | this.slider().reInit(); |
| 59 | |
| 60 | /* |
| 61 | * This is a suboptimal scrolling solution, see |
| 62 | * see https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects |
| 63 | */ |
| 64 | if (this._onscroll !== undefined) { |
| 65 | window.removeEventListener('scroll', this._onscroll); |
| 66 | }; |
| 67 | |
| 68 | this._onscroll = function () { |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 69 | this._repos(this._button); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 70 | }.bind(this); |
| 71 | |
| 72 | window.addEventListener('scroll', this._onscroll); |
| 73 | }, |
| 74 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 75 | |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 76 | // Overwrite onHide method |
| 77 | onHide : function () { |
| 78 | |
| 79 | // Remove listener |
| 80 | if (this._onscroll !== undefined) { |
| 81 | window.removeEventListener('scroll', this._onscroll); |
| 82 | }; |
Akron | c296ca2 | 2018-04-24 16:35:26 +0200 | [diff] [blame] | 83 | this.element().blur(); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 84 | }, |
| 85 | |
| 86 | _repos : function (e) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 87 | const bounding = e.getBoundingClientRect(); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 88 | this._el.style.left = bounding.left + "px"; |
| 89 | this._el.style.top = ( |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 90 | bounding.top + |
| 91 | bounding.height - |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 92 | this._el.clientHeight |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 93 | ) + "px"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 94 | } |
| 95 | }; |
| 96 | }); |