dialog.js 1.8 KB

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