notify.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. var currentOptions = __assign({}, defaultOptions);
  30. function parseOptions(message) {
  31. if (message == null) {
  32. return {};
  33. }
  34. return typeof message === 'string' ? { message: message } : message;
  35. }
  36. function getContext() {
  37. var pages = getCurrentPages();
  38. return pages[pages.length - 1];
  39. }
  40. function Notify(options) {
  41. options = __assign(__assign({}, currentOptions), parseOptions(options));
  42. var context = options.context || getContext();
  43. var notify = context.selectComponent(options.selector);
  44. delete options.context;
  45. delete options.selector;
  46. if (notify) {
  47. notify.setData(options);
  48. notify.show();
  49. return notify;
  50. }
  51. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  52. }
  53. exports.default = Notify;
  54. Notify.clear = function (options) {
  55. options = __assign(__assign({}, defaultOptions), parseOptions(options));
  56. var context = options.context || getContext();
  57. var notify = context.selectComponent(options.selector);
  58. if (notify) {
  59. notify.hide();
  60. }
  61. };
  62. Notify.setDefaultOptions = function (options) {
  63. Object.assign(currentOptions, options);
  64. };
  65. Notify.resetDefaultOptions = function () {
  66. currentOptions = __assign({}, defaultOptions);
  67. };