confirm.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import _extends from 'babel-runtime/helpers/extends';
  2. import Vue from 'vue';
  3. import ConfirmDialog from './ConfirmDialog';
  4. import { destroyFns } from './Modal';
  5. import Base from '../base';
  6. import Omit from 'omit.js';
  7. export default function confirm(config) {
  8. var div = document.createElement('div');
  9. var el = document.createElement('div');
  10. div.appendChild(el);
  11. document.body.appendChild(div);
  12. var currentConfig = _extends({}, Omit(config, ['parentContext']), { close: close, visible: true });
  13. var confirmDialogInstance = null;
  14. var confirmDialogProps = { props: {} };
  15. function close() {
  16. destroy.apply(undefined, arguments);
  17. }
  18. function update(newConfig) {
  19. currentConfig = _extends({}, currentConfig, newConfig);
  20. confirmDialogProps.props = currentConfig;
  21. }
  22. function destroy() {
  23. if (confirmDialogInstance && div.parentNode) {
  24. confirmDialogInstance.$destroy();
  25. confirmDialogInstance = null;
  26. div.parentNode.removeChild(div);
  27. }
  28. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  29. args[_key] = arguments[_key];
  30. }
  31. var triggerCancel = args.some(function (param) {
  32. return param && param.triggerCancel;
  33. });
  34. if (config.onCancel && triggerCancel) {
  35. config.onCancel.apply(config, args);
  36. }
  37. for (var i = 0; i < destroyFns.length; i++) {
  38. var fn = destroyFns[i];
  39. if (fn === close) {
  40. destroyFns.splice(i, 1);
  41. break;
  42. }
  43. }
  44. }
  45. function render(props) {
  46. confirmDialogProps.props = props;
  47. var V = Base.Vue || Vue;
  48. return new V({
  49. el: el,
  50. parent: config.parentContext,
  51. data: function data() {
  52. return { confirmDialogProps: confirmDialogProps };
  53. },
  54. render: function render() {
  55. var h = arguments[0];
  56. // 先解构,避免报错,原因不详
  57. var cdProps = _extends({}, this.confirmDialogProps);
  58. return h(ConfirmDialog, cdProps);
  59. }
  60. });
  61. }
  62. confirmDialogInstance = render(currentConfig);
  63. destroyFns.push(close);
  64. return {
  65. destroy: close,
  66. update: update
  67. };
  68. }