ModuleMixin.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // 组件混入
  2. import ModuleBase from './ModuleBase.vue';
  3. import RichText from '@/components/RichText.vue';
  4. import { snGenerationMethodList, viewMethodList } from '@/views/book/courseware/data/common';
  5. import { SaveCoursewareComponentContent, GetCoursewareComponentContent } from '@/api/book';
  6. const mixin = {
  7. data() {
  8. return {
  9. snGenerationMethodList,
  10. viewMethodList,
  11. property: {
  12. isGetContent: false, // 是否已获取内容
  13. },
  14. };
  15. },
  16. props: {
  17. id: {
  18. type: String,
  19. required: true,
  20. },
  21. deleteComponent: {
  22. type: Function,
  23. required: true,
  24. },
  25. componentMove: {
  26. type: Function,
  27. required: true,
  28. },
  29. },
  30. components: {
  31. ModuleBase,
  32. RichText,
  33. },
  34. provide() {
  35. return {
  36. showSetting: this.showSetting,
  37. id: this.id,
  38. deleteComponent: this.deleteComponent,
  39. handleComponentMove: this.handleComponentMove,
  40. property: this.property,
  41. };
  42. },
  43. inject: ['courseware_id'],
  44. created() {
  45. GetCoursewareComponentContent({ courseware_id: this.courseware_id, component_id: this.id }).then(({ content }) => {
  46. if (content) {
  47. this.data = JSON.parse(content);
  48. this.property.isGetContent = true;
  49. }
  50. this.$watch(
  51. 'data',
  52. () => {
  53. this.$emit('changeData');
  54. },
  55. { deep: true },
  56. );
  57. });
  58. },
  59. methods: {
  60. /**
  61. * @description 显示设置
  62. */
  63. showSetting() {
  64. this.$emit('showSetting', this.data.property, this.data.type, this.id);
  65. },
  66. /**
  67. * @description 更新属性
  68. * @param {object} setting 属性
  69. * @param {string} type 属性类型
  70. */
  71. updateSetting(property) {
  72. this.data.property = property;
  73. },
  74. handleComponentMove(data) {
  75. this.componentMove({ ...data, id: this.id });
  76. },
  77. saveCoursewareComponentContent() {
  78. SaveCoursewareComponentContent({
  79. courseware_id: this.courseware_id,
  80. component_id: this.id,
  81. component_type: this.data.type,
  82. content: JSON.stringify(this.data),
  83. });
  84. },
  85. },
  86. };
  87. export default mixin;