dialog.js 2.3 KB

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