blob: 7c8d8c6951386d991a7038e7633bd384b65b8119 [file] [log] [blame]
Akron52ed22d2018-07-11 17:05:19 +02001define(['buttongroup/menu','menu/item','util'], function (treeMenuClass, defaultItemClass) {
2 "use strict";
3
Akrondefa5e82018-07-10 12:09:46 +02004 return {
5 /**
6 * Create button group
7 */
8 create : function (classes) {
9 return Object.create(this)._init(classes);
10 },
11
12 // Initialize button group
13 _init : function (classes) {
Akronbf713fc2020-10-13 10:44:35 +020014 const e = document.createElement('div');
15 const cl = e.classList;
Akron4d926f12018-07-16 15:30:25 +020016 if (classes) {
Akrondefa5e82018-07-10 12:09:46 +020017 cl.add.apply(cl,classes);
18 };
19 cl.add('button-group');
20 this._element = e;
21 return this;
22 },
23
Akrond141a362018-07-10 18:12:13 +020024
Akrondefa5e82018-07-10 12:09:46 +020025 /**
26 * Return main element
27 */
28 element : function () {
29 return this._element;
30 },
31
Akrond141a362018-07-10 18:12:13 +020032
33 /**
hebasta40a85cf2020-07-15 18:10:08 +020034 * Upgrade this object to another object,
Akrond141a362018-07-10 18:12:13 +020035 * while private data stays intact.
hebasta40a85cf2020-07-15 18:10:08 +020036 *
Akrond141a362018-07-10 18:12:13 +020037 * @param {Object} An object with properties.
38 */
39 upgradeTo : function (props) {
40 for (var prop in props) {
41 this[prop] = props[prop];
42 };
43 return this;
44 },
45
46
Akrondefa5e82018-07-10 12:09:46 +020047 /**
48 * Add button in order
hebasta40a85cf2020-07-15 18:10:08 +020049 *
Akron52ed22d2018-07-11 17:05:19 +020050 * Returns the button element
Akrondefa5e82018-07-10 12:09:46 +020051 */
Akron792b1a42020-09-14 18:56:38 +020052 add : function (title, data, cb) {
53
Akronbf713fc2020-10-13 10:44:35 +020054 const b = this._element.addE('span');
Akrondefa5e82018-07-10 12:09:46 +020055 b.setAttribute('title',title);
Akron792b1a42020-09-14 18:56:38 +020056
57 if (data !== undefined) {
58 if (data['cls'] !== undefined) {
59 b.classList.add.apply(b.classList, data['cls']);
60 };
hebasta40a85cf2020-07-15 18:10:08 +020061
Akronba09ed22020-10-01 16:01:45 +020062 if (data['icon'] !== undefined) {
Akron792b1a42020-09-14 18:56:38 +020063 b.setAttribute('data-icon', data['icon']);
64 };
Akronba09ed22020-10-01 16:01:45 +020065
66 if (data['state'] !== undefined) {
67 b['state'] = data['state'];
68 }
hebasta40a85cf2020-07-15 18:10:08 +020069 };
70
Akronbec4a6a2018-07-10 14:45:15 +020071 b.addE('span').addT(title);
Akrondefa5e82018-07-10 12:09:46 +020072
Akronbf713fc2020-10-13 10:44:35 +020073 let that = this;
Akrondefa5e82018-07-10 12:09:46 +020074 b.addEventListener('click', function (e) {
75
76 // Do not bubble
77 e.halt();
78
79 // Call callback
Akronbf713fc2020-10-13 10:44:35 +020080 let obj = that._bind || this;
Akron52ed22d2018-07-11 17:05:19 +020081 obj.button = b;
82 cb.apply(obj, e)
Akrondefa5e82018-07-10 12:09:46 +020083 });
Akron52ed22d2018-07-11 17:05:19 +020084
85 return b;
Akrondefa5e82018-07-10 12:09:46 +020086 },
87
Akrond141a362018-07-10 18:12:13 +020088
Akrondefa5e82018-07-10 12:09:46 +020089 /**
Akron52ed22d2018-07-11 17:05:19 +020090 * Add button that spawns a list in order.
hebasta40a85cf2020-07-15 18:10:08 +020091 *
Akron52ed22d2018-07-11 17:05:19 +020092 * Returns the list object.
93 */
Akron792b1a42020-09-14 18:56:38 +020094 addList : function (title, data, itemClass = defaultItemClass) {
Akronbf713fc2020-10-13 10:44:35 +020095 const list = treeMenuClass.create([], itemClass);
Akron792b1a42020-09-14 18:56:38 +020096 this.add(title, data, function (e) {
Akron52ed22d2018-07-11 17:05:19 +020097 list.show();
98 list.button(this.button);
99 list.focus();
100 });
Akron52ed22d2018-07-11 17:05:19 +0200101 return list;
102 },
Akron858cbc82019-12-05 16:53:13 +0100103
104 /**
105 * Add button that can toggle a state.
106 * The state has to be a state object.
107 */
Akron792b1a42020-09-14 18:56:38 +0200108 addToggle : function (title, data, state) {
Akronbf713fc2020-10-13 10:44:35 +0200109 const b = this._element.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100110 b.setAttribute('title',title);
Akron792b1a42020-09-14 18:56:38 +0200111
112 if (data != undefined) {
113 if (data['cls'] !== undefined) {
Akronbf713fc2020-10-13 10:44:35 +0200114 b.classList.add.apply(
115 b.classList,
116 data['cls']
117 );
Akron792b1a42020-09-14 18:56:38 +0200118 };
Akron858cbc82019-12-05 16:53:13 +0100119 };
120
121 // Set check marker
Akronbf713fc2020-10-13 10:44:35 +0200122 const check = b.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100123 check.classList.add("check", "button-icon");
124 check.addE('span');
125
126 // Associate this object to state
127 // Add setState method to object
128 check.setState = function (value) {
129 if (value) {
130 this.classList.add("checked");
131 } else {
132 this.classList.remove("checked");
133 }
134 };
135 state.associate(check);
136
137 b.addE('span').addT(title);
138
139 let that = this;
140 b.addEventListener('click', function (e) {
141
142 // Do not bubble
143 e.halt();
144
145 // Toggle state
Akron237abc42020-10-07 14:14:52 +0200146 state.roll();
Akron858cbc82019-12-05 16:53:13 +0100147 });
148
149 return b;
150 },
Akron52ed22d2018-07-11 17:05:19 +0200151
152 /**
hebasta40a85cf2020-07-15 18:10:08 +0200153 * Bind an object to all callbacks of the button group.
154 * To get the button element inside the callback,
Akron52ed22d2018-07-11 17:05:19 +0200155 * use this.button
Akrondefa5e82018-07-10 12:09:46 +0200156 */
157 bind : function (obj) {
158 if (obj !== undefined) {
159 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200160 return this;
Akrondefa5e82018-07-10 12:09:46 +0200161 };
162 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200163 },
164
165
166 /**
167 * Remove all defined buttons
168 */
169 clear : function () {
170 _removeChildren(this._element);
171 return this;
Akrondefa5e82018-07-10 12:09:46 +0200172 }
173 }
174});