summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notebook/cells/output/index.vue
blob: b59ddd0d57abdb6d83648ed969607cc1222a42e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<script>
import CodeOutput from '../code/index.vue';
import HtmlOutput from './html.vue';
import ImageOutput from './image.vue';

export default {
  props: {
    codeCssClass: {
      type: String,
      required: false,
      default: '',
    },
    count: {
      type: Number,
      required: false,
      default: 0,
    },
    outputs: {
      type: Array,
      required: true,
    },
  },
  methods: {
    outputType(output) {
      if (output.text) {
        return 'text/plain';
      } else if (output.data['image/png']) {
        return 'image/png';
      } else if (output.data['text/html']) {
        return 'text/html';
      } else if (output.data['image/svg+xml']) {
        return 'image/svg+xml';
      }

      return 'text/plain';
    },
    dataForType(output, type) {
      let data = output.data[type];

      if (typeof data === 'object') {
        data = data.join('');
      }

      return data;
    },
    getComponent(output) {
      if (output.text) {
        return CodeOutput;
      } else if (output.data['image/png']) {
        return ImageOutput;
      } else if (output.data['text/html']) {
        return HtmlOutput;
      } else if (output.data['image/svg+xml']) {
        return HtmlOutput;
      }

      return CodeOutput;
    },
    rawCode(output) {
      if (output.text) {
        return output.text.join('');
      }

      return this.dataForType(output, this.outputType(output));
    },
  },
};
</script>

<template>
  <div>
    <component
      :is="getComponent(output)"
      v-for="(output, index) in outputs"
      :key="index"
      type="output"
      :output-type="outputType(output)"
      :count="count"
      :index="index"
      :raw-code="rawCode(output)"
      :code-css-class="codeCssClass"
    />
  </div>
</template>