notify.js 1.4 KB

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