Skip to content

Commit 69ca215

Browse files
committed
fix: handle select attribute when changing to corpus without it
Fixes #490
1 parent 3835c0a commit 69ca215

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
### Fixed
1616

17+
- Error when switching corpus after searching by a corpus-specific attribute [#490](https://github.com/spraakbanken/korp-frontend/issues/490)
1718
- Error when downloading example KWIC [#491](https://github.com/spraakbanken/korp-frontend/issues/491)
1819

1920
## [9.11.4] – 2025-09-29

app/scripts/components/search/extended/cqp-value.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ angular.module("korpApp").component("extendedCqpValue", {
5858
const write = (val: string) => (shouldUseRegexp() ? val : regescape(val))
5959
const read = (val: string) => (shouldUseRegexp() ? val : unregescape(val))
6060
// Set initial input value
61-
childScope.input = read(childScope.model as string)
61+
childScope.input = read((childScope.model as string) || "")
6262
// Sync from input to model, escaping special characters if needed
63-
childScope.$watch("input", () => (childScope.model = write(childScope.input)))
64-
childScope.$watch("orObj.op", () => (childScope.model = write(childScope.input)))
63+
childScope.$watch("input", () => (childScope.model = write(childScope.input || "")))
64+
childScope.$watch("orObj.op", () => (childScope.model = write(childScope.input || "")))
6565

6666
const locals = { $scope: childScope }
6767
const { template, controller } = getWidget()
6868

69+
// TODO Are we always creating a new component instance here? What happens to the old ones?
6970
// @ts-ignore
7071
$controller(controller, locals)
7172
const tmplElem = $compile(template)(childScope)

app/scripts/components/search/extended/widgets/common.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Condition } from "@/cqp_parser/cqp.types"
55
import { StoreService } from "@/services/store"
66
import { AttributeOption } from "@/corpora/corpus-set"
77
import { loadOptions } from "@/search/extended-search"
8+
import { isEqual } from "lodash"
89

910
export type Widget = {
1011
template: string
@@ -38,9 +39,9 @@ export const selectController = (autocomplete: boolean): IController => [
3839
"$scope",
3940
"store",
4041
function ($scope: SelectWidgetScope, store: StoreService) {
41-
store.watch("corpus", (selected) => {
42+
store.watch("corpus", (selected, old) => {
4243
// TODO Destroy if new corpus selection doesn't support the attribute?
43-
if (selected.length > 0) {
44+
if (selected.length > 0 && !isEqual(selected, old)) {
4445
reloadValues()
4546
}
4647
})
@@ -53,7 +54,7 @@ export const selectController = (autocomplete: boolean): IController => [
5354
$scope.loading = false
5455
$scope.options = options
5556
// Reset old selection if that option has been removed.
56-
if (!autocomplete && !currentInputExists) {
57+
if (!autocomplete && !currentInputExists && $scope.options.length) {
5758
$scope.input = $scope.options[0][0]
5859
}
5960
})

app/scripts/search/extended-search.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AttributeOption } from "@/corpora/corpus-set"
55
import { loc, locAttribute } from "@/i18n"
66

77
/** Load attribute values from backend data as selector options. */
8-
export async function loadOptions(attr: AttributeOption, lang: string) {
8+
export async function loadOptions(attr: AttributeOption, lang: string): Promise<string[][]> {
99
const name = attr.name
1010
const split = attr.type === "set"
1111

@@ -14,6 +14,8 @@ export async function loadOptions(attr: AttributeOption, lang: string) {
1414
.filter((corpus) => name in corpus.struct_attributes || name in corpus.attributes)
1515
.map((corpus) => corpus.id)
1616

17+
if (!corpora.length) return []
18+
1719
const data = await getAttrValues(corpora, name, split)
1820

1921
return uniq(data)

0 commit comments

Comments
 (0)