dialog.js 1.7 KB

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