File size: 3,369 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<script>
import DataField from './DataField.vue'

export default {
  components: {
    DataField,
  },

  props: {
    fields: {
      type: [Array, Object],
      required: true,
    },

    forceCollapse: {
      type: String,
      default: null,
    },
  },

  emits: ['editState'],

  data() {
    return {
      limit: 30,
      fieldErrors: {},
    }
  },

  computed: {
    isFieldsArray() {
      return Array.isArray(this.fields)
    },

    displayedFields() {
      if (this.isFieldsArray) {
        return this.fields.slice(0, this.limit)
      }
      else {
        return Object.keys(this.fields)
          .slice(0, this.limit)
          .reduce((obj, key) => {
            obj[key] = this.fields[key]
            return obj
          }, {})
      }
    },

    fieldsCount() {
      if (this.isFieldsArray) {
        return this.fields.length
      }
      else {
        return Object.keys(this.fields).length
      }
    },
  },

  watch: {
    fields() {
      this.fieldErrors = {}
    },
  },

  errorCaptured(err, vm, info) {
    console.error(err, vm, info)
    this.fieldErrors[vm.field.key] = err.message
  },

  methods: {
    isStateField(field) {
      return field && field.type === 'state'
    },

    showMore() {
      this.limit += 20
    },
  },
}
</script>

<template>
  <div
    class="data-fields"
  >
    <template v-if="isFieldsArray">
      <template v-for="field in displayedFields">
        <div
          v-if="fieldErrors[field.key]"
          :key="field.key"
          class="flex items-center space-x-2 font-mono ml-3"
        >
          <div>
            <span class="text-purple-700 dark:text-purple-300">{{ field.key }}</span><span>:</span>
          </div>
          <pre
            class="bg-red-500 text-white p-2 rounded text-xs my-0.5"
          >{{ fieldErrors[field.key] }}</pre>
        </div>

        <DataField
          v-else
          :key="`field-${field.key}`"
          :field="field"
          :depth="0"
          :path="field.key"
          :editable="field.editable"
          :force-collapse="forceCollapse"
          :is-state-field="isStateField(field)"
          @edit-state="(path, payload) => $emit('editState', path, payload)"
        />
      </template>
    </template>
    <template v-else>
      <template v-for="(value, key) in displayedFields">
        <div
          v-if="fieldErrors[key]"
          :key="key"
          class="flex items-center space-x-2 font-mono ml-3"
        >
          <div>
            <span class="text-purple-700 dark:text-purple-300">{{ key }}</span><span>:</span>
          </div>
          <pre
            class="bg-red-500 text-white p-2 rounded text-xs my-0.5"
          >{{ fieldErrors[key] }}</pre>
        </div>

        <DataField
          v-else
          :key="`raw-${key}`"
          :field="{ value, key }"
          :depth="0"
          :path="key.toString()"
          :editable="false"
          @edit-state="(path, payload) => $emit('editState', path, payload)"
        />
      </template>
    </template>
    <VueButton
      v-if="fieldsCount > limit"
      v-tooltip="'Show more'"
      icon-left="more_horiz"
      class="icon-button flat more"
      @click="showMore()"
    />
  </div>
</template>

<style lang="stylus" scoped>
.more
  width 20px
  height @width
  :deep(.vue-ui-icon)
    width 16px
    height @width
</style>