generateFormItem.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import _ from 'lodash'
  2. import {executeExpression, isExpression, extractExpression} from '../util/expression'
  3. export const generateFormItemMixin = {
  4. props: ['config', 'widget', 'models', 'rules', 'remote', 'blanks', 'display', 'edit',
  5. 'remoteOption', 'platform', 'preview', 'containerKey', 'dataSourceValue', 'eventFunction',
  6. 'printRead', 'isSubform', 'rowIndex', 'subName', 'subHideFields', 'subDisabledFields',
  7. 'isDialog', 'dialogName', 'group', 'fieldNode', 'isGroup', 'isTable', 'isMobile', 'tableName'],
  8. data () {
  9. return {
  10. dataModel: Object.keys(this.models).includes(this.widget.model) ? this.models[this.widget.model] : _.cloneDeep(this.widget.options.defaultValue),
  11. dataModels: this.models
  12. }
  13. },
  14. computed: {
  15. labelWidth () {
  16. return this.widget.options.hideLabel ? '0px' : (this.widget.options.isLabelWidth ? this.widget.options.labelWidth + 'px' : this.config.labelWidth + 'px')
  17. },
  18. ruleProp () {
  19. let currentProp = this.widget.model
  20. if (this.group) {
  21. currentProp = this.group + '.' + currentProp
  22. }
  23. return currentProp
  24. },
  25. currentOptions () {
  26. if (this.isTable || this.isSubform) {
  27. return {
  28. value: this.dataModel,
  29. fieldNode: this.fieldNode ? this.fieldNode + '.' + this.widget.model : this.widget.model,
  30. currentRef: this.$refs['generateElementItem']?.$refs[`fm-${this.widget.model}`],
  31. rowIndex: this.rowIndex,
  32. row: _.cloneDeep(this.models),
  33. field: this.widget.model,
  34. group: this.group
  35. }
  36. } else {
  37. return {
  38. value: this.dataModel,
  39. fieldNode: this.fieldNode ? this.fieldNode + '.' + this.widget.model : this.widget.model,
  40. currentRef: this.$refs['generateElementItem']?.$refs[`fm-${this.widget.model}`],
  41. field: this.widget.model,
  42. group: this.group
  43. }
  44. }
  45. },
  46. elementDisplay () {
  47. let curFullField = this.fieldNode ? this.fieldNode + '.' + this.widget.model : this.widget.model
  48. let curField = this.group ? this.group + '.' + this.widget.model : this.widget.model
  49. if (this.dynamicHideFields[curFullField] != undefined) {
  50. return !this.dynamicHideFields[curFullField]
  51. }
  52. if (this.dynamicHideFields[curField] != undefined) {
  53. return !this.dynamicHideFields[curField]
  54. }
  55. if (typeof this.widget.options.hidden === 'boolean') {
  56. return !this.widget.options.hidden
  57. } else {
  58. if (isExpression(this.widget.options.hidden)) {
  59. return !executeExpression(extractExpression(this.widget.options.hidden), this.currentOptions, this.formContext)
  60. }
  61. }
  62. return true
  63. },
  64. groupNode () {
  65. return this.group ? this.group + '.' + this.widget.model : this.widget.model
  66. },
  67. elementNode () {
  68. return this.fieldNode ? this.fieldNode + '.' + this.widget.model : this.widget.model
  69. }
  70. },
  71. inject: {
  72. setSubformData: {
  73. default: () => {}
  74. },
  75. setDialogData: {
  76. default: () => {}
  77. },
  78. setGroupData: {
  79. default: () => {}
  80. },
  81. setTableData: {
  82. default: () => {}
  83. },
  84. getFormComponent: {
  85. default: () => { return null }
  86. },
  87. onChange: {
  88. default: () => {}
  89. },
  90. dynamicHideFields: {
  91. default: {}
  92. },
  93. dynamicValueData: {
  94. default: []
  95. },
  96. formContext: {
  97. default: {}
  98. },
  99. generateFormItemContext: {
  100. default: () => {}
  101. },
  102. deleteFormItemContext: {
  103. default: () => {}
  104. },
  105. sendRequest: {
  106. default: () => {}
  107. }
  108. },
  109. created () {
  110. this.refreshDynamicValue()
  111. },
  112. mounted () {
  113. this.generateFormItemContext(this.elementNode, this)
  114. },
  115. beforeUnmount () {
  116. this.deleteFormItemContext(this.elementNode)
  117. },
  118. methods: {
  119. refreshDynamicValue (args) {
  120. return new Promise((resolve, reject) => {
  121. if (this.widget.options.isDynamicValue
  122. && this.widget.options.dynamicValueType == 'datasource'
  123. && this.widget.options.dynamicValueDataSource) {
  124. let key = this.group ? this.group + '.' + this.widget.model + '.' + this.widget.options.dynamicValueDataSource
  125. : this.widget.model + '.' + this.widget.options.dynamicValueDataSource
  126. let curArgs = {...this.widget.options.dynamicValueArgs}
  127. for (let key in curArgs) {
  128. if (isExpression(curArgs[key])) {
  129. curArgs[key] = executeExpression(extractExpression(curArgs[key]), this.currentOptions, this.formContext)
  130. }
  131. }
  132. if (args && typeof args === 'object') {
  133. curArgs = {...curArgs, ...args}
  134. }
  135. let _data = this.dynamicValueData.find(item => item.key === key && _.isEqual(item.args, curArgs))
  136. if (_data) {
  137. if (this.widget.options.dynamicValueStruct) {
  138. try {
  139. this.dataModel = (new Function('data', `return data.${this.widget.options.dynamicValueStruct}`))(_data.value)
  140. resolve(this.dataModel)
  141. } catch (e) {reject(e)}
  142. } else {
  143. this.dataModel = _data.value
  144. resolve(this.dataModel)
  145. }
  146. } else {
  147. this.sendRequest(this.widget.options.dynamicValueDataSource, curArgs).then(resData => {
  148. if (this.widget.options.dynamicValueStruct) {
  149. try{
  150. this.dataModel = (new Function('data', `return data.${this.widget.options.dynamicValueStruct}`))(resData)
  151. resolve(this.dataModel)
  152. } catch (e) {reject(e)}
  153. } else {
  154. this.dataModel = resData
  155. resolve(this.dataModel)
  156. }
  157. })
  158. }
  159. }
  160. if (this.widget.options.isDynamicValue
  161. && this.widget.options.dynamicValueType == 'fx'
  162. && this.widget.options.dynamicValueFx) {
  163. let value = executeExpression(this.widget.options.dynamicValueFx, this.currentOptions, this.formContext)
  164. if (value !== null) {
  165. this.$nextTick(() => {
  166. this.dataModel = value
  167. })
  168. }
  169. resolve(value)
  170. }
  171. })
  172. },
  173. },
  174. watch: {
  175. dataModel: {
  176. deep: true,
  177. handler (val, oldValue) {
  178. if (val == oldValue || JSON.stringify(val) == JSON.stringify(oldValue)) {
  179. return false
  180. }
  181. if (this.isTable) {
  182. this.setTableData(val, this.rowIndex, this.widget.model)
  183. } else if (this.isSubform) {
  184. this.setSubformData(val, this.rowIndex, this.widget.model)
  185. } else if (this.isDialog) {
  186. this.setDialogData(val, this.widget.model)
  187. } else if (this.isGroup) {
  188. this.setGroupData(val, this.widget.model)
  189. } else {
  190. this.onChange(val, this.widget.model)
  191. }
  192. // 执行 onChange 方法
  193. this.$nextTick(() => {
  194. this.eventFunction['onFormChange'] && this.eventFunction['onFormChange'](this.currentOptions)
  195. if (this.widget.events && this.widget.events.onChange) {
  196. let funcKey = this.widget.events.onChange
  197. this.eventFunction[funcKey](this.currentOptions)
  198. }
  199. })
  200. }
  201. },
  202. models: {
  203. deep: true,
  204. handler (val) {
  205. this.dataModels = val
  206. this.dataModel = Object.keys(val).includes(this.widget.model) ? val[this.widget.model] : _.cloneDeep(this.widget.options.defaultValue)
  207. }
  208. }
  209. }
  210. }