generateGroup.js 7.9 KB

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