blob: f7acfb7d08bb08cdb5f02151ef091c9af2592e44 [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) {
14 var e = document.createElement('div');
15 var 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 */
hebasta40a85cf2020-07-15 18:10:08 +020052 // TODO: Accept an object instead of a list
53 add : function (title, classes, cb, icon) {
Akrondefa5e82018-07-10 12:09:46 +020054 var b = this._element.addE('span');
Akrondefa5e82018-07-10 12:09:46 +020055 b.setAttribute('title',title);
56 if (classes !== undefined) {
57 b.classList.add.apply(b.classList, classes);
58 };
hebasta40a85cf2020-07-15 18:10:08 +020059
60 if (icon !== undefined){
61 b.setAttribute('data-icon', icon);
62 };
63
Akronbec4a6a2018-07-10 14:45:15 +020064 b.addE('span').addT(title);
Akrondefa5e82018-07-10 12:09:46 +020065
66 var that = this;
67 b.addEventListener('click', function (e) {
68
69 // Do not bubble
70 e.halt();
71
72 // Call callback
Akron52ed22d2018-07-11 17:05:19 +020073 var obj = that._bind || this;
74 obj.button = b;
75 cb.apply(obj, e)
Akrondefa5e82018-07-10 12:09:46 +020076 });
Akron52ed22d2018-07-11 17:05:19 +020077
78 return b;
Akrondefa5e82018-07-10 12:09:46 +020079 },
80
Akrond141a362018-07-10 18:12:13 +020081
Akrondefa5e82018-07-10 12:09:46 +020082 /**
Akron52ed22d2018-07-11 17:05:19 +020083 * Add button that spawns a list in order.
hebasta40a85cf2020-07-15 18:10:08 +020084 *
Akron52ed22d2018-07-11 17:05:19 +020085 * Returns the list object.
86 */
Akron3b253d32018-07-15 10:16:06 +020087 addList : function (title, classes, itemClass = defaultItemClass) {
88 var list = treeMenuClass.create([], itemClass);
Akron52ed22d2018-07-11 17:05:19 +020089 this.add(title, classes, function (e) {
90 list.show();
91 list.button(this.button);
92 list.focus();
93 });
94
95 return list;
96 },
Akron858cbc82019-12-05 16:53:13 +010097
98 /**
99 * Add button that can toggle a state.
100 * The state has to be a state object.
101 */
102 addToggle : function (title, classes, state) {
103 let b = this._element.addE('span');
104 b.setAttribute('title',title);
105 if (classes !== undefined) {
106 b.classList.add.apply(b.classList, classes);
107 };
108
109 // Set check marker
110 let check = b.addE('span');
111 check.classList.add("check", "button-icon");
112 check.addE('span');
113
114 // Associate this object to state
115 // Add setState method to object
116 check.setState = function (value) {
117 if (value) {
118 this.classList.add("checked");
119 } else {
120 this.classList.remove("checked");
121 }
122 };
123 state.associate(check);
124
125 b.addE('span').addT(title);
126
127 let that = this;
128 b.addEventListener('click', function (e) {
129
130 // Do not bubble
131 e.halt();
132
133 // Toggle state
134 if (state.get()) {
135 state.set(false)
136 } else {
137 state.set(true);
138 }
139 });
140
141 return b;
142 },
Akron52ed22d2018-07-11 17:05:19 +0200143
144 /**
hebasta40a85cf2020-07-15 18:10:08 +0200145 * Bind an object to all callbacks of the button group.
146 * To get the button element inside the callback,
Akron52ed22d2018-07-11 17:05:19 +0200147 * use this.button
Akrondefa5e82018-07-10 12:09:46 +0200148 */
149 bind : function (obj) {
150 if (obj !== undefined) {
151 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200152 return this;
Akrondefa5e82018-07-10 12:09:46 +0200153 };
154 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200155 },
156
157
158 /**
159 * Remove all defined buttons
160 */
161 clear : function () {
162 _removeChildren(this._element);
163 return this;
Akrondefa5e82018-07-10 12:09:46 +0200164 }
165 }
166});