forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldImage.vue
107 lines (93 loc) · 2.35 KB
/
fieldImage.vue
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
<template lang="pug">
div.wrapper
input.form-control.link(type="text", v-show="fieldOptions.hideInput !== true", v-model="wrappedValue", :autocomplete="fieldOptions.autocomplete", :disabled="disabled", :placeholder="placeholder", :readonly="readonly")
input.form-control.file(type="file", v-if="fieldOptions.browse !== false", :disabled="disabled", @change="fileChanged", :name="inputName")
.preview(:style="previewStyle")
.remove(title="Remove image", @click="remove")
</template>
<script>
import abstractField from "../abstractField";
export default {
name: "field-image",
mixins: [abstractField],
computed: {
previewStyle() {
if (this.fieldOptions.preview !== false) {
return {
display: "block",
"background-image": this.value != null ? "url(" + this.value + ")" : "none"
};
} else {
return {
display: "none"
};
}
},
wrappedValue: {
get() {
if (this.value && this.value.indexOf("data") === 0) return "<inline base64 image>";
else return this.value;
},
set(newValue) {
if (newValue && newValue.indexOf("http") === 0) {
this.value = newValue;
}
}
}
},
watch: {
model() {
let el = this.$el.querySelector("input.file");
if (el) {
el.value = "";
}
}
},
methods: {
remove() {
this.value = "";
},
fileChanged(event) {
let reader = new FileReader();
reader.onload = (e) => {
this.value = e.target.result;
};
if (event.target.files && event.target.files.length > 0) {
reader.readAsDataURL(event.target.files[0]);
}
}
}
};
</script>
<style lang="scss">
.vue-form-generator .field-image {
.wrapper {
width: 100%;
}
.preview {
position: relative;
margin-top: 5px;
height: 100px;
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
border: 1px solid #ccc;
border-radius: 3px;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
.remove {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAXUlEQVR42u2SwQoAIAhD88vVLy8KBlaS0i1oJwP3piGVg0Skmpq8HjqZrWl9uwCbGAmwKYGZs/6iqgMyAdJuM8W2QmYKpLt/0AG9ASCv/oAnANd3AEjmAlFT1BypAV+PnRH5YehvAAAAAElFTkSuQmCC");
width: 16px;
height: 16px;
font-size: 1.2em;
position: absolute;
right: 0.2em;
bottom: 0.2em;
opacity: 0.7;
&:hover {
opacity: 1;
cursor: pointer;
}
}
}
}
</style>