blob: 01e5a1cdf33edcf8a2443ad07ed5db26dbdc2960 [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 /**
34 * Upgrade this object to another object,
35 * while private data stays intact.
36 *
37 * @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
Akron52ed22d2018-07-11 17:05:19 +020049 *
50 * Returns the button element
Akrondefa5e82018-07-10 12:09:46 +020051 */
52 add : function (title, classes, cb) {
53 var b = this._element.addE('span');
Akrondefa5e82018-07-10 12:09:46 +020054 b.setAttribute('title',title);
55 if (classes !== undefined) {
56 b.classList.add.apply(b.classList, classes);
57 };
Akronbec4a6a2018-07-10 14:45:15 +020058 b.addE('span').addT(title);
Akrondefa5e82018-07-10 12:09:46 +020059
60 var that = this;
61 b.addEventListener('click', function (e) {
62
63 // Do not bubble
64 e.halt();
65
66 // Call callback
Akron52ed22d2018-07-11 17:05:19 +020067 var obj = that._bind || this;
68 obj.button = b;
69 cb.apply(obj, e)
Akrondefa5e82018-07-10 12:09:46 +020070 });
Akron52ed22d2018-07-11 17:05:19 +020071
72 return b;
Akrondefa5e82018-07-10 12:09:46 +020073 },
74
Akrond141a362018-07-10 18:12:13 +020075
Akrondefa5e82018-07-10 12:09:46 +020076 /**
Akron52ed22d2018-07-11 17:05:19 +020077 * Add button that spawns a list in order.
78 *
79 * Returns the list object.
80 */
Akron3b253d32018-07-15 10:16:06 +020081 addList : function (title, classes, itemClass = defaultItemClass) {
82 var list = treeMenuClass.create([], itemClass);
Akron52ed22d2018-07-11 17:05:19 +020083 this.add(title, classes, function (e) {
84 list.show();
85 list.button(this.button);
86 list.focus();
87 });
88
89 return list;
90 },
Akron858cbc82019-12-05 16:53:13 +010091
92 /**
93 * Add button that can toggle a state.
94 * The state has to be a state object.
95 */
96 addToggle : function (title, classes, state) {
97 let b = this._element.addE('span');
98 b.setAttribute('title',title);
99 if (classes !== undefined) {
100 b.classList.add.apply(b.classList, classes);
101 };
102
103 // Set check marker
104 let check = b.addE('span');
105 check.classList.add("check", "button-icon");
106 check.addE('span');
107
108 // Associate this object to state
109 // Add setState method to object
110 check.setState = function (value) {
111 if (value) {
112 this.classList.add("checked");
113 } else {
114 this.classList.remove("checked");
115 }
116 };
117 state.associate(check);
118
119 b.addE('span').addT(title);
120
121 let that = this;
122 b.addEventListener('click', function (e) {
123
124 // Do not bubble
125 e.halt();
126
127 // Toggle state
128 if (state.get()) {
129 state.set(false)
130 } else {
131 state.set(true);
132 }
133 });
134
135 return b;
136 },
Akron52ed22d2018-07-11 17:05:19 +0200137
138 /**
139 * Bind an object to all callbacks of the button group.
140 * To get the button element inside the callback,
141 * use this.button
Akrondefa5e82018-07-10 12:09:46 +0200142 */
143 bind : function (obj) {
144 if (obj !== undefined) {
145 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200146 return this;
Akrondefa5e82018-07-10 12:09:46 +0200147 };
148 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200149 },
150
151
152 /**
153 * Remove all defined buttons
154 */
155 clear : function () {
156 _removeChildren(this._element);
157 return this;
Akrondefa5e82018-07-10 12:09:46 +0200158 }
159 }
160});