-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly-badge.html
366 lines (319 loc) · 8.34 KB
/
poly-badge.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!--
@license
Copyright 2015 See the CONTRIBUTORS.txt file
("http://LM450N.github.io/poly-badge/components/poly-badge/CONTRIBUTORS.txt")
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-material/paper-material.html">
<!--
A poly-badge is a pure Polymer component to display a count or a tiny label.
##### Examples
<poly-badge>5</poly-badge>
<poly-badge>Hello</poly-badge>
<poly-badge wide>42</poly-badge>
<poly-badge elevation="1">Shadow is added</poly-badge>
<poly-badge info></poly-badge>
<poly-badge color="white" bgc="black">Black & White</poly-badge>
@element poly-badge
@demo demo/index.html
@demo https://LM450N.github.io/poly-badge/components/poly-badge/demo/
@blurb A pure Polymer component to display a count or a tiny label.
@homepage https://LM450N.github.io/poly-badge/components/poly-badge/
-->
<dom-module id="poly-badge">
<style>
:host {
display: inline-block;
line-height: initial;
text-align: center;
color: #fff;
margin: 0 2px;
}
paper-material {
border-radius: 50%;
padding: 3px 0;
}
.wide {
border-radius: 3px;
padding: 2px 4px;
}
.info {
background-color: #03A9F4;
}
.warning {
background-color: #FF9800;
}
.error {
background-color: #F44336;
}
.XS {
font-size: 0.7em;
}
.S {
font-size: 0.8em;
}
.L {
font-size: 1.2em;
}
.XL {
font-size: 1.4em;
}
</style>
<template>
<paper-material id="shadow" elevation="[[elevation]]" class$="{{_classes}}"
animated>
<template is="dom-if" if="[[_isMarker]]"><span>{{_label}}</span></template>
<template is="dom-if" if="[[!_isMarker]]"><content></content></template>
</paper-material>
</template>
<script>
(function() {
/**
* Defaults colors.
* @type {String}
*/
var fuschia = '#E91E63',
white = '#ffffff';
Polymer({
is: 'poly-badge',
properties: {
/**
* Forces the badge to be "large".
*
* @attribute wide
* @type {Boolean}
* @default false
*/
wide: {
type: Boolean,
value: false
},
/**
* The elevation level of the shadow, from 0-5. Setting this property
* after element creation has no effect. Use setElevation() instead.
*
* @attribute elevation
* @type {Number}
* @default 0
*/
elevation: {
type: Number,
value: 0
},
/**
* Sets the badge like an info marker. Small font size, white on
* background light-blue.
*
* @attribute info
* @type {Boolean}
* @default false
*/
info: {
type: Boolean,
value: false
},
/**
* Sets the badge like a warning marker. Small font size, white on
* background light-blue.
*
* @attribute warning
* @type {Boolean}
* @default false
*/
warning: {
type: Boolean,
value: false
},
/**
* Sets the badge like an error marker. Small font size, white on
* background light-blue.
*
* @attribute error
* @type {Boolean}
* @default false
*/
error: {
type: Boolean,
value: false
},
/**
* Sets the text color.
*
* @attribute color
* @type {String}
* @default #fff (white)
*/
color: {
type: String,
value: '#ffffff'
},
/**
* Sets the background color.
*
* @attribute bgc
* @type {String}
* @default #E91E63 (fuschia)
*/
bgc: {
type: String,
value: '#E91E63'
},
/**
* Sets the font-size to 0.7em.
*
* @attribute xs
* @type {Boolean}
* @default false
*/
xs: {
type: Boolean,
value: false
},
/**
* Sets the font-size to 0.8em.
*
* @attribute s
* @type {Boolean}
* @default false
*/
s: {
type: Boolean,
value: false
},
/**
* Sets the font-size to 1.2em.
*
* @attribute l
* @type {Boolean}
* @default false
*/
l: {
type: Boolean,
value: false
},
/**
* Sets the font-size to 1.4em.
*
* @attribute xl
* @type boolean
* @default false
*/
xl: {
type: Boolean,
value: false
},
/**
* Label used for info, warning and error markers.
*
* @type {String}
*/
_label: {
type: String,
computed: '_computeLabel(info, warning, error, xs)'
},
/**
* Defines if the badge is a marker.
*
* @type {Boolean}
*/
_isMarker: {
type: Boolean,
computed: '_isAMarker(_label)'
},
/**
* Classes names set.
*
* @type {String}
*/
_classes: {
type: String,
computed: '_computeClasses(wide, info, warning, error, elevation,\
xl, l, s, xs)'
}
},
ready: function() {
this.setColor(this.color || white);
this.setBgc(this.bgc || fuschia);
},
attached: function() {
this.async(function() {
/* Defines if the badge style, wide or not. */
this.wide = this.wide || this.clientWidth > 24;
/* Initializes the min-width. */
if (this.elevation) {
this.$.shadow.style.setProperty(
'min-width',
this.clientHeight + 'px'
);
} else {
this.style.setProperty('min-width', this.clientHeight + 'px');
}
}, 1);
},
/**
* Sets the badge text color. This should be used after element
* creation instead of setting the `color` property directly.
*
* @method setColor
* @param {String} newColor
*/
setColor: function(newColor) {
if (!this._isMarker) {
this.$.shadow.style.setProperty('color', newColor);
}
},
/**
* Sets the badge background color. This should be used after element
* creation instead of setting the `bgc` property directly.
*
* @method setBgc
* @param {String} newColor
*/
setBgc: function(newColor) {
if (!this._isMarker) {
this.$.shadow.style.setProperty('background-color', newColor);
}
},
/**
* Sets the z-depth of the shadow. This should be used after element
* creation instead of setting the `elevation` property directly.
*
* @method setElevation
* @param {Number} newLevel
*/
setElevation: function(newLevel) {
this.$.shadow.elevation = newLevel;
},
_computeLabel: function(info, warning, error, xs) {
return error && (xs ? "!!!" : "ERROR") ||
warning && (xs ? "!" : "WARNING") ||
info && (xs ? "i" : "INFO");
},
_isAMarker: function(label) {
return label ? true : false;
},
_computeClasses: function(wide, info, warning, error, elevation,
xl, l, s, xs) {
return (wide ? ' wide' : '')
+ (info ?' info' : '')
+ (warning ? ' warning' :'')
+ (error ? ' error' : '')
+ (elevation ? ' elevation' : '')
+ (xl ?' XL' : '')
+ (l ? ' L' : '')
+ (s ? ' S' :'')
+ (xs ? ' XS' : '');
}
})
})();
</script>
</dom-module>