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