-
Notifications
You must be signed in to change notification settings - Fork 2
/
xhr-handler.html
122 lines (106 loc) · 3.44 KB
/
xhr-handler.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
<script src="../custom-event-polyfill/custom-event-polyfill.js"></script>
<script src="../xhr-events/xhr-events.js"></script>
<link rel="import" href="../paper-spinner/paper-spinner.html">
<link rel="import" href="../paper-toast/paper-toast.html">
<!--
A web component that handles xhr events.
1. handles displaying and hiding of load mask
2. common error handling - you may set xhrErrors property for l10n/i18n.
Example:
<xhr-handler></xhr-handler>
-->
<dom-module id='xhr-handler'>
<style>
#loadMask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.3);
display: none;
}
#loadMask paper-spinner {
top: 50%;
left: 50%;
margin-left: -14px;
}
</style>
<template>
<paper-toast id="toast"></paper-toast>
<div id="loadMask">
<paper-spinner active></paper-spinner>
</div>
</template>
<script type="text/javascript">
Polymer({
is: 'xhr-handler',
properties: {
/**
* errors corresponding to HTTP failure status codes such as 4xx and 5xx.
*/
xhrErrors: {
type: Object,
value: {
401: 'Session timed out. Please login.',
403: 'Your are unauthorized to perform this action',
404: 'The application seems to be down at the moment. Please contact the administrator for more information.',
500: 'An Internal Server Error has occured. Please contact the administrator for more information.',
503: 'Temporarily unable to handle your request due to overloading or maintenance of the server. Please try again after sometime.'
}
},
/**
* Specify URI patterns (RegExp) for which error handling should be skipped.
* e.g: { 401: [ /^\/skipUri$/ ] } skips showing any error message for 401 errors on URIs matching ^/skipUri$.
*/
skipPatterns: {
type: Object,
value: {}
}
},
attached: function() {
var proto = Object.getPrototypeOf(this);
if (!proto.handled) {
proto.handled = true;
this.async(this._handle);
}
},
/**
* The event handlers are bound here.
*/
_handle: function() {
var me = this;
document.addEventListener('xhr-request', me._showMask.bind(me));
document.addEventListener('xhr-response', me._hideMask.bind(me));
document.addEventListener('xhr-error', me._hideMask.bind(me));
document.addEventListener('xhr-error', function(event) {
if (event.detail && event.detail.request && event.detail.request.status) {
var patterns = me.skipPatterns[event.detail.request.status];
if (patterns && patterns.length > 0) {
var i;
for (i = 0; i < patterns.length; i += 1) {
if (patterns[i].test(event.detail.request.url)) { return; }
}
}
if (me.xhrErrors[event.detail.request.status]) {
me.$.toast.text = me.xhrErrors[event.detail.request.status];
me.$.toast.show();
}
}
});
},
/**
* Shows the loadMask.
*/
_showMask: function() {
this.$.loadMask.style.display = 'block';
},
/**
* Hides the loadMask.
*/
_hideMask: function() {
this.$.loadMask.style.display = 'none';
}
});
</script>
</dom-module>