Skip to content

Commit

Permalink
queries: Support countable terms
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Blin <[email protected]>
  • Loading branch information
kblin committed Aug 31, 2023
1 parent eadc080 commit fe8ac85
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/components/QueryTerm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const hasFilters = computed(() => {
const showFilters = computed(() => {
return (hasFilters.value && wantShowFilters.value) || (term.value?.filters ?? []).length > 0;
});
const isCountable = computed(() => {
if (!term.value || !term.value.category) {
return false;
}
return store.categories.isCountable(term.value.category);
});
function getTermKind() {
if (!term.value) {
Expand Down Expand Up @@ -72,6 +78,9 @@ function updateFilter(idx: number, filter: QueryFilter) {
/>
</optgroup>
</select>
<span v-if="isCountable">
<input class="count" type="number" v-model="term.count" /> x
</span>
<AutoComplete
v-if="
getTermKind() == 'text' &&
Expand Down Expand Up @@ -177,6 +186,9 @@ function updateFilter(idx: number, filter: QueryFilter) {
</template>
<style scoped>
.count {
width: 4rem;
}
.expression {
width: 40%;
}
Expand Down
4 changes: 4 additions & 0 deletions src/models/__tests__/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ describe("QueryTerm", () => {
term.left = undefined;
expect(term.toString()).toBe("{[charlie|eve]}");
});
it("stringifies counts > 1 correctly", () => {
const term = createExpr(2);
expect(term.toString()).toBe("2*{[alice|bob]}");
});
});

describe("addTerm", () => {
Expand Down
13 changes: 10 additions & 3 deletions src/models/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,21 @@ export class QueryTerm {
if (!this.category) {
return "";
}

let ret = "";

if (this.count > 1) {
ret = `${this.count}*`;
}

if (!this.value) {
return `{[${this.category}]}`;
return `${ret}{[${this.category}]}`;
}
if (this.filters.length < 1) {
return `{[${this.category}|${this.value}]}`;
return `${ret}{[${this.category}|${this.value}]}`;
}

return `{[${this.category}|${this.value}]${this.filters.map((filter) =>
return `${ret}{[${this.category}|${this.value}]${this.filters.map((filter) =>
filter.toString()
)}}`;
}
Expand Down

0 comments on commit fe8ac85

Please sign in to comment.