dialog.js 2.2 KB

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