blob: cb6a95c44b7b9ad62f95282cf25bd4f9b3b10909 [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;
16 if (classes !== undefined) {
17 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 */
81 addList : function (title, classes, itemClass) {
82 var list = treeMenuClass.create([], itemClass || defaultItemClass);
83 this.add(title, classes, function (e) {
84 list.show();
85 list.button(this.button);
86 list.focus();
87 });
88
89 return list;
90 },
91
92
93 /**
94 * Bind an object to all callbacks of the button group.
95 * To get the button element inside the callback,
96 * use this.button
Akrondefa5e82018-07-10 12:09:46 +020097 */
98 bind : function (obj) {
99 if (obj !== undefined) {
100 this._bind = obj;
Akronb23e2712018-07-13 18:17:37 +0200101 return this;
Akrondefa5e82018-07-10 12:09:46 +0200102 };
103 return this._bind || this;
Akrond141a362018-07-10 18:12:13 +0200104 },
105
106
107 /**
108 * Remove all defined buttons
109 */
110 clear : function () {
111 _removeChildren(this._element);
112 return this;
Akrondefa5e82018-07-10 12:09:46 +0200113 }
114 }
115});