blob: 755ad7fd165112d6e07f1d7f1d0a9c92080d9154 [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 */
Akron37ea1192021-07-28 10:40:14 +0200109 /*
110 * addToggle() should be removed in favor of add(toggleObj)
111 * or similar, so the API of buttongroups and lists is similar
112 * for use as action plugins.
113 */
Akron792b1a42020-09-14 18:56:38 +0200114 addToggle : function (title, data, state) {
Akron24aa0052020-11-10 11:00:34 +0100115 const b = this._el.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100116 b.setAttribute('title',title);
Akron792b1a42020-09-14 18:56:38 +0200117
118 if (data != undefined) {
119 if (data['cls'] !== undefined) {
Akronbf713fc2020-10-13 10:44:35 +0200120 b.classList.add.apply(
121 b.classList,
122 data['cls']
123 );
Akron792b1a42020-09-14 18:56:38 +0200124 };
Akron858cbc82019-12-05 16:53:13 +0100125 };
126
127 // Set check marker
Akronbf713fc2020-10-13 10:44:35 +0200128 const check = b.addE('span');
Akron858cbc82019-12-05 16:53:13 +0100129 check.classList.add("check", "button-icon");
130 check.addE('span');
131
132 // Associate this object to state
133 // Add setState method to object
134 check.setState = function (value) {
135 if (value) {
136 this.classList.add("checked");
137 } else {
138 this.classList.remove("checked");
139 }
140 };
141 state.associate(check);
142
143 b.addE('span').addT(title);
144
145 let that = this;
146 b.addEventListener('click', function (e) {
147
148 // Do not bubble
149 e.halt();
150
151 // Toggle state
Akron237abc42020-10-07 14:14:52 +0200152 state.roll();
Akron858cbc82019-12-05 16:53:13 +0100153 });
154
155 return b;
156 },
Akron52ed22d2018-07-11 17:05:19 +0200157
158 /**
hebasta40a85cf2020-07-15 18:10:08 +0200159 * Bind an object to all callbacks of the button group.
160 * To get the button element inside the callback,
Akron52ed22d2018-07-11 17:05:19 +0200161 * use this.button
Akrondefa5e82018-07-10 12:09:46 +0200162 */
163 bind : function (obj) {
164 if (obj !== undefined) {
165 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200166 return this;
Akrondefa5e82018-07-10 12:09:46 +0200167 };
168 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200169 },
170
171
172 /**
173 * Remove all defined buttons
174 */
175 clear : function () {
Akron24aa0052020-11-10 11:00:34 +0100176 _removeChildren(this._el);
Akrond141a362018-07-10 18:12:13 +0200177 return this;
Akrondefa5e82018-07-10 12:09:46 +0200178 }
179 }
180});