Skip to content

Commit

Permalink
fix: do not cache opts across corpora
Browse files Browse the repository at this point in the history
Fixes #409
  • Loading branch information
arildm committed Mar 3, 2025
1 parent 5c7023a commit bab3cde
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

- Selecting a search history item used to reset params that were not part of the search
- Comparison result not showing if a search is not done first [#413](https://github.com/spraakbanken/korp-frontend/issues/413)
- Extended search: do not cache operator options across corpora [#409](https://github.com/spraakbanken/korp-frontend/issues/409)

## [9.8.4] - 2025-02-20

Expand Down
29 changes: 14 additions & 15 deletions app/scripts/components/extended/cqp-term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ExtendedCqpTermController = IController & {
change: () => void
types: AttributeOption[]
typeMapping: Record<string, AttributeOption>
opts: [string, OperatorKorp][] | undefined
opts: [string, OperatorKorp][]
valfilter: typeof valfilter
localChange: (term: Partial<Condition>) => void
setDefault: () => void
Expand Down Expand Up @@ -121,35 +121,34 @@ angular.module("korpApp").component("extendedCqpTerm", {
if (!ctrl.typeMapping[ctrl.term.type]) ctrl.term.type = ctrl.types[0].value

ctrl.opts = getOpts()

// Reset option if the selected one is no longer available
if (!ctrl.opts.find((pair) => pair[1] === ctrl.term.op)) ctrl.setDefault()
})
}

const getOpts = () => getOptsMemo(ctrl.term.type)

// returning new array each time kills angular, hence the memoizing
const getOptsMemo = _.memoize((type: string): [string, OperatorKorp][] | undefined => {
if (!(type in ctrl.typeMapping)) return
function getOpts(): [string, OperatorKorp][] {
const option = ctrl.typeMapping[ctrl.term.type]

const option = ctrl.typeMapping[type]
if (!option) {
console.error(`Attribute option missing for "${type}"`, ctrl.typeMapping)
return
console.error(`Attribute option missing for "${ctrl.term.type}"`, ctrl.typeMapping)
return Object.entries(settings["default_options"])
}

const ops: Record<string, OperatorKorp> = { ...(option?.opts || settings["default_options"]) }
// Clone to avoid modifying original
const ops = { ...(option.opts || settings["default_options"]) }

// For multi-value attributes, use the "contains" CQP operator for equality
if (option.type === "set") ops.is = "contains"

return _.toPairs(ops)
})
// Return as tuples
return Object.entries(ops)
}

ctrl.setDefault = function () {
// assign the first value from the opts
ctrl.opts = getOpts()

// TODO Correct? Was "is" before
ctrl.term.op = ctrl.opts?.[0]?.[1] || "="
ctrl.term.op = ctrl.opts[0]?.[1] || "="

ctrl.term.val = ""
ctrl.change()
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/settings/app-settings.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type AppSettings = {
label: LangString
}
cqp_prio: string[]
default_options?: Record<string, OperatorKorp>
default_options: Record<string, OperatorKorp>
default_language: string
default_overview_context: string
default_reading_context: string
Expand Down
1 change: 1 addition & 0 deletions app/scripts/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function setDefaultConfigValues() {
// some safety margin
backendURLMaxLength: 8100,
default_language: "eng",
default_options: { is: "=", is_not: "!=" },
// codes for translation ISO-639-1 to 639-2
iso_languages: {
en: "eng",
Expand Down

0 comments on commit bab3cde

Please sign in to comment.