notify.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if (message == null) {
  18. return {};
  19. }
  20. return typeof message === 'string' ? { message } : message;
  21. }
  22. function getContext() {
  23. const pages = getCurrentPages();
  24. return pages[pages.length - 1];
  25. }
  26. export default function Notify(options) {
  27. options = Object.assign(Object.assign({}, defaultOptions), parseOptions(options));
  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.showNotify();
  35. return notify;
  36. }
  37. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  38. }
  39. Notify.clear = function (options) {
  40. options = Object.assign(Object.assign({}, defaultOptions), parseOptions(options));
  41. const context = options.context || getContext();
  42. const notify = context.selectComponent(options.selector);
  43. if (notify) {
  44. notify.hide();
  45. }
  46. };