Files
element/packages/message/src/main.vue
2017-08-05 17:52:53 +08:00

108 lines
2.4 KiB
Vue

<template>
<transition name="el-message-fade">
<div
class="el-message"
:class="customClass"
v-show="visible"
@mouseenter="clearTimer"
@mouseleave="startTimer">
<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-if="!dangerouslyUseHTMLString">{{ message }}</p>
<p v-else v-html="message"></p>
</slot>
<div v-if="showClose" class="el-message__closeBtn el-icon-close" @click="close"></div>
</div>
</div>
</transition>
</template>
<script type="text/babel">
const typeMap = {
success: 'circle-check',
info: 'information',
warning: 'warning',
error: 'circle-cross'
};
export default {
data() {
return {
visible: false,
message: '',
duration: 3000,
type: 'info',
iconClass: '',
customClass: '',
onClose: null,
showClose: false,
closed: false,
timer: null,
dangerouslyUseHTMLString: false
};
},
computed: {
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`
: '';
}
},
watch: {
closed(newVal) {
if (newVal) {
this.visible = false;
this.$el.addEventListener('transitionend', this.destroyElement);
}
}
},
methods: {
destroyElement() {
this.$el.removeEventListener('transitionend', this.destroyElement);
this.$destroy(true);
this.$el.parentNode.removeChild(this.$el);
},
close() {
this.closed = true;
if (typeof this.onClose === 'function') {
this.onClose(this);
}
},
clearTimer() {
clearTimeout(this.timer);
},
startTimer() {
if (this.duration > 0) {
this.timer = setTimeout(() => {
if (!this.closed) {
this.close();
}
}, this.duration);
}
}
},
mounted() {
this.startTimer();
}
};
</script>