dialog.js 2.7 KB

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