index.js 2.2 KB

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