index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import PopupWindow from "./PopupWindow.vue";
  2. import Vue from "vue";
  3. import { mergeDeep } from "@vci/helper/src/object";
  4. import { getElementBody } from "@vci/helper/src/element";
  5. const PopupWindowExt = Vue.extend({
  6. data() {
  7. return {
  8. props: {
  9. title: "窗口",
  10. left: "center",
  11. top: "middle",
  12. defaultWidth: "600px",
  13. defaultHeight: "300px",
  14. widthMin: 300,
  15. heightMin: 150,
  16. customStyle: {},
  17. visibleHead: true,
  18. visibleTool: true,
  19. enableResize: true,
  20. url: null
  21. },
  22. on: {
  23. beforeOpen: null,
  24. afterOpen: null,
  25. beforeClose: null,
  26. close: null
  27. },
  28. component: null,
  29. propsComponent: {}
  30. };
  31. },
  32. methods: {
  33. open(option = {}) {
  34. const { props, on, component, propsComponent } = option;
  35. this.props = mergeDeep(this.props, props);
  36. this.on = mergeDeep(this.on, on);
  37. this.component = component;
  38. this.propsComponent = mergeDeep(this.propsComponent, propsComponent);
  39. },
  40. close() {
  41. getElementBody().removeChild(this.$el);
  42. this.on.close && this.on.close(this.$children[0]);
  43. this.$destroy();
  44. }
  45. },
  46. render(h) {
  47. const on = {
  48. close: vm => {
  49. this.on.close && this.on.close(vm);
  50. this.close();
  51. }
  52. };
  53. if(this.on.beforeOpen) on.beforeOpen = this.on.beforeOpen;
  54. if(this.on.afterOpen) on.afterOpen = this.on.afterOpen;
  55. if(this.on.beforeClose) on.beforeClose = this.on.beforeClose;
  56. return h(
  57. PopupWindow,
  58. {
  59. props: this.props,
  60. on,
  61. scopedSlots: {
  62. default: props => {
  63. const component = this.component || "div";
  64. return h(
  65. component,
  66. {
  67. props: {
  68. ...this.propsComponent,
  69. ...props
  70. }
  71. }
  72. );
  73. }
  74. }
  75. }
  76. );
  77. }
  78. });
  79. function open(option) {
  80. const popupWindow = new PopupWindowExt();
  81. popupWindow.$mount();
  82. getElementBody().appendChild(popupWindow.$el);
  83. popupWindow.open(option || {});
  84. return popupWindow;
  85. }
  86. function close(popupWindow) {
  87. popupWindow && popupWindow.close();
  88. }
  89. const PluginPopupWindow = {
  90. open,
  91. close,
  92. install: Vue => {
  93. Vue.prototype.$popupWindow = { open, close };
  94. }
  95. };
  96. export {
  97. PluginPopupWindow
  98. };