forked from compat-table/compat-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.js
502 lines (433 loc) · 15.7 KB
/
master.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// jshint ignore:start
var _gaq = [
['_setAccount', 'UA-1128111-24'],
['_trackPageview']
];
// jshint ignore:end
if (!document.scripts) { // Create "live list" for all scripts if it doesn't exist
document.scripts = document.getElementsByTagName("script");
}
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') +
'.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
window.test = function(expression) {
var result = (typeof expression === "string" ? expression : !!expression ? 'Yes' : 'No');
// Last SCRIPT in the document is the one currently executing
var thisScript = document.currentScript || document.scripts[document.scripts.length-1];
var td = thisScript.parentNode;
var tr = td.parentNode;
// Insert the two new TD elements after the one that contains the SCRIPT
td = tr.insertBefore(document.createElement("td"), td.nextSibling);
td.className = result.toLowerCase() + " current";
if ("textContent" in td) {
td.textContent = result;
} else {
td.innerText = result;
}
tr.insertBefore(document.createElement("td"), td.nextSibling);
};
// For async tests, this returned function is used to set results
// instead of returning true/false.
function makeAsyncPassedFn(className, textContent) {
return function(rowNum) {
return function() {
var elem = $("#table-wrapper tbody tr:not(.category)")
.eq(+rowNum)
.children(".current")
.first()
.prop("className", className)
.text(textContent);
if (window.__updateHeaderTotal) {
elem.parent().prevAll('.supertest').first().each(window.__updateSupertest);
$('th.current').each(window.__updateHeaderTotal);
}
};
};
}
window.__asyncPassedFn = makeAsyncPassedFn('yes', "Yes");
window.__strictAsyncPassedFn = makeAsyncPassedFn("no strict", "Strict");
$(function() {
'use strict';
var table = $('#table-wrapper');
var currentBrowserSelector = ":nth-of-type(2)";
var setColSpans = function() {
$('#desktop-header' ).prop('colSpan', $('.platform.desktop:visible').length);
$('#compiler-header').prop('colSpan', $('.platform.compiler:visible').length);
$('#engine-header' ).prop('colSpan', $('.platform.engine:visible').length);
$('#mobile-header' ).prop('colSpan', $('.platform.mobile:visible').length);
$('tr.category>td' ).prop('colSpan', $('tr.supertest:first>td:visible').length);
};
/** Sticky header */
var platformTypesHeader = $(
"#table-wrapper thead tr:first-child th"
);
if (platformTypesHeader != null && platformTypesHeader.length) {
var actualPlatformTypesHeaderHeight = platformTypesHeader.outerHeight();
document.documentElement.style.setProperty(
"--first-header-row-height",
String(actualPlatformTypesHeaderHeight) + 'px'
);
}
/** -- End Sticky header -- */
// Set up the Show Obsolete checkbox
$('#show-obsolete, #show-unstable').on('click', function() {
$('body').toggleClass(this.id, this.checked);
setTimeout(function() {
setColSpans();
}, 100);
}).each(function() {
if (this.checked) $(this).triggerHandler("click");
});
window.__updateSupertest = function() {
var tr = $(this);
var isNonStandardTable = $('.non-standard table').length > 0;
var subtests = tr.nextUntil('tr:not(.subtest)');
if (subtests.length === 0) {
return;
}
var tally = subtests.find(".yes" + currentBrowserSelector).length;
var grade = tally/subtests.length;
tr
.find('.tally.current, .tally.current + td:empty').remove().end()
.find('td:first-child')
.after(
'<td class="tally current" data-tally="' + tally/subtests.length
+ (isNonStandardTable ? '' : '" style="background-color:hsl(' + (120*grade|0) + ',' +((86 - (grade*44))|0) +'%,50%)')
+ '">' +
tally + '/' + subtests.length + '</td><td></td>'
);
};
$('tr.supertest').each(function() {
var tr = $(this);
var subtests = tr.nextUntil('tr:not(.subtest)');
if (subtests.length === 0) {
return;
}
// Attach dropdown indicator and onclick to those tests with subtests
$('<span class="folddown">►</span>')
.appendTo(tr.children()[0]);
tr.on('click', function(event) {
if (!$(event.target).is('a')) {
// toggle manually for perf. reasons
// it would be even better to toggle this via higher-level CSS (on a parent)
// but current optimization (getting rid of `toggle`)
// already brings time from ~500ms to ~15ms
// (mostly due to removal of recalc-heavy `css` for each element)
// so this is probably sufficient for now
subtests.each(function(i, el) {
el.style.display = el.style.display === 'table-row' ? 'none' : 'table-row';
});
var deg = subtests[0].style.display === 'table-row' ? '90deg' : '0deg';
tr.find(".folddown").css('transform', 'rotate(' + deg + ')');
}
});
// Also, work out tallies for the current browser's tally features
tr.each(window.__updateSupertest);
});
var globalFoldDown = $('<span class="folddown">►</span>');
if ($('tr.supertest').size()) {
$('.test-name').append(globalFoldDown);
}
globalFoldDown.data('is-expanded', false);
globalFoldDown.on('click', function(e) {
e.stopPropagation();
// let's horribly cheat here for now
$('tr.supertest').click();
globalFoldDown.data('is-expanded', !globalFoldDown.data('is-expanded'));
var deg = globalFoldDown.data('is-expanded') ? '90deg' : '0deg';
globalFoldDown.css('transform', 'rotate(' + deg + ')');
})
.css('cursor', 'pointer')
.prop('title', 'Expand/Collapse all tests');
// Set up the tooltip HTML
var infoTooltip = $('<pre class="info-tooltip">')
.hide()
.appendTo('body')
.on('click', function (e) {
e.stopPropagation();
});
infoTooltip.fillAndShow = function (e, scriptTag) {
return this
.text(scriptTag.attr('data-source').trim())
.show()
.moveHere(e);
};
infoTooltip.unlockAndHide = function (lockedFrom) {
lockedFrom.removeClass('tooltip-locked');
return this
.data('locked-from', null)
.hide();
};
infoTooltip.moveHere = function (e) {
return this.offset({
left: e.pageX + 10,
top: e.pageY
});
};
// Attach tooltip buttons to each feature <tr>
$('#table-wrapper td:first-child').each(function() {
var td = $(this);
var scriptTag = td.parents('tr').find('script');
if (scriptTag.length === 0) {
return;
}
$('<span class="info">c</span>')
.appendTo(td)
.on('mouseenter', function (e) {
if (!infoTooltip.data('locked-from')) {
infoTooltip.fillAndShow(e, scriptTag);
}
})
.on('mouseleave', function () {
if (!infoTooltip.data('locked-from')) {
infoTooltip.hide();
}
})
.on('mousemove', function (e) {
if (!infoTooltip.data('locked-from')) {
infoTooltip.moveHere(e);
}
})
.on('click', function (e) {
var lockedFrom = infoTooltip.data('locked-from');
if (lockedFrom) {
infoTooltip.unlockAndHide(lockedFrom);
}
var elem = $(this);
if (!elem.is(lockedFrom)) {
infoTooltip
.fillAndShow(e, scriptTag)
.data('locked-from', elem);
elem.addClass('tooltip-locked');
}
e.stopPropagation();
});
});
// Hide locked tooltip when clicking outside of it
$(window).on('click', function () {
var lockedFrom = infoTooltip.data('locked-from');
if (lockedFrom) {
infoTooltip.unlockAndHide(lockedFrom);
}
});
// Function to retrieve the platform name of a given <td> cell
function platformOf(elem) {
return elem.getAttribute('data-browser') || '';
}
// Since you can't add a :hover effect for columns,
// these handlers must suffice.
function addRemoveHover(name) {
return function() {
var c = platformOf(this);
if (c) {
$("[data-browser='" + c + "']")[name]('hover');
}
};
}
table
.on('mouseenter', 'td', addRemoveHover('addClass'))
.on('mouseleave', 'td', addRemoveHover('removeClass'));
// Cell highlighting function
function highlightSelected(elem) {
var win = $(window);
var left = win.scrollLeft();
table.detach().find('.selected').removeClass('selected');
elem.addClass('selected');
// Trigger `click` event on supertest to open dropdown.
if (!elem.is('.parent') && elem.is('.subtest:hidden')) {
var supertest = elem.prevUntil('.supertest').prev();
// If there are no other subtests before this, `prevUntil`
// will return an empty jQuery object. To select the supertest,
// we can just select the previous element.
if (!supertest.length) {
supertest = elem.prev();
}
supertest.triggerHandler('click');
}
table.addClass('one-selected').insertBefore('#footnotes');
win.scrollLeft(left);
table.triggerHandler('reflow');
}
$(document).on('click', function removeHighlighting(event) {
// Don't remove all dimming if another link was clicked in this event.
if ($(event.target).is('[href],[href] *, .supertest *'))
return;
table.removeClass('one-selected');
});
window.onhashchange = function() {
if (location.hash) {
var elem = $('[href="' + location.hash + '"]');
// If it's a feature anchor, select only the <tr>.
// (The CSS widens the selected row only vertically.)
if (elem.is('.anchor')) {
highlightSelected(elem.parents('tr'));
}
// If it's a browser name, select all matching <td> elements
// (The CSS widens the selected column only horizontally.)
else if (elem.is('.browser-name')) {
// This assumes that all <td>s in the column have a class that matches
// the browser-name's ID.
highlightSelected(table.find('td' + currentBrowserSelector + ', td[data-browser="' + elem.attr('href').slice(1) + '"]'));
}
}
};
window.onhashchange();
// browser engine color stripes
function getBrowserColour(name) {
/* Chakra */
if (/^(ie|edge)/.exec(name)) {
return "hsla(217, 85%, 54%, .5)";
}
/* SpiderMonkey */
if (/^(firefox|rhino)/.exec(name)) {
return "hsla(35, 100%, 50%, .5)";
}
/* JavaScriptCore */
if (/^(webkit|safari|jxa|phantom|ios|android4_0)/.exec(name)) {
return "hsla(220, 25%, 70%, .5)";
}
/* Carakan */
if (/^opera\d|opera_mobile1[120]/.exec(name)) {
return "hsla(358, 86%, 43%, .5)";
}
/* V8 */
if (/^(chrome|node|iojs|android4[1-9]|android[5-9]|samsung|opera_mobile)/.exec(name)) {
return "hsla(79, 100%, 37%, .5)";
}
/* KJS */
if (/^konq/.exec(name)) {
return "hsla(200, 100%, 74%, .5)";
}
/* BESEN */
if (name === "besen") {
return "rgba(173, 108, 23, .5)";
}
/* Current browser */
if (name === "current") {
return "hsla(0, 0%, 75%, .5)";
}
/* Compilers */
return "hsla(52, 85%, 63%, .5)";
}
// Store number of features for each column/browser and numeric index.
// The reason this is done at runtime instead of build time is because
// the current browser's totals must be done at runtime, and to save on
// duplicated code, we may as well do the predefined results too.
window.__updateHeaderTotal = function(i) {
var elem = $(this);
var name;
var id = 'current';
if (elem.is('.browser-name')) {
id = elem.attr('href').slice(1);
name = elem.attr('href').replace("#", '[data-browser="')+'"]';
elem = elem.parent();
} else {
name = currentBrowserSelector;
}
var yesPoints = 0;
var flaggedPoints = 0;
var totalPoints = 0;
table.find('tbody tr:not(.subtest):not(.category):not(.not-applicable)').each(function() {
var row = $(this);
var points = +(row.attr('significance') || 1);
var cell = row.find('td' + name);
if (row.find('.separator').length || cell.length === 0) {
return;
}
totalPoints += points;
if (row.is('.supertest')) {
// test with subtests
var tally = +(cell.attr('data-tally') || 0);
var flaggedTally = +(cell.attr('data-flagged-tally') || 0);
yesPoints += tally * points;
if (flaggedTally > tally) {
flaggedPoints += flaggedTally * points;
} else {
flaggedPoints += tally * points;
}
} else {
// test with yes/no
if (cell.is('.yes')) {
yesPoints += points;
flaggedPoints += points;
} else if (cell.is('.flagged')) {
flaggedPoints += points;
}
}
});
var featuresCount = yesPoints / totalPoints;
var flaggedFeaturesCount = flaggedPoints / totalPoints;
function gradient(colour, percent) {
return 'linear-gradient(to top, ' +
colour + ' 0%, ' + colour + ' ' +
(percent * 100|0) + '%, transparent ' + (percent * 100|0) +
'%,transparent 100%)';
}
var colour = getBrowserColour(id);
elem
.attr('data-num', i)
.attr('data-features', featuresCount)
.attr('data-flagged-features', flaggedFeaturesCount)
.find('.num-features').remove().end()
.append('<sup class="num-features" title="Number of implemented features">' +
// Don't bother with a HSL fallback for IE 8.
'<b style="color:hsl(' + (featuresCount * 120|0) + ',100%,25%)">' +
(Math.floor(featuresCount*100)) +
'</b>%</sup>')
// Fancy bar graph background garnish (again, no fallback required).
.css({'background-image': gradient(colour, featuresCount) +
(flaggedFeaturesCount > featuresCount
? ',' + gradient(colour.replace(".5",".2"), flaggedFeaturesCount)
: '')});
};
$('.browser-name, th.current').each(window.__updateHeaderTotal);
setColSpans();
// Cached arrays of sort orderings
var ordering = { };
var defaultSortVal = 'engine-types';
var noPlatformtype = $(".platformtype").length === 0;
if (noPlatformtype) {
$('body').addClass('hide-platformtype');
}
$('#sort').on('change', function() {
var sortSpecMap = {
'features': {attr: 'data-features', order: 1, hidePlatformtype: true},
'flagged-features': {attr: 'data-flagged-features', order: 1, hidePlatformtype: true},
'engine-types': {attr: 'data-num', order: -1, hidePlatformtype: false}
};
var sortSpec = sortSpecMap[this.value];
var sortAttr = sortSpec.attr;
// First, hide the platformtype bar if we're sorting by features.
$('body').toggleClass('hide-platformtype', noPlatformtype || sortSpec.hidePlatformtype);
// Next, cache the sort orderings
if (!ordering[sortAttr]) {
// Sort the platforms
var cells = $('th.platform').toArray().sort(function(a, b) {
return sortSpec.order * (parseFloat(b.getAttribute(sortAttr)) - parseFloat(a.getAttribute(sortAttr)));
});
ordering[sortAttr] = $.map(cells, platformOf);
}
var ord = ordering[sortAttr];
// Define a comparison function using the orderings
var comparator = function(a, b) {
return ord.indexOf(platformOf(a)) - ord.indexOf(platformOf(b));
};
// Now sort the columns using the comparison function
table.detach().find('tr').each(function(i, row) {
var cells = $(row.cells).slice(3).toArray().sort(comparator);
for (var j = 0, jlen = cells.length; j < jlen; j++) {
row.appendChild(cells[j]);
}
});
table.insertBefore('#footnotes');
});
if ($("#sort").val() !== defaultSortVal) {
$("#sort").triggerHandler('change');
}
});