ModuleMixin.js 2.1 KB

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