blob: 12870b5a02f9d06cabe696830bfb7e1a75d247cd [file] [log] [blame]
Akrondefa5e82018-07-10 12:09:46 +02001define(['util'], function () {
2 return {
3 /**
4 * Create button group
5 */
6 create : function (classes) {
7 return Object.create(this)._init(classes);
8 },
9
10 // Initialize button group
11 _init : function (classes) {
12 var e = document.createElement('div');
13 var cl = e.classList;
14 if (classes !== undefined) {
15 cl.add.apply(cl,classes);
16 };
17 cl.add('button-group');
18 this._element = e;
19 return this;
20 },
21
Akrond141a362018-07-10 18:12:13 +020022
Akrondefa5e82018-07-10 12:09:46 +020023 /**
24 * Return main element
25 */
26 element : function () {
27 return this._element;
28 },
29
Akrond141a362018-07-10 18:12:13 +020030
31 /**
32 * Upgrade this object to another object,
33 * while private data stays intact.
34 *
35 * @param {Object} An object with properties.
36 */
37 upgradeTo : function (props) {
38 for (var prop in props) {
39 this[prop] = props[prop];
40 };
41 return this;
42 },
43
44
Akrondefa5e82018-07-10 12:09:46 +020045 /**
46 * Add button in order
47 */
48 add : function (title, classes, cb) {
49 var b = this._element.addE('span');
Akrondefa5e82018-07-10 12:09:46 +020050 b.setAttribute('title',title);
51 if (classes !== undefined) {
52 b.classList.add.apply(b.classList, classes);
53 };
Akronbec4a6a2018-07-10 14:45:15 +020054 b.addE('span').addT(title);
Akrondefa5e82018-07-10 12:09:46 +020055
56 var that = this;
57 b.addEventListener('click', function (e) {
58
59 // Do not bubble
60 e.halt();
61
62 // Call callback
63 cb.apply(that._bind || this, e)
64 });
65 },
66
Akrond141a362018-07-10 18:12:13 +020067
Akrondefa5e82018-07-10 12:09:46 +020068 /**
69 * Bind an object to all callbacks of the button group
70 */
71 bind : function (obj) {
72 if (obj !== undefined) {
73 this._bind = obj;
74 };
75 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +020076 },
77
78
79 /**
80 * Remove all defined buttons
81 */
82 clear : function () {
83 _removeChildren(this._element);
84 return this;
Akrondefa5e82018-07-10 12:09:46 +020085 }
86 }
87});