blob: 7a4f8df73887a72394604f11b7952abcefd7e3d8 [file] [log] [blame]
Akrone51eaa32020-11-10 09:35:53 +01001/**
2 * Menu to choose from in a button group.
3 */
4"use strict";
Akron24aa0052020-11-10 11:00:34 +01005
Akron52ed22d2018-07-11 17:05:19 +02006define(['menu'], function (menuClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +00007
8 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +00009
10 /**
11 * Create new menu object.
Akron7f9a6a32018-07-18 15:05:23 +020012 * Pass the panel object
Nils Diewald7148c6f2015-05-04 15:07:53 +000013 * and the item parameters.
14 *
Akron7f9a6a32018-07-18 15:05:23 +020015 * @param panel The panel object
Nils Diewald7148c6f2015-05-04 15:07:53 +000016 * @param params The match menu items
17 * as an array of arrays.
18 */
Akron52ed22d2018-07-11 17:05:19 +020019 create : function (list, itemClass) {
Akronbf713fc2020-10-13 10:44:35 +020020 const obj = Object.create(menuClass)
21 .upgradeTo(this)
22 ._init(list, {itemClass : itemClass});
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 obj.limit(6);
Akroneaba63e2018-01-26 19:49:30 +010024
Akronbf713fc2020-10-13 10:44:35 +020025 const e = obj.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000026
27 // This is only domspecific
Akroneaba63e2018-01-26 19:49:30 +010028 e.addEventListener('blur', function (e) {
Akrond67d45b2017-05-18 21:47:38 +020029 this.menu.hide();
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 });
Akroneaba63e2018-01-26 19:49:30 +010031
Akron52ed22d2018-07-11 17:05:19 +020032 e.classList.add('button-group-list');
Akroneaba63e2018-01-26 19:49:30 +010033
34 // Add menu to body
35 document.getElementsByTagName('body')[0].appendChild(e);
36
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 return obj;
38 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000039
Akronbf713fc2020-10-13 10:44:35 +020040
Nils Diewald7148c6f2015-05-04 15:07:53 +000041 /**
Akron7f9a6a32018-07-18 15:05:23 +020042 * The panel object of the menu.
Nils Diewald7148c6f2015-05-04 15:07:53 +000043 */
Akron7f9a6a32018-07-18 15:05:23 +020044 panel :function (panelVar) {
45 if (panelVar !== undefined)
46 this._panel = panelVar;
Akroneaba63e2018-01-26 19:49:30 +010047
Akron7f9a6a32018-07-18 15:05:23 +020048 return this._panel;
Akroneaba63e2018-01-26 19:49:30 +010049 },
50
Akronbf713fc2020-10-13 10:44:35 +020051
Akronbec4a6a2018-07-10 14:45:15 +020052 // Attach menu to button
Akron52ed22d2018-07-11 17:05:19 +020053 button : function (btn) {
Akron257aa852018-02-06 19:29:51 +010054
Akron1801c5c2018-07-16 18:15:48 +020055 this._button = btn;
56
Akron1801c5c2018-07-16 18:15:48 +020057 this._repos(this._button);
Akron257aa852018-02-06 19:29:51 +010058 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 () {
Akron1801c5c2018-07-16 18:15:48 +020069 this._repos(this._button);
Akron257aa852018-02-06 19:29:51 +010070 }.bind(this);
71
72 window.addEventListener('scroll', this._onscroll);
73 },
74
Akronbf713fc2020-10-13 10:44:35 +020075
Akron257aa852018-02-06 19:29:51 +010076 // Overwrite onHide method
77 onHide : function () {
78
79 // Remove listener
80 if (this._onscroll !== undefined) {
81 window.removeEventListener('scroll', this._onscroll);
82 };
Akronc296ca22018-04-24 16:35:26 +020083 this.element().blur();
Akron257aa852018-02-06 19:29:51 +010084 },
85
86 _repos : function (e) {
Akronbf713fc2020-10-13 10:44:35 +020087 const bounding = e.getBoundingClientRect();
Akron24aa0052020-11-10 11:00:34 +010088 this._el.style.left = bounding.left + "px";
89 this._el.style.top = (
Akron257aa852018-02-06 19:29:51 +010090 bounding.top +
91 bounding.height -
Akron24aa0052020-11-10 11:00:34 +010092 this._el.clientHeight
Akron257aa852018-02-06 19:29:51 +010093 ) + "px";
Nils Diewald0e6992a2015-04-14 20:13:52 +000094 }
95 };
96});