dialog.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  2. var queue = [];
  3. function getContext() {
  4. var pages = getCurrentPages();
  5. return pages[pages.length - 1];
  6. }
  7. var Dialog = function Dialog(options) {
  8. options = _extends({}, Dialog.currentOptions, options);
  9. return new Promise(function (resolve, reject) {
  10. var context = options.context || getContext();
  11. var dialog = context.selectComponent(options.selector);
  12. delete options.selector;
  13. if (dialog) {
  14. dialog.set(_extends({
  15. onCancel: reject,
  16. onConfirm: resolve
  17. }, options));
  18. queue.push(dialog);
  19. } else {
  20. console.warn('未找到 van-dialog 节点,请确认 selector 及 context 是否正确');
  21. }
  22. });
  23. };
  24. Dialog.defaultOptions = {
  25. show: true,
  26. title: '',
  27. message: '',
  28. zIndex: 100,
  29. overlay: true,
  30. asyncClose: false,
  31. messageAlign: '',
  32. transition: 'scale',
  33. selector: '#van-dialog',
  34. confirmButtonText: '确认',
  35. cancelButtonText: '取消',
  36. showConfirmButton: true,
  37. showCancelButton: false,
  38. closeOnClickOverlay: false,
  39. confirmButtonOpenType: ''
  40. };
  41. Dialog.alert = Dialog;
  42. Dialog.confirm = function (options) {
  43. return Dialog(_extends({
  44. showCancelButton: true
  45. }, options));
  46. };
  47. Dialog.close = function () {
  48. queue.forEach(function (dialog) {
  49. dialog.close();
  50. });
  51. queue = [];
  52. };
  53. Dialog.stopLoading = function () {
  54. queue.forEach(function (dialog) {
  55. dialog.stopLoading();
  56. });
  57. };
  58. Dialog.setDefaultOptions = function (options) {
  59. Object.assign(Dialog.currentOptions, options);
  60. };
  61. Dialog.resetDefaultOptions = function () {
  62. Dialog.currentOptions = _extends({}, Dialog.defaultOptions);
  63. };
  64. Dialog.resetDefaultOptions();
  65. export default Dialog;