| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import Vue from "vue";
- import { cloneDeepByJSON, mergeDeep } from "@vci/helper/src/object";
- import { getElementMaxZIndex } from "@vci/helper/src/element";
- import VciLoading from "./VciLoading.vue";
- import PopupQuick from "../../popup/PopupQuick/PopupQuick.vue";
- const PropsPopup = {
- maskStyle: {
- backgroundColor: "rgba(0,0,0,0.01)"
- },
- animation: "zoom-out"
- };
- const ConfigVpLoading = {
- component: VciLoading,
- propsComponent: null
- };
- const Loading = Vue.extend({
- functional: true,
- props: {
- component: {
- type: Object,
- default: null
- },
- propsComponent: {
- type: Object,
- default: null
- }
- },
- data() {
- return {
- visible: false,
- loadAmount: 0,
- isOpening: false
- };
- },
- methods: {
- open() {
- this.visible = true;
- this.isOpening = true;
- this.loadAmount++;
- },
- close(force = false) {
- this.loadAmount--;
- if(force) this.loadAmount = 0;
- if(this.loadAmount < 1) {
- this.loadAmount = 0;
- this.$children[0] && this.$children[0].close();
- }
- }
- },
- render(h) {
- return h(
- PopupQuick,
- {
- props: {
- visible: this.visible,
- maskStyle: PropsPopup.maskStyle,
- animation: PropsPopup.animation
- },
- scopedSlots: {
- default: () => {
- const component = this.component ? this.component : (ConfigVpLoading.component || VciLoading);
- return h(
- component,
- { props: mergeDeep(cloneDeepByJSON(ConfigVpLoading.propsComponent), this.propsComponent) }
- );
- }
- },
- on: {
- afterOpen: () => this.isOpening = false,
- afterClose: () => {
- this.visible = false;
- // setTimeout(() => this.isOpening && (this.visible = true));
- }
- }
- }
- );
- }
- });
- const vl = {
- vm: null
- };
- function open(propsComponent, component) {
- if(!vl.vm) {
- vl.vm = new Loading();
- vl.vm.$mount();
- document.querySelector("body").appendChild(vl.vm.$el);
- }
- vl.vm.component = component;
- vl.vm.propsComponent = propsComponent;
- vl.vm.zIndex = getElementMaxZIndex(vl.vm.$el.parentNode);
- vl.vm.open();
- }
- function close(force = false) {
- vl.vm && vl.vm.close(force);
- }
- const PluginLoading = {
- open,
- close,
- install: (Vue, option = {}) => {
- const { component, propsComponent, propsPopup } = option;
- // 安装插件时配置默认的渲染组件及组件的props
- component && (ConfigVpLoading.component = component);
- propsComponent && (ConfigVpLoading.propsComponent = propsComponent);
- // 定义加载弹框默认属性
- mergeDeep(PropsPopup, propsPopup);
- // 暴露加赞函数
- Vue.prototype.$loading = PluginLoading;
- }
- };
- export { PluginLoading };
|