notify.js 1.2 KB

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