|
| 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