dialog.js 1.8 KB

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