-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbootstrap-native.js
292 lines (268 loc) · 10.3 KB
/
bootstrap-native.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Native Javascript for Bootstrap 3 v1.1.0 | © dnp_theme | MIT-License
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD support:
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like:
module.exports = factory();
} else {
// Browser globals (root is window)
var bsn = factory();
root.Affix = bsn.Affix;
root.Alert = bsn.Alert;
root.Button = bsn.Button;
root.Carousel = bsn.Carousel;
root.Collapse = bsn.Collapse;
root.Dropdown = bsn.Dropdown;
root.Modal = bsn.Modal;
root.Popover = bsn.Popover;
root.ScrollSpy = bsn.ScrollSpy;
root.Tab = bsn.Tab;
root.Tooltip = bsn.Tooltip;
}
}(this, function() {
// Native Javascript for Bootstrap 3 | Internal Utility Functions
// by dnp_theme
var addClass = function(el, c) { // where modern browsers fail, use classList
if (el.classList) {
el.classList.add(c);
} else {
el.className += ' ' + c;
el.offsetWidth;
}
},
removeClass = function(el, c) {
if (el.classList) {
el.classList.remove(c);
} else {
el.className = el.className.replace(c, '').replace(/^\s+|\s+$/g, '');
}
},
isIE = (new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})").exec(navigator.userAgent) != null) ? parseFloat(RegExp.$1) : false,
getClosest = function(el, s) { //el is the element and s the selector of the closest item to find
// source http://gomakethings.com/climbing-up-and-down-the-dom-tree-with-vanilla-javascript/
var f = s.charAt(0);
for (; el && el !== document; el = el.parentNode) { // Get closest match
if (f === '.') { // If selector is a class
if (document.querySelector(s) !== undefined) {
return el;
}
}
if (f === '#') { // If selector is an ID
if (el.id === s.substr(1)) {
return el;
}
}
}
return false;
},
// tooltip / popover stuff
isElementInViewport = function(t) { // check if this.tooltip is in viewport
var r = t.getBoundingClientRect();
return (
r.top >= 0 && r.left >= 0 && r.bottom <= (window.innerHeight || document.documentElement.clientHeight) && r.right <= (window.innerWidth || document.documentElement.clientWidth))
},
getScroll = function() { // also Affix and scrollSpy uses it
return {
y: window.pageYOffset || document.documentElement.scrollTop,
x: window.pageXOffset || document.documentElement.scrollLeft
}
},
mouseHover = ('onmouseleave' in document) ? ['mouseenter', 'mouseleave'] : ['mouseover', 'mouseout'],
tipPositions = /\b(top|bottom|left|top)+/;
// Native Javascript for Bootstrap 3 | Collapse
// by dnp_theme
// COLLAPSE DEFINITION
// ===================
var Collapse = function(element, options) {
options = options || {};
this.btn = typeof element === 'object' ? element : document.querySelector(element);
this.accordion = null;
this.collapse = null;
this.duration = 300; // default collapse transition duration
this.options = {};
this.options.duration = (isIE && isIE < 10) ? 0 : (options.duration || this.duration);
var self = this;
var getOuterHeight = function(el) {
var s = el && (el.currentStyle || window.getComputedStyle(el)),
// the getComputedStyle polyfill would do this for us, but we want to make sure it does
btp = /px/.test(s.borderTopWidth) ? Math.round(s.borderTopWidth.replace('px', '')) : 0,
mtp = /px/.test(s.marginTop) ? Math.round(s.marginTop.replace('px', '')) : 0,
mbp = /px/.test(s.marginBottom) ? Math.round(s.marginBottom.replace('px', '')) : 0,
mte = /em/.test(s.marginTop) ? Math.round(s.marginTop.replace('em', '') * parseInt(s.fontSize)) : 0,
mbe = /em/.test(s.marginBottom) ? Math.round(s.marginBottom.replace('em', '') * parseInt(s.fontSize)) : 0;
return el.clientHeight + parseInt(btp) + parseInt(mtp) + parseInt(mbp) + parseInt(mte) + parseInt(mbe); //we need an accurate margin value
};
this.toggle = function(e) {
e.preventDefault();
if (!/\bin/.test(self.collapse.className)) {
self.open();
} else {
self.close();
}
};
this.close = function() {
this._close(this.collapse);
addClass(this.btn, 'collapsed');
};
this.open = function() {
this._open(this.collapse);
removeClass(this.btn, 'collapsed');
if (this.accordion !== null) {
var active = this.accordion.querySelectorAll('.collapse.in'),
al = active.length,
i = 0;
for (i; i < al; i++) {
if (active[i] !== this.collapse) this._close(active[i]);
}
}
};
this._open = function(c) {
this.removeEvent();
addClass(c, 'in');
c.setAttribute('aria-expanded', 'true');
addClass(c, 'collapsing');
setTimeout(function() {
c.style.height = self.getMaxHeight(c) + 'px'
c.style.overflowY = 'hidden';
}, 0);
setTimeout(function() {
c.style.height = '';
c.style.overflowY = '';
removeClass(c, 'collapsing');
self.addEvent();
}, this.options.duration);
};
this._close = function(c) {
this.removeEvent();
c.setAttribute('aria-expanded', 'false');
c.style.height = this.getMaxHeight(c) + 'px'
setTimeout(function() {
c.style.height = '0px';
c.style.overflowY = 'hidden';
addClass(c, 'collapsing');
}, 0);
setTimeout(function() {
removeClass(c, 'collapsing');
removeClass(c, 'in');
c.style.overflowY = '';
c.style.height = '';
self.addEvent();
}, this.options.duration);
};
this.getMaxHeight = function(l) { // get collapse trueHeight and border
var h = 0;
for (var k = 0, ll = l.children.length; k < ll; k++) {
h += getOuterHeight(l.children[k]);
}
return h;
};
this.removeEvent = function() {
this.btn.removeEventListener('click', this.toggle, false);
};
this.addEvent = function() {
this.btn.addEventListener('click', this.toggle, false);
};
this.getTarget = function() {
var t = this.btn,
h = t.href && t.getAttribute('href').replace('#', ''),
d = t.getAttribute('data-target') && (t.getAttribute('data-target')),
id = h || (d && /#/.test(d)) && d.replace('#', ''),
cl = (d && d.charAt(0) === '.') && d,
//the navbar collapse trigger targets a class
c = id && document.getElementById(id) || cl && document.querySelector(cl);
return c;
};
// init
this.addEvent();
this.collapse = this.getTarget();
this.accordion = this.btn.getAttribute('data-parent') && getClosest(this.btn, this.btn.getAttribute('data-parent'));
};
// COLLAPSE DATA API
// =================
var Collapses = document.querySelectorAll('[data-toggle="collapse"]');
for (var o = 0, cll = Collapses.length; o < cll; o++) {
var collapse = Collapses[o],
options = {};
options.duration = collapse.getAttribute('data-duration');
new Collapse(collapse, options);
}
// Native Javascript for Bootstrap 3 | Tab
// by dnp_theme
// TAB DEFINITION
// ===================
var Tab = function( element,options ) {
options = options || {};
this.tab = typeof element === 'object' ? element : document.querySelector(element);
this.tabs = this.tab.parentNode.parentNode;
this.dropdown = this.tabs.querySelector('.dropdown');
if ( /\bdropdown-menu/.test(this.tabs.className) ) {
this.dropdown = this.tabs.parentNode;
this.tabs = this.tabs.parentNode.parentNode;
}
this.options = options;
// default tab transition duration
this.duration = 150;
this.options.duration = (isIE && isIE < 10) ? 0 : (options.duration || this.duration);
var self = this;
this.handle = function(e) {
e = e || window.e; e.preventDefault();
var next = e.target; //the tab we clicked is now the next tab
var nextContent = document.getElementById(next.getAttribute('href').replace('#','')); //this is the actual object, the next tab content to activate
// get current active tab and content
var activeTab = self.getActiveTab();
var activeContent = self.getActiveContent();
if ( !/\bactive/.test(next.parentNode.className) ) {
// toggle "active" class name
removeClass(activeTab,'active');
addClass(next.parentNode,'active');
// handle dropdown menu "active" class name
if ( self.dropdown ) {
if ( !(/\bdropdown-menu/.test(self.tab.parentNode.parentNode.className)) ) {
if (/\bactive/.test(self.dropdown.className)) removeClass(self.dropdown,'active');
} else {
if (!/\bactive/.test(self.dropdown.className)) addClass(self.dropdown,'active');
}
}
//1. hide current active content first
removeClass(activeContent,'in');
setTimeout(function() {
//2. toggle current active content from view
removeClass(activeContent,'active');
addClass(nextContent,'active');
}, self.options.duration);
setTimeout(function() {
//3. show next active content
addClass(nextContent,'in');
}, self.options.duration*2);
}
};
this.getActiveTab = function() {
var activeTabs = this.tabs.querySelectorAll('.active');
if ( activeTabs.length === 1 && !/\bdropdown/.test(activeTabs[0].className) ) {
return activeTabs[0]
} else if ( activeTabs.length > 1 ) {
return activeTabs[activeTabs.length-1]
}
};
this.getActiveContent = function() {
var active = this.getActiveTab().getElementsByTagName('A')[0].getAttribute('href').replace('#','');
return active && document.getElementById(active)
};
// init
this.tab.addEventListener('click', this.handle, false);
};
// TAB DATA API
// =================
var Tabs = document.querySelectorAll("[data-toggle='tab'], [data-toggle='pill']");
for ( var tb = 0, tbl = Tabs.length; tb<tbl; tb++ ) {
var tab = Tabs[tb], options = {};
options.duration = tab.getAttribute('data-duration') && tab.getAttribute('data-duration') || false;
new Tab(tab,options);
}
return {
Tab: Tab,
Collapse: Collapse
};
}));