dialog.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. message: '',
  33. zIndex: 100,
  34. overlay: true,
  35. selector: '#van-dialog',
  36. className: '',
  37. asyncClose: false,
  38. transition: 'scale',
  39. customStyle: '',
  40. messageAlign: '',
  41. overlayStyle: '',
  42. confirmButtonText: '确认',
  43. cancelButtonText: '取消',
  44. showConfirmButton: true,
  45. showCancelButton: false,
  46. closeOnClickOverlay: false,
  47. confirmButtonOpenType: '',
  48. };
  49. Dialog.alert = Dialog;
  50. Dialog.confirm = (options) =>
  51. Dialog(Object.assign({ showCancelButton: true }, options));
  52. Dialog.close = () => {
  53. queue.forEach((dialog) => {
  54. dialog.close();
  55. });
  56. queue = [];
  57. };
  58. Dialog.stopLoading = () => {
  59. queue.forEach((dialog) => {
  60. dialog.stopLoading();
  61. });
  62. };
  63. Dialog.setDefaultOptions = (options) => {
  64. Object.assign(Dialog.currentOptions, options);
  65. };
  66. Dialog.resetDefaultOptions = () => {
  67. Dialog.currentOptions = Object.assign({}, Dialog.defaultOptions);
  68. };
  69. Dialog.resetDefaultOptions();
  70. export default Dialog;