widgetTable.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import _ from 'lodash'
  2. import { CloneLayout } from '../util/layout-clone.js'
  3. import { EventBus } from '../util/event-bus.js'
  4. import { addClass, removeClass } from '../util'
  5. export const widgetTableMixin = {
  6. props: ['element', 'select', 'index', 'data', 'platform', 'formKey'],
  7. emits: ['select-change', 'update:select'],
  8. inject: ['setDragging', 'getDragging'],
  9. data () {
  10. return {
  11. selectWidget: this.select || {},
  12. columnsWidthStyle: '200px'
  13. }
  14. },
  15. mounted () {
  16. this.calcTableColumnsWidth()
  17. },
  18. methods: {
  19. handleMouseover (e) {
  20. !this.getDragging() && addClass(this.$refs['widgetTable'], 'is-hover')
  21. },
  22. handleMouseout (e) {
  23. removeClass(this.$refs['widgetTable'], 'is-hover')
  24. },
  25. handleSelectWidget (index) {
  26. this.$emit('update:select', this.data.list[index])
  27. },
  28. handleWidgetDelete (index) {
  29. if (this.data.list.length == 1) {
  30. this.$emit('select-change', -1)
  31. } else {
  32. if (this.data.list.length - 1 == index) {
  33. this.$emit('select-change', index - 1)
  34. } else {
  35. this.$emit('select-change', index)
  36. }
  37. }
  38. this.data.list.splice(index, 1)
  39. setTimeout(() => {
  40. EventBus.$emit('on-history-add-' + this.formKey)
  41. }, 20)
  42. },
  43. handleTableClone (index) {
  44. let cloneData = _.cloneDeep(this.data.list[index])
  45. this.data.list.splice(index + 1, 0, CloneLayout(cloneData))
  46. this.$nextTick(() => {
  47. this.$emit('update:select', this.data.list[index + 1])
  48. this.$nextTick(() => { EventBus.$emit('on-history-add-' + this.formKey) })
  49. })
  50. },
  51. handleWidgetTableUpdate (evt) {
  52. this.$nextTick(() => { EventBus.$emit('on-history-add-' + this.formKey) })
  53. },
  54. calcTableColumnsWidth () {
  55. this.columnsWidthStyle = 'calc(200px)'
  56. let widthArray = []
  57. for (let i = 0; i < this.element.tableColumns.length; i++) {
  58. if (!this.element.tableColumns[i].options.width) {
  59. widthArray.push('200px')
  60. } else {
  61. widthArray.push(this.element.tableColumns[i].options.width)
  62. }
  63. }
  64. widthArray.length && (this.columnsWidthStyle = `calc(200px + ${widthArray.join(' + ')})`)
  65. },
  66. handlePut (a, b, c) {
  67. if (c.className.split(' ').indexOf('widget-col') >=0 ||
  68. c.className.split(' ').indexOf('widget-table') >= 0 ||
  69. c.className.split(' ').indexOf('widget-tab') >= 0 ||
  70. c.className.split(' ').indexOf('widget-report') >= 0 ||
  71. c.className.split(' ').indexOf('widget-inline') >= 0 ||
  72. c.className.split(' ').indexOf('widget-subform') >= 0 ||
  73. c.className.split(' ').indexOf('widget-dialog') >= 0 ||
  74. c.className.split(' ').indexOf('widget-card') >= 0 ||
  75. c.className.split(' ').indexOf('widget-group') >= 0 ||
  76. c.className.split(' ').indexOf('no-put') >= 0 ||
  77. c.children[0].className.split(' ').indexOf('no-put') >= 0) {
  78. return false
  79. }
  80. return true
  81. },
  82. handleWidgetTableAdd ($event, table) {
  83. const newIndex = $event.newIndex
  84. const key = Math.random().toString(36).slice(-8)
  85. table.tableColumns[newIndex] = _.cloneDeep(table.tableColumns[newIndex])
  86. table.tableColumns[newIndex] = {
  87. ...table.tableColumns[newIndex],
  88. options: {
  89. ...table.tableColumns[newIndex].options,
  90. remoteFunc: table.tableColumns[newIndex].options.remoteFunc || 'func_'+key,
  91. remoteOption: table.tableColumns[newIndex].options.remoteOption || 'option_'+key,
  92. tableColumn: true,
  93. subform: this.subform ? true : false
  94. },
  95. novalid: {
  96. ...table.tableColumns[newIndex].novalid,
  97. },
  98. key: table.tableColumns[newIndex].key ? table.tableColumns[newIndex].key : key,
  99. model: table.tableColumns[newIndex].model ? table.tableColumns[newIndex].model : table.tableColumns[newIndex].type + '_' + key,
  100. rules: table.tableColumns[newIndex].rules ? [...table.tableColumns[newIndex].rules] : []
  101. }
  102. this.$nextTick(() => {
  103. this.selectWidget = table.tableColumns[newIndex]
  104. EventBus.$emit('on-history-add-' + this.formKey)
  105. })
  106. },
  107. handleSelectChange (index, item) {
  108. setTimeout(() => {
  109. index >=0 ? (
  110. this.$emit('update:select', this.element.tableColumns[index])
  111. ) : (
  112. this.$emit('update:select', this.data.list[this.index])
  113. )
  114. })
  115. }
  116. },
  117. watch: {
  118. select (val) {
  119. this.selectWidget = val
  120. },
  121. selectWidget (val) {
  122. this.$emit('update:select', val)
  123. },
  124. element: {
  125. deep: true,
  126. handler (val) {
  127. this.calcTableColumnsWidth()
  128. }
  129. }
  130. }
  131. }