notify.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(Object.assign({}, defaultOptions), parseOptions(options));
  25. const context = options.context || getContext();
  26. const notify = context.selectComponent(options.selector);
  27. delete options.context;
  28. delete options.selector;
  29. if (notify) {
  30. notify.setData(options);
  31. notify.show();
  32. return notify;
  33. }
  34. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  35. }
  36. Notify.clear = function (options) {
  37. options = Object.assign(Object.assign({}, defaultOptions), parseOptions(options));
  38. const context = options.context || getContext();
  39. const notify = context.selectComponent(options.selector);
  40. if (notify) {
  41. notify.hide();
  42. }
  43. };