-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube-icon.js
142 lines (121 loc) · 3.45 KB
/
cube-icon.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
import {PolymerElement, html} from '@polymer/polymer/polymer-element.js';
import '@webcomponents/shadycss/apply-shim.min.js'
import '@polymer/paper-tooltip/paper-tooltip.js';
import '@polymer/iron-icon/iron-icon.js';
import '@kubex/cube-pagelet/cube-pagelet-content.js';
import {CubeTextContentBehavior} from '@kubex/cube-behaviors/cube-text-content-behavior.js';
/**
An element providing a solution to no problem in particular.
Example:
<cube-icon></cube-icon>
Example:
<cube-icon>
<h2>Hello cube-icon</h2>
</cube-icon>
@demo demo/index.html
@hero hero.svg
*/
class CubeIcon extends CubeTextContentBehavior(PolymerElement) {
static get is() {return 'cube-icon';}
static get template()
{
return html`<style>
:host {
display: inline-flex;
line-height: normal;
box-sizing: content-box;
border-color: var(--cube-icon-background-color);
background-color: var(--cube-icon-background-color);
color: var(--cube-icon-color);
fill: currentColor;
padding: var(--cube-icon-padding, 5px);
width: var(--cube-icon-size, 24px);
height: var(--cube-icon-size, 24px);
}
:host([round]) {
border-radius: 100%;
}
iron-icon,
svg,
::slotted(svg) {
display: block;
width: 100%;
height: 100%;
}
</style>
<template is="dom-if" if="[[_iconIsToken]]">
<iron-icon icon="[[icon]]" src="[[src]]"></iron-icon>
</template>
<template is="dom-if" if="[[!_iconIsToken]]">
<cube-pagelet-content html="[[_decodedIcon]]"></cube-pagelet-content>
</template>
<slot></slot>
<template is="dom-if" if="[[alt]]">
<paper-tooltip>[[alt]]</paper-tooltip>
</template>`
}
static get properties()
{
return {
icon: {type: String},
src: {type: String},
round: {type: Boolean, reflectToAttribute: true},
alt: {type: String, value: ''},
size: {type: Number, observer: '_styleChanged'},
color: {type: String, value: '', observer: '_styleChanged'},
background: {type: String, value: '', observer: '_styleChanged'},
_iconIsToken: {type: Boolean, value: false, computed: '_isToken(icon,src)'},
_decodedIcon: {type: String, computed: '_computeDecodedIcon(_iconIsToken,icon)'}
}
}
connectedCallback()
{
super.connectedCallback();
// strip width/height from svg to allow it to scale to [[size]]
let svg = this.querySelector('svg');
if(svg)
{
svg.style.width = '100%';
svg.style.height = '100%';
if(svg.hasAttribute('width'))
{
svg.removeAttribute('width');
}
if(svg.hasAttribute('height'))
{
svg.removeAttribute('height');
}
}
}
_styleChanged()
{
let props = {
'--cube-icon-background-color': this.background,
'--cube-icon-color': this.color
};
if(this.size)
{
props['--cube-icon-size'] = this.size + 'px';
}
this.updateStyles(props);
}
_isToken(icon, src)
{
return src || (icon && !!(/^[a-z0-9:-]+$/i.test(icon)));
}
_computeDecodedIcon(_iconIsToken, icon)
{
if(!_iconIsToken && icon)
{
return icon.replace(/%([a-z0-9]{2})/g, this.__decodeChar);
}
return '';
}
__decodeChar(full, code)
{
return String.fromCharCode(
parseInt(code, full[0] === '%' ? 16 : 10)
);
}
}
customElements.define(CubeIcon.is, CubeIcon);