index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import Vue from 'vue';
  3. import VanNotify from './Notify';
  4. import { isObject, isServer } from '../utils';
  5. import { mount } from '../utils/functional';
  6. var timer;
  7. var instance;
  8. function parseOptions(message) {
  9. return isObject(message) ? message : {
  10. message: message
  11. };
  12. }
  13. function Notify(options) {
  14. /* istanbul ignore if */
  15. if (isServer) {
  16. return;
  17. }
  18. if (!instance) {
  19. instance = mount(VanNotify, {
  20. on: {
  21. click: function click(event) {
  22. if (instance.onClick) {
  23. instance.onClick(event);
  24. }
  25. },
  26. close: function close() {
  27. if (instance.onClose) {
  28. instance.onClose();
  29. }
  30. },
  31. opened: function opened() {
  32. if (instance.onOpened) {
  33. instance.onOpened();
  34. }
  35. }
  36. }
  37. });
  38. }
  39. options = _extends({}, Notify.currentOptions, parseOptions(options));
  40. _extends(instance, options);
  41. clearTimeout(timer);
  42. if (options.duration && options.duration > 0) {
  43. timer = setTimeout(Notify.clear, options.duration);
  44. }
  45. return instance;
  46. }
  47. function defaultOptions() {
  48. return {
  49. type: 'danger',
  50. value: true,
  51. message: '',
  52. color: undefined,
  53. background: undefined,
  54. duration: 3000,
  55. className: '',
  56. onClose: null,
  57. onClick: null,
  58. onOpened: null
  59. };
  60. }
  61. Notify.clear = function () {
  62. if (instance) {
  63. instance.value = false;
  64. }
  65. };
  66. Notify.currentOptions = defaultOptions();
  67. Notify.setDefaultOptions = function (options) {
  68. _extends(Notify.currentOptions, options);
  69. };
  70. Notify.resetDefaultOptions = function () {
  71. Notify.currentOptions = defaultOptions();
  72. };
  73. Notify.install = function () {
  74. Vue.use(VanNotify);
  75. };
  76. Notify.Component = VanNotify;
  77. Vue.prototype.$notify = Notify;
  78. export default Notify;