index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import Vue from "vue";
  2. import VciNotification from "./VciNotification.vue";
  3. import { mergeDeep } from "@vci/helper/src/object";
  4. import { addCss, getElementBody, getElementMaxZIndex } from "@vci/helper/src/element";
  5. const vms = [];
  6. const ConfigVpNotification = {
  7. component: VciNotification,
  8. propsComponent: null,
  9. style: {
  10. bottom: 32,
  11. right: 20,
  12. margin: 20
  13. }
  14. };
  15. const Index = Vue.extend({
  16. functional: true,
  17. props: {
  18. duration: {
  19. type: Number,
  20. default: 2e3
  21. },
  22. alwaysPresent: {
  23. type: Boolean,
  24. default: false
  25. },
  26. transitionDuration: {
  27. type: Number,
  28. default: 350
  29. },
  30. transitionTimingFunction: {
  31. type: String,
  32. default: "cubic-bezier(0.65, 0.05, 0.36, 1)"
  33. },
  34. component: {
  35. type: Object,
  36. default: null
  37. },
  38. title: {
  39. type: String,
  40. default: "提示"
  41. },
  42. message: {
  43. type: String,
  44. default: "来自勤奋的vci"
  45. },
  46. theme: {
  47. type: String,
  48. default: "#000"
  49. }
  50. },
  51. data() {
  52. return {};
  53. },
  54. methods: {
  55. open() {
  56. addCss(this.$el, {
  57. transform: `translateX(0)`,
  58. opacity: 1
  59. });
  60. !this.alwaysPresent && setTimeout(this.close, this.duration);
  61. },
  62. close() {
  63. addCss(this.$el, {
  64. opacity: 0,
  65. transform: "translateX(100%) skewX(15deg)"
  66. });
  67. this.isDestroyed = true;
  68. const indexInVms = vms.findIndex(v => this === v);
  69. vms.forEach((v, i) => {
  70. if(v) {
  71. !this.mark && (this.mark = v);
  72. const isFirstVm = this.mark === v;
  73. if(v !== this) {
  74. if(i > indexInVms) {
  75. v.bottom -= (this.$el.clientHeight + (isFirstVm ? 0 : ConfigVpNotification.style.margin));
  76. addCss(v.$el, { bottom: `${v.bottom}px` });
  77. }
  78. }
  79. }
  80. });
  81. setTimeout(() => {
  82. vms.splice(vms.findIndex(v => v === this), 1, null);
  83. this.$el.parentNode && this.$el.parentNode.removeChild(this.$el);
  84. this.$destroy();
  85. }, this.transitionDuration);
  86. }
  87. },
  88. render(h) {
  89. const component = this.component || ConfigVpNotification.component || "div";
  90. return h(
  91. component,
  92. {
  93. props: mergeDeep(ConfigVpNotification.propsComponent, this.$props),
  94. on: { close: this.close }
  95. }
  96. );
  97. }
  98. });
  99. function open(propsData) {
  100. const vm = new Index({ propsData });
  101. vm.$mount();
  102. const style = {
  103. position: "fixed",
  104. right: `${ConfigVpNotification.style.right}px`,
  105. bottom: ConfigVpNotification.style.bottom,
  106. zIndex: getElementMaxZIndex(),
  107. transition: `${vm.transitionDuration / 1e3}s ${vm.transitionTimingFunction}`,
  108. transform: `translateX(100%) skewX(-15deg)`,
  109. opacity: 0
  110. };
  111. vms.forEach(v => v && !v.isDestroyed && (style.bottom += (v.$el.clientHeight + ConfigVpNotification.style.margin)));
  112. vm.bottom = style.bottom;
  113. style.bottom += "px";
  114. addCss(vm.$el, style);
  115. getElementBody().appendChild(vm.$el);
  116. setTimeout(() => vm.open(), 50);
  117. vms.push(vm);
  118. return vm;
  119. }
  120. const PluginNotification = {
  121. open,
  122. install: (Vue, option = {}) => {
  123. const { component, propsComponent, customStatus } = option;
  124. // 安装插件时配置默认的渲染组件及组件的props
  125. component && (ConfigVpNotification.component = component);
  126. propsComponent && (ConfigVpNotification.propsComponent = propsComponent);
  127. // 注册插件使用方法
  128. const status = {
  129. "success": "#3effb0",
  130. "warning": "#ffc83e",
  131. "error": "#ff3e3e",
  132. "tip": "#000",
  133. ...(customStatus || {})
  134. };
  135. Object.keys(status).forEach(key => PluginNotification[key] = (message, option) => open({
  136. message,
  137. theme: status[key],
  138. ...option
  139. }));
  140. Vue.prototype.$notify = PluginNotification;
  141. }
  142. };
  143. export {
  144. PluginNotification
  145. };