Skip to content

Commit d2cfde7

Browse files
committed
Introduce store service
1 parent 1398d51 commit d2cfde7

File tree

13 files changed

+170
-46
lines changed

13 files changed

+170
-46
lines changed

app/scripts/app.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import "@/components/searchtabs"
2626
import "@/components/frontpage"
2727
import "@/components/results"
2828
import "@/components/korp-error"
29+
import "@/services/store"
2930
import { JQueryExtended } from "./jquery.types"
3031
import { LocationService } from "./urlparams"
3132
import { LocLangMap } from "@/i18n/types"
33+
import { StoreService } from "./services/store"
3234

3335
// load all custom components
3436
let customComponents: Record<string, IComponentOptions> = {}
@@ -95,6 +97,7 @@ korpApp.run([
9597
"$q",
9698
"$timeout",
9799
"$uibModal",
100+
"store",
98101
async function (
99102
$rootScope: RootScope,
100103
$location: LocationService,
@@ -103,7 +106,8 @@ korpApp.run([
103106
tmhDynamicLocaleCache: ICacheObject,
104107
$q: IQService,
105108
$timeout: ITimeoutService,
106-
$uibModal: ui.bootstrap.IModalService
109+
$uibModal: ui.bootstrap.IModalService,
110+
store: StoreService
107111
) {
108112
const s = $rootScope
109113
s._settings = settings
@@ -265,6 +269,20 @@ korpApp.run([
265269
},
266270
})
267271
} else {
272+
// Sync corpus selection between location and store
273+
store.watch("selectedCorpusIds", (corpusIds) => {
274+
settings.corpusListing.select(corpusIds)
275+
$location.search("corpus", corpusIds.join(","))
276+
})
277+
$rootScope.$watch(
278+
() => $location.search().corpus,
279+
(corpusIdsComma) => {
280+
const corpusIds = corpusIdsComma ? corpusIdsComma.split(",") : []
281+
settings.corpusListing.select(corpusIds)
282+
store.set("selectedCorpusIds", corpusIds)
283+
}
284+
)
285+
268286
// here $timeout must be used so that message is not sent before all controllers/componenters are initialized
269287
settings.corpusListing.select(selectedIds)
270288
$timeout(() => $rootScope.$broadcast("initialcorpuschooserchange", selectedIds), 0)

app/scripts/components/corpus-chooser/corpus-chooser.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import {
1717
import "@/components/corpus-chooser/corpus-time-graph"
1818
import "@/components/corpus-chooser/info-box"
1919
import "@/components/corpus-chooser/tree"
20+
import "@/services/store"
2021
import { RootScope } from "@/root-scope.types"
2122
import { LocationService } from "@/urlparams"
2223
import { CorpusTransformed } from "@/settings/config-transformed.types"
2324
import { LangString } from "@/i18n/types"
25+
import { StoreService } from "@/services/store"
2426

2527
type CorpusChooserController = IController & {
2628
credentials: string[]
@@ -131,8 +133,8 @@ angular.module("korpApp").component("corpusChooser", {
131133
bindings: {},
132134
controller: [
133135
"$rootScope",
134-
"$location",
135-
function ($rootScope: RootScope, $location: LocationService) {
136+
"store",
137+
function ($rootScope: RootScope, store: StoreService) {
136138
const $ctrl = this as CorpusChooserController
137139

138140
statemachine.listen("login", function () {
@@ -187,14 +189,7 @@ angular.module("korpApp").component("corpusChooser", {
187189
select(corpusIds, true)
188190

189191
// Sync when corpus selection is modified elsewhere.
190-
$rootScope.$watch(
191-
() => $location.search().corpus,
192-
(corpusIdsComma) => {
193-
const corpusIds = corpusIdsComma ? corpusIdsComma.split(",") : []
194-
select(corpusIds)
195-
}
196-
)
197-
$rootScope.$on("corpuschooserchange", (e, selected) => select(selected))
192+
store.watch("selectedCorpusIds", updateSelection)
198193
})
199194

200195
$ctrl.updateSelectedCount = (selection) => {
@@ -233,14 +228,21 @@ angular.module("korpApp").component("corpusChooser", {
233228
}
234229
}
235230

231+
/** Apply a selection made locally */
236232
function select(corporaIds: string[], force?: boolean) {
237233
// Exit if no actual change
238234
const selectedIds = settings.corpusListing.mapSelectedCorpora((corpus) => corpus.id)
239235
if (!force && _.isEqual(corporaIds, selectedIds)) return
240236

237+
const selection = updateSelection(corporaIds)
238+
store.set("selectedCorpusIds", selection)
239+
}
240+
241+
/** Filter requested corpus selection and update internal state */
242+
function updateSelection(corpusIds: string[]): string[] {
241243
const selection = filterCorporaOnCredentials(
242244
Object.values(settings.corpora),
243-
corporaIds,
245+
corpusIds,
244246
$ctrl.credentials
245247
)
246248

@@ -251,9 +253,7 @@ angular.module("korpApp").component("corpusChooser", {
251253
$ctrl.firstCorpus = settings.corpora[selection[0]].title
252254
}
253255

254-
settings.corpusListing.select(selection)
255-
$rootScope.$broadcast("corpuschooserchange", selection)
256-
$location.search("corpus", selection.join(","))
256+
return selection
257257
}
258258

259259
$ctrl.onShowInfo = (node: ChooserFolderSub | CorpusTransformed) => {

app/scripts/components/corpus-chooser/corpus-time-graph.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import {
1313
} from "@/timeseries"
1414
import { html } from "@/util"
1515
import { RootScope } from "@/root-scope.types"
16+
import { StoreService } from "@/services/store"
17+
import "@/services/store"
1618

1719
angular.module("korpApp").component("corpusTimeGraph", {
1820
template: html`<canvas id="time-graph-chart" height="80"></canvas>`,
1921
controller: [
2022
"$rootScope",
21-
function ($rootScope: RootScope) {
23+
"store",
24+
function ($rootScope: RootScope, store: StoreService) {
2225
const { min, max } = getSpan()
2326

2427
const datasetsDated = [
@@ -128,7 +131,7 @@ angular.module("korpApp").component("corpusTimeGraph", {
128131
},
129132
})
130133

131-
$rootScope.$on("corpuschooserchange", () => {
134+
store.watch("selectedCorpusIds", () => {
132135
updateSelectedData()
133136
// `'none'` to disable animations. Animations would be nice, but they look weird when new data has different min/max year.
134137
// TODO Do animations look better if data is given as array including empty years, not a record?

app/scripts/components/extended/cqp-term.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import settings from "@/settings"
55
import { html, valfilter } from "@/util"
66
const minusImage = require("../../../img/minus.png")
77
import "@/components/extended/cqp-value"
8+
import "@/services/store"
89

910
/**
1011
* TODO
@@ -55,9 +56,11 @@ angular.module("korpApp").component("extendedCqpTerm", {
5556
change: "&",
5657
},
5758
controller: [
59+
"$location",
5860
"$rootScope",
5961
"$timeout",
60-
function ($rootScope, $timeout) {
62+
"store",
63+
function ($location, $rootScope, $timeout, store) {
6164
const ctrl = this
6265

6366
ctrl.valfilter = valfilter
@@ -69,17 +72,22 @@ angular.module("korpApp").component("extendedCqpTerm", {
6972
ctrl.term.val = ""
7073
ctrl.change()
7174
}
72-
$rootScope.$on("corpuschooserchange", (e, selected) => $timeout(() => onCorpusChange(e, selected), 0))
75+
store.watch("selectedCorpusIds", () => $timeout(() => onCorpusChange(), 0))
76+
$rootScope.$watch(
77+
() => $location.search().parallel_corpora,
78+
() => $timeout(() => onCorpusChange())
79+
)
7380

74-
onCorpusChange(null, settings.corpusListing.selected)
81+
onCorpusChange()
7582
}
7683

7784
ctrl.localChange = (term) => {
7885
Object.assign(ctrl.term, term)
7986
ctrl.change()
8087
}
8188

82-
const onCorpusChange = function (event, selected) {
89+
function onCorpusChange() {
90+
const selected = store.get("selectedCorpusIds")
8391
// TODO: respect the setting 'wordAttributeSelector' and similar
8492
if (!(selected && selected.length)) {
8593
return

app/scripts/components/extended/extended-parallel.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { expandOperators } from "@/cqp_parser/cqp"
66
import { html } from "@/util"
77
import { matomoSend } from "@/matomo"
88
import "@/services/searches"
9+
import "@/services/store"
910
import "@/components/extended/tokens"
1011

1112
angular.module("korpApp").component("extendedParallel", {
@@ -63,7 +64,8 @@ angular.module("korpApp").component("extendedParallel", {
6364
"$rootScope",
6465
"$timeout",
6566
"searches",
66-
function ($location, $rootScope, $timeout, searches) {
67+
"store",
68+
function ($location, $rootScope, $timeout, searches, store) {
6769
const ctrl = this
6870

6971
ctrl.initialized = false
@@ -72,7 +74,7 @@ angular.module("korpApp").component("extendedParallel", {
7274
ctrl.onLangChange()
7375
ctrl.initialized = true
7476

75-
$rootScope.$on("corpuschooserchange", () => ctrl.onLangChange(false))
77+
store.watch("selectedCorpusIds", () => ctrl.onLangChange())
7678
}
7779

7880
ctrl.negates = []
@@ -140,15 +142,10 @@ angular.module("korpApp").component("extendedParallel", {
140142
return output
141143
}
142144

143-
ctrl.onLangChange = function (broadcast = true) {
145+
ctrl.onLangChange = function () {
144146
var currentLangList = _.map(ctrl.langs, "lang")
145147
settings.corpusListing.setActiveLangs(currentLangList)
146148
$location.search("parallel_corpora", currentLangList.join(","))
147-
148-
// hacky fix to make attributes update when switching languages
149-
if (ctrl.initialized && broadcast) {
150-
$rootScope.$broadcast("corpuschooserchange", [""])
151-
}
152149
searches.langDef.resolve()
153150
}
154151

app/scripts/components/extended/extended-standard.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { expandOperators, mergeCqpExprs, parse, stringify, supportsInOrder } fro
77
import { html } from "@/util"
88
import { matomoSend } from "@/matomo"
99
import "@/services/compare-searches"
10+
import "@/services/store"
1011
import "@/components/extended/tokens"
1112
import "@/components/search-submit"
1213
import "@/global-filter/global-filters"
@@ -50,9 +51,10 @@ angular.module("korpApp").component("extendedStandard", {
5051
"$location",
5152
"$rootScope",
5253
"$scope",
53-
"compareSearches",
5454
"$timeout",
55-
function ($location, $rootScope, $scope, compareSearches, $timeout) {
55+
"compareSearches",
56+
"store",
57+
function ($location, $rootScope, $scope, $timeout, compareSearches, store) {
5658
const ctrl = this
5759

5860
$scope.freeOrder = $location.search().in_order != null
@@ -153,7 +155,7 @@ angular.module("korpApp").component("extendedStandard", {
153155
return output
154156
}
155157

156-
$rootScope.$on("corpuschooserchange", function () {
158+
store.watch("selectedCorpusIds", () => {
157159
ctrl.withins = ctrl.getWithins()
158160
ctrl.within = ctrl.withins[0] && ctrl.withins[0].value
159161
})

app/scripts/components/extended/struct-token.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import angular from "angular"
33
import settings from "@/settings"
44
import { html } from "@/util"
5+
import "@/services/store"
56

67
angular.module("korpApp").component("extendedStructToken", {
78
template: html`
@@ -55,7 +56,8 @@ angular.module("korpApp").component("extendedStructToken", {
5556
},
5657
controller: [
5758
"$scope",
58-
function ($scope) {
59+
"store",
60+
function ($scope, store) {
5961
const ctrl = this
6062

6163
ctrl.$onInit = () => {
@@ -70,7 +72,7 @@ angular.module("korpApp").component("extendedStructToken", {
7072
ctrl.change()
7173
}
7274

73-
$scope.$on("corpuschooserchange", onCorpusChange)
75+
store.watch("selectedCorpusIds", onCorpusChange)
7476
}
7577

7678
const onCorpusChange = () => {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import "@/directives/escaper"
77
import { IController, IScope } from "angular"
88
import { Condition } from "@/cqp_parser/cqp.types"
99
import { StructService, StructServiceOptions } from "@/backend/struct-service"
10-
import { RootScope } from "@/root-scope.types"
1110
import { LocMap } from "@/i18n/types"
11+
import { StoreService } from "@/services/store"
12+
import "@/services/store"
1213

1314
export type WidgetDefinition = Widget | WidgetWithOptions
1415
export type WidgetWithOptions<T extends {} = {}> = (options: T) => Widget
@@ -44,10 +45,10 @@ export const selectTemplate = html`<select
4445

4546
export const selectController = (autocomplete: boolean): IController => [
4647
"$scope",
47-
"$rootScope",
48+
"store",
4849
"structService",
49-
function ($scope: SelectWidgetScope, $rootScope: RootScope, structService: StructService) {
50-
$rootScope.$on("corpuschooserchange", function (event, selected: string[]) {
50+
function ($scope: SelectWidgetScope, store: StoreService, structService: StructService) {
51+
store.watch("selectedCorpusIds", (selected: string[]) => {
5152
if (selected.length > 0) {
5253
reloadValues()
5354
}

app/scripts/components/extended/widgets/date-interval.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import settings from "@/settings"
55
import { html } from "@/util"
66
import { Widget, WidgetScope } from "./common"
77
import "@/components/datetime-picker"
8+
import { StoreService } from "@/services/store"
9+
import "@/services/store"
810

911
type DateIntervalScope = WidgetScope<(string | number)[]> & {
1012
minDate: Date
@@ -76,7 +78,8 @@ export const dateInterval: Widget = {
7678
`,
7779
controller: [
7880
"$scope",
79-
function ($scope: DateIntervalScope) {
81+
"store",
82+
function ($scope: DateIntervalScope, store: StoreService) {
8083
function updateIntervals() {
8184
const moments = settings.corpusListing.getMomentInterval()
8285
if (moments) {
@@ -105,7 +108,7 @@ export const dateInterval: Widget = {
105108
}
106109
}
107110

108-
$scope.$on("corpuschooserchange", function () {
111+
store.watch("selectedCorpusIds", () => {
109112
updateIntervals()
110113
})
111114

app/scripts/components/searchtabs.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { html } from "@/util"
66
import { loc } from "@/i18n"
77
import "@/services/compare-searches"
88
import "@/services/searches"
9+
import "@/services/store"
910
import "@/components/simple-search"
1011
import "@/components/extended/extended-standard"
1112
import "@/components/extended/extended-parallel"
@@ -22,6 +23,7 @@ import { SearchesService } from "@/services/searches"
2223
import { LocationService } from "@/urlparams"
2324
import { SavedSearch } from "@/local-storage"
2425
import { AttributeOption } from "@/corpus_listing"
26+
import { StoreService } from "@/services/store"
2527

2628
type SearchtabsController = IController & {
2729
parallelMode: boolean
@@ -130,14 +132,16 @@ angular.module("korpApp").component("searchtabs", {
130132
`,
131133
controller: [
132134
"$location",
133-
"searches",
134135
"$rootScope",
135136
"compareSearches",
137+
"searches",
138+
"store",
136139
function (
137140
$location: LocationService,
138-
searches: SearchesService,
139141
$rootScope: RootScope,
140-
compareSearches: CompareSearches
142+
compareSearches: CompareSearches,
143+
searches: SearchesService,
144+
store: StoreService
141145
) {
142146
const $ctrl = this as SearchtabsController
143147

@@ -220,7 +224,7 @@ angular.module("korpApp").component("searchtabs", {
220224
$location.search("stats_reduce_insensitive", "word")
221225
}
222226

223-
$rootScope.$on("corpuschooserchange", function (event, selected) {
227+
store.watch("selectedCorpusIds", (selected) => {
224228
$ctrl.noCorporaSelected = !selected.length
225229
const allAttrs = settings.corpusListing.getStatsAttributeGroups(settings.corpusListing.getReduceLang())
226230
$ctrl.statCurrentAttrs = _.filter(allAttrs, (item) => !item["hide_statistics"])

0 commit comments

Comments
 (0)