blob: dabe710df78436678eb5c4a9571ff9fd59e260ba [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
67 /**
hebasta40a85cf2020-07-15 18:10:08 +020068 * Upgrade this object to another object,
Akrond141a362018-07-10 18:12:13 +020069 * while private data stays intact.
hebasta40a85cf2020-07-15 18:10:08 +020070 *
Akrond141a362018-07-10 18:12:13 +020071 * @param {Object} An object with properties.
72 */
73 upgradeTo : function (props) {
74 for (var prop in props) {
75 this[prop] = props[prop];
76 };
77 return this;
78 },
79
Akronf8af3b82021-07-21 20:24:00 +020080 _insert : function (tag = 'span') {
81 const span = document.createElement(tag);
82 if (this._anchor) {
83 this._el.insertBefore(span, this._anchor);
84 return span;
85 }
86 return this._el.appendChild(span);
87 },
Akrond141a362018-07-10 18:12:13 +020088
Akrondefa5e82018-07-10 12:09:46 +020089 /**
90 * Add button in order
hebasta40a85cf2020-07-15 18:10:08 +020091 *
Akron52ed22d2018-07-11 17:05:19 +020092 * Returns the button element
Akrondefa5e82018-07-10 12:09:46 +020093 */
Akron792b1a42020-09-14 18:56:38 +020094 add : function (title, data, cb) {
Akronf8af3b82021-07-21 20:24:00 +020095 const b = this._insert('span');
Akrondefa5e82018-07-10 12:09:46 +020096 b.setAttribute('title',title);
Akron792b1a42020-09-14 18:56:38 +020097
98 if (data !== undefined) {
99 if (data['cls'] !== undefined) {
100 b.classList.add.apply(b.classList, data['cls']);
101 };
hebasta40a85cf2020-07-15 18:10:08 +0200102
Akronba09ed22020-10-01 16:01:45 +0200103 if (data['icon'] !== undefined) {
Akron792b1a42020-09-14 18:56:38 +0200104 b.setAttribute('data-icon', data['icon']);
105 };
Akronba09ed22020-10-01 16:01:45 +0200106
107 if (data['state'] !== undefined) {
108 b['state'] = data['state'];
109 }
hebasta40a85cf2020-07-15 18:10:08 +0200110 };
111
Akronbec4a6a2018-07-10 14:45:15 +0200112 b.addE('span').addT(title);
Akrondefa5e82018-07-10 12:09:46 +0200113
Akronbf713fc2020-10-13 10:44:35 +0200114 let that = this;
Akrondefa5e82018-07-10 12:09:46 +0200115 b.addEventListener('click', function (e) {
116
117 // Do not bubble
118 e.halt();
119
120 // Call callback
Akronbf713fc2020-10-13 10:44:35 +0200121 let obj = that._bind || this;
Akron52ed22d2018-07-11 17:05:19 +0200122 obj.button = b;
123 cb.apply(obj, e)
Akrondefa5e82018-07-10 12:09:46 +0200124 });
Akron52ed22d2018-07-11 17:05:19 +0200125
126 return b;
Akrondefa5e82018-07-10 12:09:46 +0200127 },
128
Akrond141a362018-07-10 18:12:13 +0200129
Akrondefa5e82018-07-10 12:09:46 +0200130 /**
Akron52ed22d2018-07-11 17:05:19 +0200131 * Add button that spawns a list in order.
hebasta40a85cf2020-07-15 18:10:08 +0200132 *
Akron52ed22d2018-07-11 17:05:19 +0200133 * Returns the list object.
134 */
Akron792b1a42020-09-14 18:56:38 +0200135 addList : function (title, data, itemClass = defaultItemClass) {
Akronbf713fc2020-10-13 10:44:35 +0200136 const list = treeMenuClass.create([], itemClass);
Akron4510a3e2021-09-10 15:09:56 +0200137
138 list.openAt(
139 this._omLeft ? true : false,
140 this._omBelow ? true : false,
141 this._omOutside ? true : false,
142 );
143
Akron792b1a42020-09-14 18:56:38 +0200144 this.add(title, data, function (e) {
Akron52ed22d2018-07-11 17:05:19 +0200145 list.show();
146 list.button(this.button);
147 list.focus();
148 });
Akron52ed22d2018-07-11 17:05:19 +0200149 return list;
150 },
Akron858cbc82019-12-05 16:53:13 +0100151
152 /**
153 * Add button that can toggle a state.
154 * The state has to be a state object.
155 */
Akron37ea1192021-07-28 10:40:14 +0200156 /*
157 * addToggle() should be removed in favor of add(toggleObj)
158 * or similar, so the API of buttongroups and lists is similar
159 * for use as action plugins.
160 */
Akron792b1a42020-09-14 18:56:38 +0200161 addToggle : function (title, data, state) {
Akronf8af3b82021-07-21 20:24:00 +0200162 const b = this._insert('span');
Akron858cbc82019-12-05 16:53:13 +0100163 b.setAttribute('title',title);
Akron792b1a42020-09-14 18:56:38 +0200164
165 if (data != undefined) {
166 if (data['cls'] !== undefined) {
Akronbf713fc2020-10-13 10:44:35 +0200167 b.classList.add.apply(
168 b.classList,
169 data['cls']
170 );
Akron792b1a42020-09-14 18:56:38 +0200171 };
Akron858cbc82019-12-05 16:53:13 +0100172 };
173
174 // Set check marker
Akronbf713fc2020-10-13 10:44:35 +0200175 const check = b.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100176 check.classList.add("check", "button-icon");
177 check.addE('span');
178
179 // Associate this object to state
180 // Add setState method to object
181 check.setState = function (value) {
182 if (value) {
183 this.classList.add("checked");
184 } else {
185 this.classList.remove("checked");
186 }
187 };
188 state.associate(check);
189
190 b.addE('span').addT(title);
191
192 let that = this;
193 b.addEventListener('click', function (e) {
194
195 // Do not bubble
196 e.halt();
197
198 // Toggle state
Akron237abc42020-10-07 14:14:52 +0200199 state.roll();
Akron858cbc82019-12-05 16:53:13 +0100200 });
201
202 return b;
203 },
Akron52ed22d2018-07-11 17:05:19 +0200204
205 /**
hebasta40a85cf2020-07-15 18:10:08 +0200206 * Bind an object to all callbacks of the button group.
207 * To get the button element inside the callback,
Akron52ed22d2018-07-11 17:05:19 +0200208 * use this.button
Akrondefa5e82018-07-10 12:09:46 +0200209 */
210 bind : function (obj) {
211 if (obj !== undefined) {
212 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200213 return this;
Akrondefa5e82018-07-10 12:09:46 +0200214 };
215 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200216 },
217
218
219 /**
220 * Remove all defined buttons
221 */
222 clear : function () {
Akron24aa0052020-11-10 11:00:34 +0100223 _removeChildren(this._el);
Akrond141a362018-07-10 18:12:13 +0200224 return this;
Akrondefa5e82018-07-10 12:09:46 +0200225 }
226 }
227});