GenerateDialog.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <template>
  2. <a-modal
  3. v-model="visible"
  4. :title="element.options.title"
  5. :width="element.options.width || '50%'"
  6. :mask-closable="false"
  7. :closable="element.options.showClose"
  8. destroy-on-close
  9. :ref="`generate-dialog-${element.model}`"
  10. :style="{
  11. top: element.options.top || '15vh'
  12. }"
  13. :z-index="3000"
  14. :wrap-class-name="'fm-generate-ant-dialog fm-form '
  15. + ((!element.options.showCancel && !element.options.showOk) ? 'no-footer ' : '')
  16. + (element.options.center ? 'center ' : '')
  17. + (element.options?.customClass ? element.options?.customClass : '')"
  18. @cancel="close"
  19. >
  20. <div >
  21. <template v-for="item in element.list">
  22. <generate-col-item
  23. v-if="item.type == 'grid'"
  24. :key="item.key"
  25. :model="dialogModel"
  26. :rules="rules"
  27. :element="item"
  28. :remote="remote"
  29. :blanks="blanks"
  30. :display="displayFields"
  31. :edit="edit"
  32. :remote-option="remoteOption"
  33. :platform="platform"
  34. :preview="preview"
  35. :container-key="containerKey"
  36. :data-source-value="dataSourceValue"
  37. :event-function="eventFunction"
  38. :print-read="printRead"
  39. :is-dialog="true"
  40. :dialog-name="element.model"
  41. :config="config"
  42. :group="element.model"
  43. :field-node="element.model"
  44. >
  45. <template v-slot:[blank.name]="scope" v-for="blank in blanks">
  46. <slot :name="blank.name" :model="scope.model"></slot>
  47. </template>
  48. </generate-col-item>
  49. <generate-tab-item
  50. v-else-if="item.type == 'tabs'"
  51. :key="item.key"
  52. :model="dialogModel"
  53. :rules="rules"
  54. :element="item"
  55. :remote="remote"
  56. :blanks="blanks"
  57. :display="displayFields"
  58. :edit="edit"
  59. :remote-option="remoteOption"
  60. :platform="platform"
  61. :preview="preview"
  62. :container-key="containerKey"
  63. :data-source-value="dataSourceValue"
  64. :event-function="eventFunction"
  65. :print-read="printRead"
  66. :is-dialog="true"
  67. :dialog-name="element.model"
  68. :config="config"
  69. :group="element.model"
  70. :field-node="element.model"
  71. >
  72. <template v-slot:[blank.name]="scope" v-for="blank in blanks">
  73. <slot :name="blank.name" :model="scope.model"></slot>
  74. </template>
  75. </generate-tab-item>
  76. <generate-collapse
  77. v-else-if="item.type == 'collapse'"
  78. :key="item.key"
  79. :model="dialogModel"
  80. :rules="rules"
  81. :element="item"
  82. :remote="remote"
  83. :blanks="blanks"
  84. :display="displayFields"
  85. :edit="edit"
  86. :remote-option="remoteOption"
  87. :platform="platform"
  88. :preview="preview"
  89. :container-key="containerKey"
  90. :data-source-value="dataSourceValue"
  91. :event-function="eventFunction"
  92. :print-read="printRead"
  93. :is-dialog="true"
  94. :dialog-name="element.model"
  95. :config="config"
  96. :group="element.model"
  97. :field-node="element.model"
  98. >
  99. <template v-slot:[blank.name]="scope" v-for="blank in blanks">
  100. <slot :name="blank.name" :model="scope.model"></slot>
  101. </template>
  102. </generate-collapse>
  103. <generate-report
  104. v-else-if="item.type == 'report'"
  105. :key="item.key"
  106. :model="dialogModel"
  107. :rules="rules"
  108. :element="item"
  109. :remote="remote"
  110. :blanks="blanks"
  111. :display="displayFields"
  112. :edit="edit"
  113. :remote-option="remoteOption"
  114. :platform="platform"
  115. :preview="preview"
  116. :container-key="containerKey"
  117. :data-source-value="dataSourceValue"
  118. :event-function="eventFunction"
  119. :print-read="printRead"
  120. :is-dialog="true"
  121. :dialog-name="element.model"
  122. :config="config"
  123. :group="element.model"
  124. :field-node="element.model"
  125. >
  126. <template v-slot:[blank.name]="scope" v-for="blank in blanks">
  127. <slot :name="blank.name" :model="scope.model"></slot>
  128. </template>
  129. </generate-report>
  130. <generate-card
  131. v-else-if="item.type == 'card'"
  132. :key="item.key"
  133. :model="dialogModel"
  134. :rules="rules"
  135. :element="item"
  136. :remote="remote"
  137. :blanks="blanks"
  138. :display="displayFields"
  139. :edit="edit"
  140. :remote-option="remoteOption"
  141. :platform="platform"
  142. :preview="preview"
  143. :container-key="containerKey"
  144. :data-source-value="dataSourceValue"
  145. :event-function="eventFunction"
  146. :print-read="printRead"
  147. :is-dialog="true"
  148. :dialog-name="element.model"
  149. :config="config"
  150. :group="element.model"
  151. :field-node="element.model"
  152. >
  153. <template v-slot:[blank.name]="scope" v-for="blank in blanks">
  154. <slot :name="blank.name" :model="scope.model"></slot>
  155. </template>
  156. </generate-card>
  157. <generate-inline
  158. v-else-if="item.type == 'inline'"
  159. :key="item.key"
  160. :model="dialogModel"
  161. :rules="rules"
  162. :element="item"
  163. :remote="remote"
  164. :blanks="blanks"
  165. :display="displayFields"
  166. :edit="edit"
  167. :remote-option="remoteOption"
  168. :platform="platform"
  169. :preview="preview"
  170. :container-key="containerKey"
  171. :data-source-value="dataSourceValue"
  172. :event-function="eventFunction"
  173. :print-read="printRead"
  174. :is-dialog="true"
  175. :dialog-name="element.model"
  176. :config="config"
  177. :group="element.model"
  178. :field-node="element.model"
  179. >
  180. <template v-slot:[blank.name]="scope" v-for="blank in blanks">
  181. <slot :name="blank.name" :model="scope.model"></slot>
  182. </template>
  183. </generate-inline>
  184. <generate-form-item
  185. v-else
  186. :key="item.key"
  187. :models="dialogModel"
  188. :rules="rules"
  189. :widget="item"
  190. :remote="remote"
  191. :blanks="blanks"
  192. :display="displayFields"
  193. :edit="edit"
  194. :remote-option="remoteOption"
  195. :platform="platform"
  196. :preview="preview"
  197. :container-key="containerKey"
  198. :data-source-value="dataSourceValue"
  199. :event-function="eventFunction"
  200. :print-read="printRead"
  201. :is-dialog="true"
  202. :dialog-name="element.model"
  203. :config="config"
  204. :group="element.model"
  205. :field-node="element.model"
  206. >
  207. <template v-slot:[blank.name]="scope" v-for="blank in blanks">
  208. <slot :name="blank.name" :model="scope.model"></slot>
  209. </template>
  210. </generate-form-item>
  211. </template>
  212. </div>
  213. <template #footer>
  214. <div>
  215. <a-button key="back" v-if="element.options.showCancel" @click="handleCancel">{{element.options.cancelText}}</a-button>
  216. <a-button key="submit" type="primary" v-if="element.options.showOk" :loading="element.options.confirmLoading" @click="handleConfirm">{{element.options.okText}}</a-button>
  217. </div>
  218. </template>
  219. </a-modal>
  220. </template>
  221. <script>
  222. import GenerateFormItem from './GenerateFormItem.vue'
  223. import GenerateColItem from './GenerateColItem.vue'
  224. import GenerateTabItem from './GenerateTabItem.vue'
  225. import GenerateReport from './GenerateReport.vue'
  226. import GenerateInline from './GenereteInline.vue'
  227. import GenerateCollapse from './GenerateCollapse.vue'
  228. import GenerateCard from './GenerateCard.vue'
  229. import { updateClassName } from '../../util/reuse-methods'
  230. import _ from 'lodash'
  231. export default {
  232. components: {
  233. GenerateColItem,
  234. GenerateFormItem,
  235. GenerateInline,
  236. GenerateReport,
  237. GenerateTabItem,
  238. GenerateCollapse,
  239. GenerateCard
  240. },
  241. props: ['config', 'models', 'rules', 'element', 'remote', 'blanks', 'edit', 'remoteOption', 'platform', 'preview', 'containerKey', 'dataSourceValue', 'eventFunction', 'printRead', 'group', 'fieldNode', 'componentInstance'],
  242. data () {
  243. return {
  244. visible: this.element.options.visible,
  245. displayFields: {},
  246. dialogModel: this.models[this.element.model] || {},
  247. dialogDisabled: {},
  248. activeElement: ''
  249. }
  250. },
  251. inject: ['generateComponentInstance', 'deleteComponentInstance', 'getFormComponent', 'onFormHide', 'onFormDisplay'],
  252. created () {
  253. this._generateModel(this.element.list)
  254. },
  255. mounted () {
  256. this.generateComponentInstance && this.generateComponentInstance(this.element.model, this.$refs[`generate-dialog-${this.element.model}`].$parent)
  257. },
  258. beforeDestroy () {
  259. this.deleteComponentInstance && this.deleteComponentInstance(this.element.model)
  260. },
  261. provide () {
  262. return {
  263. 'setDialogData': this.setDialogData
  264. }
  265. },
  266. methods: {
  267. setDialogData (value, field) {
  268. this.$set(this.dialogModel, field, value)
  269. },
  270. _generateModel (genList) {
  271. for (let i = 0; i < genList.length; i++) {
  272. if (genList[i].type == 'grid') {
  273. this.displayFields[genList[i].model] = !genList[i].options.hidden
  274. genList[i].columns.forEach(item => {
  275. this._generateModel(item.list)
  276. })
  277. } else if (genList[i].type === 'tabs') {
  278. genList[i].tabs.forEach(item => {
  279. this._generateModel(item.list)
  280. })
  281. this.displayFields[genList[i].model] = !genList[i].options.hidden
  282. } else if (genList[i].type === 'collapse') {
  283. genList[i].tabs.forEach(item => {
  284. this._generateModel(item.list)
  285. })
  286. this.displayFields[genList[i].model] = !genList[i].options.hidden
  287. } else if (genList[i].type === 'report') {
  288. genList[i].rows.forEach(row => {
  289. row.columns.forEach(column => {
  290. this._generateModel(column.list)
  291. })
  292. })
  293. this.displayFields[genList[i].model] = !genList[i].options.hidden
  294. } else if (genList[i].type === 'inline') {
  295. this._generateModel(genList[i].list)
  296. this.displayFields[genList[i].model] = !genList[i].options.hidden
  297. } else if (genList[i].type === 'card') {
  298. this._generateModel(genList[i].list)
  299. this.displayFields[genList[i].model] = !genList[i].options.hidden
  300. } else {
  301. if (genList[i].type == 'blank') {
  302. this.$set(this.dialogModel, genList[i].model, genList[i].options.defaultType === 'String' ? '' : (genList[i].options.defaultType === 'Object' ? {} : []))
  303. } else {
  304. this.$set(this.dialogModel, genList[i].model, this.models[this.element.model][genList[i].model] || _.cloneDeep(genList[i].options.defaultValue))
  305. }
  306. this.displayFields[genList[i].model] = !genList[i].options.hidden
  307. this.dialogDisabled[genList[i].model] = !genList[i].options.disabled
  308. }
  309. }
  310. },
  311. open () {
  312. this.activeElement = document.activeElement
  313. document.activeElement && document.activeElement.blur()
  314. this.visible = true
  315. if (this.element && this.element.events && this.element.events.onOpen) {
  316. let funcKey = this.element.events.onOpen
  317. this.$nextTick(() => {
  318. this.eventFunction[funcKey]({field: this.element.model, currentRef: this})
  319. })
  320. }
  321. },
  322. close () {
  323. if (this.activeElement) {
  324. this.activeElement.focus()
  325. this.activeElement = ''
  326. }
  327. this.visible = false
  328. if (this.element && this.element.events && this.element.events.onClose) {
  329. let funcKey = this.element.events.onClose
  330. this.eventFunction[funcKey]({field: this.element.model, currentRef: this})
  331. }
  332. },
  333. hide (fields) {
  334. if (typeof fields === 'string') {
  335. fields = [fields]
  336. }
  337. fields.forEach(field => {
  338. this.onFormHide(`${this.fieldNode}.${field}`)
  339. })
  340. },
  341. display (fields) {
  342. if (typeof fields === 'string') {
  343. fields = [fields]
  344. }
  345. fields.forEach(field => {
  346. this.onFormDisplay(`${this.fieldNode}.${field}`)
  347. })
  348. },
  349. disabled (fields, disabled) {
  350. if (typeof fields === 'string') {
  351. fields = [fields]
  352. }
  353. this._setDisabled(this.element.list, fields, disabled)
  354. },
  355. validate () {
  356. return new Promise((resolve, reject) => {
  357. let realFields = this._getAllRuleFields()
  358. Promise.all(realFields.map(fieldItem => {
  359. return new Promise((resolve2) => {
  360. this.getFormComponent().validateField(fieldItem, (error, values) => {
  361. resolve2(values)
  362. })
  363. })
  364. })).then((values) => {
  365. console.log('promise all success', values)
  366. if (values.findIndex(v => v) >= 0) {
  367. reject()
  368. } else {
  369. resolve()
  370. }
  371. })
  372. })
  373. },
  374. clearValidate () {
  375. let realFields = this._getAllRuleFields()
  376. this.getFormComponent().clearValidate(realFields)
  377. },
  378. handleCancel () {
  379. if (this.element && this.element.events && this.element.events.onCancel) {
  380. let funcKey = this.element.events.onCancel
  381. this.eventFunction[funcKey]({field: this.element.model, currentRef: this})
  382. }
  383. },
  384. handleConfirm () {
  385. if (this.element && this.element.events && this.element.events.onConfirm) {
  386. let funcKey = this.element.events.onConfirm
  387. this.eventFunction[funcKey]({field: this.element.model, currentRef: this})
  388. }
  389. },
  390. confirmLoading (status) {
  391. this.element.options.confirmLoading = status
  392. },
  393. setOptions (fields, options) {
  394. if (typeof fields === 'string') {
  395. fields = [fields]
  396. }
  397. this._setOptions(this.element.list, fields, options)
  398. },
  399. setData (value) {
  400. return new Promise((resolve) => {
  401. this.$nextTick(() => {
  402. Object.keys(value).forEach(item => {
  403. this.$set(this.dialogModel, item, value[item])
  404. })
  405. resolve()
  406. })
  407. })
  408. },
  409. getValues () {
  410. return this.dialogModel
  411. },
  412. getValue (fieldName) {
  413. return this.dialogModel[fieldName]
  414. },
  415. addClassName (fields, className) {
  416. if (typeof fields === 'string') {
  417. fields = [fields]
  418. }
  419. fields.forEach(item => {
  420. updateClassName(this.element.list, item.split('.'), className, 'add')
  421. })
  422. },
  423. removeClassName (fields, className) {
  424. if (typeof fields === 'string') {
  425. fields = [fields]
  426. }
  427. fields.forEach(item => {
  428. updateClassName(this.element.list, item.split('.'), className, 'remove')
  429. })
  430. },
  431. _setDisabled (genList, fields, disabled) {
  432. for (let i = 0; i < genList.length; i++) {
  433. if (genList[i].type === 'grid') {
  434. genList[i].columns.forEach(item => {
  435. this._setDisabled(item.list, fields, disabled)
  436. })
  437. } else if (genList[i].type === 'tabs') {
  438. genList[i].tabs.forEach(item => {
  439. this._setDisabled(item.list, fields, disabled)
  440. })
  441. } else if (genList[i].type === 'collapse') {
  442. genList[i].tabs.forEach(item => {
  443. this._setDisabled(item.list, fields, disabled)
  444. })
  445. } else if (genList[i].type === 'report') {
  446. genList[i].rows.forEach(row => {
  447. row.columns.forEach(column => {
  448. this._setDisabled(column.list, fields, disabled)
  449. })
  450. })
  451. } else if (genList[i].type === 'inline') {
  452. this._setDisabled(genList[i].list, fields, disabled)
  453. } else if (genList[i].type === 'card') {
  454. this._setDisabled(genList[i].list, fields, disabled)
  455. } else {
  456. if (fields.indexOf(genList[i].model) >= 0) {
  457. genList[i].options.disabled = disabled
  458. }
  459. }
  460. }
  461. },
  462. _setOptions (genList, fields, opts) {
  463. for (let i = 0; i < genList.length; i++) {
  464. if (genList[i].type === 'grid') {
  465. genList[i].columns.forEach(item => {
  466. this._setOptions(item.list, fields, opts)
  467. })
  468. } else if (genList[i].type === 'tabs') {
  469. genList[i].tabs.forEach(item => {
  470. this._setOptions(item.list, fields, opts)
  471. })
  472. } else if (genList[i].type === 'collapse') {
  473. genList[i].tabs.forEach(item => {
  474. this._setOptions(item.list, fields, opts)
  475. })
  476. } else if (genList[i].type === 'report') {
  477. genList[i].rows.forEach(row => {
  478. row.columns.forEach(column => {
  479. this._setOptions(column.list, fields, opts)
  480. })
  481. })
  482. } else if (genList[i].type === 'inline') {
  483. this._setOptions(genList[i].list, fields, opts)
  484. } else if (genList[i].type === 'card') {
  485. this._setOptions(genList[i].list, fields, opts)
  486. }
  487. if (fields.indexOf(genList[i].model) >= 0) {
  488. Object.keys(opts).forEach(key => {
  489. genList[i].options[key] = opts[key]
  490. })
  491. }
  492. }
  493. },
  494. _getAllRuleFields () {
  495. let realFields = []
  496. Object.keys(this.dialogModel).forEach((v) => {
  497. if (Array.isArray(this.dialogModel[v])) {
  498. const currentArray = this.dialogModel[v]
  499. currentArray.forEach((o, i) => {
  500. Object.keys(o).forEach((key) => {
  501. this.componentInstance[`${this.element.model}.${v}.${i}.${key}`] && realFields.push(`${this.element.model}.${v}.${i}.${key}`)
  502. })
  503. })
  504. } else {
  505. this.componentInstance[`${this.element.model}.${v}`] && realFields.push(`${this.element.model}.${v}`)
  506. }
  507. })
  508. return realFields
  509. }
  510. },
  511. watch: {
  512. dialogModel: {
  513. deep: true,
  514. handler (val) {
  515. this.$emit('update:models', {
  516. ...this.models,
  517. [this.element.model]: val
  518. })
  519. }
  520. },
  521. models: {
  522. deep: true,
  523. handler (val) {
  524. this.dialogModel = this.models[this.element.model]
  525. }
  526. }
  527. }
  528. }
  529. </script>
  530. <style lang="scss">
  531. .fm-generate-ant-dialog{
  532. &.no-footer{
  533. .ant-modal-footer{
  534. display: none;
  535. }
  536. }
  537. &.center{
  538. .ant-modal-header{
  539. text-align: center;
  540. }
  541. .ant-modal-footer{
  542. text-align: center;
  543. }
  544. }
  545. .ant-form-item{
  546. .ant-pagination{
  547. float: right;
  548. }
  549. }
  550. }
  551. </style>