blob: 85543e406f079354051c144937ace279254f35f1 [file] [log] [blame]
Akrone51eaa32020-11-10 09:35:53 +01001"use strict";
2
Akron52ed22d2018-07-11 17:05:19 +02003define(['buttongroup/menu','menu/item','util'], function (treeMenuClass, defaultItemClass) {
Akron52ed22d2018-07-11 17:05:19 +02004
Akrondefa5e82018-07-10 12:09:46 +02005 return {
6 /**
7 * Create button group
8 */
9 create : function (classes) {
10 return Object.create(this)._init(classes);
11 },
12
13 // Initialize button group
14 _init : function (classes) {
Akronbf713fc2020-10-13 10:44:35 +020015 const e = document.createElement('div');
16 const cl = e.classList;
Akron4d926f12018-07-16 15:30:25 +020017 if (classes) {
Akrondefa5e82018-07-10 12:09:46 +020018 cl.add.apply(cl,classes);
19 };
20 cl.add('button-group');
Akron24aa0052020-11-10 11:00:34 +010021 this._el = e;
Akrondefa5e82018-07-10 12:09:46 +020022 return this;
23 },
24
Akrond141a362018-07-10 18:12:13 +020025
Akrondefa5e82018-07-10 12:09:46 +020026 /**
27 * Return main element
28 */
29 element : function () {
Akron24aa0052020-11-10 11:00:34 +010030 return this._el;
Akrondefa5e82018-07-10 12:09:46 +020031 },
32
Akrond141a362018-07-10 18:12:13 +020033
34 /**
hebasta40a85cf2020-07-15 18:10:08 +020035 * Upgrade this object to another object,
Akrond141a362018-07-10 18:12:13 +020036 * while private data stays intact.
hebasta40a85cf2020-07-15 18:10:08 +020037 *
Akrond141a362018-07-10 18:12:13 +020038 * @param {Object} An object with properties.
39 */
40 upgradeTo : function (props) {
41 for (var prop in props) {
42 this[prop] = props[prop];
43 };
44 return this;
45 },
46
47
Akrondefa5e82018-07-10 12:09:46 +020048 /**
49 * Add button in order
hebasta40a85cf2020-07-15 18:10:08 +020050 *
Akron52ed22d2018-07-11 17:05:19 +020051 * Returns the button element
Akrondefa5e82018-07-10 12:09:46 +020052 */
Akron792b1a42020-09-14 18:56:38 +020053 add : function (title, data, cb) {
54
Akron24aa0052020-11-10 11:00:34 +010055 const b = this._el.addE('span');
Akrondefa5e82018-07-10 12:09:46 +020056 b.setAttribute('title',title);
Akron792b1a42020-09-14 18:56:38 +020057
58 if (data !== undefined) {
59 if (data['cls'] !== undefined) {
60 b.classList.add.apply(b.classList, data['cls']);
61 };
hebasta40a85cf2020-07-15 18:10:08 +020062
Akronba09ed22020-10-01 16:01:45 +020063 if (data['icon'] !== undefined) {
Akron792b1a42020-09-14 18:56:38 +020064 b.setAttribute('data-icon', data['icon']);
65 };
Akronba09ed22020-10-01 16:01:45 +020066
67 if (data['state'] !== undefined) {
68 b['state'] = data['state'];
69 }
hebasta40a85cf2020-07-15 18:10:08 +020070 };
71
Akronbec4a6a2018-07-10 14:45:15 +020072 b.addE('span').addT(title);
Akrondefa5e82018-07-10 12:09:46 +020073
Akronbf713fc2020-10-13 10:44:35 +020074 let that = this;
Akrondefa5e82018-07-10 12:09:46 +020075 b.addEventListener('click', function (e) {
76
77 // Do not bubble
78 e.halt();
79
80 // Call callback
Akronbf713fc2020-10-13 10:44:35 +020081 let obj = that._bind || this;
Akron52ed22d2018-07-11 17:05:19 +020082 obj.button = b;
83 cb.apply(obj, e)
Akrondefa5e82018-07-10 12:09:46 +020084 });
Akron52ed22d2018-07-11 17:05:19 +020085
86 return b;
Akrondefa5e82018-07-10 12:09:46 +020087 },
88
Akrond141a362018-07-10 18:12:13 +020089
Akrondefa5e82018-07-10 12:09:46 +020090 /**
Akron52ed22d2018-07-11 17:05:19 +020091 * Add button that spawns a list in order.
hebasta40a85cf2020-07-15 18:10:08 +020092 *
Akron52ed22d2018-07-11 17:05:19 +020093 * Returns the list object.
94 */
Akron792b1a42020-09-14 18:56:38 +020095 addList : function (title, data, itemClass = defaultItemClass) {
Akronbf713fc2020-10-13 10:44:35 +020096 const list = treeMenuClass.create([], itemClass);
Akron792b1a42020-09-14 18:56:38 +020097 this.add(title, data, function (e) {
Akron52ed22d2018-07-11 17:05:19 +020098 list.show();
99 list.button(this.button);
100 list.focus();
101 });
Akron52ed22d2018-07-11 17:05:19 +0200102 return list;
103 },
Akron858cbc82019-12-05 16:53:13 +0100104
105 /**
106 * Add button that can toggle a state.
107 * The state has to be a state object.
108 */
Akron792b1a42020-09-14 18:56:38 +0200109 addToggle : function (title, data, state) {
Akron24aa0052020-11-10 11:00:34 +0100110 const b = this._el.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100111 b.setAttribute('title',title);
Akron792b1a42020-09-14 18:56:38 +0200112
113 if (data != undefined) {
114 if (data['cls'] !== undefined) {
Akronbf713fc2020-10-13 10:44:35 +0200115 b.classList.add.apply(
116 b.classList,
117 data['cls']
118 );
Akron792b1a42020-09-14 18:56:38 +0200119 };
Akron858cbc82019-12-05 16:53:13 +0100120 };
121
122 // Set check marker
Akronbf713fc2020-10-13 10:44:35 +0200123 const check = b.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100124 check.classList.add("check", "button-icon");
125 check.addE('span');
126
127 // Associate this object to state
128 // Add setState method to object
129 check.setState = function (value) {
130 if (value) {
131 this.classList.add("checked");
132 } else {
133 this.classList.remove("checked");
134 }
135 };
136 state.associate(check);
137
138 b.addE('span').addT(title);
139
140 let that = this;
141 b.addEventListener('click', function (e) {
142
143 // Do not bubble
144 e.halt();
145
146 // Toggle state
Akron237abc42020-10-07 14:14:52 +0200147 state.roll();
Akron858cbc82019-12-05 16:53:13 +0100148 });
149
150 return b;
151 },
Akron52ed22d2018-07-11 17:05:19 +0200152
153 /**
hebasta40a85cf2020-07-15 18:10:08 +0200154 * Bind an object to all callbacks of the button group.
155 * To get the button element inside the callback,
Akron52ed22d2018-07-11 17:05:19 +0200156 * use this.button
Akrondefa5e82018-07-10 12:09:46 +0200157 */
158 bind : function (obj) {
159 if (obj !== undefined) {
160 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200161 return this;
Akrondefa5e82018-07-10 12:09:46 +0200162 };
163 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200164 },
165
166
167 /**
168 * Remove all defined buttons
169 */
170 clear : function () {
Akron24aa0052020-11-10 11:00:34 +0100171 _removeChildren(this._el);
Akrond141a362018-07-10 18:12:13 +0200172 return this;
Akrondefa5e82018-07-10 12:09:46 +0200173 }
174 }
175});