dialog.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. let queue = [];
  2. function getContext() {
  3. const pages = getCurrentPages();
  4. return pages[pages.length - 1];
  5. }
  6. const Dialog = options => {
  7. options = Object.assign({}, Dialog.currentOptions, options);
  8. return new Promise((resolve, reject) => {
  9. const context = options.context || getContext();
  10. const dialog = context.selectComponent(options.selector);
  11. delete options.context;
  12. delete options.selector;
  13. if (dialog) {
  14. dialog.set(Object.assign({ onCancel: reject, onConfirm: resolve }, options));
  15. queue.push(dialog);
  16. }
  17. else {
  18. console.warn('未找到 van-dialog 节点,请确认 selector 及 context 是否正确');
  19. }
  20. });
  21. };
  22. Dialog.defaultOptions = {
  23. show: true,
  24. title: '',
  25. message: '',
  26. zIndex: 100,
  27. overlay: true,
  28. className: '',
  29. customStyle: '',
  30. asyncClose: false,
  31. messageAlign: '',
  32. transition: 'scale',
  33. selector: '#van-dialog',
  34. confirmButtonText: '确认',
  35. cancelButtonText: '取消',
  36. showConfirmButton: true,
  37. showCancelButton: false,
  38. closeOnClickOverlay: false,
  39. confirmButtonOpenType: ''
  40. };
  41. Dialog.alert = Dialog;
  42. Dialog.confirm = options => Dialog(Object.assign({ showCancelButton: true }, options));
  43. Dialog.close = () => {
  44. queue.forEach(dialog => {
  45. dialog.close();
  46. });
  47. queue = [];
  48. };
  49. Dialog.stopLoading = () => {
  50. queue.forEach(dialog => {
  51. dialog.stopLoading();
  52. });
  53. };
  54. Dialog.setDefaultOptions = options => {
  55. Object.assign(Dialog.currentOptions, options);
  56. };
  57. Dialog.resetDefaultOptions = () => {
  58. Dialog.currentOptions = Object.assign({}, Dialog.defaultOptions);
  59. };
  60. Dialog.resetDefaultOptions();
  61. export default Dialog;