ModuleMixin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // 组件混入
  2. import Vue from 'vue';
  3. import ModuleBase from './ModuleBase.vue';
  4. import RichText from '@/components/RichText.vue';
  5. import { displayList, viewMethodList, isEnable } from '@/views/book/courseware/data/common';
  6. import { ContentSaveCoursewareComponentContent, ContentGetCoursewareComponentContent } from '@/api/book';
  7. const mixin = {
  8. data() {
  9. return {
  10. displayList,
  11. viewMethodList,
  12. isEnable,
  13. property: {
  14. isGetContent: false, // 是否已获取内容
  15. },
  16. borderColorObj: Vue.observable({ value: this.borderColor }), // 边框颜色
  17. };
  18. },
  19. props: {
  20. id: {
  21. type: String,
  22. required: true,
  23. },
  24. deleteComponent: {
  25. type: Function,
  26. required: true,
  27. },
  28. componentMove: {
  29. type: Function,
  30. required: true,
  31. },
  32. borderColor: {
  33. type: String,
  34. required: true,
  35. },
  36. },
  37. components: {
  38. ModuleBase,
  39. RichText,
  40. },
  41. provide() {
  42. return {
  43. showSetting: this.showSetting,
  44. id: this.id,
  45. deleteComponent: this.deleteComponent,
  46. handleComponentMove: this.handleComponentMove,
  47. property: this.property,
  48. borderColor: this.borderColorObj,
  49. };
  50. },
  51. watch: {
  52. borderColor(newVal) {
  53. this.borderColorObj.value = newVal;
  54. },
  55. },
  56. inject: ['courseware_id'],
  57. created() {
  58. ContentGetCoursewareComponentContent({ courseware_id: this.courseware_id, component_id: this.id }).then(
  59. ({ content }) => {
  60. if (content) {
  61. this.data = JSON.parse(content);
  62. this.property.isGetContent = true;
  63. }
  64. this.$watch(
  65. 'data',
  66. () => {
  67. this.$emit('changeData');
  68. },
  69. { deep: true },
  70. );
  71. },
  72. );
  73. // 初始化 mind_map.node_list[0].id
  74. if (!this.data?.mind_map?.node_list?.[0]?.id) {
  75. this.data.mind_map = this.data.mind_map ?? {};
  76. this.data.mind_map.node_list = this.data.mind_map.node_list ?? [{}];
  77. this.data.mind_map.node_list[0].id = this.id;
  78. }
  79. },
  80. methods: {
  81. /**
  82. * @description 显示设置
  83. */
  84. showSetting() {
  85. this.$emit('showSetting', this.data.property, this.data.type, this.id);
  86. },
  87. /**
  88. * @description 更新属性
  89. * @param {object} setting 属性
  90. * @param {string} type 属性类型
  91. */
  92. updateSetting(property) {
  93. this.data.property = property;
  94. },
  95. handleComponentMove(data) {
  96. this.componentMove({ ...data, id: this.id });
  97. },
  98. saveCoursewareComponentContent() {
  99. ContentSaveCoursewareComponentContent({
  100. courseware_id: this.courseware_id,
  101. component_id: this.id,
  102. component_type: this.data.type,
  103. content: JSON.stringify(this.data),
  104. });
  105. },
  106. },
  107. };
  108. export default mixin;