-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
details-element-polyfill.js
297 lines (275 loc) · 9.38 KB
/
details-element-polyfill.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
293
294
295
296
297
/*
Details Element Polyfill 2.0.4
Copyright © 2018 Javan Makhmali
*/
(function() {
}).call(this);
(function() {
var addAttributesForSummary, eventIsSignificant, findClosestElementWithTagName, findElementsWithTagName, onTogglingTrigger, polyfillFocusAndARIA, polyfillProperties, polyfillStyles, polyfillToggle, polyfillToggleEvent, support, triggerToggle, triggerToggleIfToggled;
support = {
element: (function() {
var closedHeight, element, openedHeight, parent, ref;
element = document.createElement("details");
if (!("open" in element)) {
return false;
}
element.innerHTML = "<summary>a</summary>b";
element.setAttribute("style", "position: absolute; left: -9999px");
parent = (ref = document.body) != null ? ref : document.documentElement;
parent.appendChild(element);
closedHeight = element.offsetHeight;
element.open = true;
openedHeight = element.offsetHeight;
parent.removeChild(element);
return closedHeight !== openedHeight;
})(),
toggleEvent: (function() {
var element;
element = document.createElement("details");
return "ontoggle" in element;
})()
};
if (support.element && support.toggleEvent) {
return;
}
polyfillStyles = function() {
return document.head.insertAdjacentHTML("afterbegin", "<style>@charset\"UTF-8\";details:not([open])>*:not(summary){display:none;}details>summary{display:block;}details>summary::before{content:\"►\";padding-right:0.3rem;font-size:0.6rem;cursor:default;}details[open]>summary::before{content:\"▼\";}</style>");
};
polyfillProperties = function() {
var open, prototype, removeAttribute, setAttribute;
prototype = document.createElement("details").constructor.prototype;
setAttribute = prototype.setAttribute, removeAttribute = prototype.removeAttribute;
open = Object.getOwnPropertyDescriptor(prototype, "open");
return Object.defineProperties(prototype, {
open: {
get: function() {
var ref;
if (this.tagName === "DETAILS") {
return this.hasAttribute("open");
} else {
return open != null ? (ref = open.get) != null ? ref.call(this) : void 0 : void 0;
}
},
set: function(value) {
var ref;
if (this.tagName === "DETAILS") {
if (value) {
this.setAttribute("open", "");
} else {
this.removeAttribute("open");
}
return value;
} else {
return open != null ? (ref = open.set) != null ? ref.call(this, value) : void 0 : void 0;
}
}
},
setAttribute: {
value: function(name, value) {
return triggerToggleIfToggled(this, (function(_this) {
return function() {
return setAttribute.call(_this, name, value);
};
})(this));
}
},
removeAttribute: {
value: function(name) {
return triggerToggleIfToggled(this, (function(_this) {
return function() {
return removeAttribute.call(_this, name);
};
})(this));
}
}
});
};
polyfillToggle = function() {
return onTogglingTrigger(function(element) {
var summary;
summary = element.querySelector("summary");
if (element.hasAttribute("open")) {
element.removeAttribute("open");
return summary.setAttribute("aria-expanded", false);
} else {
element.setAttribute("open", "");
return summary.setAttribute("aria-expanded", true);
}
});
};
polyfillFocusAndARIA = function() {
var i, len, observer, ref, summary;
ref = findElementsWithTagName(document.documentElement, "SUMMARY");
for (i = 0, len = ref.length; i < len; i++) {
summary = ref[i];
addAttributesForSummary(summary);
}
if (typeof MutationObserver !== "undefined" && MutationObserver !== null) {
observer = new MutationObserver(function(mutations) {
var addedNodes, j, len1, results, target;
results = [];
for (j = 0, len1 = mutations.length; j < len1; j++) {
addedNodes = mutations[j].addedNodes;
results.push((function() {
var k, len2, results1;
results1 = [];
for (k = 0, len2 = addedNodes.length; k < len2; k++) {
target = addedNodes[k];
results1.push((function() {
var l, len3, ref1, results2;
ref1 = findElementsWithTagName(target, "SUMMARY");
results2 = [];
for (l = 0, len3 = ref1.length; l < len3; l++) {
summary = ref1[l];
results2.push(addAttributesForSummary(summary));
}
return results2;
})());
}
return results1;
})());
}
return results;
});
return observer.observe(document.documentElement, {
subtree: true,
childList: true
});
} else {
return document.addEventListener("DOMNodeInserted", function(event) {
var j, len1, ref1, results;
ref1 = findElementsWithTagName(event.target, "SUMMARY");
results = [];
for (j = 0, len1 = ref1.length; j < len1; j++) {
summary = ref1[j];
results.push(addAttributesForSummary(summary));
}
return results;
});
}
};
addAttributesForSummary = function(summary, details) {
if (details == null) {
details = findClosestElementWithTagName(summary, "DETAILS");
}
summary.setAttribute("aria-expanded", details.hasAttribute("open"));
if (!summary.hasAttribute("tabindex")) {
summary.setAttribute("tabindex", "0");
}
if (!summary.hasAttribute("role")) {
return summary.setAttribute("role", "button");
}
};
polyfillToggleEvent = function() {
var observer;
if (typeof MutationObserver !== "undefined" && MutationObserver !== null) {
observer = new MutationObserver(function(mutations) {
var attributeName, i, len, ref, results, target;
results = [];
for (i = 0, len = mutations.length; i < len; i++) {
ref = mutations[i], target = ref.target, attributeName = ref.attributeName;
if (target.tagName === "DETAILS" && attributeName === "open") {
results.push(triggerToggle(target));
} else {
results.push(void 0);
}
}
return results;
});
return observer.observe(document.documentElement, {
attributes: true,
subtree: true
});
} else {
return onTogglingTrigger(function(element) {
var open;
open = element.getAttribute("open");
return setTimeout(function() {
if (element.getAttribute("open") !== open) {
return triggerToggle(element);
}
}, 1);
});
}
};
eventIsSignificant = function(event) {
return !(event.defaultPrevented || event.ctrlKey || event.metaKey || event.shiftKey || event.target.isContentEditable);
};
onTogglingTrigger = function(callback) {
addEventListener("click", function(event) {
var element, ref;
if (eventIsSignificant(event) && event.which <= 1) {
if (element = findClosestElementWithTagName(event.target, "SUMMARY")) {
if (((ref = element.parentElement) != null ? ref.tagName : void 0) === "DETAILS") {
return callback(element.parentElement);
}
}
}
}, false);
return addEventListener("keydown", function(event) {
var element, ref, ref1;
if (eventIsSignificant(event) && ((ref = event.keyCode) === 13 || ref === 32)) {
if (element = findClosestElementWithTagName(event.target, "SUMMARY")) {
if (((ref1 = element.parentElement) != null ? ref1.tagName : void 0) === "DETAILS") {
callback(element.parentElement);
return event.preventDefault();
}
}
}
}, false);
};
findElementsWithTagName = function(root, tagName) {
var elements;
elements = [];
if (root.nodeType === Node.ELEMENT_NODE) {
if (root.tagName === tagName) {
elements.push(root);
}
elements.push.apply(elements, root.getElementsByTagName(tagName));
}
return elements;
};
findClosestElementWithTagName = (function() {
if (typeof Element.prototype.closest === "function") {
return function(element, tagName) {
return element.closest(tagName);
};
} else {
return function(element, tagName) {
while (element) {
if (element.tagName === tagName) {
return element;
} else {
element = element.parentNode;
}
}
};
}
})();
triggerToggle = function(element) {
var event;
event = document.createEvent("Events");
event.initEvent("toggle", true, false);
return element.dispatchEvent(event);
};
triggerToggleIfToggled = function(element, fn) {
var open, result;
open = element.getAttribute("open");
result = fn();
if (element.getAttribute("open") !== open) {
triggerToggle(element);
}
return result;
};
if (!support.element) {
polyfillStyles();
polyfillProperties();
polyfillToggle();
polyfillFocusAndARIA();
}
if (support.element && !support.toggleEvent) {
polyfillToggleEvent();
}
}).call(this);
(function() {
}).call(this);