2015-10-09 06:49:18 +08:00
|
|
|
|
|
|
|
<template>
|
|
|
|
<div class="image-picker">
|
|
|
|
<div>
|
2015-11-02 04:00:57 +08:00
|
|
|
<img v-if="image && image !== 'none'" :src="image" :class="imageClass" alt="Image Preview">
|
2015-11-10 03:20:44 +08:00
|
|
|
<img v-if="image === '' && defaultImage" :src="defaultImage" :class="imageClass" alt="Image Preview">
|
2015-10-09 06:49:18 +08:00
|
|
|
</div>
|
2015-11-10 03:20:44 +08:00
|
|
|
<button class="button" type="button" @click="showImageManager">Select Image</button>
|
2015-10-09 06:49:18 +08:00
|
|
|
<br>
|
2015-12-10 06:30:55 +08:00
|
|
|
<button class="text-button" @click="reset" type="button">Reset</button> <span v-show="showRemove" class="sep">|</span> <button v-show="showRemove" class="text-button neg" @click="remove" type="button">Remove</button>
|
|
|
|
<input type="hidden" :name="name" :id="name" v-model="value">
|
2015-10-09 06:49:18 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
module.exports = {
|
2015-12-10 06:30:55 +08:00
|
|
|
props: {
|
|
|
|
currentImage: {
|
|
|
|
required: true,
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
currentId: {
|
|
|
|
required: false,
|
|
|
|
default: 'false',
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
required: true,
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
defaultImage: {
|
|
|
|
required: true,
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
imageClass: {
|
|
|
|
required: true,
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
resizeWidth: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
resizeHeight: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
resizeCrop: {
|
|
|
|
type: Boolean
|
|
|
|
},
|
|
|
|
showRemove: {
|
|
|
|
type: Boolean,
|
|
|
|
default: 'true'
|
|
|
|
}
|
|
|
|
},
|
2015-10-09 06:49:18 +08:00
|
|
|
data: function() {
|
|
|
|
return {
|
2015-12-10 06:30:55 +08:00
|
|
|
image: this.currentImage,
|
|
|
|
value: false
|
2015-10-09 06:49:18 +08:00
|
|
|
}
|
|
|
|
},
|
2015-12-10 06:30:55 +08:00
|
|
|
compiled: function() {
|
|
|
|
this.value = this.currentId === 'false' ? this.currentImage : this.currentId;
|
|
|
|
},
|
2015-10-09 06:49:18 +08:00
|
|
|
methods: {
|
2015-12-10 06:30:55 +08:00
|
|
|
setCurrentValue: function(imageModel, imageUrl) {
|
|
|
|
this.image = imageUrl;
|
|
|
|
this.value = this.currentId === 'false' ? imageUrl : imageModel.id;
|
|
|
|
},
|
2015-10-09 06:49:18 +08:00
|
|
|
showImageManager: function(e) {
|
2015-12-28 23:58:13 +08:00
|
|
|
ImageManager.show((image) => {
|
|
|
|
this.updateImageFromModel(image);
|
2015-10-09 06:49:18 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
reset: function() {
|
2015-12-10 06:30:55 +08:00
|
|
|
this.setCurrentValue({id: 0}, this.defaultImage);
|
2015-10-09 06:49:18 +08:00
|
|
|
},
|
|
|
|
remove: function() {
|
|
|
|
this.image = 'none';
|
2015-12-28 23:58:13 +08:00
|
|
|
this.value = 'none';
|
2015-12-10 06:30:55 +08:00
|
|
|
},
|
|
|
|
updateImageFromModel: function(model) {
|
2015-12-28 23:58:13 +08:00
|
|
|
var isResized = this.resizeWidth && this.resizeHeight;
|
2015-12-10 06:30:55 +08:00
|
|
|
|
|
|
|
if (!isResized) {
|
2015-12-28 23:58:13 +08:00
|
|
|
this.setCurrentValue(model, model.url);
|
2015-12-10 06:30:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:58:13 +08:00
|
|
|
var cropped = this.resizeCrop ? 'true' : 'false';
|
|
|
|
var requestString = '/images/thumb/' + model.id + '/' + this.resizeWidth + '/' + this.resizeHeight + '/' + cropped;
|
|
|
|
this.$http.get(requestString).then((response) => {
|
|
|
|
this.setCurrentValue(model, response.data.url);
|
2015-12-10 06:30:55 +08:00
|
|
|
});
|
2015-10-09 06:49:18 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-02 04:00:57 +08:00
|
|
|
};
|
2015-10-09 06:49:18 +08:00
|
|
|
</script>
|