forked from hendryluk/Dojo-Bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonGroup.js
More file actions
executable file
·141 lines (129 loc) · 5.01 KB
/
ButtonGroup.js
File metadata and controls
executable file
·141 lines (129 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ==========================================================
* ButtonGroup.js v2.0.0
* ==========================================================
* Copyright 2012 xsokev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================== */
define([
"./Support",
"./_BootstrapWidget",
"./Button",
"dojo/_base/declare",
"dijit/_Container",
"dijit/registry",
"dojo/query",
"dojo/on",
"dojo/_base/lang",
"dojo/_base/array",
"dojo/dom-class",
"dojo/NodeList-traverse"
], function (support, _BootstrapWidget, Button, declare, _Container, registry, query, on, lang, array, domClass) {
"use strict";
// module:
// Button
return declare("ButtonGroup", [_BootstrapWidget, _Container], {
// summary:
// Attach event handlers to a group of buttons.
// example:
// | <div class="btn-group" data-dojo-type="ButtonGroup">...</div>
//
// example:
// | new ButtonGroup({mode:"radio"}, dojo.byId("div"));
//
// mode: String
// mode for group of buttons: radio, checkbox. Default is 'checkbox'.
mode: "checkbox",
postCreate:function () {
// summary:
// creates event handler for click event.
// tags:
// private extension
this.own(on(this.domNode, "click", lang.hitch(this, function(e){
var btn = e.target;
if (!domClass.contains(btn, 'btn')){
btn = query(btn).closest('.btn');
}
this.toggle(btn);
})));
},
startup: function(){
// summary:
// finds existing buttons and add them as Button widgets.
// tags:
// private extension
query(".btn", this.domNode).forEach(function(btn){
var foundButton = this.hasChildren && array.filter(this.getChildren(), function(b){ return b.id === btn.id; })[0];
if(foundButton){ this.add(foundButton); } else { this.add(btn); }
}, this);
},
toggle: function(/*HTMLElement*/ btn){
// summary:
// toggles a passed button of a button group
if (btn && this.mode === "radio") {
this.deactivate();
var widget = registry.byNode(btn);
widget && widget.toggle();
}
},
deactivate: function(){
// summary:
// deactivates all buttons of a button group
array.forEach(this.getChildren(), function(btn){
btn.set("active", false);
});
},
add: function(/*Object*/ button){
// summary:
// adds a HTMLElement button or Button widget to the button group
// button: HTMLElement Button
// the button widget or element that is to be added to the group
// returns:
// instance of the ButtonGroup
if (button.isInstanceOf && button.isInstanceOf(Button)) {
button.set("toggleable", true);
if(array.filter(this.getChildren(), function(b){ return b.id === button.id; }).length === 0){
this.addChild(button);
}
} else if(support.isElement(button)) {
this.addChild(new Button({toggleable:true}, button));
} else {
throw "Invalid button! Cannot add to button group.";
}
return this;
},
remove: function(/*Object*/ button){
// summary:
// adds a HTMLElement button or Button widget to the button group
// button: HTMLElement Number
// the button widget or index of the widget that is to be deleted from the group
if(button === null){ return; }
if(typeof button === "number"){
button = this.getChildren()[button];
}
if (button.isInstanceOf && button.isInstanceOf(Button)) {
this.removeChild(button);
button.destroy();
}
},
getActive: function(){
// summary:
// returns a list of selected(active) Buttons.
// returns:
// Array of active Buttons
return array.filter(this.getChildren(), function(btn){
return btn.get("active") === true;
});
}
});
});