blob: 32ba0ebe99e4d3edf9f06d464378c08ddbe7ea12 [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.
Akron20b6d312021-07-26 15:14:42 +020012 * Pass the list of items and the itemClass.
Nils Diewald7148c6f2015-05-04 15:07:53 +000013 */
Akron52ed22d2018-07-11 17:05:19 +020014 create : function (list, itemClass) {
Akronbf713fc2020-10-13 10:44:35 +020015 const obj = Object.create(menuClass)
16 .upgradeTo(this)
17 ._init(list, {itemClass : itemClass});
Nils Diewald0e6992a2015-04-14 20:13:52 +000018 obj.limit(6);
Akroneaba63e2018-01-26 19:49:30 +010019
Akronbf713fc2020-10-13 10:44:35 +020020 const e = obj.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000021
22 // This is only domspecific
Akroneaba63e2018-01-26 19:49:30 +010023 e.addEventListener('blur', function (e) {
Akron20b6d312021-07-26 15:14:42 +020024 this.menu.hide();
Nils Diewald0e6992a2015-04-14 20:13:52 +000025 });
Akroneaba63e2018-01-26 19:49:30 +010026
Akron52ed22d2018-07-11 17:05:19 +020027 e.classList.add('button-group-list');
Akroneaba63e2018-01-26 19:49:30 +010028
29 // Add menu to body
30 document.getElementsByTagName('body')[0].appendChild(e);
31
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 return obj;
33 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000034
Akronbf713fc2020-10-13 10:44:35 +020035
Nils Diewald7148c6f2015-05-04 15:07:53 +000036 /**
Akron20b6d312021-07-26 15:14:42 +020037 * The panel object of the menu,
38 * in case the menu was spawned by a panel.
Nils Diewald7148c6f2015-05-04 15:07:53 +000039 */
Akron20b6d312021-07-26 15:14:42 +020040 panel : function (panelVar) {
Akron7f9a6a32018-07-18 15:05:23 +020041 if (panelVar !== undefined)
42 this._panel = panelVar;
Akroneaba63e2018-01-26 19:49:30 +010043
Akron7f9a6a32018-07-18 15:05:23 +020044 return this._panel;
Akroneaba63e2018-01-26 19:49:30 +010045 },
46
Akronbf713fc2020-10-13 10:44:35 +020047
Akronbec4a6a2018-07-10 14:45:15 +020048 // Attach menu to button
Akron52ed22d2018-07-11 17:05:19 +020049 button : function (btn) {
Akron257aa852018-02-06 19:29:51 +010050
Akron1801c5c2018-07-16 18:15:48 +020051 this._button = btn;
52
Akron1801c5c2018-07-16 18:15:48 +020053 this._repos(this._button);
Akron257aa852018-02-06 19:29:51 +010054 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 () {
Akron1801c5c2018-07-16 18:15:48 +020065 this._repos(this._button);
Akron257aa852018-02-06 19:29:51 +010066 }.bind(this);
67
68 window.addEventListener('scroll', this._onscroll);
69 },
70
Akron20b6d312021-07-26 15:14:42 +020071
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 },
Akronbf713fc2020-10-13 10:44:35 +020095
Akron257aa852018-02-06 19:29:51 +010096 // Overwrite onHide method
97 onHide : function () {
98
99 // Remove listener
100 if (this._onscroll !== undefined) {
101 window.removeEventListener('scroll', this._onscroll);
102 };
Akronc296ca22018-04-24 16:35:26 +0200103 this.element().blur();
Akron257aa852018-02-06 19:29:51 +0100104 },
105
106 _repos : function (e) {
Akronbf713fc2020-10-13 10:44:35 +0200107 const bounding = e.getBoundingClientRect();
Akron24aa0052020-11-10 11:00:34 +0100108 this._el.style.left = bounding.left + "px";
109 this._el.style.top = (
Akron257aa852018-02-06 19:29:51 +0100110 bounding.top +
111 bounding.height -
Akron24aa0052020-11-10 11:00:34 +0100112 this._el.clientHeight
Akron257aa852018-02-06 19:29:51 +0100113 ) + "px";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000114 }
115 };
116});