dialog.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. var defaultOptions = {
  16. show: false,
  17. title: '',
  18. width: null,
  19. theme: 'default',
  20. message: '',
  21. zIndex: 100,
  22. overlay: true,
  23. selector: '#van-dialog',
  24. className: '',
  25. asyncClose: false,
  26. beforeClose: null,
  27. transition: 'scale',
  28. customStyle: '',
  29. messageAlign: '',
  30. overlayStyle: '',
  31. confirmButtonText: '确认',
  32. cancelButtonText: '取消',
  33. showConfirmButton: true,
  34. showCancelButton: false,
  35. closeOnClickOverlay: false,
  36. confirmButtonOpenType: '',
  37. };
  38. var currentOptions = __assign({}, defaultOptions);
  39. function getContext() {
  40. var pages = getCurrentPages();
  41. return pages[pages.length - 1];
  42. }
  43. var Dialog = function (options) {
  44. options = __assign(__assign({}, currentOptions), options);
  45. return new Promise(function (resolve, reject) {
  46. var context = options.context || getContext();
  47. var dialog = context.selectComponent(options.selector);
  48. delete options.context;
  49. delete options.selector;
  50. if (dialog) {
  51. dialog.setData(__assign({ callback: function (action, instance) {
  52. action === 'confirm' ? resolve(instance) : reject(instance);
  53. } }, options));
  54. wx.nextTick(function () {
  55. dialog.setData({ show: true });
  56. });
  57. queue.push(dialog);
  58. }
  59. else {
  60. console.warn('未找到 van-dialog 节点,请确认 selector 及 context 是否正确');
  61. }
  62. });
  63. };
  64. Dialog.alert = function (options) { return Dialog(options); };
  65. Dialog.confirm = function (options) {
  66. return Dialog(__assign({ showCancelButton: true }, options));
  67. };
  68. Dialog.close = function () {
  69. queue.forEach(function (dialog) {
  70. dialog.close();
  71. });
  72. queue = [];
  73. };
  74. Dialog.stopLoading = function () {
  75. queue.forEach(function (dialog) {
  76. dialog.stopLoading();
  77. });
  78. };
  79. Dialog.currentOptions = currentOptions;
  80. Dialog.defaultOptions = defaultOptions;
  81. Dialog.setDefaultOptions = function (options) {
  82. currentOptions = __assign(__assign({}, currentOptions), options);
  83. Dialog.currentOptions = currentOptions;
  84. };
  85. Dialog.resetDefaultOptions = function () {
  86. currentOptions = __assign({}, defaultOptions);
  87. Dialog.currentOptions = currentOptions;
  88. };
  89. Dialog.resetDefaultOptions();
  90. exports.default = Dialog;