index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 initInstance() {
  11. if (instance) {
  12. instance.$destroy();
  13. }
  14. instance = new (_vue["default"].extend(_Dialog["default"]))({
  15. el: document.createElement('div'),
  16. // avoid missing animation when first rendered
  17. propsData: {
  18. lazyRender: false
  19. }
  20. });
  21. instance.$on('input', function (value) {
  22. instance.value = value;
  23. });
  24. }
  25. function Dialog(options) {
  26. /* istanbul ignore if */
  27. if (_utils.isServer) {
  28. return Promise.resolve();
  29. }
  30. return new Promise(function (resolve, reject) {
  31. if (!instance || !(0, _utils.isInDocument)(instance.$el)) {
  32. initInstance();
  33. }
  34. (0, _extends2["default"])(instance, Dialog.currentOptions, options, {
  35. resolve: resolve,
  36. reject: reject
  37. });
  38. });
  39. }
  40. Dialog.defaultOptions = {
  41. value: true,
  42. title: '',
  43. message: '',
  44. overlay: true,
  45. className: '',
  46. lockScroll: true,
  47. beforeClose: null,
  48. messageAlign: '',
  49. getContainer: 'body',
  50. cancelButtonText: '',
  51. cancelButtonColor: null,
  52. confirmButtonText: '',
  53. confirmButtonColor: null,
  54. showConfirmButton: true,
  55. showCancelButton: false,
  56. closeOnClickOverlay: false,
  57. callback: function callback(action) {
  58. instance[action === 'confirm' ? 'resolve' : 'reject'](action);
  59. }
  60. };
  61. Dialog.alert = Dialog;
  62. Dialog.confirm = function (options) {
  63. return Dialog((0, _extends2["default"])({
  64. showCancelButton: true
  65. }, options));
  66. };
  67. Dialog.close = function () {
  68. if (instance) {
  69. instance.value = false;
  70. }
  71. };
  72. Dialog.setDefaultOptions = function (options) {
  73. (0, _extends2["default"])(Dialog.currentOptions, options);
  74. };
  75. Dialog.resetDefaultOptions = function () {
  76. Dialog.currentOptions = (0, _extends2["default"])({}, Dialog.defaultOptions);
  77. };
  78. Dialog.resetDefaultOptions();
  79. Dialog.install = function () {
  80. _vue["default"].use(_Dialog["default"]);
  81. };
  82. _vue["default"].prototype.$dialog = Dialog;
  83. var _default = Dialog;
  84. exports["default"] = _default;