notify.js 1.3 KB

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