element/packages/message/src/main.vue

106 lines
2.3 KiB
Vue
Raw Normal View History

2016-08-15 03:51:25 +00:00
<template>
2016-08-15 08:10:12 +00:00
<transition name="el-message-fade">
<div
class="el-message"
:class="customClass"
v-show="visible"
@mouseenter="clearTimer"
@mouseleave="startTimer">
2017-08-01 12:32:20 +00:00
<div :class="iconWrapClass">
<i :class="iconClass" v-if="iconClass"></i>
<i :class="typeClass" v-else></i>
</div>
<div class="el-message__group">
<slot>
<p v-html="message"></p>
</slot>
<div v-if="showClose" class="el-message__closeBtn el-icon-close" @click="close"></div>
2016-08-15 03:51:25 +00:00
</div>
</div>
</transition>
</template>
<script type="text/babel">
2017-08-01 12:32:20 +00:00
const typeMap = {
success: 'circle-check',
info: 'information',
warning: 'warning',
error: 'circle-cross'
};
2016-08-15 03:51:25 +00:00
export default {
data() {
return {
visible: false,
message: '',
2016-08-15 08:10:12 +00:00
duration: 3000,
type: 'info',
iconClass: '',
customClass: '',
2016-08-15 03:51:25 +00:00
onClose: null,
2016-08-15 08:10:12 +00:00
showClose: false,
2016-08-15 03:51:25 +00:00
closed: false,
timer: null
};
},
computed: {
2017-08-01 12:32:20 +00:00
iconWrapClass() {
const classes = ['el-message__icon'];
if (this.type && !this.iconClass) {
classes.push(`el-message__icon--${ this.type }`);
}
return classes;
},
typeClass() {
return this.type && !this.iconClass
? `el-message__type el-icon-${ typeMap[this.type] }-plain`
: '';
2016-08-15 03:51:25 +00:00
}
},
watch: {
closed(newVal) {
if (newVal) {
this.visible = false;
this.$el.addEventListener('transitionend', this.destroyElement);
2016-08-15 03:51:25 +00:00
}
}
},
methods: {
destroyElement() {
this.$el.removeEventListener('transitionend', this.destroyElement);
this.$destroy(true);
this.$el.parentNode.removeChild(this.$el);
},
close() {
2016-08-15 03:51:25 +00:00
this.closed = true;
if (typeof this.onClose === 'function') {
2016-10-21 03:22:35 +00:00
this.onClose(this);
2016-08-15 03:51:25 +00:00
}
},
clearTimer() {
clearTimeout(this.timer);
},
startTimer() {
if (this.duration > 0) {
this.timer = setTimeout(() => {
if (!this.closed) {
this.close();
2016-08-15 03:51:25 +00:00
}
}, this.duration);
}
}
},
mounted() {
2016-08-15 08:10:12 +00:00
this.startTimer();
2016-08-15 03:51:25 +00:00
}
};
</script>