Files

213 lines
5.5 KiB
Vue
Executable File

<template>
<view v-show="dialogShow" class="opsoft-dialog">
<opsoft-center-popup v-bind="$attrs" :maskBackgroundColor="dialogMaskBackground"
@maskClick="dialogMaskClick">
<view class="opsoft-dialog-title">
<slot name="dialogTitle">
<view style="overflow: hidden;width: 100%;">
{{dialogTitle}}
</view>
</slot>
</view>
<view class="opsoft-dialog-content" ref="dialogContentRef">
<slot name="dialogContent">
<view ref="dialogContentTextRef" style="word-break: break-all;">
<text>{{content}}</text>
</view>
</slot>
</view>
<view class="opsoft-dialog-buttons-margin-top" />
<slot name="dialogButton">
<view class="opsoft-dialog-buttons">
<view class="opsoft-dialog-confirm" @tap="dialogConfirmClick">
<view>
{{dialogConfirm}}
</view>
</view>
<view class="opsoft-dialog-line"/>
<view class="opsoft-dialog-cancel" @tap="dialogCancelClick">
<view>
{{dialogCancel}}
</view>
</view>
</view>
</slot>
</opsoft-center-popup>
</view>
</template>
<script>
export default {
name: "opsoft-dialog",
props: {
dialogContentTextEllipsis: {type:Boolean, default:true},
dialogContent: {type:String, default:''},
dialogTitle: {type:String, default:''},
dialogConfirm: {type:String, default:'确定'},
dialogCancel: {type:String, default:'取消'},
dialogShow: {type:Boolean, default:false},
dialogClickCallback: {type:Object},
dialogMaskBackground: {type:String, default:''},
},
data() {
return {
dialogContentLength: 0,
stopLoop: true,
overflowed: false,
content: '',
}
},
watch:{
dialogContent:function(newValue,oldValue){
if (newValue) {
this.content = newValue;
this.dialogContentLength = newValue.length;
this.$nextTick(() => {
if (this.stopLoop && this.dialogContentLength > 0) {
this.stopLoop = false;
this.checkLoop(0, this.dialogContentLength);
}
});
}
},
},
created() {
if (this.dialogContent) {
this.dialogContentLength = this.dialogContent.length;
this.content = this.dialogContent;
}
},
mounted() {
this.$nextTick(() => {
if (this.stopLoop && this.content.length > 0) {
this.stopLoop = false;
this.checkLoop(0, this.content.length);
}
});
},
methods: {
checkLoop(start, end) {
if (this.stopLoop || start + 1 >= end) {
if (this.dialogContentTextEllipsis && this.overflowed) {
this.content = this.dialogContent.substr(0, this.dialogContentLength-3) + "...";
}
this.stopLoop = true;
return;
}
const textHeight = this.$refs.dialogContentTextRef.$el.offsetHeight;
const maxHeight = this.$refs.dialogContentRef.$el.offsetHeight;
const overflow = textHeight > maxHeight;
if (overflow) {
this.overflowed = true;
end = this.dialogContentLength;
}
else {
start = this.dialogContentLength;
}
this.dialogContentLength = Math.floor((start + end) / 2);
this.content = this.dialogContent.substr(0, this.dialogContentLength);
this.$nextTick(() => this.checkLoop(start, end));
},
dialogConfirmClick(event) {
this.$emit("update:dialogShow",false);
if (this.dialogClickCallback) {
this.dialogClickCallback('confirm', event);
}
this.$emit("dialogConfirmClick", event);
},
dialogCancelClick(event) {
this.$emit("update:dialogShow",false);
if (this.dialogClickCallback) {
this.dialogClickCallback('cancel', event);
}
this.$emit("dialogCancelClick", event);
},
dialogMaskClick(event) {
this.$emit("update:dialogShow",false);
if (this.dialogClickCallback) {
this.dialogClickCallback('cancel', event);
}
this.$emit("dialogMaskClick", event);
},
showDialog(show) {
this.dialogShow = show;
}
}
}
</script>
<style lang="scss">
.opsoft-dialog {
.opsoft-dialog-title {
overflow: hidden;
padding: 48rpx;
flex-grow: 0;
flex-shrink: 0;
color: rgba(51, 51, 51, 1);
font-size: 18rpx;
text-align: center;
}
.opsoft-dialog-content {
flex-grow: 1;
flex-shrink: 1;
padding: 0rpx 48rpx 0rpx 48rpx;
display: flex;
align-items: center;
text-align: center;
font-size: 14rpx;
color: rgba(96, 98, 102, 1);
text-align: left;
}
.opsoft-dialog-buttons-margin-top {
height: 48rpx;
flex-shrink: 0;
flex-grow: 0;
}
.opsoft-dialog-buttons {
height: 80rpx;
flex-grow: 0;
flex-shrink: 0;
border-top: 0.5px solid rgba(235, 237, 240, 1);
display: flex;
justify-content: center;
align-items: center;
width: 100%;
.opsoft-dialog-confirm {
color: rgba(56, 56, 56, 1);
font-size: 32rpx;
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
flex-shrink: 1;
}
.opsoft-dialog-cancel {
color: rgba(41, 121, 255, 1);
font-size: 32rpx;
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
flex-shrink: 1;
}
.opsoft-dialog-line {
width: 0.5px;
height: 100%;
position: relative;
flex-grow: 0;
flex-shrink: 0;
}
.opsoft-dialog-line:after {
content: "";
position: absolute;
left: 0px;
top: 0px;
height: 100%;
transform-origin: 0 0;
border-left: 0.5px solid rgba(235, 237, 240, 1);
box-sizing: border-box;
}
}
}
</style>