forked from hendryluk/Dojo-Bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollapse.js
More file actions
executable file
·201 lines (184 loc) · 7.57 KB
/
Collapse.js
File metadata and controls
executable file
·201 lines (184 loc) · 7.57 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* ==========================================================
* Collapse.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([
"./CollapseItem",
"./Support",
"./_BootstrapWidget",
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/_base/array",
"dojo/query",
"dojo/on",
"dojo/dom-class",
"dijit/_Container",
"dojo/NodeList-traverse"
], function (CollapseItem, support, _BootstrapWidget, declare, lang, array, query, on, domClass, _Container) {
"use strict";
// module:
// Collapse
var _dimension = function(node){
return domClass.contains(node, 'width') ? 'width' : 'height';
},
_beforeShow = function(targetNode){
if(!this.allowAllOpen && targetNode){
this.getChildren().forEach(function(collapse){
if(collapse._transitioning){ return; }
if(collapse.domNode.id !== targetNode.id){
collapse.hide(true);
}
});
}
},
_removeCollapseItemEvent = function(targetCi, list){
var _list = array.filter(list, function(ci){
return targetCi.id !== ci.id;
});
return _list;
};
return declare("Collapse", [_BootstrapWidget, _Container], {
// summary:
// Provides collapsible support for a widget.
// description:
// This widget provides a Twitter Bootstrap accordion support for collapsing and expanding. It
// manages the number of groups that are open simultaneously and collapses other groups
// appropriately when only group is allowed open at once. It also supports adding and removing
// groups.
//
// ## Events ##
// Call `widget.on("started", func)` to monitor when the widget has finished its startup.
//
// Call `widget.on("show", func)` to monitor when a CollapseItem is about to show.
//
// Call `widget.on("shown", func)` to monitor when a CollapseItem has been shown.
//
// Call `widget.on("hide", func)` to monitor when a CollapseItem is about to hide.
//
// Call `widget.on("hidden", func)` to monitor when a CollapseItem has been hidden.
//
baseClass: "accordion",
// allowAllOpen: Boolean
// determines if all groups can be open at the same time. Default is false.
allowAllOpen: false,
postCreate:function () {
// summary:
// adds baseClass and initializes events cache.
// tags:
// private extension
if(!domClass.contains(this.domNode, this.baseClass)){
domClass.add(this.domNode, this.baseClass);
}
this._events = [];
},
startup: function(){
// summary:
// registers delegated handler for each child group events. Displays first group.
// tags:
// private extension
if(this.getChildren().length){
array.forEach(this.getChildren(), function(ci){
var evt = this.own(ci.on("show, shown, hide, hidden", lang.hitch(this, function(e){
if(e.type === "show"){ _beforeShow.call(this, e.target); }
this.emit(e.type, e);
})));
this._events.push({"id":ci.id, "event": evt});
}, this);
var first = this.getChildren()[0];
first.show();
}
this.emit("started");
},
toggle: function(/*CollapseItem|Number*/ widgetRef){
// summary:
// toggles a collapse group. Accepts the widget index or CollapseItem
if(widgetRef === null){ return; }
if(typeof widgetRef === "number"){
widgetRef = this.getChildren()[widgetRef];
}
if (widgetRef.isInstanceOf && widgetRef.isInstanceOf(CollapseItem)) {
if(widgetRef.isCollapsed()){
this.show(widgetRef);
} else {
this.hide(widgetRef);
}
}
},
show: function(/*CollapseItem|Number*/ widgetRef){
// summary:
// expands a group. Accepts the widget index or CollapseItem
if(typeof widgetRef === "number"){
widgetRef = this.getChildren()[widgetRef];
}
if (widgetRef.isInstanceOf && widgetRef.isInstanceOf(CollapseItem)) {
widgetRef.show();
}
},
hide: function(/*CollapseItem|Number*/ widgetRef){
// summary:
// collapses a group. Accepts the widget index or CollapseItem
if(widgetRef === null){ return; }
if(typeof widgetRef === "number"){
widgetRef = this.getChildren()[widgetRef];
}
if (widgetRef.isInstanceOf && widgetRef.isInstanceOf(CollapseItem)) {
widgetRef.hide();
}
},
collapseAll: function(){
// summary:
// collapses all collapse groups.
this.getChildren().forEach(function(collapse){
if(collapse._transitioning){ return; }
collapse.hide(true);
});
},
expandAll: function(){
// summary:
// expands all collapse groups.
if(!this.allowAllOpen){ return; }
this.getChildren().forEach(function(collapse){
if(collapse._transitioning){ return; }
collapse.show(true);
});
},
add: function(/*CollapseItem*/ collapseItem){
// summary:
// add a CollapseItem to the Collapse widget
if (collapseItem.isInstanceOf && collapseItem.isInstanceOf(CollapseItem)) {
this.addChild(collapseItem);
var evt = this.own(collapseItem.on("show, shown, hide, hidden", lang.hitch(this, function(e){
if(e.type === "show"){ _beforeShow.call(this, e.target); }
this.emit(e.type, e);
})));
this._events.push({"id":collapseItem.id, "event": evt});
}
},
remove: function(/*CollapseItem*/ collaspeItem){
// summary:
// remove a CollapseItem from the Collapse widget
if(collaspeItem === null){ return; }
if(typeof collaspeItem === "number"){
collaspeItem = this.getChildren()[collaspeItem];
}
if (collaspeItem.isInstanceOf && collaspeItem.isInstanceOf(CollapseItem)) {
this.removeChild(collaspeItem);
collaspeItem.destroy();
this._events = _removeCollapseItemEvent(collaspeItem, this._events);
}
}
});
});