notify.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var color_1 = require("../common/color");
  15. var defaultOptions = {
  16. selector: '#van-notify',
  17. type: 'danger',
  18. message: '',
  19. background: '',
  20. duration: 3000,
  21. zIndex: 110,
  22. top: 0,
  23. color: color_1.WHITE,
  24. safeAreaInsetTop: false,
  25. onClick: function () { },
  26. onOpened: function () { },
  27. onClose: function () { },
  28. };
  29. function parseOptions(message) {
  30. if (message == null) {
  31. return {};
  32. }
  33. return typeof message === 'string' ? { message: message } : message;
  34. }
  35. function getContext() {
  36. var pages = getCurrentPages();
  37. return pages[pages.length - 1];
  38. }
  39. function Notify(options) {
  40. options = __assign(__assign({}, defaultOptions), parseOptions(options));
  41. var context = options.context || getContext();
  42. var notify = context.selectComponent(options.selector);
  43. delete options.context;
  44. delete options.selector;
  45. if (notify) {
  46. notify.setData(options);
  47. notify.show();
  48. return notify;
  49. }
  50. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  51. }
  52. exports.default = Notify;
  53. Notify.clear = function (options) {
  54. options = __assign(__assign({}, defaultOptions), parseOptions(options));
  55. var context = options.context || getContext();
  56. var notify = context.selectComponent(options.selector);
  57. if (notify) {
  58. notify.hide();
  59. }
  60. };