dialog.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = (typeof options.context === 'function'
  47. ? options.context()
  48. : options.context) || getContext();
  49. var dialog = context.selectComponent(options.selector);
  50. delete options.context;
  51. delete options.selector;
  52. if (dialog) {
  53. dialog.setData(__assign({ callback: function (action, instance) {
  54. action === 'confirm' ? resolve(instance) : reject(instance);
  55. } }, options));
  56. wx.nextTick(function () {
  57. dialog.setData({ show: true });
  58. });
  59. queue.push(dialog);
  60. }
  61. else {
  62. console.warn('未找到 van-dialog 节点,请确认 selector 及 context 是否正确');
  63. }
  64. });
  65. };
  66. Dialog.alert = function (options) { return Dialog(options); };
  67. Dialog.confirm = function (options) {
  68. return Dialog(__assign({ showCancelButton: true }, options));
  69. };
  70. Dialog.close = function () {
  71. queue.forEach(function (dialog) {
  72. dialog.close();
  73. });
  74. queue = [];
  75. };
  76. Dialog.stopLoading = function () {
  77. queue.forEach(function (dialog) {
  78. dialog.stopLoading();
  79. });
  80. };
  81. Dialog.currentOptions = currentOptions;
  82. Dialog.defaultOptions = defaultOptions;
  83. Dialog.setDefaultOptions = function (options) {
  84. currentOptions = __assign(__assign({}, currentOptions), options);
  85. Dialog.currentOptions = currentOptions;
  86. };
  87. Dialog.resetDefaultOptions = function () {
  88. currentOptions = __assign({}, defaultOptions);
  89. Dialog.currentOptions = currentOptions;
  90. };
  91. Dialog.resetDefaultOptions();
  92. exports.default = Dialog;