forked from leftlogic/fullfrontal2010
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html5.js
185 lines (161 loc) · 4.67 KB
/
html5.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
/*
Author: Remy Sharp
Contact: remy [at] remysharp.com
*/
// attribute support
(function () {
function each(list, fn) {
var l = list.length;
for (var i = 0; i < l; i++) {
if (fn.call(list[i], list[i], i, list) === false) {
break;
}
}
}
// made global by myself to be reused elsewhere
window.addEvent = (function () {
if (document.addEventListener) {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.addEventListener(type, fn, false);
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
} else {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
}
})();
var i = document.createElement('input'), inputs = document.getElementsByTagName('input');
/** placeholder support */
if (!('placeholder' in i)) {
// placeholder fix
each(inputs, function (el) {
// note - we're using el instead of this across the board because it compresses better
var lastValue = el.value, placeholder = el.getAttribute('placeholder');
if (placeholder) {
var focus = function () {
if (el.value == placeholder) {
el.value = '';
el.style.color = '';
}
};
var blur = function () {
if (el.value == '') {
el.value = placeholder;
el.style.color = '#A29797';
}
};
addEvent(el, 'focus', focus);
addEvent(el, 'blur', blur);
// remove the placeholder if the page is reload or the form is submitted
addEvent(el.form, 'submit', function () { focus.call(el); });
addEvent(window, 'unload', function () { focus.call(el); });
// set the default state
if (el.value == '') {
blur.call(el);
}
}
});
}
/** autofocus support (for kicks) */
if (!('autofocus' in i)) {
// auto focus
each(inputs, function (el) {
if (el.getAttribute('autofocus') != null) {
el.focus();
return false; // "there can only be one"
}
});
}
/** details support - typically in it's own script */
// find the first /real/ node
function firstNode(source) {
var node = null;
if (source.firstChild.nodeName != "#text") {
return source.firstChild;
} else {
source = source.firstChild;
do {
source = source.nextSibling;
} while (source && source.nodeName == '#text');
return source || null;
}
}
function isSummary(el) {
var nn = el.nodeName.toUpperCase();
if (nn == 'DETAILS') {
return false;
} else if (nn == 'SUMMARY') {
return true;
} else {
return isSummary(el.parentNode);
}
}
function toggleDetails(event) {
// more sigh - need to check the clicked object
var keypress = event.type == 'keypress',
target = event.target || event.srcElement;
if (keypress || isSummary(target)) {
if (keypress) {
// if it's a keypress, make sure it was enter or space
keypress = event.which || event.keyCode;
// console.log(keypress);
if (keypress == 32 || keypress == 13) {
// all's good, go ahead and toggle
} else {
return;
}
}
var open = this.getAttribute('open');
if (open === null) {
this.setAttribute('open', 'open');
} else {
this.removeAttribute('open');
}
// this.className = open ? 'open' : ''; // Lame
// trigger reflow (required in IE)
setTimeout(function () {
document.body.className = document.body.className;
}, 13);
if (keypress) {
if (event.preventDefault) event.preventDefault();
return false;
}
}
}
var details = document.getElementsByTagName('details'),
i = details.length,
first = null,
label = document.createElement('summary');
label.appendChild(document.createTextNode('Details'));
while (i--) {
first = firstNode(details[i]);
if (first != null && first.nodeName.toUpperCase() == 'SUMMARY') {
// we've found that there's a details label already
} else {
// first = label.cloneNode(true); // cloned nodes weren't picking up styles in IE - random
first = document.createElement('summary');
first.appendChild(document.createTextNode('Details'));
if (details[i].firstChild) {
details[i].insertBefore(first, details[i].firstChild);
} else {
details[i].appendChild(first);
}
}
first.legend = true;
first.tabIndex = 0;
}
addEvent(details, 'click', toggleDetails);
addEvent(details, 'keypress', toggleDetails);
})();