index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Vue from "vue";
  2. import { cloneDeepByJSON, mergeDeep } from "@vci/helper/src/object";
  3. import { getElementMaxZIndex } from "@vci/helper/src/element";
  4. import VciLoading from "./VciLoading.vue";
  5. import PopupQuick from "../../popup/PopupQuick/PopupQuick.vue";
  6. const PropsPopup = {
  7. maskStyle: {
  8. backgroundColor: "rgba(0,0,0,0.01)"
  9. },
  10. animation: "zoom-out"
  11. };
  12. const ConfigVpLoading = {
  13. component: VciLoading,
  14. propsComponent: null
  15. };
  16. const Loading = Vue.extend({
  17. functional: true,
  18. props: {
  19. component: {
  20. type: Object,
  21. default: null
  22. },
  23. propsComponent: {
  24. type: Object,
  25. default: null
  26. }
  27. },
  28. data() {
  29. return {
  30. visible: false,
  31. loadAmount: 0,
  32. isOpening: false
  33. };
  34. },
  35. methods: {
  36. open() {
  37. this.visible = true;
  38. this.isOpening = true;
  39. this.loadAmount++;
  40. },
  41. close(force = false) {
  42. this.loadAmount--;
  43. if(force) this.loadAmount = 0;
  44. if(this.loadAmount < 1) {
  45. this.loadAmount = 0;
  46. this.$children[0] && this.$children[0].close();
  47. }
  48. }
  49. },
  50. render(h) {
  51. return h(
  52. PopupQuick,
  53. {
  54. props: {
  55. visible: this.visible,
  56. maskStyle: PropsPopup.maskStyle,
  57. animation: PropsPopup.animation
  58. },
  59. scopedSlots: {
  60. default: () => {
  61. const component = this.component ? this.component : (ConfigVpLoading.component || VciLoading);
  62. return h(
  63. component,
  64. { props: mergeDeep(cloneDeepByJSON(ConfigVpLoading.propsComponent), this.propsComponent) }
  65. );
  66. }
  67. },
  68. on: {
  69. afterOpen: () => this.isOpening = false,
  70. afterClose: () => {
  71. this.visible = false;
  72. // setTimeout(() => this.isOpening && (this.visible = true));
  73. }
  74. }
  75. }
  76. );
  77. }
  78. });
  79. const vl = {
  80. vm: null
  81. };
  82. function open(propsComponent, component) {
  83. if(!vl.vm) {
  84. vl.vm = new Loading();
  85. vl.vm.$mount();
  86. document.querySelector("body").appendChild(vl.vm.$el);
  87. }
  88. vl.vm.component = component;
  89. vl.vm.propsComponent = propsComponent;
  90. vl.vm.zIndex = getElementMaxZIndex(vl.vm.$el.parentNode);
  91. vl.vm.open();
  92. }
  93. function close(force = false) {
  94. vl.vm && vl.vm.close(force);
  95. }
  96. const PluginLoading = {
  97. open,
  98. close,
  99. install: (Vue, option = {}) => {
  100. const { component, propsComponent, propsPopup } = option;
  101. // 安装插件时配置默认的渲染组件及组件的props
  102. component && (ConfigVpLoading.component = component);
  103. propsComponent && (ConfigVpLoading.propsComponent = propsComponent);
  104. // 定义加载弹框默认属性
  105. mergeDeep(PropsPopup, propsPopup);
  106. // 暴露加赞函数
  107. Vue.prototype.$loading = PluginLoading;
  108. }
  109. };
  110. export { PluginLoading };