-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ui): improve the labels behavior (#7397)
- Loading branch information
1 parent
41712b8
commit bf19a8f
Showing
3 changed files
with
74 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,78 @@ | ||
<template> | ||
<span data-component="FILENAME_PLACEHOLDER" v-if="labels"> | ||
<!-- 'el-check-tag' would be a better fit but it currently lacks customization (missing size, bold font) --> | ||
<template | ||
v-for="(value, key) in labelMap" | ||
:key="key" | ||
<span v-if="props.labels.length"> | ||
<el-check-tag | ||
v-for="(label, index) in props.labels" | ||
:key="index" | ||
:disabled="readOnly" | ||
:checked="isChecked(label)" | ||
@change="updateLabel(label)" | ||
class="me-1 el-tag label" | ||
> | ||
<router-link v-if="filterEnabled" :to="link(key, value)" class="me-1 labels el-tag el-tag--small" :class="{'el-tag--primary': checked(key, value)}"> | ||
{{ key }}: {{ value }} | ||
</router-link> | ||
<div v-else class="me-1 labels el-tag el-tag--small" :class="{'el-tag--primary': checked(key, value)}">{{ key }}: {{ value }}</div> | ||
</template> | ||
{{ label.key }}: {{ label.value }} | ||
</el-check-tag> | ||
</span> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
labels: { | ||
type: Object, | ||
default: () => {} | ||
}, | ||
filterEnabled: { | ||
type: Boolean, | ||
default: true | ||
} | ||
}, | ||
// this is needed as flows uses a Map and Execution a List of Labels. | ||
// if we align both of them this can be removed | ||
computed: { | ||
labelMap() { | ||
if (Array.isArray(this.labels)) { | ||
return Object.fromEntries(this.labels.map(label => [label.key, label.value])); | ||
} else { | ||
return this.labels; | ||
} | ||
}, | ||
labelsFromQuery() { | ||
const labels = new Map(); | ||
(this.$route.query.labels !== undefined ? | ||
(typeof(this.$route.query.labels) === "string" ? [this.$route.query.labels] : this.$route.query.labels) : | ||
[] | ||
) | ||
.forEach(label => { | ||
const separatorIndex = label.indexOf(":"); | ||
<script setup lang="ts"> | ||
import {watch} from "vue"; | ||
if (separatorIndex === -1) { | ||
return; | ||
} | ||
import {useRouter, useRoute} from "vue-router"; | ||
const router = useRouter(); | ||
const route = useRoute(); | ||
labels.set(label.slice(0, separatorIndex), label.slice(separatorIndex + 1)); | ||
}) | ||
interface Label { | ||
key: string; | ||
value: string; | ||
} | ||
return labels; | ||
} | ||
}, | ||
methods: { | ||
checked(key, value) { | ||
return this.labelsFromQuery.has(key) && this.labelsFromQuery.get(key) === value; | ||
}, | ||
link(key, value) { | ||
const labels = this.labelsFromQuery; | ||
const props = withDefaults( | ||
defineProps<{ labels: Label[]; readOnly?: boolean }>(), | ||
{labels: () => [], readOnly: false}, | ||
); | ||
if (labels.has(key)) { | ||
labels.delete(key); | ||
} else { | ||
labels.set(key, value); | ||
} | ||
import {decodeSearchParams} from "../../components/filter/utils/helpers"; | ||
let query: any[] = []; | ||
watch( | ||
() => route.query, | ||
(q: any) => (query = decodeSearchParams(q, undefined, [])), | ||
{immediate: true}, | ||
); | ||
const qs = { | ||
...this.$route.query, | ||
...{"labels": Array.from(labels.keys()).map((key) => key + ":" + labels.get(key))} | ||
}; | ||
const isChecked = (label: Label) => { | ||
return query.some((l) => { | ||
if (typeof l?.value !== "string") return false; | ||
delete qs.page; | ||
const [key, value] = l.value.split(":"); | ||
return key === label.key && value === label.value; | ||
}); | ||
}; | ||
const updateLabel = (label: Label) => { | ||
const getKey = (key: string) => `filters[labels][$eq][${key}]`; | ||
return {name: this.$route.name, params: this.$route.params, query: qs}; | ||
} | ||
if (isChecked(label)) { | ||
const replacementQuery = {...route.query}; | ||
delete replacementQuery[getKey(label.key)]; | ||
router.replace({query: replacementQuery}); | ||
} else { | ||
router.replace({ | ||
query: {...route.query, [getKey(label.key)]: label.value}, | ||
}); | ||
} | ||
}; | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.label { | ||
font-weight: normal; | ||
&:hover { | ||
background-color: var(--ks-tag-background-hover); | ||
} | ||
} | ||
.el-check-tag.el-check-tag--primary.is-checked { | ||
background-color: var(--el-color-primary); | ||
color: var(--ks-content-primary); | ||
} | ||
</style> |