widgetForm.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { EventBus } from '../util/event-bus.js'
  2. import { generateKeyToTD, generateKeyToCol, generateKeyToTH } from '../util'
  3. import _ from 'lodash'
  4. import { ElMessage } from '../util/message.js'
  5. export const widgetFormMixin = {
  6. props: ['data', 'select', 'platform', 'formKey'],
  7. emits: ['update:select'],
  8. inject: ['changeConfigTab', 'setDragging'],
  9. data () {
  10. return {
  11. selectWidget: this.select || {}
  12. }
  13. },
  14. mounted () {
  15. document.body.ondrop = function (event) {
  16. let isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
  17. if (isFirefox) {
  18. event.preventDefault()
  19. event.stopPropagation()
  20. }
  21. }
  22. EventBus.$on('on-field-add-' + this.formKey, item => {
  23. const key = Math.random().toString(36).slice(-8)
  24. let widgetItem = _.cloneDeep({
  25. ...item,
  26. options: {
  27. ...item.options,
  28. remoteFunc: 'func_' + key,
  29. remoteOption: 'option_' + key
  30. },
  31. key,
  32. model: item.type + '_' + key,
  33. rules: []
  34. })
  35. if (widgetItem.type == 'report') {
  36. widgetItem.rows = generateKeyToTD(widgetItem.rows)
  37. widgetItem.headerRow = generateKeyToTH(widgetItem.headerRow)
  38. }
  39. if (widgetItem.type == 'grid') {
  40. widgetItem.columns = generateKeyToCol(widgetItem.columns)
  41. }
  42. widgetItem.options.subform = this.selectWidget?.options?.subform
  43. widgetItem.options.tableColumn = this.selectWidget?.options?.tableColumn
  44. this._addWidget(this.data.list, widgetItem)
  45. })
  46. },
  47. beforeUnmount () {
  48. EventBus.$off('on-field-add-' + this.formKey)
  49. },
  50. methods: {
  51. _addWidget (list, widget, isTable = false) {
  52. if (isTable
  53. && (widget.type == 'subform'
  54. || widget.type == 'grid'
  55. || widget.type == 'table'
  56. || widget.type == 'tabs'
  57. || widget.type == 'collapse'
  58. || widget.type == 'divider'
  59. || widget.type == 'report'
  60. || widget.type == 'inline'
  61. || widget.type == 'dialog'
  62. || widget.type == 'card'
  63. )) {
  64. ElMessage({
  65. message: this.$t('fm.message.noPut'),
  66. type: 'warning'
  67. }, this)
  68. return 'table'
  69. }
  70. if (this.selectWidget && this.selectWidget.key) {
  71. const index = list.findIndex(item => item.key == this.selectWidget.key)
  72. if (index >= 0) {
  73. list.splice(index + 1, 0, widget)
  74. this.selectWidget = list[index + 1]
  75. setTimeout(() => {
  76. this.scrollTo()
  77. }, 200)
  78. this.$nextTick(() => { EventBus.$emit('on-history-add-' + this.formKey) })
  79. } else {
  80. for (let l = 0; l < list.length; l++) {
  81. let item = list[l]
  82. if (item.type === 'grid') {
  83. item.columns.forEach(column => {
  84. this._addWidget(column.list, widget)
  85. })
  86. }
  87. if (item.type === 'table') {
  88. if ('table' == this._addWidget(item.tableColumns, widget, true)) {
  89. return 'table'
  90. }
  91. }
  92. if (item.type === 'subform') {
  93. if ('table' == this._addWidget(item.list, widget, true)) {
  94. return 'table'
  95. }
  96. }
  97. if (item.type === 'tabs') {
  98. item.tabs.forEach(tab => {
  99. this._addWidget(tab.list, widget)
  100. })
  101. }
  102. if (item.type === 'collapse') {
  103. item.tabs.forEach(tab => {
  104. this._addWidget(tab.list, widget)
  105. })
  106. }
  107. if (item.type === 'report') {
  108. for (let i = 0; i < item.rows.length; i++) {
  109. for (let j = 0; j < item.rows[i].columns.length; j++) {
  110. widget.options.hideLabel = true
  111. if ('table' == this._addWidget(item.rows[i].columns[j].list, widget, false)){
  112. return 'table'
  113. }
  114. }
  115. }
  116. }
  117. if (item.type === 'inline') {
  118. if ('table' == this._addWidget(item.list, widget, true)) {
  119. return 'table'
  120. }
  121. }
  122. if (item.type === 'dialog') {
  123. if ('table' == this._addWidget(item.list, widget, true)) {
  124. return 'table'
  125. }
  126. }
  127. if (item.type === 'card') {
  128. if ('table' == this._addWidget(item.list, widget, true)) {
  129. return 'table'
  130. }
  131. }
  132. if (item.type === 'group') {
  133. if ('table' == this._addWidget(item.list, widget, true)) {
  134. return 'table'
  135. }
  136. }
  137. }
  138. }
  139. } else {
  140. list.push(widget)
  141. this.selectWidget = list[list.length - 1]
  142. setTimeout(() => {
  143. this.scrollTo()
  144. }, 200)
  145. this.$nextTick(() => { EventBus.$emit('on-history-add-' + this.formKey) })
  146. }
  147. },
  148. handleMoveStart () {
  149. this.setDragging(true)
  150. },
  151. handleMoveEnd () {
  152. this.setDragging(false)
  153. },
  154. handleWidgetUpdate (evt) {
  155. this.$nextTick(() => { EventBus.$emit('on-history-add-' + this.formKey) })
  156. },
  157. handleWidgetAdd (evt) {
  158. const newIndex = evt.newIndex
  159. const to = evt.to
  160. this.data.list[newIndex] = _.cloneDeep(this.data.list[newIndex])
  161. //为拖拽到容器的元素添加唯一 key
  162. const key = Math.random().toString(36).slice(-8)
  163. this.data.list[newIndex] = {
  164. ...this.data.list[newIndex],
  165. options: {
  166. ...this.data.list[newIndex].options,
  167. remoteFunc: this.data.list[newIndex].options.remoteFunc || 'func_' + key,
  168. remoteOption: this.data.list[newIndex].options.remoteOption || 'option_' + key,
  169. tableColumn: false,
  170. subform: false
  171. },
  172. key: this.data.list[newIndex].key ? this.data.list[newIndex].key : key,
  173. // 绑定键值
  174. model: this.data.list[newIndex].model ? this.data.list[newIndex].model : this.data.list[newIndex].type + '_' + key,
  175. rules: this.data.list[newIndex].rules ? [...this.data.list[newIndex].rules] : []
  176. }
  177. if (this.data.list[newIndex].type == 'report') {
  178. this.data.list[newIndex].rows = generateKeyToTD(this.data.list[newIndex].rows)
  179. this.data.list[newIndex].headerRow = generateKeyToTH(this.data.list[newIndex].headerRow)
  180. }
  181. if (this.data.list[newIndex].type == 'grid') {
  182. this.data.list[newIndex].columns = generateKeyToCol(this.data.list[newIndex].columns)
  183. }
  184. this.$nextTick(() => {
  185. this.selectWidget = this.data.list[newIndex]
  186. EventBus.$emit('on-history-add-' + this.formKey)
  187. })
  188. },
  189. handleWidgetDelete (index) {
  190. if (this.data.list.length - 1 === index) {
  191. if (index === 0) {
  192. this.selectWidget = {}
  193. } else {
  194. this.selectWidget = this.data.list[index - 1]
  195. }
  196. } else {
  197. this.selectWidget = this.data.list[index + 1]
  198. }
  199. this.$nextTick(() => {
  200. this.data.list.splice(index, 1)
  201. this.$nextTick(() => { EventBus.$emit('on-history-add-' + this.formKey) })
  202. })
  203. },
  204. handleSelectChange (index) {
  205. setTimeout(() => {
  206. index >=0 ? (this.selectWidget = this.data.list[index]) : (this.selectWidget = {})
  207. })
  208. },
  209. scrollTo () {
  210. let activeElement = document.querySelector('.widget-form-container .active')
  211. if (!activeElement) {
  212. return false
  213. }
  214. let activeTop = activeElement.getBoundingClientRect().top
  215. let activeHeight = activeElement.offsetHeight
  216. let containerHeight = document.querySelector('.widget-form-container').offsetHeight
  217. let containerTop = document.querySelector('.widget-form-container').getBoundingClientRect().top
  218. let scorllTop = document.querySelector('.widget-form-container .el-scrollbar__view').getBoundingClientRect().top
  219. let y = activeTop - scorllTop
  220. let top = scorllTop - containerTop
  221. if ( y + activeHeight > -top + containerHeight || y < -top ) {
  222. this.$refs.formScrollRef.scrollTo({top: y - 5, behavior: "smooth"})
  223. }
  224. }
  225. },
  226. watch: {
  227. select (val) {
  228. this.selectWidget = val
  229. if (Object.keys(val).length) {
  230. this.changeConfigTab('widget')
  231. } else {
  232. this.changeConfigTab('form')
  233. }
  234. },
  235. selectWidget (val) {
  236. this.$emit('update:select', val)
  237. }
  238. }
  239. }