-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathh2-image-view.js
144 lines (124 loc) · 2.99 KB
/
h2-image-view.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
143
144
/*
`h2-image-view`
Example:
```html
<h2-image-view src="https://test.com/test.jpg"></h2-image-view>
```
```
## Styling
The following custom properties and mixins are available for styling:
|Custom property | Description | Default|
|----------------|-------------|----------|
|`--h2-image-upload-button` | Mixin applied to tool button of the viewer | {}
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import './h2-button.js';
/**
* @customElement
* @polymer
* @demo demo/h2-image-view/index.html
*/
class H2ImageView extends Polymer.mixinBehaviors([], Polymer.Element) {
static get template() {
return Polymer.html`
<style>
:host {
display: flex;
width: 140px;
height: 180px;
flex-flow: column nowrap;
text-align: center;
border: 1px dashed #ccc;
font-size: 14px;
background: #f0f0f0;
}
.img__container {
flex: 1;
cursor: zoom-in;
background-size: contain;
background: no-repeat center;
}
.toolbar {
display: flex;
background: rgba(79, 79, 79, 0.33);
height: 36px;
justify-content: center;
align-items: center;
justify-self: flex-end;
}
.toolbar h2-button {
height: 26px;
--h2-button: {
background-color: #5cb85c;
@apply --h2-image-upload-button;
}
}
.img__viewer {
display: flex;
overflow: hidden;
width: 90%;
height: 90%;
background: transparent;
padding: 0;
}
#viewer-img {
cursor: zoom-out;
padding: 0;
margin: auto;
width: 100%;
height: 100%;
}
</style>
<div class="img__container" id="result-img" on-click="openViewZoom"></div>
<div class="toolbar">
<h2-button on-click="openViewZoom">查看大图</h2-button>
</div>
<paper-dialog class="img__viewer" id="dialog" on-click="closeViewZoom" with-backdrop="">
<div id="viewer-img"></div>
</paper-dialog>
`;
}
static get properties() {
return {
/**
* The remote uri of image.
*/
src: {
type: String,
observer: '_changeSrc'
},
};
}
static get is() {
return "h2-image-view";
}
_changeSrc(newValue) {
if (newValue) {
const style = this.$["result-img"].style;
const viewerStyle = this.$['viewer-img'].style;
style.background = `url(${newValue}) no-repeat center`;
style.backgroundSize = "contain";
viewerStyle.background = `url(${newValue}) no-repeat center`;
viewerStyle.backgroundSize = "contain";
}
}
/**
* Open the view zoom
*/
openViewZoom() {
if (this.src) {
this.$.dialog.open();
}
}
/**
* Close the view zoom.
*/
closeViewZoom() {
this.$.dialog.close();
}
}
window.customElements.define(H2ImageView.is, H2ImageView);