index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  6. var _vue = _interopRequireDefault(require("vue"));
  7. var _Dialog = _interopRequireDefault(require("./Dialog"));
  8. var _utils = require("../utils");
  9. var instance;
  10. function isInDocument(element) {
  11. return document.body.contains(element);
  12. }
  13. function initInstance() {
  14. if (instance) {
  15. instance.$destroy();
  16. }
  17. instance = new (_vue.default.extend(_Dialog.default))({
  18. el: document.createElement('div'),
  19. // avoid missing animation when first rendered
  20. propsData: {
  21. lazyRender: false
  22. }
  23. });
  24. instance.$on('input', function (value) {
  25. instance.value = value;
  26. });
  27. }
  28. function Dialog(options) {
  29. /* istanbul ignore if */
  30. if (_utils.isServer) {
  31. return Promise.resolve();
  32. }
  33. return new Promise(function (resolve, reject) {
  34. if (!instance || !isInDocument(instance.$el)) {
  35. initInstance();
  36. }
  37. (0, _extends2.default)(instance, Dialog.currentOptions, options, {
  38. resolve: resolve,
  39. reject: reject
  40. });
  41. });
  42. }
  43. Dialog.defaultOptions = {
  44. value: true,
  45. title: '',
  46. width: '',
  47. theme: null,
  48. message: '',
  49. overlay: true,
  50. className: '',
  51. allowHtml: true,
  52. lockScroll: true,
  53. transition: 'van-dialog-bounce',
  54. beforeClose: null,
  55. overlayClass: '',
  56. overlayStyle: null,
  57. messageAlign: '',
  58. getContainer: 'body',
  59. cancelButtonText: '',
  60. cancelButtonColor: null,
  61. confirmButtonText: '',
  62. confirmButtonColor: null,
  63. showConfirmButton: true,
  64. showCancelButton: false,
  65. closeOnPopstate: true,
  66. closeOnClickOverlay: false,
  67. callback: function callback(action) {
  68. instance[action === 'confirm' ? 'resolve' : 'reject'](action);
  69. }
  70. };
  71. Dialog.alert = Dialog;
  72. Dialog.confirm = function (options) {
  73. return Dialog((0, _extends2.default)({
  74. showCancelButton: true
  75. }, options));
  76. };
  77. Dialog.close = function () {
  78. if (instance) {
  79. instance.value = false;
  80. }
  81. };
  82. Dialog.setDefaultOptions = function (options) {
  83. (0, _extends2.default)(Dialog.currentOptions, options);
  84. };
  85. Dialog.resetDefaultOptions = function () {
  86. Dialog.currentOptions = (0, _extends2.default)({}, Dialog.defaultOptions);
  87. };
  88. Dialog.resetDefaultOptions();
  89. Dialog.install = function () {
  90. _vue.default.use(_Dialog.default);
  91. };
  92. Dialog.Component = _Dialog.default;
  93. _vue.default.prototype.$dialog = Dialog;
  94. var _default = Dialog;
  95. exports.default = _default;