blob: 461556493da3035b1b0e5619674791490293e848 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001 /**
Akron52ed22d2018-07-11 17:05:19 +02002 * Menu to choose from in a button group.
Nils Diewald0e6992a2015-04-14 20:13:52 +00003 */
Akron52ed22d2018-07-11 17:05:19 +02004define(['menu'], function (menuClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +00005 "use strict";
6
7 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +00008
9 /**
10 * Create new menu object.
11 * Pass the match information object
12 * and the item parameters.
13 *
14 * @param info The match info object
15 * @param params The match menu items
16 * as an array of arrays.
17 */
Akron52ed22d2018-07-11 17:05:19 +020018 create : function (list, itemClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 var obj = Object.create(menuClass)
Akrond67d45b2017-05-18 21:47:38 +020020 .upgradeTo(this)
Akron0988d882017-11-10 16:13:12 +010021 ._init(list, {itemClass : itemClass});
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 obj.limit(6);
Akroneaba63e2018-01-26 19:49:30 +010023
24 var e = obj.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000025
26 // This is only domspecific
Akroneaba63e2018-01-26 19:49:30 +010027 e.addEventListener('blur', function (e) {
Akrond67d45b2017-05-18 21:47:38 +020028 this.menu.hide();
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 });
Akroneaba63e2018-01-26 19:49:30 +010030
Akron52ed22d2018-07-11 17:05:19 +020031 e.classList.add('button-group-list');
Akroneaba63e2018-01-26 19:49:30 +010032
33 // Add menu to body
34 document.getElementsByTagName('body')[0].appendChild(e);
35
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 return obj;
37 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000038
39 /**
40 * The match information object of the menu.
Akron52ed22d2018-07-11 17:05:19 +020041 * TODO:
42 * Rename to 'Panel'
Nils Diewald7148c6f2015-05-04 15:07:53 +000043 */
Akroneaba63e2018-01-26 19:49:30 +010044 info :function (infoVar) {
45 if (infoVar !== undefined)
46 this._info = infoVar;
47
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 return this._info;
Akroneaba63e2018-01-26 19:49:30 +010049 },
50
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
Akron257aa852018-02-06 19:29:51 +010056 // this._attached = e;
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
75
76 // 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) {
Akroneaba63e2018-01-26 19:49:30 +010087 var bounding = e.getBoundingClientRect();
88 this._element.style.left = bounding.left + "px";
Akron257aa852018-02-06 19:29:51 +010089 this._element.style.top = (
90 bounding.top +
91 bounding.height -
92 this._element.clientHeight
93 ) + "px";
Nils Diewald0e6992a2015-04-14 20:13:52 +000094 }
95 };
96});