generateForm.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. import {loadJs, updateStyleSheets, splitStyleSheets, clearStyleSheets, consoleError, getBindModels } from '../util/index.js'
  2. import { updateClassName } from '../util/reuse-methods.js'
  3. import { EventBus } from '../util/event-bus.js'
  4. import _ from 'lodash'
  5. import axios from 'axios'
  6. import { exportPDF } from '../util/export.js'
  7. import { ruleToFunction } from '../util/rule-funcs.js'
  8. import { isExpression, extractExpression, executeExpression } from '../util/expression'
  9. export const generateFormMixin = {
  10. props: {
  11. data: {
  12. type: Object,
  13. default: () => ({
  14. "list": [],
  15. "config": {
  16. "labelWidth": 100,
  17. "labelPosition": "right",
  18. "size": "small",
  19. "customClass": "",
  20. "ui": "element",
  21. "layout": "horizontal",
  22. }
  23. })
  24. },
  25. remote: {
  26. type: Object,
  27. default: () => ({})
  28. },
  29. value: {
  30. type: Object,
  31. default: () => ({})
  32. },
  33. edit: {
  34. type: Boolean,
  35. default: true
  36. },
  37. printRead: {
  38. type: Boolean,
  39. default: false
  40. },
  41. remoteOption: {
  42. type: Object,
  43. default: () => ({})
  44. },
  45. preview: {
  46. type: Boolean,
  47. default: false
  48. },
  49. platform: {
  50. type: String,
  51. default: 'pc'
  52. }
  53. },
  54. emits: ['on-change'],
  55. data () {
  56. return {
  57. formData: {},
  58. models: {},
  59. rules: {},
  60. blanks: [],
  61. displayFields: {},
  62. dataBindFields: {},
  63. generateShow: false,
  64. resetModels: {},
  65. formKey: Math.random().toString(36).slice(-8),
  66. formStyleKey: Math.random().toString(36).slice(-8),
  67. formValue: this.value,
  68. formShow: false,
  69. formRef: Math.random().toString(36).slice(-8) + 'Form',
  70. containerKey: Math.random().toString(36).slice(-8),
  71. dataSourceValue: [],
  72. eventFunction: {},
  73. instanceObject: {},
  74. dataSourceInterface: [],
  75. dynamicHideFields: {},
  76. dynamicValueData: [],
  77. dynamicValueFxInterface: {},
  78. dynamicOptionFxInterface: {},
  79. formItemContexts: {},
  80. }
  81. },
  82. created () {
  83. this._initForm()
  84. },
  85. mounted () {
  86. this.$nextTick(() => {
  87. this.eventFunction['mounted'] && this.eventFunction['mounted']({})
  88. })
  89. },
  90. beforeUnmount () {
  91. let head = '.fm-' + this.formStyleKey + ' '
  92. clearStyleSheets(head)
  93. },
  94. provide() {
  95. return {
  96. 'generateComponentInstance': this.generateComponentInstance,
  97. 'deleteComponentInstance': this.deleteComponentInstance,
  98. 'eventScriptConfig': this.getEventScriptConfig,
  99. 'getFormComponent': this.getFormComponent,
  100. 'onChange': this._changeFormValue,
  101. 'onFormDisabled': this.disabled,
  102. 'dynamicHideFields': this.dynamicHideFields,
  103. 'onFormHide': this.hide,
  104. 'onFormDisplay': this.display,
  105. 'dynamicValueData': this.dynamicValueData,
  106. 'dynamicOptionData': this.dynamicOptionData,
  107. 'formContext': this,
  108. 'generateFormItemContext': this.generateFormItemContext,
  109. 'deleteFormItemContext': this.deleteFormItemContext,
  110. 'sendRequest': this.sendRequest
  111. }
  112. },
  113. methods: {
  114. getFormComponent () {
  115. return this.$refs[this.formRef]
  116. },
  117. _changeFormValue (value, field) {
  118. this.models[field] = value
  119. this.$emit('on-change', field, value, this.models)
  120. },
  121. _initForm () {
  122. this.formShow = false
  123. this.formData = _.cloneDeep(this.data)
  124. this.models = {}
  125. this.rules = {}
  126. this.blanks = []
  127. this.displayFields = {}
  128. this.dataBindFields = {}
  129. this.resetModels = {}
  130. this.dataSourceValue = []
  131. this.eventFunction = []
  132. this.instanceObject = {}
  133. this.dataSourceInterface = []
  134. // 不能直接设置为[], 防止响应式丢失
  135. this.dynamicValueData.splice(0, this.dynamicValueData.length)
  136. Object.keys(this.dynamicHideFields).forEach(key => delete this.dynamicHideFields[key])
  137. this.dynamicValueFxInterface = {}
  138. this.dynamicOptionFxInterface = {}
  139. if (Object.keys(this.data).length) {
  140. this.generateModel(this.formData.list)
  141. } else {
  142. this.generateModel([])
  143. }
  144. this.models = {...this.models}
  145. return new Promise((resolve) => {
  146. this.$nextTick(() => {
  147. this.formShow = true
  148. if (this.formData.config && this.formData.config.styleSheets) {
  149. let head = '.fm-' + this.formStyleKey + ' '
  150. updateStyleSheets(splitStyleSheets(this.formData.config.styleSheets), head)
  151. }
  152. this.loadDataSource()
  153. this.loadDynamicValueFx()
  154. this.loadEvents()
  155. this.$nextTick(() => {
  156. this.eventFunction['refresh'] && this.eventFunction['refresh']({})
  157. this.resetModels = _.cloneDeep(this.models)
  158. resolve()
  159. })
  160. })
  161. })
  162. },
  163. getEventScriptConfig () {
  164. return this.data?.config?.eventScript ?? []
  165. },
  166. loadEvents () {
  167. if (this.formData.config && this.formData.config.eventScript) {
  168. for (let i = 0; i < this.formData.config.eventScript.length; i++) {
  169. let currentScript = this.formData.config.eventScript[i]
  170. if (currentScript.type === 'rule') {
  171. // 加载异步方法
  172. // console.log(ruleToFunction(currentScript.rules))
  173. const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor
  174. this.eventFunction[currentScript.key] = new AsyncFunction(ruleToFunction(currentScript.rules)).bind(this)
  175. } else {
  176. this.eventFunction[currentScript.key] = Function(currentScript.func).bind(this)
  177. }
  178. }
  179. }
  180. },
  181. triggerEvent (eventName, args) {
  182. if (this.formData.config && this.formData.config.eventScript) {
  183. let eventScript = this.formData.config.eventScript.find(item => item.name == eventName)
  184. if (eventScript) {
  185. return this.eventFunction[eventScript.key](args)
  186. }
  187. }
  188. },
  189. refreshDynamicValueAll () {
  190. this.refreshDynamicValue()
  191. },
  192. refreshDynamicValue (fields, args) {
  193. if (fields === undefined) {
  194. let refreshItems = []
  195. Object.keys(this.dynamicValueFxInterface).forEach(field => {
  196. if (this.formItemContexts[field]) {
  197. if (Array.isArray(this.formItemContexts[field])) {
  198. this.formItemContexts[field].forEach(fItem => {
  199. refreshItems.push(fItem)
  200. })
  201. } else {
  202. refreshItems.push(this.formItemContexts[field])
  203. }
  204. } else {
  205. let groupFormItems = []
  206. Object.keys(this.formItemContexts).forEach(key => {
  207. if (this.formItemContexts[key].groupNode == field) {
  208. groupFormItems.push(this.formItemContexts[key])
  209. }
  210. })
  211. groupFormItems.forEach(fItem => {
  212. refreshItems.push(fItem)
  213. })
  214. }
  215. })
  216. for (let i = 0; i < this.dataSourceInterface.length; i++) {
  217. let curRequest = this.dataSourceInterface[i]
  218. if (curRequest.dynamicValueFields.length) {
  219. curRequest.dynamicValueFields.forEach(({field, struct}) => {
  220. if (this.formItemContexts[field]) {
  221. if (Array.isArray(this.formItemContexts[field])) {
  222. this.formItemContexts[field].forEach(fItem => {
  223. refreshItems.push(fItem)
  224. })
  225. } else {
  226. refreshItems.push(this.formItemContexts[field])
  227. }
  228. } else {
  229. let groupFormItems = []
  230. Object.keys(this.formItemContexts).forEach(key => {
  231. if (this.formItemContexts[key].groupNode == field) {
  232. groupFormItems.push(this.formItemContexts[key])
  233. }
  234. })
  235. groupFormItems.forEach(fItem => {
  236. refreshItems.push(fItem)
  237. })
  238. }
  239. })
  240. }
  241. }
  242. return new Promise((resolve, reject) => {
  243. Promise.all(refreshItems.map(item => item.refreshDynamicValue())).then(values => {
  244. resolve(values)
  245. }).catch(e => {
  246. reject(e)
  247. })
  248. })
  249. }
  250. if (typeof fields === 'string') {
  251. fields = [fields]
  252. }
  253. let refreshItems = []
  254. const curArgs = (args && typeof args === 'object') ? {...args} : {}
  255. fields.forEach(field => {
  256. if (this.formItemContexts[field]) {
  257. if (Array.isArray(this.formItemContexts[field])) {
  258. this.formItemContexts[field].forEach(fItem => {
  259. refreshItems.push(fItem)
  260. })
  261. } else {
  262. refreshItems.push(this.formItemContexts[field])
  263. }
  264. } else {
  265. let groupFormItems = []
  266. Object.keys(this.formItemContexts).forEach(key => {
  267. if (this.formItemContexts[key].groupNode == field) {
  268. groupFormItems.push(this.formItemContexts[key])
  269. }
  270. })
  271. groupFormItems.forEach(fItem => {
  272. refreshItems.push(fItem)
  273. })
  274. }
  275. })
  276. return new Promise((resolve, reject) => {
  277. Promise.all(refreshItems.map(item => item.refreshDynamicValue(curArgs))).then(values => {
  278. resolve(values)
  279. }).catch(e => {
  280. reject(e)
  281. })
  282. })
  283. },
  284. loadDataSource () {
  285. for (let i = 0; i < this.dataSourceInterface.length; i++) {
  286. let curRequest = this.dataSourceInterface[i]
  287. let requestObj = this.formData.config.dataSource.find(item => item.key == curRequest.key)
  288. if (requestObj && requestObj.auto)
  289. requestObj.name && this.sendRequest(requestObj.name, {...curRequest.args}).then(data => {
  290. let curArgs = {...curRequest.args}
  291. for (let key in curArgs) {
  292. if (isExpression(curArgs[key])) {
  293. curArgs[key] = executeExpression(extractExpression(curArgs[key]), {}, this)
  294. }
  295. }
  296. curRequest.fields.forEach(field => {
  297. let curKey = field + '.' + curRequest.key
  298. let sourceValue = this.dataSourceValue.find(item => item.key === curKey)
  299. if (sourceValue) {
  300. sourceValue.value = data
  301. } else {
  302. this.dataSourceValue.push({
  303. key: curKey,
  304. args: curArgs,
  305. value: data
  306. })
  307. }
  308. })
  309. curRequest.dynamicValueFields.forEach(({field, struct}) => {
  310. let curKey = field + '.' + curRequest.key
  311. let sourceValue = this.dynamicValueData.find(item => item.key === curKey)
  312. if (sourceValue) {
  313. sourceValue.value = data
  314. } else {
  315. this.dynamicValueData.push({
  316. key: curKey,
  317. args: curArgs,
  318. value: data
  319. })
  320. }
  321. })
  322. })
  323. }
  324. // 处理需要初始化请求但没有进行绑定的数据源
  325. if (this.formData.config?.dataSource?.length > 0) {
  326. for (let i = 0; i < this.formData.config.dataSource.length; i++) {
  327. let currentDataSource = this.formData.config.dataSource[i]
  328. if (currentDataSource.auto && this.dataSourceInterface.findIndex(item => item.key == currentDataSource.key) < 0) {
  329. this.sendRequest(currentDataSource.name, {})
  330. }
  331. }
  332. }
  333. },
  334. loadDynamicValueFx () {
  335. Object.keys(this.dynamicValueFxInterface).forEach(field => {
  336. if (this.formItemContexts[field]) {
  337. if (Array.isArray(this.formItemContexts[field])) {
  338. this.formItemContexts[field].forEach(fItem => {
  339. fItem.refreshDynamicValue()
  340. })
  341. } else {
  342. this.formItemContexts[field].refreshDynamicValue()
  343. }
  344. } else {
  345. let groupFormItems = []
  346. Object.keys(this.formItemContexts).forEach(key => {
  347. if (this.formItemContexts[key].groupNode == field) {
  348. groupFormItems.push(this.formItemContexts[key])
  349. }
  350. })
  351. groupFormItems.forEach(fItem => {
  352. fItem.refreshDynamicValue()
  353. })
  354. }
  355. })
  356. },
  357. refreshFieldOptionData (fields, args) {
  358. if (fields === undefined) return Promise.resolve([])
  359. if (typeof fields === 'string') {
  360. fields = [fields]
  361. }
  362. let refreshInstances = []
  363. const curArgs = (args && typeof args === 'object') ? {...args} : {}
  364. fields.forEach(field => {
  365. if (this.instanceObject[field]) {
  366. if (Array.isArray(this.instanceObject[field])) {
  367. this.instanceObject[field].forEach(fItem => {
  368. fItem?.$parent && refreshInstances.push(fItem?.$parent)
  369. })
  370. } else {
  371. this.instanceObject[field]?.$parent && refreshInstances.push(this.instanceObject[field]?.$parent)
  372. }
  373. } else {
  374. let groupFormItems = []
  375. Object.keys(this.instanceObject).forEach(key => {
  376. if (this.instanceObject[key]?.$parent?.groupNode == field) {
  377. groupFormItems.push(this.instanceObject[key])
  378. }
  379. })
  380. groupFormItems.forEach(fItem => {
  381. fItem?.$parent && refreshInstances.push(fItem?.$parent)
  382. })
  383. }
  384. })
  385. return new Promise((resolve, reject) => {
  386. Promise.all(refreshInstances.map(item => item.refreshOptionData(curArgs))).then(values => {
  387. resolve(values)
  388. }).catch(e => {
  389. reject(e)
  390. })
  391. })
  392. },
  393. refreshFieldDataSource (field, args) {
  394. let curRequest = this.dataSourceInterface.find(item => item.fields.includes(field))
  395. return new Promise((resolve, reject) => {
  396. if (curRequest) {
  397. let requestName = this.formData.config.dataSource.find(item => item.key == curRequest.key)?.name
  398. requestName && this.sendRequest(requestName, {...curRequest.args, ...args}).then(data => {
  399. let curKey = field + '.' + curRequest.key
  400. let sourceValue = this.dataSourceValue.find(item => item.key === curKey)
  401. if (sourceValue) {
  402. sourceValue.value = data
  403. } else {
  404. this.dataSourceValue.push({
  405. key: curKey,
  406. value: data
  407. })
  408. }
  409. resolve(data)
  410. }).catch(() => {
  411. reject()
  412. })
  413. } else {
  414. resolve()
  415. }
  416. })
  417. },
  418. getFieldDataSource (field) {
  419. let curRequest = this.dataSourceInterface.find(item => item.fields.includes(field))
  420. if (curRequest) {
  421. let curKey = field + '.' + curRequest.key
  422. let sourceValue = this.dataSourceValue.find(item => item.key === curKey)
  423. if (sourceValue) {
  424. return sourceValue.value
  425. }
  426. }
  427. return null
  428. },
  429. sendRequest(name, args = {}) {
  430. return new Promise((resolve, reject) => {
  431. let currentDataSource = this.formData.config.dataSource.find(item => item.name === name)
  432. if (!currentDataSource) {
  433. currentDataSource = this.formData.config.dataSource.find(item => item.key === name)
  434. }
  435. if (currentDataSource) {
  436. for (let key in args) {
  437. if (isExpression(args[key])) {
  438. args[key] = executeExpression(extractExpression(args[key]), {}, this)
  439. }
  440. }
  441. let options = {
  442. method: currentDataSource.method,
  443. url: currentDataSource.url,
  444. headers: {},
  445. params: {},
  446. data: {}
  447. }
  448. let optionsHeaders = {}
  449. currentDataSource.headers && Object.keys(currentDataSource.headers).forEach(key => {
  450. if (isExpression(currentDataSource.headers[key])) {
  451. optionsHeaders[key] = executeExpression(extractExpression(currentDataSource.headers[key]),args, this)
  452. } else {
  453. optionsHeaders[key] = currentDataSource.headers[key]
  454. }
  455. })
  456. Object.keys(optionsHeaders).length && (options['headers'] = optionsHeaders)
  457. let optionsParams = {}
  458. currentDataSource.params && Object.keys(currentDataSource.params).forEach(key => {
  459. if (isExpression(currentDataSource.params[key])) {
  460. optionsParams[key] = executeExpression(extractExpression(currentDataSource.params[key]),args, this)
  461. } else {
  462. optionsParams[key] = currentDataSource.params[key]
  463. }
  464. })
  465. if (options.method === 'POST' || options.method === 'PUT') {
  466. options['data'] = optionsParams
  467. } else {
  468. options['params'] = optionsParams
  469. }
  470. //请求发送前处理函数
  471. if (currentDataSource.requestFunc) {
  472. const requestDynamicFunc = Function('config, args', currentDataSource.requestFunc).bind(this)
  473. options = requestDynamicFunc(options, args)
  474. }
  475. axios(options).then(res => {
  476. let data = res
  477. if (currentDataSource.responseFunc) {
  478. const dynamicFunc = Function('res, args', currentDataSource.responseFunc).bind(this)
  479. data = dynamicFunc(res.data, args)
  480. resolve(data)
  481. } else {
  482. resolve(res.data)
  483. }
  484. }).catch((error) => {
  485. //请求错误处理函数
  486. if (currentDataSource.errorFunc) {
  487. const errorDynamicFunc = Function('error', currentDataSource.errorFunc).bind(this)
  488. errorDynamicFunc(error)
  489. }
  490. reject(error)
  491. })
  492. }
  493. })
  494. },
  495. generateSubformModel (subName, genList, dataBindFields) {
  496. for (let i = 0; i < genList.length; i++) {
  497. if (genList[i].type === 'grid') {
  498. genList[i].columns.forEach(item => {
  499. this.generateSubformModel(subName, item.list, dataBindFields)
  500. })
  501. } else if (genList[i].type === 'tabs') {
  502. genList[i].tabs.forEach(item => {
  503. this.generateSubformModel(subName, item.list, dataBindFields)
  504. })
  505. } else if (genList[i].type === 'collapse') {
  506. genList[i].tabs.forEach(item => {
  507. this.generateSubformModel(subName, item.list, dataBindFields)
  508. })
  509. } else if (genList[i].type === 'report') {
  510. genList[i].rows.forEach(row => {
  511. row.columns.forEach(column => {
  512. this.generateSubformModel(subName, column.list, dataBindFields)
  513. })
  514. })
  515. } else if (genList[i].type === 'inline') {
  516. this.generateSubformModel(subName, genList[i].list, dataBindFields)
  517. } else if (genList[i].type === 'card') {
  518. this.generateSubformModel(subName, genList[i].list, dataBindFields)
  519. } else {
  520. if (genList[i].type === 'blank') {
  521. this.blanks.push({
  522. name: genList[i].model
  523. })
  524. }
  525. if (genList[i].options.dataBind) {
  526. if (['subform', 'group', 'table'].includes(genList[i].type)) {
  527. dataBindFields[genList[i].model] = {}
  528. } else {
  529. dataBindFields[genList[i].model] = true
  530. }
  531. }
  532. genList[i].tableColumns && genList[i].tableColumns.length && genList[i].tableColumns.forEach(item => {
  533. if (item.type === 'blank') {
  534. this.blanks.push({
  535. name: item.model
  536. })
  537. }
  538. if (item.options.dataBind && genList[i].options.dataBind) {
  539. dataBindFields[genList[i].model][item.model] = true
  540. }
  541. // 处理 rules
  542. this._generateRules(`${subName}.${genList[i].model}.${item.model}`, item.rules)
  543. // 处理子表单中的DataSource
  544. this._generateDataSource(item, `${subName}.${genList[i].model}.${item.model}`)
  545. })
  546. if (genList[i].type == 'subform') {
  547. this.generateSubformModel(`${subName}.${genList[i].model}`, genList[i].list, dataBindFields[genList[i].model] || {})
  548. }
  549. if (genList[i].type == 'group') {
  550. this.generateGroupModel(`${subName}.${genList[i].model}`, genList[i].list, dataBindFields[genList[i].model] || {})
  551. }
  552. this._generateRules(`${subName}.${genList[i].model}`, genList[i].rules)
  553. // 处理子表单中的DataSource
  554. this._generateDataSource(genList[i], `${subName}.${genList[i].model}`)
  555. }
  556. }
  557. },
  558. generateDialogModel (dialogName, genList, dataBindFields) {
  559. for (let i = 0; i < genList.length; i++) {
  560. if (genList[i].type === 'grid') {
  561. genList[i].columns.forEach(item => {
  562. this.generateDialogModel(dialogName, item.list, dataBindFields)
  563. })
  564. } else if (genList[i].type === 'tabs') {
  565. genList[i].tabs.forEach(item => {
  566. this.generateDialogModel(dialogName, item.list, dataBindFields)
  567. })
  568. } else if (genList[i].type === 'collapse') {
  569. genList[i].tabs.forEach(item => {
  570. this.generateDialogModel(dialogName, item.list, dataBindFields)
  571. })
  572. } else if (genList[i].type === 'report') {
  573. genList[i].rows.forEach(row => {
  574. row.columns.forEach(column => {
  575. this.generateDialogModel(dialogName, column.list, dataBindFields)
  576. })
  577. })
  578. } else if (genList[i].type === 'inline') {
  579. this.generateDialogModel(dialogName, genList[i].list, dataBindFields)
  580. } else if (genList[i].type === 'card') {
  581. this.generateDialogModel(dialogName, genList[i].list, dataBindFields)
  582. } else {
  583. if (genList[i].type === 'blank') {
  584. this.blanks.push({
  585. name: genList[i].model
  586. })
  587. }
  588. if (genList[i].options.dataBind) {
  589. if (['subform', 'group', 'table'].includes(genList[i].type)) {
  590. dataBindFields[genList[i].model] = {}
  591. } else {
  592. dataBindFields[genList[i].model] = true
  593. }
  594. }
  595. if (genList[i].type == 'subform') {
  596. this.generateSubformModel(`${dialogName}.${genList[i].model}`, genList[i].list, dataBindFields[genList[i].model] || {})
  597. }
  598. if (genList[i].type == 'group') {
  599. this.generateGroupModel(`${dialogName}.${genList[i].model}`, genList[i].list, dataBindFields[genList[i].model] || {})
  600. }
  601. genList[i].tableColumns && genList[i].tableColumns.length && genList[i].tableColumns.forEach(item => {
  602. if (item.type === 'blank') {
  603. this.blanks.push({
  604. name: item.model
  605. })
  606. }
  607. if (item.options.dataBind && genList[i].options.dataBind) {
  608. dataBindFields[genList[i].model][item.model] = true
  609. }
  610. // 处理 rules
  611. this._generateRules(`${dialogName}.${genList[i].model}.${item.model}`, item.rules)
  612. // 处理子表单中的DataSource
  613. this._generateDataSource(item, `${dialogName}.${genList[i].model}.${item.model}`)
  614. })
  615. this._generateRules(`${dialogName}.${genList[i].model}`, genList[i].rules)
  616. // 处理弹框中的DataSource
  617. this._generateDataSource(genList[i], `${dialogName}.${genList[i].model}`)
  618. }
  619. }
  620. },
  621. generateGroupModel (groupName, genList, dataBindFields) {
  622. for (let i = 0; i < genList.length; i++) {
  623. if (genList[i].type === 'grid') {
  624. genList[i].columns.forEach(item => {
  625. this.generateGroupModel(groupName, item.list, dataBindFields)
  626. })
  627. } else if (genList[i].type === 'tabs') {
  628. genList[i].tabs.forEach(item => {
  629. this.generateGroupModel(groupName, item.list, dataBindFields)
  630. })
  631. } else if (genList[i].type === 'collapse') {
  632. genList[i].tabs.forEach(item => {
  633. this.generateGroupModel(groupName, item.list, dataBindFields)
  634. })
  635. } else if (genList[i].type === 'report') {
  636. genList[i].rows.forEach(row => {
  637. row.columns.forEach(column => {
  638. this.generateGroupModel(groupName, column.list, dataBindFields)
  639. })
  640. })
  641. } else if (genList[i].type === 'inline') {
  642. this.generateGroupModel(groupName, genList[i].list, dataBindFields)
  643. } else if (genList[i].type === 'card') {
  644. this.generateGroupModel(groupName, genList[i].list, dataBindFields)
  645. } else {
  646. if (genList[i].type === 'blank') {
  647. this.blanks.push({
  648. name: genList[i].model
  649. })
  650. }
  651. if (genList[i].options.dataBind) {
  652. if (['subform', 'group', 'table'].includes(genList[i].type)) {
  653. dataBindFields[genList[i].model] = {}
  654. } else {
  655. dataBindFields[genList[i].model] = true
  656. }
  657. }
  658. if (genList[i].type == 'subform') {
  659. this.generateSubformModel(`${groupName}.${genList[i].model}`, genList[i].list, dataBindFields[genList[i].model] || {})
  660. }
  661. if (genList[i].type == 'group') {
  662. this.generateGroupModel(`${groupName}.${genList[i].model}`, genList[i].list, dataBindFields[genList[i].model] || {})
  663. }
  664. genList[i].tableColumns && genList[i].tableColumns.length && genList[i].tableColumns.forEach(item => {
  665. if (item.type === 'blank') {
  666. this.blanks.push({
  667. name: item.model
  668. })
  669. }
  670. if (item.options.dataBind && genList[i].options.dataBind) {
  671. dataBindFields[genList[i].model][item.model] = true
  672. }
  673. // 处理 rules
  674. this._generateRules(`${groupName}.${genList[i].model}.${item.model}`, item.rules)
  675. // 处理子表单中的DataSource
  676. this._generateDataSource(item, `${groupName}.${genList[i].model}.${item.model}`)
  677. })
  678. this._generateRules(`${groupName}.${genList[i].model}`, genList[i].rules)
  679. // 处理弹框中的DataSource
  680. this._generateDataSource(genList[i], `${groupName}.${genList[i].model}`)
  681. }
  682. }
  683. },
  684. generateModel (genList) {
  685. for (let i = 0; i < genList.length; i++) {
  686. if (genList[i].type === 'grid') {
  687. this.displayFields[genList[i].model] = !genList[i].options.hidden
  688. genList[i].columns.forEach(item => {
  689. this.generateModel(item.list)
  690. })
  691. } else if (genList[i].type === 'tabs') {
  692. genList[i].tabs.forEach(item => {
  693. this.generateModel(item.list)
  694. })
  695. this.displayFields[genList[i].model] = !genList[i].options.hidden
  696. } else if (genList[i].type === 'collapse') {
  697. genList[i].tabs.forEach(item => {
  698. this.generateModel(item.list)
  699. })
  700. this.displayFields[genList[i].model] = !genList[i].options.hidden
  701. } else if (genList[i].type === 'report') {
  702. genList[i].rows.forEach(row => {
  703. row.columns.forEach(column => {
  704. this.generateModel(column.list)
  705. })
  706. })
  707. this.displayFields[genList[i].model] = !genList[i].options.hidden
  708. } else if (genList[i].type === 'inline') {
  709. this.generateModel(genList[i].list)
  710. this.displayFields[genList[i].model] = !genList[i].options.hidden
  711. } else if (genList[i].type === 'card') {
  712. this.generateModel(genList[i].list)
  713. this.displayFields[genList[i].model] = !genList[i].options.hidden
  714. } else {
  715. if (Object.keys(this.formValue).indexOf(genList[i].model) >= 0) {
  716. this.models[genList[i].model] = this.formValue[genList[i].model]
  717. this.displayFields[genList[i].model] = !genList[i].options.hidden
  718. if (genList[i].type === 'blank') {
  719. this.blanks.push({
  720. name: genList[i].model
  721. })
  722. }
  723. } else {
  724. if (genList[i].type === 'blank') {
  725. // bound the default value
  726. this.models[genList[i].model] = genList[i].options.defaultType === 'String' ? '' : (genList[i].options.defaultType === 'Object' ? {} : [])
  727. this.displayFields[genList[i].model] = !genList[i].options.hidden
  728. this.blanks.push({
  729. name: genList[i].model
  730. })
  731. } else {
  732. this.models[genList[i].model] = _.cloneDeep(genList[i].options.defaultValue)
  733. this.displayFields[genList[i].model] = !genList[i].options.hidden
  734. }
  735. }
  736. if ((Object.keys(genList[i].options).indexOf('dataBind') < 0 || genList[i].options.dataBind) && genList[i].key && genList[i].model) {
  737. if (['subform', 'table', 'dialog', 'group'].includes(genList[i].type)) {
  738. this.dataBindFields[genList[i].model] = {}
  739. } else {
  740. this.dataBindFields[genList[i].model] = true
  741. }
  742. }
  743. if (genList[i].type == 'subform') {
  744. this.generateSubformModel(genList[i].model, genList[i].list, this.dataBindFields[genList[i].model] || {})
  745. }
  746. if (genList[i].type == 'dialog') {
  747. this.generateDialogModel(genList[i].model, genList[i].list, this.dataBindFields[genList[i].model] || {})
  748. }
  749. if (genList[i].type == 'group') {
  750. this.generateGroupModel(genList[i].model, genList[i].list, this.dataBindFields[genList[i].model] || {})
  751. }
  752. genList[i].tableColumns && genList[i].tableColumns.length && genList[i].tableColumns.forEach(item => {
  753. if (item.type === 'blank') {
  754. this.blanks.push({
  755. name: item.model
  756. })
  757. }
  758. if (item.options.dataBind && genList[i].options.dataBind) {
  759. this.dataBindFields[genList[i].model][item.model] = true
  760. }
  761. // 处理 rules
  762. this._generateRules(`${genList[i].model}.${item.model}`, item.rules)
  763. // 处理子表单中的DataSource
  764. this._generateDataSource(item, `${genList[i].model}.${item.model}`)
  765. })
  766. this._generateRules(genList[i].model, genList[i].rules)
  767. // 处理DataSource
  768. this._generateDataSource(genList[i], genList[i].model)
  769. }
  770. }
  771. },
  772. _generateDataSource (element, model) {
  773. if (element.options.remote && element.options.remoteType === 'datasource' && element.options.remoteDataSource) {
  774. this._setDataSourceInterface(model, element.options.remoteArgs, element.options.remoteDataSource)
  775. }
  776. if (element.options.remote && element.options.remoteType === 'fx' && element.options.remoteFx) {
  777. this.dynamicOptionFxInterface[model] = element.options.remoteFx
  778. }
  779. if ((element.type == 'imgupload' || element.type == 'fileupload')
  780. && element.options.tokenType == 'datasource' && element.options.tokenDataSource
  781. ) {
  782. this._setDataSourceInterface(model, element.options.remoteArgs, element.options.tokenDataSource)
  783. }
  784. if (element.options.isDynamicValue && element.options.dynamicValueType === 'datasource' && element.options.dynamicValueDataSource) {
  785. this._setDynamicValueInterface(model, element.options.dynamicValueArgs, element.options.dynamicValueDataSource, element.options.dynamicValueStruct)
  786. }
  787. if (element.options.isDynamicValue && element.options.dynamicValueType === 'fx') {
  788. this.dynamicValueFxInterface[model] = element.options.dynamicValueFx
  789. }
  790. },
  791. _generateRules (model, rules) {
  792. if (this.rules[model]) {
  793. this.rules[model] = [
  794. ...this.rules[model],
  795. ...(rules ? rules.map(im => {
  796. if (im.pattern) {
  797. return {...im, pattern: eval(im.pattern)}
  798. } else if (im.func) {
  799. const validatorFunc = Function('rule', 'value', 'callback', im.func).bind(this)
  800. return {...im, validator: validatorFunc}
  801. } else {
  802. return {...im}
  803. }
  804. }) : [])
  805. ]
  806. } else {
  807. this.rules[model] = [
  808. ...(rules ? rules.map(im => {
  809. if (im.pattern) {
  810. return {...im, pattern: eval(im.pattern)}
  811. } else if (im.func) {
  812. const validatorFunc = Function('rule', 'value', 'callback', im.func).bind(this)
  813. return {...im, validator: validatorFunc}
  814. } else {
  815. return {...im}
  816. }
  817. }) : [])
  818. ]
  819. }
  820. },
  821. _setDataSourceInterface (field, args, key) {
  822. let argsObj
  823. if (typeof args == 'string') {
  824. argsObj = (new Function('"use strict";return (' + (args || '{}') + ')').bind(this))()
  825. } else {
  826. argsObj = {...args}
  827. }
  828. for (let key in argsObj) {
  829. if (isExpression(argsObj[key])) {
  830. argsObj[key] = executeExpression(extractExpression(argsObj[key]), {}, this)
  831. }
  832. }
  833. let findCurInterfaceIndex = this.dataSourceInterface.findIndex(item => item.key == key && _.isEqual(item.args, argsObj))
  834. if (findCurInterfaceIndex >= 0) {
  835. this.dataSourceInterface[findCurInterfaceIndex].fields.push(field)
  836. } else {
  837. this.dataSourceInterface.push({
  838. key: key,
  839. args: argsObj,
  840. fields: [field],
  841. dynamicValueFields: []
  842. })
  843. }
  844. },
  845. _setDynamicValueInterface (field, args, key, struct) {
  846. let findCurInterfaceIndex = this.dataSourceInterface.findIndex(item => item.key == key && _.isEqual(item.args, args))
  847. if (findCurInterfaceIndex >= 0) {
  848. this.dataSourceInterface[findCurInterfaceIndex].dynamicValueFields.push({field, struct})
  849. } else {
  850. this.dataSourceInterface.push({
  851. key,
  852. args,
  853. dynamicValueFields: [{field, struct}],
  854. fields: []
  855. })
  856. }
  857. },
  858. _setSubDisabled (genList, fields, disabled) {
  859. for (let i = 0; i < genList.length; i++) {
  860. if (genList[i].type === 'grid') {
  861. genList[i].columns.forEach(item => {
  862. this._setSubDisabled(item.list, fields, disabled)
  863. })
  864. } else if (genList[i].type === 'tabs') {
  865. genList[i].tabs.forEach(item => {
  866. this._setSubDisabled(item.list, fields, disabled)
  867. })
  868. } else if (genList[i].type === 'collapse') {
  869. genList[i].tabs.forEach(item => {
  870. this._setSubDisabled(item.list, fields, disabled)
  871. })
  872. } else if (genList[i].type === 'report') {
  873. genList[i].rows.forEach(row => {
  874. row.columns.forEach(column => {
  875. this._setSubDisabled(column.list, fields, disabled)
  876. })
  877. })
  878. } else if (genList[i].type === 'inline') {
  879. this._setSubDisabled(genList[i].list, fields, disabled)
  880. } else if (genList[i].type === 'card') {
  881. this._setSubDisabled(genList[i].list, fields, disabled)
  882. } else {
  883. if (fields.length === 1) {
  884. if (genList[i].model === fields[0]) {
  885. genList[i].options.disabled = disabled
  886. }
  887. } else {
  888. if (genList[i].model !== fields[0]) continue
  889. let newFields = [...fields]
  890. newFields.splice(0, 1)
  891. if (genList[i].type === 'table') {
  892. this._setSubDisabled(genList[i].tableColumns, newFields, disabled)
  893. }
  894. if (genList[i].type === 'subform') {
  895. this._setSubDisabled(genList[i].list, newFields, disabled)
  896. }
  897. if (genList[i].type === 'group') {
  898. this._setSubDisabled(genList[i].list, newFields, disabled)
  899. }
  900. if (genList[i].type === 'dialog') {
  901. this._setSubDisabled(genList[i].list, newFields, disabled)
  902. }
  903. }
  904. }
  905. }
  906. },
  907. _setSubOptions (genList, fields, opts) {
  908. for (let i = 0; i < genList.length; i++) {
  909. if (fields.length === 1) {
  910. if (genList[i].model === fields[0]) {
  911. Object.keys(opts).forEach(key => {
  912. genList[i].options[key] = opts[key]
  913. })
  914. } else {
  915. if (genList[i].type === 'grid') {
  916. genList[i].columns.forEach(item => {
  917. this._setSubOptions(item.list, fields, opts)
  918. })
  919. } else if (genList[i].type === 'tabs') {
  920. genList[i].tabs.forEach(item => {
  921. this._setSubOptions(item.list, fields, opts)
  922. })
  923. } else if (genList[i].type === 'collapse') {
  924. genList[i].tabs.forEach(item => {
  925. this._setSubOptions(item.list, fields, opts)
  926. })
  927. } else if (genList[i].type === 'report') {
  928. genList[i].rows.forEach(row => {
  929. row.columns.forEach(column => {
  930. this._setSubOptions(column.list, fields, opts)
  931. })
  932. })
  933. } else if (genList[i].type === 'inline') {
  934. this._setSubOptions(genList[i].list, fields, opts)
  935. } else if (genList[i].type === 'card') {
  936. this._setSubOptions(genList[i].list, fields, opts)
  937. }
  938. }
  939. } else {
  940. if (genList[i].type === 'grid') {
  941. genList[i].columns.forEach(item => {
  942. this._setSubOptions(item.list, fields, opts)
  943. })
  944. } else if (genList[i].type === 'tabs') {
  945. genList[i].tabs.forEach(item => {
  946. this._setSubOptions(item.list, fields, opts)
  947. })
  948. } else if (genList[i].type === 'collapse') {
  949. genList[i].tabs.forEach(item => {
  950. this._setSubOptions(item.list, fields, opts)
  951. })
  952. } else if (genList[i].type === 'report') {
  953. genList[i].rows.forEach(row => {
  954. row.columns.forEach(column => {
  955. this._setSubOptions(column.list, fields, opts)
  956. })
  957. })
  958. } else if (genList[i].type === 'inline') {
  959. this._setSubOptions(genList[i].list, fields, opts)
  960. } else if (genList[i].type === 'card') {
  961. this._setSubOptions(genList[i].list, fields, opts)
  962. } else {
  963. if (genList[i].model !== fields[0]) continue
  964. let newFields = [...fields]
  965. newFields.splice(0, 1)
  966. if (genList[i].type === 'table') {
  967. this._setSubOptions(genList[i].tableColumns, newFields, opts)
  968. }
  969. if (genList[i].type === 'subform') {
  970. this._setSubOptions(genList[i].list, newFields, opts)
  971. }
  972. if (genList[i].type === 'group') {
  973. this._setSubOptions(genList[i].list, newFields, opts)
  974. }
  975. if (genList[i].type === 'dialog') {
  976. this._setSubOptions(genList[i].list, newFields, opts)
  977. }
  978. }
  979. }
  980. }
  981. },
  982. _getSubOptions(genList, fields) {
  983. for (let i = 0; i < genList.length; i++) {
  984. if (fields.length === 1) {
  985. if (genList[i].model === fields[0]) {
  986. return genList[i].options
  987. } else {
  988. if (genList[i].type === 'grid') {
  989. for (const item of genList[i].columns) {
  990. const options = this._getSubOptions(item.list, fields)
  991. if (options) return options
  992. }
  993. } else if (genList[i].type === 'tabs') {
  994. for (const item of genList[i].tabs) {
  995. const options = this._getSubOptions(item.list, fields)
  996. if (options) return options
  997. }
  998. } else if (genList[i].type === 'collapse') {
  999. for (const item of genList[i].tabs) {
  1000. const options = this._getSubOptions(item.list, fields)
  1001. if (options) return options
  1002. }
  1003. } else if (genList[i].type === 'report') {
  1004. for (const row of genList[i].rows) {
  1005. for (const column of row.columns) {
  1006. const options = this._getSubOptions(column.list, fields)
  1007. if (options) return options
  1008. }
  1009. }
  1010. } else if (genList[i].type === 'inline') {
  1011. const options = this._getSubOptions(genList[i].list, fields)
  1012. if (options) return options
  1013. } else if (genList[i].type === 'card') {
  1014. const options = this._getSubOptions(genList[i].list, fields)
  1015. if (options) return options
  1016. }
  1017. }
  1018. } else {
  1019. if (genList[i].type === 'grid') {
  1020. for (const item of genList[i].columns) {
  1021. const options = this._getSubOptions(item.list, fields)
  1022. if (options) return options
  1023. }
  1024. } else if (genList[i].type === 'tabs') {
  1025. for (const item of genList[i].tabs) {
  1026. const options = this._getSubOptions(item.list, fields)
  1027. if (options) return options
  1028. }
  1029. } else if (genList[i].type === 'collapse') {
  1030. for (const item of genList[i].tabs) {
  1031. const options = this._getSubOptions(item.list, fields)
  1032. if (options) return options
  1033. }
  1034. } else if (genList[i].type === 'report') {
  1035. for (const row of genList[i].rows) {
  1036. for (const column of row.columns) {
  1037. const options = this._getSubOptions(column.list, fields)
  1038. if (options) return options
  1039. }
  1040. }
  1041. } else if (genList[i].type === 'inline') {
  1042. const options = this._getSubOptions(genList[i].list, fields)
  1043. if (options) return options
  1044. } else if (genList[i].type === 'card') {
  1045. const options = this._getSubOptions(genList[i].list, fields)
  1046. if (options) return options
  1047. } else {
  1048. if (genList[i].model !== fields[0]) continue
  1049. let newFields = [...fields]
  1050. newFields.splice(0, 1)
  1051. if (genList[i].type === 'table') {
  1052. const options = this._getSubOptions(genList[i].tableColumns, newFields)
  1053. if(options) return options
  1054. }
  1055. if (genList[i].type === 'subform') {
  1056. const options = this._getSubOptions(genList[i].list, newFields)
  1057. if (options) return options
  1058. }
  1059. if (genList[i].type === 'group') {
  1060. const options = this._getSubOptions(genList[i].list, newFields)
  1061. if (options) return options
  1062. }
  1063. if (genList[i].type === 'dialog') {
  1064. const options = this._getSubOptions(genList[i].list, newFields)
  1065. if (options) return options
  1066. }
  1067. }
  1068. }
  1069. }
  1070. return null
  1071. },
  1072. validate (fields) {
  1073. if (typeof fields === 'string') {
  1074. fields = [fields]
  1075. }
  1076. return new Promise((resolve, reject) => {
  1077. if (fields && fields.length) {
  1078. this.$refs[this.formRef].validateField(fields, (valid, error) => {
  1079. if (valid) {
  1080. resolve()
  1081. } else {
  1082. reject(error)
  1083. }
  1084. })
  1085. } else {
  1086. this.$refs[this.formRef].validate((valid, error) => {
  1087. if (valid) {
  1088. resolve()
  1089. } else {
  1090. reject(error)
  1091. }
  1092. })
  1093. }
  1094. })
  1095. },
  1096. getData (isValidate = true) {
  1097. return new Promise((resolve, reject) => {
  1098. if (isValidate) {
  1099. this.$refs[this.formRef].validate(valid => {
  1100. if (valid) {
  1101. resolve(getBindModels(this.models, this.dataBindFields))
  1102. } else {
  1103. reject(new Error(this.$t('fm.message.validError')).message)
  1104. }
  1105. })
  1106. } else {
  1107. resolve(getBindModels(this.models, this.dataBindFields))
  1108. }
  1109. })
  1110. },
  1111. reset () {
  1112. return new Promise((resolve) => {
  1113. this.setData(_.cloneDeep(this.resetModels)).then(() => {
  1114. setTimeout(() => {
  1115. this.$refs[this.formRef].clearValidate()
  1116. resolve()
  1117. })
  1118. })
  1119. })
  1120. },
  1121. onInputChange (value, field) {
  1122. this.$emit('on-change', field, value, this.models)
  1123. this.$emit(`on-${field}-change`, value)
  1124. },
  1125. display (fields) {
  1126. if (typeof fields === 'string') {
  1127. fields = [fields]
  1128. }
  1129. fields.forEach(field => {
  1130. this.dynamicHideFields[field] = false
  1131. })
  1132. },
  1133. hide (fields) {
  1134. if (typeof fields === 'string') {
  1135. fields = [fields]
  1136. }
  1137. fields.forEach(field => {
  1138. this.dynamicHideFields[field] = true
  1139. })
  1140. },
  1141. disabled (fields, disabled) {
  1142. if (typeof fields === 'string') {
  1143. fields = [fields]
  1144. }
  1145. fields.forEach(item => {
  1146. let currentComponent = this.instanceObject[item]
  1147. if (currentComponent) {
  1148. if (Array.isArray(currentComponent)) {
  1149. currentComponent.forEach(cc => {
  1150. cc.$parent?.disabledElement(disabled)
  1151. })
  1152. } else {
  1153. currentComponent.$parent?.disabledElement(disabled)
  1154. }
  1155. } else {
  1156. this._setSubDisabled(this.formData.list, item.split('.'), disabled)
  1157. }
  1158. })
  1159. },
  1160. addClassName (fields, className) {
  1161. if (typeof fields === 'string') {
  1162. fields = [fields]
  1163. }
  1164. fields.forEach(item => {
  1165. updateClassName(this.formData.list, item.split('.'), className, 'add')
  1166. })
  1167. },
  1168. removeClassName (fields, className) {
  1169. if (typeof fields === 'string') {
  1170. fields = [fields]
  1171. }
  1172. fields.forEach(item => {
  1173. updateClassName(this.formData.list, item.split('.'), className, 'remove')
  1174. })
  1175. },
  1176. async refresh () {
  1177. await this._initForm()
  1178. },
  1179. setData (value) {
  1180. const _setNestedValue = (currentObj, keys, value) => {
  1181. const key = keys.shift()
  1182. if (keys.length === 0) {
  1183. currentObj[key] = value
  1184. } else {
  1185. if (key in currentObj) {
  1186. if (Array.isArray(currentObj[key])) {
  1187. if (Number.isNaN(Number(keys[0]))) {
  1188. currentObj[key].forEach(cur => {
  1189. _setNestedValue(cur, [...keys], value)
  1190. })
  1191. } else {
  1192. const indexKey = keys.shift()
  1193. currentObj[key].length > Number(indexKey) && _setNestedValue(currentObj[key][Number(indexKey)], [...keys], value)
  1194. }
  1195. } else {
  1196. _setNestedValue(currentObj[key], [...keys], value)
  1197. }
  1198. }
  1199. }
  1200. }
  1201. return new Promise((resolve) => {
  1202. this.$nextTick(() => {
  1203. Object.keys(value).forEach(item => {
  1204. _setNestedValue(this.models, item.split('.'), value[item])
  1205. })
  1206. resolve()
  1207. })
  1208. })
  1209. },
  1210. getComponent (name) {
  1211. if (name === undefined) {
  1212. return this.instanceObject
  1213. }
  1214. if (this.instanceObject[name]) {
  1215. return this.instanceObject[name]
  1216. } else {
  1217. consoleError('No component instance found with ' + name)
  1218. return null
  1219. }
  1220. },
  1221. getValues () {
  1222. return _.cloneDeep(this.models)
  1223. },
  1224. getValue (fieldName) {
  1225. const resModels = _.cloneDeep(this.models)
  1226. let fieldArray = fieldName.split('.')
  1227. const _getFieldRes = (res, array) => {
  1228. if (array.length === 1) {
  1229. return res[array[0]]
  1230. } else {
  1231. let newRes = res[array[0]]
  1232. let newFields = [...array]
  1233. newFields.splice(0, 1)
  1234. return _getFieldRes(newRes, newFields)
  1235. }
  1236. }
  1237. return _getFieldRes(resModels, fieldArray)
  1238. },
  1239. setRules (field, rules) {
  1240. this.rules[field] = [...rules]
  1241. this.$nextTick(() => {
  1242. if (field?.split('.').length > 1) {
  1243. const tableRowLength = this.getValue(field.split('.')[0])?.length
  1244. if (tableRowLength) {
  1245. for (let i= 0; i < tableRowLength; i++) {
  1246. this.$refs[this.formRef].validateField([`${field.split('.')[0]}.${i}.${field.split('.')[1]}`])
  1247. }
  1248. }
  1249. } else {
  1250. this.$refs[this.formRef].validateField([field])
  1251. }
  1252. })
  1253. },
  1254. setOptions (fields, options) {
  1255. if (typeof fields === 'string') {
  1256. fields = [fields]
  1257. }
  1258. fields.forEach(item => {
  1259. this._setSubOptions(this.formData.list, item.split('.'), options)
  1260. })
  1261. },
  1262. getOptions (field, key) {
  1263. const result = this._getSubOptions(this.formData.list, field.split('.'))
  1264. if (key) {
  1265. return result[key]
  1266. } else {
  1267. return result
  1268. }
  1269. },
  1270. generateComponentInstance (key, instance) {
  1271. if (this.instanceObject[key]) {
  1272. if (Array.isArray(this.instanceObject[key])) {
  1273. this.instanceObject[key] = [...this.instanceObject[key], instance]
  1274. } else {
  1275. this.instanceObject[key] = [this.instanceObject[key], instance]
  1276. }
  1277. } else {
  1278. this.instanceObject[key] = instance
  1279. }
  1280. },
  1281. deleteComponentInstance (key) {
  1282. if (this.instanceObject[key]) {
  1283. delete this.instanceObject[key]
  1284. }
  1285. },
  1286. generateFormItemContext (key, instance) {
  1287. if (this.formItemContexts[key]) {
  1288. if (Array.isArray(this.formItemContexts[key])) {
  1289. this.formItemContexts[key] = [...this.formItemContexts[key], instance]
  1290. } else {
  1291. this.formItemContexts[key] = [this.formItemContexts[key], instance]
  1292. }
  1293. } else {
  1294. this.formItemContexts[key] = instance
  1295. }
  1296. },
  1297. deleteFormItemContext (key) {
  1298. if (this.formItemContexts[key]) {
  1299. delete this.formItemContexts[key]
  1300. }
  1301. },
  1302. setOptionData (fields, data) {
  1303. if (typeof fields === 'string') {
  1304. fields = [fields]
  1305. }
  1306. fields.forEach(field => {
  1307. const curRef = this.instanceObject[field]
  1308. curRef?.$parent?.loadOptions(data)
  1309. })
  1310. },
  1311. getOptionData (field) {
  1312. const curRef = this.instanceObject[field]
  1313. return curRef?.$parent?.getOptions()
  1314. },
  1315. exportPDF () {
  1316. if (this.printRead) {
  1317. return exportPDF(document.querySelector('.fm-'+this.formStyleKey))
  1318. } else {
  1319. return Promise.reject()
  1320. }
  1321. },
  1322. print () {
  1323. if (this.printRead) {
  1324. return new Promise((resolve, reject) => {
  1325. exportPDF(document.querySelector('.fm-'+this.formStyleKey)).then(data => {
  1326. const pdfUrl = URL.createObjectURL(data)
  1327. const iframeclass = 'fmiframe-print-container'
  1328. Array.from(document.getElementsByClassName(iframeclass)).forEach(item => {
  1329. document.body.removeChild(item)
  1330. })
  1331. let iframe = document.createElement('iframe')
  1332. iframe.style.display = 'none'
  1333. iframe.src = pdfUrl
  1334. iframe.className = iframeclass
  1335. iframe.onload = function() {
  1336. setTimeout(() => {
  1337. iframe.contentWindow.print()
  1338. resolve(data)
  1339. }, 100)
  1340. }
  1341. document.body.appendChild(iframe)
  1342. })
  1343. })
  1344. } else {
  1345. return Promise.reject('Set the print-read attribute to true for printing.')
  1346. }
  1347. },
  1348. openDialog (dialogField) {
  1349. const dialogComponent = this.getComponent(dialogField)
  1350. if (dialogComponent) {
  1351. dialogComponent.open()
  1352. }
  1353. },
  1354. closeDialog (dialogField) {
  1355. const dialogComponent = this.getComponent(dialogField)
  1356. if (dialogComponent) {
  1357. dialogComponent.close()
  1358. }
  1359. },
  1360. getDynamicValue (key) {
  1361. return this.dynamicValueData.find(item => item.key == key)
  1362. }
  1363. }
  1364. }