notify.js 989 B

1234567891011121314151617181920212223242526272829303132333435
  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. color: WHITE,
  10. safeAreaInsetTop: false,
  11. onClick: () => { },
  12. onOpened: () => { },
  13. onClose: () => { }
  14. };
  15. function parseOptions(message) {
  16. return typeof message === 'string' ? { message } : message;
  17. }
  18. function getContext() {
  19. const pages = getCurrentPages();
  20. return pages[pages.length - 1];
  21. }
  22. export default function Notify(options) {
  23. options = Object.assign({}, defaultOptions, parseOptions(options));
  24. const context = options.context || getContext();
  25. const notify = context.selectComponent(options.selector);
  26. delete options.context;
  27. delete options.selector;
  28. if (notify) {
  29. notify.set(options);
  30. notify.show();
  31. }
  32. else {
  33. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  34. }
  35. }