| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <template>
- <teleport to="body">
- <div
- v-if="show"
- :transition="transition"
- >
- <div
- class="modal"
- @click.self="clickMask"
- >
- <div
- class="modal-dialog"
- :class="modalClass"
- name="dialog"
- >
- <div class="modal-content">
- <!--Header-->
- <div class="modal-header">
- <a
- v-if="showTopRightCloseButton"
- type="button"
- class="close"
- @click="cancel"
- >x</a>
- <h4 class="modal-title">
- <slot name="header">
- {{ title }}
- </slot>
- </h4>
- </div>
- <!--Container-->
- <div class="modal-body">
- <slot />
- </div>
- <!--Footer-->
- <div class="modal-footer">
- <button
- v-if="showCanelButton"
- type="button"
- class="btn btn-info"
- @click="cancel"
- >
- {{ cancelText }}
- </button>
- <button
- v-if="showOkButton"
- type="button"
- class="btn btn-primary"
- @click="ok"
- >
- {{ okText }}
- </button>
- <slot name="footer" />
- </div>
- </div>
- </div>
- </div>
- <div class="modal-backdrop in" />
- </div>
- </teleport>
- </template>
- <script>
- import ModalFix from './ModalFix.js';
- export default {
- // eslint-disable-next-line
- name: 'Modal',
- props: {
- /**
- * 是否显示模态框
- */
- show: {
- type: Boolean,
- default: false,
- },
- title: {
- type: String,
- default: '',
- },
- small: {
- type: [Boolean, String],
- default: false,
- },
- large: {
- type: [Boolean, String],
- default: false,
- },
- full: {
- type: [Boolean, String],
- default: false,
- },
- // 为true时无法通过点击遮罩层关闭modal
- force: {
- type: Boolean,
- default: null,
- },
- // 自定义组件transition
- transition: {
- type: String,
- default: '',
- },
- // 显示确定按钮
- showOkButton: {
- type: Boolean,
- default: true,
- },
- // 显示取消按钮
- showCanelButton: {
- type: Boolean,
- default: true,
- },
- // 显示标题栏的关闭按钮
- showTopRightCloseButton: {
- type: Boolean,
- default: true,
- },
- },
- emits: ['ok', 'cancel', 'close', 'update:show'],
- data: function () {
- return {
- okText: this.$t('lang.modal.confirm'),
- cancelText: this.$t('lang.modal.cancel'),
- closeText: this.$t('lang.modal.close'),
- okClass: '',
- cancelClass: '',
- closeClass: '',
- };
- },
- computed: {
- modalClass: function () {
- return {
- 'modal-lg': this.large || this.large == 'true',
- 'modal-sm': this.small || this.small == 'true',
- 'modal-full': this.full || this.full == 'true',
- };
- },
- },
- watch: {
- show: function (value) {
- console.log('modal watch show = ' + value);
- // 在显示时去掉body滚动条,防止出现双滚动条
- if (value) {
- if (document.body.className.indexOf('modal-open') < 0) {
- document.body.className += ' modal-open';
- }
- } else {
- this.$nextTick(function () {
- ModalFix.fix();
- });
- }
- },
- },
- created: function () {
- if (this.show) {
- if (document.body.className.indexOf('modal-open') < 0) {
- document.body.className += ' modal-open';
- }
- }
- },
- methods: {
- ok: function () {
- this.$emit('ok');
- this.$emit('update:show', false);
- this.$nextTick(function () {
- ModalFix.fix();
- });
- },
- cancel: function () {
- this.$emit('cancel');
- this.$emit('update:show', false);
- this.$nextTick(function () {
- ModalFix.fix();
- });
- },
- close: function () {
- this.$emit('close');
- this.$emit('update:show', false);
- this.$nextTick(function () {
- ModalFix.fix();
- });
- },
- // 点击遮罩层
- clickMask: function () {
- if (!this.force) {
- this.cancel();
- this.$nextTick(function () {
- ModalFix.fix();
- });
- }
- },
- },
- };
- </script>
- <style scoped>
- .modal {
- display: block;
- }
- .modal-transition {
- transition: all 0.6s ease;
- }
- .modal-leave {
- /* 样式没什么用,但可以让根标签的transitionEnd生效,以去掉modal-leave */
- border-radius: 1px !important;
- }
- .modal-transition .modal-dialog,
- .modal-transition .modal-backdrop {
- transition: all 0.5s ease;
- }
- .modal-enter .modal-dialog,
- .modal-leave .modal-dialog {
- opacity: 0;
- transform: translateY(-30%);
- }
- .modal-enter .modal-backdrop,
- .modal-leave .modal-backdrop {
- opacity: 0;
- }
- .modal-full {
- width: 95%;
- }
- .modal-lg {
- width: 75%;
- }
- .modal-sm {
- width: 50%;
- }
- </style>
|