notify.js 1.2 KB

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