12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.selector;
  12. if (dialog) {
  13. dialog.set(Object.assign({ onCancel: reject, onConfirm: resolve }, options));
  14. queue.push(dialog);
  15. }
  16. else {
  17. console.warn('未找到 van-dialog 节点,请确认 selector 及 context 是否正确');
  18. }
  19. });
  20. };
  21. Dialog.defaultOptions = {
  22. show: true,
  23. title: '',
  24. message: '',
  25. zIndex: 100,
  26. overlay: true,
  27. asyncClose: false,
  28. messageAlign: '',
  29. transition: 'scale',
  30. selector: '#van-dialog',
  31. confirmButtonText: '确认',
  32. cancelButtonText: '取消',
  33. showConfirmButton: true,
  34. showCancelButton: false,
  35. closeOnClickOverlay: false,
  36. confirmButtonOpenType: ''
  37. };
  38. Dialog.alert = Dialog;
  39. Dialog.confirm = options => Dialog(Object.assign({ showCancelButton: true }, options));
  40. Dialog.close = () => {
  41. queue.forEach(dialog => {
  42. dialog.close();
  43. });
  44. queue = [];
  45. };
  46. Dialog.stopLoading = () => {
  47. queue.forEach(dialog => {
  48. dialog.stopLoading();
  49. });
  50. };
  51. Dialog.setDefaultOptions = options => {
  52. Object.assign(Dialog.currentOptions, options);
  53. };
  54. Dialog.resetDefaultOptions = () => {
  55. Dialog.currentOptions = Object.assign({}, Dialog.defaultOptions);
  56. };
  57. Dialog.resetDefaultOptions();
  58. export default Dialog;