notify.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. let currentOptions = Object.assign({}, defaultOptions);
  17. function parseOptions(message) {
  18. if (message == null) {
  19. return {};
  20. }
  21. return typeof message === 'string' ? { message } : message;
  22. }
  23. function getContext() {
  24. const pages = getCurrentPages();
  25. return pages[pages.length - 1];
  26. }
  27. export default function Notify(options) {
  28. options = Object.assign(Object.assign({}, currentOptions), parseOptions(options));
  29. const context = options.context || getContext();
  30. const notify = context.selectComponent(options.selector);
  31. delete options.context;
  32. delete options.selector;
  33. if (notify) {
  34. notify.setData(options);
  35. notify.show();
  36. return notify;
  37. }
  38. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  39. }
  40. Notify.clear = function (options) {
  41. options = Object.assign(Object.assign({}, defaultOptions), parseOptions(options));
  42. const context = options.context || getContext();
  43. const notify = context.selectComponent(options.selector);
  44. if (notify) {
  45. notify.hide();
  46. }
  47. };
  48. Notify.setDefaultOptions = (options) => {
  49. Object.assign(currentOptions, options);
  50. };
  51. Notify.resetDefaultOptions = () => {
  52. currentOptions = Object.assign({}, defaultOptions);
  53. };