index.js 2.1 KB

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