forked from yui/yui3-gallery
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap-collapse.js
218 lines (179 loc) · 6.11 KB
/
bootstrap-collapse.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
A Plugin which provides collapsing/expanding behaviors on a Node with
compatible syntax and markup from Twitter's Bootstrap project.
@module gallery-bootstrap-collapse
**/
/**
A Plugin which provides collapsing and expanding behaviors on a Node with
compatible syntax and markup from Twitter's Bootstrap project.
It possible to have dynamic behaviors without incorporating any
JavaScript by setting <code>data-toggle=collapse</code> on any element.
However, it can be manually plugged into any node or node list.
@example
var node = Y.one('.someNode');
node.plug( Y.Bootstrap.Collapse, config );
node.collapse.show();
@class Bootstrap.Collapse
**/
function CollapsePlugin(config) {
CollapsePlugin.superclass.constructor.apply(this, arguments);
}
CollapsePlugin.NAME = 'Bootstrap.Collapse';
CollapsePlugin.NS = 'collapse';
Y.extend(CollapsePlugin, Y.Plugin.Base, {
defaults : {
duration : 0.25,
easing : 'ease-in',
showClass : 'in',
hideClass : 'out',
groupSelector : '> .accordion-group > .in'
},
transitioning: false,
initializer : function(config) {
this._node = config.host;
this.config = Y.mix( config, this.defaults );
this.publish('show', { preventable : true, defaultFn : this.show });
this.publish('hide', { preventable : true, defaultFn : this.hide });
this._node.on('click', this.toggle, this);
},
_getTarget: function() {
var node = this._node,
container;
if ( node.getData('target') ) {
container = Y.one( node.getData('target') );
}
else if ( node.getAttribute('href').indexOf('#') >= 0 ) {
Y.log('No target, looking at href: ' + node.getAttribute('href'), 'debug', 'Bootstrap.Collapse');
container = Y.one( node.getAttribute('href').substr( node.getAttribute('href').indexOf('#') ) );
}
return container;
},
/**
* @method hide
* @description Hide the collapsible target, specified by the host's
* <code>data-target</code> or <code>href</code> attribute.
*/
hide: function() {
var showClass = this.config.showClass,
hideClass = this.config.hideClass,
node = this._getTarget();
if ( this.transitioning ) {
return;
}
if ( node ) {
this._hideElement(node);
}
},
/**
* @method show
* @description Show the collapsible target, specified by the host's
* <code>data-target</code> or <code>href</code> attribute.
*/
show: function() {
var showClass = this.config.showClass,
hideClass = this.config.hideClass,
node = this._getTarget(),
host = this._node,
self = this,
parent,
group_selector = this.config.groupSelector;
if ( this.transitioning ) {
return;
}
if ( host.getData('parent') ) {
parent = Y.one( host.getData('parent') );
if ( parent ) {
parent.all(group_selector).each( function(el) {
Y.log('Hiding element: ' + el, 'debug', 'Bootstrap.Collapse');
self._hideElement(el);
});
}
}
this._showElement(node);
},
/**
@method toggle
@description Toggle the state of the collapsible target, specified
by the host's <code>data-target</code> or <code>href</code>
attribute. Calls the <code>show</code> or <code>hide</code> method.
**/
toggle : function(e) {
if ( e && Y.Lang.isFunction(e.preventDefault) ) {
e.preventDefault();
}
var target = this._getTarget();
if ( target.hasClass( this.config.showClass ) ) {
this.fire('hide');
} else {
this.fire('show');
}
},
/**
@method _transition
@description Handles the transition between showing and hiding.
@protected
@param node {Node} node to apply transitions to
@param method {String} 'hide' or 'show'
**/
_transition : function(node, method) {
var self = this,
config = this.config,
duration = config.duration,
easing = config.easing,
// If we are hiding, then remove the show class.
removeClass = method === 'hide' ? config.showClass : config.hideClass,
// And if we are hiding, add the hide class.
addClass = method === 'hide' ? config.hideClass : config.showClass,
to_height = method === 'hide' ? 0 : null,
event = method === 'hide' ? 'hidden' : 'shown',
complete = function() {
node.removeClass(removeClass);
node.addClass(addClass);
self.transitioning = false;
this.fire( event );
};
if ( to_height === null ) {
to_height = 0;
node.all('> *').each(function(el) {
to_height += el.get('scrollHeight');
});
}
this.transitioning = true;
node.transition({
height : to_height +'px',
duration : duration,
easing : easing
}, complete);
},
/**
@method _hideElement
@description Calls the <code>_transition</code> method to hide a node.
@protected
@param node {Node} node to hide.
**/
_hideElement : function(node) {
this._transition(node, 'hide');
/*
var showClass = this.showClass,
hideClass = this.hideClass;
node.removeClass(showClass);
node.addClass(hideClass);
*/
},
/**
@method _showElement
@description Calls the <code>_transition</code> method to show a node.
@protected
@param node {Node} node to show.
**/
_showElement : function(node) {
this._transition(node, 'show');
/*
var showClass = this.showClass,
hideClass = this.hideClass;
node.removeClass(hideClass);
node.addClass(showClass);
*/
}
});
Y.namespace('Bootstrap').Collapse = CollapsePlugin;