dialog.js 2.2 KB

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