Skip to content

Commit 053f445

Browse files
committed
Image field added.
1 parent 4e7d5ba commit 053f445

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ TODO
4040
* [x] Time picker
4141
* [x] HTML5 Color picker
4242
* [x] Color picker with spectrum
43-
* [ ] Image editor
43+
* [x] Image editor
4444
* [ ] Better slider
4545
* [ ] Groupable fields
4646
* [ ] Validation handling with multiple models

dev/data.js

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = {
3434
});
3535
user.role = faker.helpers.randomize(roles).id;
3636
//user.mobile = faker.phone.phoneNumber();
37+
user.avatar = faker.internet.avatar();
3738

3839
user.skills = [];
3940
user.skills.push(faker.helpers.randomize(skills));

dev/schema.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@ module.exports = {
126126
colorOptions: {
127127
//preferredFormat: "rgb"
128128
}
129-
},
129+
},
130+
{
131+
type: "image",
132+
label: "Avatar",
133+
model: "avatar",
134+
browse: true
135+
136+
},
130137
{
131138
type: "number",
132139
label: "Age",
@@ -290,8 +297,8 @@ module.exports = {
290297
if (!model.address.geo)
291298
model.address.geo = {};
292299

293-
model.address.geo.lat = pos.coords.latitude;
294-
model.address.geo.lng = pos.coords.longitude;
300+
model.address.geo.lat = pos.coords.latitude.toFixed(5);
301+
model.address.geo.lng = pos.coords.longitude.toFixed(5);
295302
});
296303
} else {
297304
alert("Geolocation is not supported by this browser.");

src/fields/fieldImage.vue

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<template lang="jade">
2+
div.wrapper
3+
input.form-control.link(type="text", v-model="wrappedValue", :readonly="schema.readonly", :disabled="disabled", :placeholder="schema.placeholder")
4+
input.form-control.file(type="file", :readonly="schema.readonly", :disabled="disabled", v-if="schema.browse !== false", @change="fileChanged")
5+
.preview(:style="previewStyle")
6+
.remove(title="Remove image", @click="remove")
7+
</template>
8+
9+
<script>
10+
import abstractField from './abstractField';
11+
12+
export default {
13+
mixins: [ abstractField ],
14+
15+
computed: {
16+
previewStyle() {
17+
if (this.schema.preview !== false) {
18+
return {
19+
display: "block",
20+
"background-image": "url(" + this.value + ")"
21+
}
22+
} else {
23+
return {
24+
display: "none"
25+
}
26+
}
27+
},
28+
29+
wrappedValue: {
30+
get() {
31+
if (this.value && this.value.indexOf("data") == 0)
32+
return "<inline base64 image>"
33+
else
34+
return this.value;
35+
},
36+
set(newValue) {
37+
if (newValue && newValue.indexOf("http") == 0) {
38+
this.value = newValue
39+
}
40+
}
41+
}
42+
},
43+
44+
watch: {
45+
model() {
46+
$(this.$el).find("input.file").val("");
47+
}
48+
},
49+
50+
methods: {
51+
remove() {
52+
this.value = "";
53+
},
54+
55+
fileChanged(event) {
56+
var reader = new FileReader();
57+
reader.onload = (e) => {
58+
this.value = e.target.result;
59+
}
60+
61+
if (event.target.files && event.target.files.length > 0) {
62+
reader.readAsDataURL(event.target.files[0]);
63+
}
64+
}
65+
}
66+
}
67+
</script>
68+
69+
<style lang="sass" scoped>
70+
.wrapper, input {
71+
width: 100%;
72+
}
73+
74+
.preview {
75+
position: relative;
76+
margin-top: 5px;
77+
height: 100px;
78+
background-repeat: no-repeat;
79+
background-size: contain;
80+
background-position: center center;
81+
border: 1px solid #ccc;
82+
border-radius: 3px;
83+
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
84+
85+
.remove {
86+
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAXUlEQVR42u2SwQoAIAhD88vVLy8KBlaS0i1oJwP3piGVg0Skmpq8HjqZrWl9uwCbGAmwKYGZs/6iqgMyAdJuM8W2QmYKpLt/0AG9ASCv/oAnANd3AEjmAlFT1BypAV+PnRH5YehvAAAAAElFTkSuQmCC');
87+
width: 16px;
88+
height: 16px;
89+
90+
position: absolute;
91+
right: 2px;
92+
bottom: 2px;
93+
opacity: 0.7;
94+
95+
&:hover {
96+
opacity: 1.0;
97+
cursor: pointer;
98+
}
99+
}
100+
}
101+
</style>

0 commit comments

Comments
 (0)