blob: 4a4abfc044d4e214fa41723e36414248feb1f68e [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
Akron37ea1192021-07-28 10:40:14 +02006// TODO:
7// Add addToggle() method
8
Akron52ed22d2018-07-11 17:05:19 +02009define(['menu'], function (menuClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000010
11 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000012
13 /**
14 * Create new menu object.
Akron20b6d312021-07-26 15:14:42 +020015 * Pass the list of items and the itemClass.
Nils Diewald7148c6f2015-05-04 15:07:53 +000016 */
Akron52ed22d2018-07-11 17:05:19 +020017 create : function (list, itemClass) {
Akronbf713fc2020-10-13 10:44:35 +020018 const obj = Object.create(menuClass)
19 .upgradeTo(this)
20 ._init(list, {itemClass : itemClass});
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 obj.limit(6);
Akroneaba63e2018-01-26 19:49:30 +010022
Akronbf713fc2020-10-13 10:44:35 +020023 const e = obj.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000024
25 // This is only domspecific
Akroneaba63e2018-01-26 19:49:30 +010026 e.addEventListener('blur', function (e) {
Akron20b6d312021-07-26 15:14:42 +020027 this.menu.hide();
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 });
Akroneaba63e2018-01-26 19:49:30 +010029
Akron52ed22d2018-07-11 17:05:19 +020030 e.classList.add('button-group-list');
Akroneaba63e2018-01-26 19:49:30 +010031
32 // Add menu to body
33 document.getElementsByTagName('body')[0].appendChild(e);
34
Nils Diewald0e6992a2015-04-14 20:13:52 +000035 return obj;
36 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000037
Akronbf713fc2020-10-13 10:44:35 +020038
Nils Diewald7148c6f2015-05-04 15:07:53 +000039 /**
Akron20b6d312021-07-26 15:14:42 +020040 * The panel object of the menu,
41 * in case the menu was spawned by a panel.
Nils Diewald7148c6f2015-05-04 15:07:53 +000042 */
Akron20b6d312021-07-26 15:14:42 +020043 panel : function (panelVar) {
Akron7f9a6a32018-07-18 15:05:23 +020044 if (panelVar !== undefined)
45 this._panel = panelVar;
Akroneaba63e2018-01-26 19:49:30 +010046
Akron7f9a6a32018-07-18 15:05:23 +020047 return this._panel;
Akroneaba63e2018-01-26 19:49:30 +010048 },
49
Akronbf713fc2020-10-13 10:44:35 +020050
Akronbec4a6a2018-07-10 14:45:15 +020051 // Attach menu to button
Akron52ed22d2018-07-11 17:05:19 +020052 button : function (btn) {
Akron257aa852018-02-06 19:29:51 +010053
Akron1801c5c2018-07-16 18:15:48 +020054 this._button = btn;
55
Akron1801c5c2018-07-16 18:15:48 +020056 this._repos(this._button);
Akron257aa852018-02-06 19:29:51 +010057 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 () {
Akron1801c5c2018-07-16 18:15:48 +020068 this._repos(this._button);
Akron257aa852018-02-06 19:29:51 +010069 }.bind(this);
70
71 window.addEventListener('scroll', this._onscroll);
72 },
73
Akron20b6d312021-07-26 15:14:42 +020074
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 },
Akronbf713fc2020-10-13 10:44:35 +020098
Akron257aa852018-02-06 19:29:51 +010099 // Overwrite onHide method
100 onHide : function () {
101
102 // Remove listener
103 if (this._onscroll !== undefined) {
104 window.removeEventListener('scroll', this._onscroll);
105 };
Akronc296ca22018-04-24 16:35:26 +0200106 this.element().blur();
Akron257aa852018-02-06 19:29:51 +0100107 },
108
109 _repos : function (e) {
Akronbf713fc2020-10-13 10:44:35 +0200110 const bounding = e.getBoundingClientRect();
Akron24aa0052020-11-10 11:00:34 +0100111 this._el.style.left = bounding.left + "px";
112 this._el.style.top = (
Akron257aa852018-02-06 19:29:51 +0100113 bounding.top +
114 bounding.height -
Akron24aa0052020-11-10 11:00:34 +0100115 this._el.clientHeight
Akron257aa852018-02-06 19:29:51 +0100116 ) + "px";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117 }
118 };
119});