notify.js 1.7 KB

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