-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw-status.html
171 lines (160 loc) · 6.96 KB
/
sw-status.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
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
<link href="../paper-fab/paper-fab.html" rel="import">
<link href="../core-icons/core-icons.html" rel="import">
<link href="../paper-shadow/paper-shadow.html" rel="import">
<!--
Element that displays the ServiceWorker status for this page. Currently it displays whether the page is controlled or not, and the registration for the ServiceWorker that can control this page. In the future this can enumerate the info for all ServiceWorkers under the current domain.
##### Example
<sw-status></sw-status>
@element sw-status
@blurb Element showing ServiceWorker status.
@status beta
@homepage http://dmurph.github.io/sw-status-element
-->
<polymer-element name="sw-status">
<template>
<link rel="stylesheet" href="sw-status.css">
<paper-shadow z="1" id="paper-container">
<div id="container">
<div id="title" class="{{titleStyle}}">
<paper-fab id="refresh-button" icon="refresh" mini on-click="{{refreshClicked}}"></paper-fab>
</div>
<div id="content">
<div class="tabs">
<paper-shadow class="tab {{controlledStyle}}" hidden?="{{(controlledText == null)}}">
<h1>{{controlledTitle}}</h1>
<template repeat="{{text in controlledText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
<paper-shadow class="tab {{infoStyle}}" hidden?="{{(infoText == null)}}">
<h1>Info</h1>
<template repeat="{{text in infoText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
<paper-shadow class="tab error" hidden?="{{(errorText == null)}}">
<h1>Error</h1>
<template repeat="{{text in errorText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
</div>
<template repeat="{{registration in registrations}}">
<div class="tabs">
<paper-shadow class="tab {{registration.registrationClass}}" hidden?="{{registration.registrationText == null}}">
<h1>Registration</h1>
<template repeat="{{text in registration.registrationText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
<paper-shadow class="tab installing" hidden?="{{registration.installingText == null}}">
<h1>Installing Service Worker</h1>
<template repeat="{{text in registration.installingText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
<paper-shadow class="tab waiting" hidden?="{{registration.waitingText == null}}">
<h1>Waiting Service Worker</h1>
<template repeat="{{text in registration.waitingText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
<paper-shadow class="tab active" hidden?="{{registration.activeText == null}}">
<h1>Active Service Worker</h1>
<template repeat="{{text in registration.activeText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
<paper-shadow class="tab error" hidden?="{{(registration.errorText == null)}}">
<h1>Error</h1>
<template repeat="{{text in registration.errorText}}">
<p>{{text}}</p>
</template>
</paper-shadow>
</div>
</template>
</div>
</div>
<div style="clear: right"></div>
</paper-shadow>
</template>
<script>
Polymer({
titleStyle: 'badText',
controlledStyle: 'installing',
controlledTitle: 'Not Controlled',
controlledText: ['This page is not controlled by a ServiceWorker.'],
registrations: [],
errorTitle: 'Error',
errorText: null,
infoText: null,
infoStyle: '',
ready: function() {
this.refreshAll();
},
getServiceWorkerDescription: function(worker) {
return ['Script: ' + worker.scriptURL, 'State: ' + worker.state];
},
refreshAll: function() {
this.registrations = [];
this.errorText = null;
this.infoText = null;
this.infoStyle = '';
this.controlledStyle = 'installing';
this.controlledTitle = 'Not Controlled';
this.controlledText = ['This page is not controlled by a ServiceWorker.'];
if (!navigator.serviceWorker) {
this.errorText = ['Service Workers are not enabled in this browser.'];
return;
}
var controller = navigator.serviceWorker.controller;
if (controller) {
this.controlledStyle = 'active';
this.controlledTitle = 'Controlled';
this.controlledText = ['This page is controlled by a ServiceWorker.'];
}
var _this = this;
navigator.serviceWorker.getRegistration(window.location.pathname)
.then(function(registration) {
var registrationObject = {
registrationText: null,
installingText: null,
waitingText: null,
activeText: null,
errorText: null,
registrationClass: null
};
if (!registration) {
_this.infoStyle = 'installing';
_this.infoText = ['No registration available to control page "' + window.location.pathname + '".'];
// We are still controlled, but the SW is unregistered.
if (controller) {
registrationObject.registrationText = ['No registration, but this page is still controlled.'];
registrationObject.registrationClass = 'error';
registrationObject.activeText = _this.getServiceWorkerDescription(controller);
}
_this.registrations.push(registrationObject);
return;
}
registrationObject.registrationText = ['Scope: ' + registration.scope];
var worker = null;
if (registration.installing) {
registrationObject.installingText = _this.getServiceWorkerDescription(registration.installing);
} else if (registration.waiting) {
registrationObject.waitingText = _this.getServiceWorkerDescription(registration.waiting);
} else if (registration.active) {
registrationObject.activeText = _this.getServiceWorkerDescription(registration.active);
} else {
registrationObject.errorText = ['No registration found.'];
}
_this.registrations.push(registrationObject);
}, function(why) {
_this.errorText = ['Error getting registration for path "' + window.location.pathname + '": ' + why];
});
},
refreshClicked: function(event, detail, sender) {
this.refreshAll();
}
});
</script>
</polymer-element>