-
Notifications
You must be signed in to change notification settings - Fork 4
/
bootstrap-without-jquery.js
125 lines (109 loc) · 4.46 KB
/
bootstrap-without-jquery.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
/*!
* Bootstrap without jQuery v0.3.1
* By Daniel Davis under MIT License
* https://github.com/tagawa/bootstrap-without-jquery
*/
;(function() {
'use strict';
// querySelectorAll support for older IE
// Source: http://weblogs.asp.net/bleroy/archive/2009/08/31/queryselectorall-on-old-ie-versions-something-that-doesn-t-work.aspx
if (!document.querySelectorAll) {
document.querySelectorAll = function(selector) {
var style = document.styleSheets[0] || document.createStyleSheet();
style.addRule(selector, "foo:bar");
var all = document.all, resultSet = [];
for (var i = 0, l = all.length; i < l; i++) {
if (all[i].currentStyle.foo === "bar") {
resultSet[resultSet.length] = all[i];
}
}
style.removeRule(0);
return resultSet;
};
}
// Get the "hidden" height of a collapsed element
function getHiddenHeight(el) {
var children = el.children;
var height = 0;
for (var i = 0, len = children.length, child; i < len; i++) {
child = children[i];
height += Math.max(child['clientHeight'], child['offsetHeight'], child['scrollHeight']);
}
return height;
}
// Collapse and expand the relevent element
function doCollapse(event) {
event = event || window.event;
var evTarget = event.currentTarget || event.srcElement;
var dataTarget = evTarget.getAttribute('data-target');
var target = document.querySelector(dataTarget);
var targetHeight = getHiddenHeight(target);
var className = (' ' + target.className + ' ');
if (className.indexOf(' ' + 'in' + ' ') > -1) {
// Hide the element
className = className.replace(' in ', ' ');
target.className = className;
target.style.height = '0';
} else {
// Show the element
target.className += ' in ';
target.style.height = targetHeight + 'px';
}
return false;
}
// Show a dropdown menu
function doDropdown(event) {
event = event || window.event;
var evTarget = event.currentTarget || event.srcElement;
var target = evTarget.parentElement;
var className = (' ' + target.className + ' ');
if (className.indexOf(' ' + 'open' + ' ') > -1) {
// Hide the menu
className = className.replace(' open ', ' ');
target.className = className;
} else {
// Close menus that are currently open (added by TAD).
for (var i = 0, dropdown, len = dropdowns.length; i < len; i++) {
dropdown = dropdowns[i];
dropdown.parentElement.className = (' ' + dropdown.parentElement.className + ' ').replace(' open ', ' ');
}
// Show the menu
target.className += ' open ';
}
return false;
}
// Close a dropdown menu
function closeDropdown(event) {
event = event || window.event;
var evTarget = event.currentTarget || event.srcElement;
var target = evTarget.parentElement;
target.className = (' ' + target.className + ' ').replace(' open ', ' ');
return false;
}
// Close an alert box by removing it from the DOM
function closeAlert(event) {
event = event || window.event;
var evTarget = event.currentTarget || event.srcElement;
var alertBox = evTarget.parentElement;
alertBox.parentElement.removeChild(alertBox);
return false;
}
// Set event listeners for collapsible menus
var collapsibles = document.querySelectorAll('[data-toggle=collapse]');
for (var i = 0, len = collapsibles.length; i < len; i++) {
collapsibles[i].onclick = doCollapse;
}
// Set event listeners for dropdown menus
var dropdowns = document.querySelectorAll('[data-toggle=dropdown]');
for (var i = 0, dropdown, len = dropdowns.length; i < len; i++) {
dropdown = dropdowns[i];
dropdown.setAttribute('tabindex', '0'); // Fix to make onblur work in Chrome
dropdown.onclick = doDropdown;
// dropdown.onblur = closeDropdown;
}
// Set event listeners for alert boxes
var alerts = document.querySelectorAll('[data-dismiss=alert]');
for (var i = 0, len = alerts.length; i < len; i++) {
alerts[i].onclick = closeAlert;
}
})();