a4f28c7dd190752bcbdb9cd5fa6f1292378855ee.svn-base 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <j-modal :visible="visible" :confirmLoading="loading" :after-close="afterClose" v-bind="modalProps" @ok="onOk" @cancel="onCancel">
  3. <a-spin :spinning="loading">
  4. <div v-html="content"></div>
  5. <a-form-model ref="form" :model="model" :rules="rules">
  6. <a-form-model-item prop="input">
  7. <a-input ref="input" v-model="model.input" v-bind="inputProps" @pressEnter="onInputPressEnter"/>
  8. </a-form-model-item>
  9. </a-form-model>
  10. </a-spin>
  11. </j-modal>
  12. </template>
  13. <script>
  14. import pick from 'lodash.pick'
  15. export default {
  16. name: 'JPrompt',
  17. data() {
  18. return {
  19. visible: false,
  20. loading: false,
  21. content: '',
  22. // 弹窗参数
  23. modalProps: {
  24. title: '',
  25. },
  26. inputProps: {
  27. placeholder: '',
  28. },
  29. // form model
  30. model: {
  31. input: '',
  32. },
  33. // 校验
  34. rule: [],
  35. // 回调函数
  36. callback: {},
  37. }
  38. },
  39. computed: {
  40. rules() {
  41. return {
  42. input: this.rule
  43. }
  44. },
  45. },
  46. methods: {
  47. show(options) {
  48. this.content = options.content
  49. if (Array.isArray(options.rule)) {
  50. this.rule = options.rule
  51. }
  52. if (options.defaultValue != null) {
  53. this.model.input = options.defaultValue
  54. }
  55. // 取出常用的弹窗参数
  56. let pickModalProps = pick(options, 'title', 'centered', 'cancelText', 'closable', 'mask', 'maskClosable', 'okText', 'okType', 'okButtonProps', 'cancelButtonProps', 'width', 'wrapClassName', 'zIndex', 'dialogStyle', 'dialogClass')
  57. this.modalProps = Object.assign({}, pickModalProps, options.modalProps)
  58. // 取出常用的input参数
  59. let pickInputProps = pick(options, 'placeholder', 'allowClear')
  60. this.inputProps = Object.assign({}, pickInputProps, options.inputProps)
  61. // 回调函数
  62. this.callback = pick(options, 'onOk', 'onOkAsync', 'onCancel')
  63. this.visible = true
  64. this.$nextTick(() => this.$refs.input.focus())
  65. },
  66. onOk() {
  67. this.$refs.form.validate((ok, err) => {
  68. if (ok) {
  69. let event = {value: this.model.input, target: this}
  70. // 异步方法优先级高于同步方法
  71. if (typeof this.callback.onOkAsync === 'function') {
  72. this.callback.onOkAsync(event)
  73. } else if (typeof this.callback.onOk === 'function') {
  74. this.callback.onOk(event)
  75. this.close()
  76. } else {
  77. this.close()
  78. }
  79. }
  80. })
  81. },
  82. onCancel() {
  83. if (typeof this.callback.onCancel === 'function') {
  84. this.callback.onCancel(this.model.input)
  85. }
  86. this.close()
  87. },
  88. onInputPressEnter() {
  89. this.onOk()
  90. },
  91. close() {
  92. this.visible = this.loading ? this.visible : false
  93. },
  94. forceClose() {
  95. this.visible = false
  96. },
  97. showLoading() {
  98. this.loading = true
  99. },
  100. hideLoading() {
  101. this.loading = false
  102. },
  103. afterClose(e) {
  104. if (typeof this.modalProps.afterClose === 'function') {
  105. this.modalProps.afterClose(e)
  106. }
  107. this.$emit('after-close', e)
  108. },
  109. },
  110. }
  111. </script>
  112. <style scoped>
  113. </style>