-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathinterop-summary.js
350 lines (315 loc) · 12.1 KB
/
interop-summary.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
/**
* Copyright 2023 The WPT Dashboard Project. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
import { CountUp } from '../node_modules/countup.js/dist/countUp.js';
import '../node_modules/@polymer/polymer/lib/elements/dom-if.js';
import { html, PolymerElement } from '../node_modules/@polymer/polymer/polymer-element.js';
import {afterNextRender} from '../node_modules/@polymer/polymer/lib/utils/render-status.js';
// This min-height is added to this section to ensure that the section below
// is not moved after the user selects between STABLE and EXPERIMENTAL
// (experimental browser names are longer and add additional lines).
// Different years have different initial heights for these sections.
const SUMMARY_CONTAINER_MIN_HEIGHTS = {
'2021': '275px',
'2022': '470px',
'2023': '470px',
'2024': '380px',
'2025': '380px',
};
class InteropSummary extends PolymerElement {
static get template() {
return html`
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap" rel="stylesheet">
<style>
#summaryNumberRow {
display: flex;
justify-content: center;
gap: 30px;
margin-bottom: 20px;
}
.summary-container {
min-height: 470px;
}
.summary-number {
font-size: 4.5em;
width: 3ch;
height: 3ch;
padding: 10px;
font-family: 'Roboto Mono', monospace;
display: grid;
place-content: center;
aspect-ratio: 1;
border-radius: 50%;
margin-bottom: 10px;
margin-left: auto;
margin-right: auto;
}
.smaller-summary-number {
font-size: 3.5em;
width: 2.5ch;
height: 2.5ch;
padding: 8px;
}
.summary-browser-name {
text-align: center;
display: flex;
place-content: center;
justify-content: space-around;
gap: 2ch;
}
.summary-title {
margin: 10px 0;
text-align: center;
font-size: 1em;
}
.summary-browser-name > figure {
margin: 0;
flex: 1;
}
.summary-browser-name > figure > figcaption {
line-height: 1.1;
}
.summary-browser-name[data-stable-browsers] > :not(.stable) {
display: none;
}
.summary-browser-name:not([data-stable-browsers]) > .stable {
display: none;
}
</style>
<div class="summary-container">
<div id="summaryNumberRow">
<!-- Interop -->
<div id="interopSummary" class="summary-flex-item" tabindex="0">
<div class="summary-number score-number smaller-summary-number">--</div>
<h3 class="summary-title">INTEROP</h3>
</div>
<!-- Investigations -->
<div id="investigationSummary" class="summary-flex-item" tabindex="0">
<div id="investigationNumber" class="summary-number smaller-summary-number">--</div>
<h3 class="summary-title">INVESTIGATIONS</h3>
</div>
</div>
<div id="summaryNumberRow">
<template is="dom-repeat" items="{{getYearProp('browserInfo')}}" as="browserInfo">
<div class="summary-flex-item" tabindex="0">
<div class="summary-number score-number smaller-summary-number">--</div>
<template is="dom-if" if="{{isChromeEdgeCombo(browserInfo)}}">
<!-- Chrome/Edge -->
<template is="dom-if" if="[[stable]]">
<div class="summary-browser-name">
<figure>
<img src="/static/chrome_64x64.png" width="36" alt="Chrome" />
<figcaption>Chrome</figcaption>
</figure>
<figure>
<img src="/static/edge_64x64.png" width="36" alt="Edge" />
<figcaption>Edge</figcaption>
</figure>
</div>
</template>
<template is="dom-if" if="[[!stable]]">
<div class="summary-browser-name">
<figure>
<img src="/static/chrome-dev_64x64.png" width="36" alt="Chrome Dev" />
<figcaption>Chrome<br>Dev</figcaption>
</figure>
<figure>
<img src="/static/edge-dev_64x64.png" width="36" alt="Edge Dev" />
<figcaption>Edge<br>Dev</figcaption>
</figure>
</div>
</template>
</template>
<template is="dom-if" if="{{!isChromeEdgeCombo(browserInfo)}}">
<div class="summary-browser-name">
<figure>
<img src="[[getBrowserIcon(browserInfo, stable)]]" width="36" alt="[[getBrowserIconName(browserInfo, stable)]]" />
<template is="dom-if" if="[[stable]]">
<figcaption>[[browserInfo.tableName]]</figcaption>
</template>
<template is="dom-if" if="[[!stable]]">
<figcaption>
<template is="dom-repeat" items="[[getBrowserNameParts(browserInfo)]]" as="namePart">
[[namePart]]<br>
</template>
</figcaption>
</template>
</figure>
</div>
</template>
</div>
</template>
<template is="dom-if" if="{{isMobileScoresView}}">
<div class="summary-flex-item" tabindex="0">
<div class="summary-number score-number smaller-summary-number">--</div>
<div class="summary-browser-name">
<figure>
<img src="/static/wktr_64x64.png" width="36" alt="Safari iOS" />
<template is="dom-if" if="[[stable]]">
<figcaption>Safari</figcaption>
</template>
<template is="dom-if" if="[[!stable]]">
<figcaption>
Safari<br>iOS<br>
</figcaption>
</template>
</figure>
</div>
</div>
</template>
</div>
</div>
`;
}
static get is() {
return 'interop-summary';
}
static get properties() {
return {
year: String,
dataManager: Object,
scores: Object,
isMobileScoresView: Boolean,
stable: {
type: Boolean,
observer: '_stableChanged',
}
};
}
_stableChanged() {
this.updateSummaryScores();
}
ready() {
super.ready();
// Hide the top summary numbers if there is no investigation value.
if (!this.shouldDisplayInvestigationNumber()) {
const investigationDiv = this.shadowRoot.querySelector('#investigationSummary');
investigationDiv.style.display = 'none';
}
const summaryDiv = this.shadowRoot.querySelector('.summary-container');
summaryDiv.style.minHeight = SUMMARY_CONTAINER_MIN_HEIGHTS[this.year] || '470px';
// Don't display the interop score for Interop 2021.
if (this.year === '2021') {
const interopDiv = this.shadowRoot.querySelector('#interopSummary');
interopDiv.style.display = 'none';
}
// The summary score elements are given class names asynchronously,
// so we have to wait until they've finished rendering to update them.
afterNextRender(this, this.updateSummaryScores);
afterNextRender(this, this.setSummaryNumberSizes);
}
shouldDisplayInvestigationNumber() {
const scores = this.dataManager.getYearProp('investigationScores');
return scores !== null && scores !== undefined;
}
// roundScore defines the rounding rules for the top-level scores.
roundScore(score) {
// Round down before interop 2024.
if (parseInt(this.year) < 2024) {
return Math.floor(score / 10);
}
const roundedScore = Math.round(score / 10);
// A special case for 100.
if (roundedScore === 100 && score < 1000) {
return 99;
}
return roundedScore;
}
// Takes a summary number div and changes the value to match the score (with CountUp).
updateSummaryScore(number, score) {
score = this.roundScore(score);
const curScore = number.innerText;
new CountUp(number, score, {
startVal: curScore === '--' ? 0 : curScore
}).start();
const colors = this.calculateColor(score);
number.style.color = `color-mix(in lch, ${colors[0]} 70%, black)`;
number.style.backgroundColor = colors[1];
}
async updateSummaryScores() {
const scoreElements = this.shadowRoot.querySelectorAll('.score-number');
const scores = this.stable ? this.scores.stable : this.scores.experimental;
const summaryFeatureName = this.dataManager.getYearProp('summaryFeatureName');
// If the elements have not rendered yet, don't update the scores.
if ((!this.isMobileScoresView && scoreElements.length !== scores.length) ||
(this.isMobileScoresView && scoreElements.length !== scores.length + 1)) {
return;
}
// Update interop summary number first.
this.updateSummaryScore(scoreElements[0], scores[scores.length - 1][summaryFeatureName]);
// Update the rest of the browser scores.
for (let i = 1; i < scores.length; i++) {
this.updateSummaryScore(scoreElements[i], scores[i - 1][summaryFeatureName]);
}
// Update investigation summary separately.
if (this.shouldDisplayInvestigationNumber()) {
const investigationNumber = this.shadowRoot.querySelector('#investigationNumber');
this.updateSummaryScore(
investigationNumber, this.dataManager.getYearProp('investigationTotalScore'));
}
}
// Sets the size of the summary number bubbles based on the number of browsers.
setSummaryNumberSizes() {
const numBrowsers = this.dataManager.getYearProp('numBrowsers');
if (numBrowsers < 4) {
const scoreElements = this.shadowRoot.querySelectorAll('.summary-number');
scoreElements.forEach(scoreElement => scoreElement.classList.remove('smaller-summary-number'));
}
}
getYearProp(prop) {
return this.dataManager.getYearProp(prop);
}
// Checks if this section is displaying the Chrome/Edge combo together.
isChromeEdgeCombo(browserInfo) {
return browserInfo.tableName === 'Chrome/Edge';
}
getBrowserIcon(browserInfo, isStable) {
const icon = (isStable) ? browserInfo.stableIcon : browserInfo.experimentalIcon;
return `/static/${icon}_64x64.png`;
}
getBrowserIconName(browserInfo, isStable) {
if (isStable) {
return browserInfo.tableName;
}
return `${browserInfo.tableName} ${browserInfo.experimentalName}`;
}
// Returns the browser full names as a list of strings so we can
// render them with breaks. e.g. ["Safari", "Technology", "Preview"]
getBrowserNameParts(browserInfo) {
return [browserInfo.tableName, ...browserInfo.experimentalName.split(' ')];
}
calculateColor(score) {
const gradient = [
// Red.
{ scale: 0, color: [250, 0, 0] },
// Orange.
{ scale: 33.33, color: [250, 125, 0] },
// Yellow.
{ scale: 66.67, color: [220, 220, 0] },
// Green.
{ scale: 100, color: [0, 160, 0] },
];
let color1, color2;
for (let i = 1; i < gradient.length; i++) {
if (score <= gradient[i].scale) {
color1 = gradient[i - 1];
color2 = gradient[i];
break;
}
}
const colorWeight = ((score - color1.scale) / (color2.scale - color1.scale));
const color = [
Math.round(color1.color[0] * (1 - colorWeight) + color2.color[0] * colorWeight),
Math.round(color1.color[1] * (1 - colorWeight) + color2.color[1] * colorWeight),
Math.round(color1.color[2] * (1 - colorWeight) + color2.color[2] * colorWeight),
];
return [
`rgb(${color[0]}, ${color[1]}, ${color[2]})`,
`rgba(${color[0]}, ${color[1]}, ${color[2]}, 0.15)`,
];
}
}
export { InteropSummary };