generateDialog.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { updateClassName } from '../util/reuse-methods'
  2. import _ from 'lodash'
  3. export const generateDialogMixin = {
  4. props: ['config', 'models', 'rules', 'element', 'remote', 'blanks', 'edit', 'remoteOption', 'platform', 'preview', 'containerKey', 'dataSourceValue', 'eventFunction', 'printRead', 'group', 'fieldNode'],
  5. data () {
  6. return {
  7. visible: this.element.options.visible,
  8. displayFields: {},
  9. dialogModel: this.models[this.element.model] ?? {},
  10. dialogDisabled: {}
  11. }
  12. },
  13. emits: ['update:models'],
  14. inject: ['generateComponentInstance', 'deleteComponentInstance', 'getFormComponent', 'onFormHide', 'onFormDisplay'],
  15. created () {
  16. this._generateModel(this.element.list)
  17. },
  18. mounted () {
  19. this.generateComponentInstance && this.generateComponentInstance(this.element.model, this.$refs[`generate-dialog-${this.element.model}`].$parent)
  20. },
  21. beforeUnmount () {
  22. this.deleteComponentInstance && this.deleteComponentInstance(this.element.model)
  23. },
  24. provide () {
  25. return {
  26. 'setDialogData': this.setDialogData
  27. }
  28. },
  29. methods: {
  30. setDialogData (value, field) {
  31. this.dialogModel[field] = value
  32. },
  33. _generateModel (genList) {
  34. for (let i = 0; i < genList.length; i++) {
  35. if (genList[i].type == 'grid') {
  36. this.displayFields[genList[i].model] = !genList[i].options.hidden
  37. genList[i].columns.forEach(item => {
  38. this._generateModel(item.list)
  39. })
  40. } else if (genList[i].type === 'tabs') {
  41. genList[i].tabs.forEach(item => {
  42. this._generateModel(item.list)
  43. })
  44. this.displayFields[genList[i].model] = !genList[i].options.hidden
  45. } else if (genList[i].type === 'collapse') {
  46. genList[i].tabs.forEach(item => {
  47. this._generateModel(item.list)
  48. })
  49. this.displayFields[genList[i].model] = !genList[i].options.hidden
  50. } else if (genList[i].type === 'report') {
  51. genList[i].rows.forEach(row => {
  52. row.columns.forEach(column => {
  53. this._generateModel(column.list)
  54. })
  55. })
  56. this.displayFields[genList[i].model] = !genList[i].options.hidden
  57. } else if (genList[i].type === 'inline') {
  58. this._generateModel(genList[i].list)
  59. this.displayFields[genList[i].model] = !genList[i].options.hidden
  60. } else if (genList[i].type === 'card') {
  61. this._generateModel(genList[i].list)
  62. this.displayFields[genList[i].model] = !genList[i].options.hidden
  63. } else {
  64. if (genList[i].type == 'blank') {
  65. this.dialogModel[genList[i].model] = genList[i].options.defaultType === 'String' ? '' : (genList[i].options.defaultType === 'Object' ? {} : [])
  66. } else {
  67. this.dialogModel[genList[i].model] = this.models[this.element.model][genList[i].model] ?? _.cloneDeep(genList[i].options.defaultValue)
  68. }
  69. this.displayFields[genList[i].model] = !genList[i].options.hidden
  70. this.dialogDisabled[genList[i].model] = !genList[i].options.disabled
  71. }
  72. }
  73. },
  74. open () {
  75. this.visible = true
  76. },
  77. close () {
  78. this.visible = false
  79. },
  80. hide (fields) {
  81. if (typeof fields === 'string') {
  82. fields = [fields]
  83. }
  84. fields.forEach(field => {
  85. this.onFormHide(`${this.fieldNode}.${field}`)
  86. })
  87. },
  88. display (fields) {
  89. if (typeof fields === 'string') {
  90. fields = [fields]
  91. }
  92. fields.forEach(field => {
  93. this.onFormDisplay(`${this.fieldNode}.${field}`)
  94. })
  95. },
  96. disabled (fields, disabled) {
  97. if (typeof fields === 'string') {
  98. fields = [fields]
  99. }
  100. this._setDisabled(this.element.list, fields, disabled)
  101. },
  102. handleCancel () {
  103. if (this.element && this.element.events && this.element.events.onCancel) {
  104. let funcKey = this.element.events.onCancel
  105. this.eventFunction[funcKey]({
  106. field: this.element.model,
  107. currentRef: this
  108. })
  109. }
  110. },
  111. handleConfirm () {
  112. if (this.element && this.element.events && this.element.events.onConfirm) {
  113. let funcKey = this.element.events.onConfirm
  114. this.eventFunction[funcKey]({
  115. field: this.element.model,
  116. currentRef: this
  117. })
  118. }
  119. },
  120. confirmLoading (status) {
  121. this.element.options.confirmLoading = status
  122. },
  123. setOptions (fields, options) {
  124. if (typeof fields === 'string') {
  125. fields = [fields]
  126. }
  127. this._setOptions(this.element.list, fields, options)
  128. },
  129. setData (value) {
  130. return new Promise((resolve) => {
  131. this.$nextTick(() => {
  132. Object.keys(value).forEach(item => {
  133. this.dialogModel[item] = value[item]
  134. })
  135. resolve()
  136. })
  137. })
  138. },
  139. getValues () {
  140. return this.dialogModel
  141. },
  142. getValue (fieldName) {
  143. return this.dialogModel[fieldName]
  144. },
  145. addClassName (fields, className) {
  146. if (typeof fields === 'string') {
  147. fields = [fields]
  148. }
  149. fields.forEach(item => {
  150. updateClassName(this.element.list, item.split('.'), className, 'add')
  151. })
  152. },
  153. removeClassName (fields, className) {
  154. if (typeof fields === 'string') {
  155. fields = [fields]
  156. }
  157. fields.forEach(item => {
  158. updateClassName(this.element.list, item.split('.'), className, 'remove')
  159. })
  160. },
  161. _setDisabled (genList, fields, disabled) {
  162. for (let i = 0; i < genList.length; i++) {
  163. if (genList[i].type === 'grid') {
  164. genList[i].columns.forEach(item => {
  165. this._setDisabled(item.list, fields, disabled)
  166. })
  167. } else if (genList[i].type === 'tabs') {
  168. genList[i].tabs.forEach(item => {
  169. this._setDisabled(item.list, fields, disabled)
  170. })
  171. } else if (genList[i].type === 'collapse') {
  172. genList[i].tabs.forEach(item => {
  173. this._setDisabled(item.list, fields, disabled)
  174. })
  175. } else if (genList[i].type === 'report') {
  176. genList[i].rows.forEach(row => {
  177. row.columns.forEach(column => {
  178. this._setDisabled(column.list, fields, disabled)
  179. })
  180. })
  181. } else if (genList[i].type === 'inline') {
  182. this._setDisabled(genList[i].list, fields, disabled)
  183. } else if (genList[i].type === 'card') {
  184. this._setDisabled(genList[i].list, fields, disabled)
  185. } else {
  186. if (fields.indexOf(genList[i].model) >= 0) {
  187. genList[i].options.disabled = disabled
  188. }
  189. }
  190. }
  191. },
  192. _setOptions (genList, fields, opts) {
  193. for (let i = 0; i < genList.length; i++) {
  194. if (genList[i].type === 'grid') {
  195. genList[i].columns.forEach(item => {
  196. this._setOptions(item.list, fields, opts)
  197. })
  198. } else if (genList[i].type === 'tabs') {
  199. genList[i].tabs.forEach(item => {
  200. this._setOptions(item.list, fields, opts)
  201. })
  202. } else if (genList[i].type === 'collapse') {
  203. genList[i].tabs.forEach(item => {
  204. this._setOptions(item.list, fields, opts)
  205. })
  206. } else if (genList[i].type === 'report') {
  207. genList[i].rows.forEach(row => {
  208. row.columns.forEach(column => {
  209. this._setOptions(column.list, fields, opts)
  210. })
  211. })
  212. } else if (genList[i].type === 'inline') {
  213. this._setOptions(genList[i].list, fields, opts)
  214. } else if (genList[i].type === 'card') {
  215. this._setOptions(genList[i].list, fields, opts)
  216. }
  217. if (fields.indexOf(genList[i].model) >= 0) {
  218. Object.keys(opts).forEach(key => {
  219. genList[i].options[key] = opts[key]
  220. })
  221. }
  222. }
  223. },
  224. _getAllRuleFields () {
  225. let realFields = []
  226. Object.keys(this.dialogModel).forEach((v) => {
  227. if (Array.isArray(this.dialogModel[v])) {
  228. const currentArray = this.dialogModel[v]
  229. currentArray.forEach((o, i) => {
  230. Object.keys(o).forEach((key) => {
  231. realFields.push(`${this.element.model}.${v}.${i}.${key}`)
  232. })
  233. })
  234. } else {
  235. realFields.push(`${this.element.model}.${v}`)
  236. }
  237. })
  238. return realFields
  239. },
  240. },
  241. watch: {
  242. dialogModel: {
  243. deep: true,
  244. handler (val) {
  245. this.$emit('update:models', {
  246. ...this.models,
  247. [this.element.model]: val
  248. })
  249. }
  250. },
  251. models: {
  252. deep: true,
  253. handler (val) {
  254. this.dialogModel = this.models[this.element.model]
  255. }
  256. }
  257. }
  258. }