widgetElementItem.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {executeExpression, isExpression, extractExpression} from '../util/expression'
  2. import {defineComponent} from 'vue'
  3. export const ErrorBoundary = defineComponent({
  4. template: `
  5. <div>
  6. <slot v-if="!error"></slot>
  7. <div v-else>发生错误: {{ error.message }}</div>
  8. </div>
  9. `,
  10. data() {
  11. return {
  12. error: null
  13. };
  14. },
  15. errorCaptured(err) {
  16. this.error = err; // 捕获错误
  17. return false; // 防止向上传递
  18. }
  19. })
  20. export const widgetElementItemMixin = {
  21. props: ['element', 'isTable'],
  22. data () {
  23. return {
  24. key: Math.random().toString(36).slice(-8),
  25. elementKey: Math.random().toString(36).slice(-8),
  26. }
  27. },
  28. computed: {
  29. elementDisabled () {
  30. if (isExpression(this.element.options.disabled)) {
  31. return true
  32. }
  33. return this.element.options.disabled
  34. },
  35. extendProps () {
  36. let obj = {}
  37. this.element.options.extendProps && Object.keys(this.element.options.extendProps).forEach(key => {
  38. let value = this.element.options.extendProps[key]
  39. if (!isExpression(value)) {
  40. obj[key] = value
  41. } else {
  42. obj[key] = executeExpression(extractExpression(value), {}, null)
  43. }
  44. })
  45. return obj
  46. },
  47. },
  48. created () {
  49. if (this.element.type == 'component') {
  50. this.$options.components[`component-${this.element.key}`] = {
  51. template: (`<ErrorBoundary>${this.element.options.template}</ErrorBoundary>`).replace(/formState(\.\w+)+/g, (match) => {
  52. return match.replace(/\./g, '?.');
  53. }),
  54. props: ['modelValue'],
  55. components: {
  56. ErrorBoundary
  57. },
  58. data: () => ({
  59. dataModel: this.modelValue,
  60. formState: {}
  61. }),
  62. errorCaptured(err) {
  63. return false
  64. }
  65. }
  66. }
  67. },
  68. beforeUnmount () {
  69. if (this.$options && this.$options.components && this.$options.components[`component-${this.element.key}`]) {
  70. this.$options.components[`component-${this.element.key}`] = null
  71. }
  72. },
  73. watch: {
  74. 'element.options.template': function (val) {
  75. this.$options.components[`component-${this.element.key}`] = {
  76. template: (`<ErrorBoundary>${val}</ErrorBoundary>`).replace(/formState(\.\w+)+/g, (match) => {
  77. return match.replace(/\./g, '?.');
  78. }),
  79. props: ['modelValue'],
  80. components: {
  81. ErrorBoundary
  82. },
  83. data: () => ({
  84. dataModel: this.modelValue,
  85. formState: {}
  86. }),
  87. errorCaptured(err) {
  88. return false
  89. }
  90. }
  91. this.key = Math.random().toString(36).slice(-8)
  92. },
  93. 'element.options.defaultValue': function (val) {
  94. this.elementKey = Math.random().toString(36).slice(-8)
  95. }
  96. }
  97. }