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 | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 12 | * Pass the list of items and the itemClass. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 13 | */ |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 14 | create : function (list, itemClass) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 15 | const obj = Object.create(menuClass) |
| 16 | .upgradeTo(this) |
| 17 | ._init(list, {itemClass : itemClass}); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | obj.limit(6); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 19 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 20 | const e = obj.element(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 21 | |
| 22 | // This is only domspecific |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 23 | e.addEventListener('blur', function (e) { |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 24 | this.menu.hide(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 25 | }); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 26 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 27 | e.classList.add('button-group-list'); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 28 | |
| 29 | // Add menu to body |
| 30 | document.getElementsByTagName('body')[0].appendChild(e); |
| 31 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 32 | return obj; |
| 33 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 34 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 35 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 36 | /** |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 37 | * The panel object of the menu, |
| 38 | * in case the menu was spawned by a panel. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 39 | */ |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 40 | panel : function (panelVar) { |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 41 | if (panelVar !== undefined) |
| 42 | this._panel = panelVar; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 43 | |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 44 | return this._panel; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 45 | }, |
| 46 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 47 | |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 48 | // Attach menu to button |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 49 | button : function (btn) { |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 50 | |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 51 | this._button = btn; |
| 52 | |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 53 | this._repos(this._button); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 54 | this.slider().reInit(); |
| 55 | |
| 56 | /* |
| 57 | * This is a suboptimal scrolling solution, see |
| 58 | * see https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects |
| 59 | */ |
| 60 | if (this._onscroll !== undefined) { |
| 61 | window.removeEventListener('scroll', this._onscroll); |
| 62 | }; |
| 63 | |
| 64 | this._onscroll = function () { |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 65 | this._repos(this._button); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 66 | }.bind(this); |
| 67 | |
| 68 | window.addEventListener('scroll', this._onscroll); |
| 69 | }, |
| 70 | |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Add button in order |
| 74 | * |
| 75 | * Returns the button element |
| 76 | */ |
| 77 | add : function (title, data, cb) { |
| 78 | |
| 79 | let that = this; |
| 80 | |
| 81 | const cbWrap = function (e) { |
| 82 | |
| 83 | // Call callback |
| 84 | let obj = that._bind || this; |
| 85 | obj.button = b; |
| 86 | cb.apply(obj) |
| 87 | }; |
| 88 | |
| 89 | // TODO: |
| 90 | // support classes, data-icon and state in itemClass! |
| 91 | const b = this.itemClass().create([title, title, cbWrap]); |
| 92 | this.append(b); |
| 93 | return b; |
| 94 | }, |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 95 | |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 96 | // Overwrite onHide method |
| 97 | onHide : function () { |
| 98 | |
| 99 | // Remove listener |
| 100 | if (this._onscroll !== undefined) { |
| 101 | window.removeEventListener('scroll', this._onscroll); |
| 102 | }; |
Akron | c296ca2 | 2018-04-24 16:35:26 +0200 | [diff] [blame] | 103 | this.element().blur(); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 104 | }, |
| 105 | |
| 106 | _repos : function (e) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 107 | const bounding = e.getBoundingClientRect(); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 108 | this._el.style.left = bounding.left + "px"; |
| 109 | this._el.style.top = ( |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 110 | bounding.top + |
| 111 | bounding.height - |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 112 | this._el.clientHeight |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 113 | ) + "px"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | }); |