notify.js 1.2 KB

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