-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdash-performance-budget.html
122 lines (107 loc) · 3.47 KB
/
webdash-performance-budget.html
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
<link rel="import" href="/bower_components/polymer/polymer-element.html">
<link rel="import" href="/bower_components/paper-spinner/paper-spinner.html">
<link rel="import" href="/bower_components/iron-icons/iron-icons.html">
<link rel="import" href="/bower_components/empty-state-webdash/empty-state-webdash.html">
<script src="/bower_components/circles-el/circles.min.js"></script>
<dom-module id="webdash-performance-budget">
<template>
<style>
:host {
display: block;
}
#plugin {
margin-top: 25px;
margin-bottom: 25px;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.circles-text {
font-size: 48px !important;
}
.limit-reached {
position: absolute;
margin-top: -50px;
color: var(--danger);
}
</style>
<div id="plugin">
<template is="dom-if" if="{{limitReached}}">
<iron-icon icon="warning" class="limit-reached"></iron-icon>
</template>
<div class="circle" id="chart"></div>
<template is="dom-if" if="{{emptyState}}">
<empty-state-webdash title="No JavaScript files found">
Adjust the
<strong>jsBudgetPath</strong> option in your webdash.json configuration file.
</empty-state-webdash>
</template>
</div>
</template>
<script>
/**
* `webdash-performance-budget`
*
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class WebdashPerformanceBudget extends Polymer.Element {
static get is() { return 'webdash-performance-budget'; }
ready() {
super.ready();
this.backend = new Backend('webdash-performance-budget');
this.accent = getComputedStyle(this).getPropertyValue('--accent');
// this.backend.get('gzipped').then(console.log);
this._drawChart();
}
_drawChart() {
this.backend.get('js-size')
.then(data => {
if (!data || !data.size) {
this.emptyState = true;
return false;
}
const value = data.size;
let limit = window.webdashConfig.jsBudgetLimit || 200;
let maxValue = limit * 1024;
let limitReached = false;
if (value > limit * 1024) {
maxValue = value;
limitReached = true;
}
this.limitReached = limitReached;
var myCircle = Circles.create({
el: this.$.chart,
radius: 110,
value,
maxValue,
width: 10,
colors: ['rgba(255, 255, 255, 0.5)', this.accent],
duration: 200,
wrpClass: 'circles-wrp',
textClass: 'circles-text',
valueStrokeClass: 'circles-valueStroke',
maxValueStrokeClass: 'circles-maxValueStroke',
styleWrapper: true,
styleText: true,
text: (value) => {
if (value >= (1024 * 1024)) {
return `${Math.round(value / 1024 / 1024)} MB`;
}
if (value >= 1024) {
return `${Math.round(value / 1024)} KB`;
}
return `${value} B`;
}
});
})
.catch(console.error);
}
}
window.customElements.define(WebdashPerformanceBudget.is, WebdashPerformanceBudget);
</script>
</dom-module>