blob: c6a772e8f78bb96eb2f947237b4f33eeab9e1def [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 },
Akronf8af3b82021-07-21 20:24:00 +020012
13 /**
14 * Adopt existing button group element
15 */
16 adopt : function (element) {
17 const obj = Object.create(this);
18 obj._el = element;
19 return obj;
20 },
21
Akrondefa5e82018-07-10 12:09:46 +020022 // Initialize button group
23 _init : function (classes) {
Akronbf713fc2020-10-13 10:44:35 +020024 const e = document.createElement('div');
25 const cl = e.classList;
Akron4d926f12018-07-16 15:30:25 +020026 if (classes) {
Akrondefa5e82018-07-10 12:09:46 +020027 cl.add.apply(cl,classes);
Akron4510a3e2021-09-10 15:09:56 +020028 classes.forEach(i => {
29 switch (i) {
30 case "open-menu-below" : {
31 this._omBelow = true;
32 break;
33 }
34 case "open-menu-outside" : {
35 this._omOutside = true;
36 break;
37 }
38 case "open-menu-left" : {
39 this._omLeft = true;
40 }
41 }
42 })
Akrondefa5e82018-07-10 12:09:46 +020043 };
44 cl.add('button-group');
Akron24aa0052020-11-10 11:00:34 +010045 this._el = e;
Akrondefa5e82018-07-10 12:09:46 +020046 return this;
47 },
Akrond141a362018-07-10 18:12:13 +020048
Akrondefa5e82018-07-10 12:09:46 +020049 /**
50 * Return main element
51 */
52 element : function () {
Akron24aa0052020-11-10 11:00:34 +010053 return this._el;
Akrondefa5e82018-07-10 12:09:46 +020054 },
55
Akronf8af3b82021-07-21 20:24:00 +020056 /**
57 * Define element following newly added buttons.
58 */
59 anchor : function (anchor) {
Akronc8c8bf12021-09-24 11:30:45 +020060 if (anchor !== null && anchor.parentNode == this._el) {
Akronf8af3b82021-07-21 20:24:00 +020061 this._anchor = anchor;
62 return true;
63 };
64 return false;
65 },
Akrond141a362018-07-10 18:12:13 +020066
Akrond141a362018-07-10 18:12:13 +020067
Akronf8af3b82021-07-21 20:24:00 +020068 _insert : function (tag = 'span') {
69 const span = document.createElement(tag);
70 if (this._anchor) {
71 this._el.insertBefore(span, this._anchor);
72 return span;
73 }
74 return this._el.appendChild(span);
75 },
Akrond141a362018-07-10 18:12:13 +020076
Akrondefa5e82018-07-10 12:09:46 +020077 /**
78 * Add button in order
hebasta40a85cf2020-07-15 18:10:08 +020079 *
Akron52ed22d2018-07-11 17:05:19 +020080 * Returns the button element
Akrondefa5e82018-07-10 12:09:46 +020081 */
Akron792b1a42020-09-14 18:56:38 +020082 add : function (title, data, cb) {
Akronf8af3b82021-07-21 20:24:00 +020083 const b = this._insert('span');
Akron83a58bc2024-11-08 09:55:19 +010084 let desc = title;
Akron792b1a42020-09-14 18:56:38 +020085
86 if (data !== undefined) {
87 if (data['cls'] !== undefined) {
88 b.classList.add.apply(b.classList, data['cls']);
89 };
hebasta40a85cf2020-07-15 18:10:08 +020090
Akronba09ed22020-10-01 16:01:45 +020091 if (data['icon'] !== undefined) {
Akron792b1a42020-09-14 18:56:38 +020092 b.setAttribute('data-icon', data['icon']);
93 };
Akronba09ed22020-10-01 16:01:45 +020094
95 if (data['state'] !== undefined) {
96 b['state'] = data['state'];
Akron83a58bc2024-11-08 09:55:19 +010097 };
98
99 if (data['desc'] !== undefined) {
100 desc = data['desc'];
101 };
hebasta40a85cf2020-07-15 18:10:08 +0200102 };
Akron83a58bc2024-11-08 09:55:19 +0100103
104 b.setAttribute('title', desc);
Akronbec4a6a2018-07-10 14:45:15 +0200105 b.addE('span').addT(title);
Akrondefa5e82018-07-10 12:09:46 +0200106
Akronbf713fc2020-10-13 10:44:35 +0200107 let that = this;
Akrondefa5e82018-07-10 12:09:46 +0200108 b.addEventListener('click', function (e) {
109
110 // Do not bubble
111 e.halt();
112
113 // Call callback
Akronbf713fc2020-10-13 10:44:35 +0200114 let obj = that._bind || this;
Akron52ed22d2018-07-11 17:05:19 +0200115 obj.button = b;
116 cb.apply(obj, e)
Akrondefa5e82018-07-10 12:09:46 +0200117 });
Akron52ed22d2018-07-11 17:05:19 +0200118
119 return b;
Akrondefa5e82018-07-10 12:09:46 +0200120 },
121
Akrond141a362018-07-10 18:12:13 +0200122
Akrondefa5e82018-07-10 12:09:46 +0200123 /**
Akron52ed22d2018-07-11 17:05:19 +0200124 * Add button that spawns a list in order.
hebasta40a85cf2020-07-15 18:10:08 +0200125 *
Akron52ed22d2018-07-11 17:05:19 +0200126 * Returns the list object.
127 */
Akron792b1a42020-09-14 18:56:38 +0200128 addList : function (title, data, itemClass = defaultItemClass) {
Akronbf713fc2020-10-13 10:44:35 +0200129 const list = treeMenuClass.create([], itemClass);
Akron4510a3e2021-09-10 15:09:56 +0200130
131 list.openAt(
132 this._omLeft ? true : false,
133 this._omBelow ? true : false,
134 this._omOutside ? true : false,
135 );
136
Akron644ad9f2021-07-26 16:12:59 +0200137 let b = this.add(title, data, function (e) {
Akron52ed22d2018-07-11 17:05:19 +0200138 list.show();
139 list.button(this.button);
140 list.focus();
141 });
Akron644ad9f2021-07-26 16:12:59 +0200142 b.list = list;
143 return b;
Akron52ed22d2018-07-11 17:05:19 +0200144 },
Akron858cbc82019-12-05 16:53:13 +0100145
146 /**
147 * Add button that can toggle a state.
148 * The state has to be a state object.
149 */
Akron37ea1192021-07-28 10:40:14 +0200150 /*
151 * addToggle() should be removed in favor of add(toggleObj)
152 * or similar, so the API of buttongroups and lists is similar
153 * for use as action plugins.
154 */
Akron792b1a42020-09-14 18:56:38 +0200155 addToggle : function (title, data, state) {
Akronf8af3b82021-07-21 20:24:00 +0200156 const b = this._insert('span');
Akron83a58bc2024-11-08 09:55:19 +0100157 let desc = title;
Akron792b1a42020-09-14 18:56:38 +0200158
159 if (data != undefined) {
160 if (data['cls'] !== undefined) {
Akronbf713fc2020-10-13 10:44:35 +0200161 b.classList.add.apply(
162 b.classList,
163 data['cls']
164 );
Akron792b1a42020-09-14 18:56:38 +0200165 };
Akron83a58bc2024-11-08 09:55:19 +0100166
167 if (data['icon'] !== undefined) {
168 b.setAttribute('data-icon', data['icon']);
169 };
170
171 if (data['desc'] !== undefined) {
172 desc = data['desc'];
173 };
Akron858cbc82019-12-05 16:53:13 +0100174 };
175
Akron83a58bc2024-11-08 09:55:19 +0100176 b.setAttribute('title',desc);
177
Akron858cbc82019-12-05 16:53:13 +0100178 // Set check marker
Akronbf713fc2020-10-13 10:44:35 +0200179 const check = b.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100180 check.classList.add("check", "button-icon");
181 check.addE('span');
182
183 // Associate this object to state
184 // Add setState method to object
185 check.setState = function (value) {
186 if (value) {
187 this.classList.add("checked");
188 } else {
189 this.classList.remove("checked");
190 }
191 };
192 state.associate(check);
193
194 b.addE('span').addT(title);
195
196 let that = this;
197 b.addEventListener('click', function (e) {
198
199 // Do not bubble
200 e.halt();
201
202 // Toggle state
Akron237abc42020-10-07 14:14:52 +0200203 state.roll();
Akron858cbc82019-12-05 16:53:13 +0100204 });
205
206 return b;
207 },
Akron52ed22d2018-07-11 17:05:19 +0200208
209 /**
hebasta40a85cf2020-07-15 18:10:08 +0200210 * Bind an object to all callbacks of the button group.
211 * To get the button element inside the callback,
Akron52ed22d2018-07-11 17:05:19 +0200212 * use this.button
Akrondefa5e82018-07-10 12:09:46 +0200213 */
214 bind : function (obj) {
215 if (obj !== undefined) {
216 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200217 return this;
Akrondefa5e82018-07-10 12:09:46 +0200218 };
219 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200220 },
221
222
223 /**
224 * Remove all defined buttons
225 */
226 clear : function () {
Akron24aa0052020-11-10 11:00:34 +0100227 _removeChildren(this._el);
Akrond141a362018-07-10 18:12:13 +0200228 return this;
Akrondefa5e82018-07-10 12:09:46 +0200229 }
230 }
231});