refactor: object-view.value.vue to composition api
Some checks failed
ci/woodpecker/pr/lint-backend Pipeline failed
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/lint-client Pipeline failed
ci/woodpecker/pr/test Pipeline failed

This commit is contained in:
Norm 2022-07-28 13:52:27 -04:00
parent 9abbe94108
commit 2be26e810e
Signed by: norm
GPG key ID: 7123E30E441E80DE

View file

@ -6,13 +6,13 @@
<div v-else-if="typeof value === 'number'" class="number">{{ number(value) }}</div> <div v-else-if="typeof value === 'number'" class="number">{{ number(value) }}</div>
<div v-else-if="isArray(value) && isEmpty(value)" class="array empty">[]</div> <div v-else-if="isArray(value) && isEmpty(value)" class="array empty">[]</div>
<div v-else-if="isArray(value)" class="array"> <div v-else-if="isArray(value)" class="array">
<div v-for="i in value.length" class="element"> <div v-for="i in value.length" :key="i" class="element">
{{ i }}: <XValue :value="value[i - 1]" collapsed/> {{ i }}: <XValue :value="value[i - 1]" collapsed/>
</div> </div>
</div> </div>
<div v-else-if="isObject(value) && isEmpty(value)" class="object empty">{}</div> <div v-else-if="isObject(value) && isEmpty(value)" class="object empty">{}</div>
<div v-else-if="isObject(value)" class="object"> <div v-else-if="isObject(value)" class="object">
<div v-for="k in Object.keys(value)" class="kv"> <div v-for="k in Object.keys(value)" :key="k" class="kv">
<button class="toggle _button" :class="{ visible: collapsable(value[k]) }" @click="collapsed[k] = !collapsed[k]">{{ collapsed[k] ? '+' : '-' }}</button> <button class="toggle _button" :class="{ visible: collapsable(value[k]) }" @click="collapsed[k] = !collapsed[k]">{{ collapsed[k] ? '+' : '-' }}</button>
<div class="k">{{ k }}:</div> <div class="k">{{ k }}:</div>
<div v-if="collapsed[k]" class="v"> <div v-if="collapsed[k]" class="v">
@ -28,54 +28,38 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { computed, defineComponent, reactive, ref } from 'vue'; import { reactive, defineProps } from 'vue';
import number from '@/filters/number'; import number from '@/filters/number';
export default defineComponent({ const props = defineProps<{
name: 'XValue', value: any;
}>();
props: { const collapsed = reactive({});
value: {
required: true,
},
},
setup(props) { if (isObject(props.value)) {
const collapsed = reactive({}); for (const key in props.value) {
collapsed[key] = collapsable(props.value[key]);
}
}
if (isObject(props.value)) { function isObject(v): boolean {
for (const key in props.value) { return typeof v === 'object' && !Array.isArray(v) && v !== null;
collapsed[key] = collapsable(props.value[key]); }
}
}
function isObject(v): boolean { function isArray(v): boolean {
return typeof v === 'object' && !Array.isArray(v) && v !== null; return Array.isArray(v);
} }
function isArray(v): boolean { function isEmpty(v): boolean {
return Array.isArray(v); return (isArray(v) && v.length === 0) || (isObject(v) && Object.keys(v).length === 0);
} }
function isEmpty(v): boolean { function collapsable(v): boolean {
return (isArray(v) && v.length === 0) || (isObject(v) && Object.keys(v).length === 0); return (isObject(v) || isArray(v)) && !isEmpty(v);
} }
function collapsable(v): boolean {
return (isObject(v) || isArray(v)) && !isEmpty(v);
}
return {
number,
collapsed,
isObject,
isArray,
isEmpty,
collapsable,
};
},
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>