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 | |
Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 35 | this._left = false; |
| 36 | this._below = false; |
| 37 | this._outside = false; |
| 38 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 39 | return obj; |
| 40 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 41 | |
Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 42 | /** |
| 43 | * Open the menu at a certain position |
| 44 | * relative to the button. |
| 45 | */ |
| 46 | openAt : function (left, below, outside) { |
| 47 | this._left = left ? true : false; |
| 48 | this._below = below ? true : false; |
| 49 | this._outside = outside ? true : false; |
| 50 | }, |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 51 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 52 | /** |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 53 | * The panel object of the menu, |
| 54 | * in case the menu was spawned by a panel. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 55 | */ |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 56 | panel : function (panelVar) { |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 57 | if (panelVar !== undefined) |
| 58 | this._panel = panelVar; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 59 | |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 60 | return this._panel; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 61 | }, |
| 62 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 63 | |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 64 | // Attach menu to button |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 65 | button : function (btn) { |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 66 | |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 67 | this._button = btn; |
| 68 | |
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 | this.slider().reInit(); |
| 71 | |
| 72 | /* |
| 73 | * This is a suboptimal scrolling solution, see |
| 74 | * see https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects |
| 75 | */ |
| 76 | if (this._onscroll !== undefined) { |
| 77 | window.removeEventListener('scroll', this._onscroll); |
| 78 | }; |
| 79 | |
| 80 | this._onscroll = function () { |
Akron | 1801c5c | 2018-07-16 18:15:48 +0200 | [diff] [blame] | 81 | this._repos(this._button); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 82 | }.bind(this); |
| 83 | |
| 84 | window.addEventListener('scroll', this._onscroll); |
| 85 | }, |
| 86 | |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Add button in order |
| 90 | * |
| 91 | * Returns the button element |
| 92 | */ |
| 93 | add : function (title, data, cb) { |
| 94 | |
| 95 | let that = this; |
| 96 | |
| 97 | const cbWrap = function (e) { |
| 98 | |
| 99 | // Call callback |
| 100 | let obj = that._bind || this; |
| 101 | obj.button = b; |
| 102 | cb.apply(obj) |
| 103 | }; |
| 104 | |
| 105 | // TODO: |
| 106 | // support classes, data-icon and state in itemClass! |
| 107 | const b = this.itemClass().create([title, title, cbWrap]); |
| 108 | this.append(b); |
| 109 | return b; |
| 110 | }, |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 111 | |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 112 | // Overwrite onHide method |
| 113 | onHide : function () { |
| 114 | |
| 115 | // Remove listener |
| 116 | if (this._onscroll !== undefined) { |
| 117 | window.removeEventListener('scroll', this._onscroll); |
| 118 | }; |
Akron | c296ca2 | 2018-04-24 16:35:26 +0200 | [diff] [blame] | 119 | this.element().blur(); |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 120 | }, |
| 121 | |
| 122 | _repos : function (e) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 123 | const bounding = e.getBoundingClientRect(); |
Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 124 | const s = this._el.style; |
| 125 | |
| 126 | let left = window.pageXOffset; |
| 127 | let top = 0; |
| 128 | |
| 129 | s.right = 'auto'; |
| 130 | s.bottom = 'auto'; |
| 131 | |
| 132 | left += bounding.left; |
| 133 | |
| 134 | if (this._left) { |
| 135 | left += bounding.width - this._el.clientWidth |
| 136 | }; |
| 137 | |
| 138 | if (this._below) { |
| 139 | top += bounding.bottom; |
| 140 | |
| 141 | if (!this._outside) { |
| 142 | top -= bounding.height; |
| 143 | }; |
| 144 | |
| 145 | } else { |
| 146 | top -= this._el.clientHeight; |
| 147 | if (this._outside) { |
| 148 | top += bounding.top; |
| 149 | } else { |
| 150 | top += bounding.bottom; |
| 151 | }; |
| 152 | }; |
| 153 | |
| 154 | s.left = left + "px"; |
| 155 | s.top = top + "px"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 156 | } |
| 157 | }; |
| 158 | }); |