notify.js 2.0 KB

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