Repository navigation
Expand file tree
/
Copy pathcodespeed.js
More file actions
146 lines (133 loc) · 4.7 KB
/
Copy pathcodespeed.js
File metadata and controls
146 lines (133 loc) · 4.7 KB
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
function readCheckbox(el) {
/* Builds a string that holds all checked values in an input form */
var config = "";
$(el).each(function() {
config += $(this).val() + ",";
});
// Remove last comma
config = config.slice(0, -1);
return config;
}
function getLoadText(text, h) {
var loadtext = '<div style="text-align:center;">';
var pstyle = "";
if (h > 0) {
h = h - 32;
if(h < 80) { h = 180; }
else if (h > 400) { h = 400; }
pstyle = ' style="line-height:' + h + 'px;"';
}
loadtext += '<p' + pstyle + '>'+ text;
loadtext += '</p></div>';
return loadtext;
}
$(function() {
// Check all and none links
$('.checkall').each(function() {
var inputs = $(this).closest("ul").children("li").children("input");
$(this).click(function() {
inputs.prop("checked", true);
return false;
});
});
$('.uncheckall').each(function() {
var inputs = $(this).closest("ul").children("li").children("input");
$(this).click(function() {
inputs.prop("checked", false);
return false;
});
});
$('.togglefold').each(function() {
var lis = $(this).parent().children("li");
// count radios too (timeline groups use radio inputs, comparison checkboxes)
var allUnchecked = lis.find("input").filter(':checked').length === 0;
if (allUnchecked) {
lis.hide();
$(this).addClass('folded');
}
$(this).click(function() {
lis.slideToggle();
$(this).toggleClass('folded');
return false;
});
});
});
/* Colour theme.
*
* base.html sets data-theme on <html> before first paint. This keeps it in
* step with the header toggle, the choice stored in localStorage, the OS
* setting (followed until the user picks one) and other tabs, and tells the
* chart scripts when to redraw: canvas charts cannot read the stylesheet.
*/
var Theme = (function() {
var STORAGE_KEY = 'codespeed-theme';
var root = document.documentElement;
var listeners = [];
var osDark = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null;
function current() {
return root.getAttribute('data-theme') === 'dark' ? 'dark' : 'light';
}
function stored() {
var value = null;
try { value = localStorage.getItem(STORAGE_KEY); } catch (e) {}
return (value === 'light' || value === 'dark') ? value : null;
}
function updateButton() {
var label = 'Switch to ' + (current() === 'dark' ? 'light' : 'dark') + ' theme';
$('#theme-toggle').attr({'aria-label': label, title: label});
}
function apply(theme) {
if (theme === current()) { return; }
root.setAttribute('data-theme', theme);
updateButton();
listeners.forEach(function(fn) { fn(theme); });
}
// The colours a canvas chart needs, resolved from the stylesheet's tokens
function chartColors() {
var style = getComputedStyle(root);
function token(name, fallback) {
return style.getPropertyValue(name).trim() || fallback;
}
return {
text: token('--chart-text', '#666'),
grid: token('--chart-grid', 'rgba(0, 0, 0, 0.1)'),
faint: token('--text-faint', '#aaa'),
surface: token('--surface', '#fff')
};
}
// Dygraphs options for the parts it draws on the canvas. Hovering a
// series fades the others by painting the plot with the highlight
// background, which defaults to white, so it must match the surface.
function dygraphOptions() {
var colors = chartColors();
return {
axisLineColor: colors.text,
gridLineColor: colors.grid,
gridLineWidth: 1,
highlightSeriesBackgroundColor: colors.surface
};
}
$(function() {
updateButton();
$('#theme-toggle').click(function() {
var next = current() === 'dark' ? 'light' : 'dark';
try { localStorage.setItem(STORAGE_KEY, next); } catch (e) {}
apply(next);
});
if (osDark && osDark.addEventListener) {
osDark.addEventListener('change', function(e) {
if (!stored()) { apply(e.matches ? 'dark' : 'light'); }
});
}
// A toggle in another tab of the site
window.addEventListener('storage', function(e) {
if (e.key === STORAGE_KEY && stored()) { apply(stored()); }
});
});
return {
current: current,
chartColors: chartColors,
dygraphOptions: dygraphOptions,
onChange: function(fn) { listeners.push(fn); }
};
})();