Skip to content

Commit

Permalink
refactor: move activeSearch to root scope
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Nov 25, 2024
1 parent 6142f10 commit ae8fab7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 41 deletions.
7 changes: 2 additions & 5 deletions app/scripts/components/frontpage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import angular from "angular"
import settings from "@/settings"
import { RootScope } from "@/root-scope.types"
import { SearchesService } from "@/services/searches"
import { html } from "@/util"
import { isEnabled } from "@/news-service"
import "@/services/searches"
import "@/components/corpus-updates"
import "@/components/newsdesk"
import "@/components/search-examples"
Expand Down Expand Up @@ -35,8 +33,7 @@ export default angular.module("korpApp").component("frontpage", {
controller: [
"$rootScope",
"$scope",
"searches",
function ($rootScope: RootScope, $scope, searches: SearchesService) {
function ($rootScope: RootScope, $scope) {
const $ctrl = this
$ctrl.showDescription = false

Expand All @@ -47,7 +44,7 @@ export default angular.module("korpApp").component("frontpage", {
$scope.examples = settings.frontpage?.examples

$ctrl.hasResult = () =>
searches.activeSearch ||
$rootScope.activeSearch ||
$rootScope.compareTabs.length ||
$rootScope.graphTabs.length ||
$rootScope.mapTabs.length
Expand Down
9 changes: 4 additions & 5 deletions app/scripts/components/results.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @format */
import angular, { IController } from "angular"
import { html } from "@/util"
import "@/services/searches"
import "@/components/korp-error"
import "@/components/kwic"
import "@/components/loglike-meter"
Expand All @@ -20,7 +19,7 @@ import "@/controllers/text_reader_controller"
import "@/controllers/trend_diagram_controller"
import "@/controllers/word_picture_controller"
import "@/directives/tab-hash"
import { SearchesService } from "@/services/searches"
import { RootScope } from "@/root-scope.types"

type ResultsController = IController & {
onSidebarShow: () => void
Expand Down Expand Up @@ -298,12 +297,12 @@ angular.module("korpApp").component("results", {
`,
bindings: {},
controller: [
"searches",
function (searches: SearchesService) {
"$rootScope",
function ($rootScope: RootScope) {
const $ctrl = this as ResultsController
$ctrl.onSidebarShow = () => ($ctrl.sidebarVisible = true)
$ctrl.onSidebarHide = () => ($ctrl.sidebarVisible = false)
$ctrl.hasResult = () => !!searches.activeSearch
$ctrl.hasResult = () => !!$rootScope.activeSearch
},
],
})
1 change: 1 addition & 0 deletions app/scripts/components/search-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LocationService, SearchParams } from "@/urlparams"
import { html, splitFirst, unregescape } from "@/util"
import angular, { IScope } from "angular"
import "@/services/search-history"
import "@/services/searches"

type HistoryScope = IScope & {
getOptions: () => Option[]
Expand Down
14 changes: 5 additions & 9 deletions app/scripts/components/simple-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ angular.module("korpApp").component("simpleSearch", {
$location.search("isCaseInsensitive", "")
}

// triggers watch on searches.activeSearch
// triggers watch on activeSearch, via the Searches service
ctrl.updateSearch = function () {
$location.search("in_order", ctrl.freeOrder && ctrl.freeOrderEnabled ? false : null)
$location.search("prefix", ctrl.prefix ? true : null)
Expand All @@ -161,7 +161,7 @@ angular.module("korpApp").component("simpleSearch", {
$location.search("isCaseInsensitive", ctrl.isCaseInsensitive ? true : null)
$location.search("within", null)

// Unset and set query in next time step in order to trigger changes correctly in `searches`.
// Unset and set query in next time step in order to trigger changes correctly in the Searches service.
$location.search("search", null)
$location.replace()
$timeout(function () {
Expand Down Expand Up @@ -285,12 +285,8 @@ angular.module("korpApp").component("simpleSearch", {
})
}

// TODO Bad pattern: adding a service to root scope in order to watch a prop on it
// TODO Maybe Searches should keep `activeSearch` directly in root scope
// @ts-ignore
$rootScope.searches = searches
$rootScope.$watch("searches.activeSearch", () => {
const search = searches.activeSearch
$rootScope.$watch("activeSearch", () => {
const search = $rootScope.activeSearch
if (!search) return

if (search.type === "word" || search.type === "lemgram") {
Expand Down Expand Up @@ -339,7 +335,7 @@ angular.module("korpApp").component("simpleSearch", {
}

ctrl.doSearch = function () {
const search = searches.activeSearch
const search = $rootScope.activeSearch
ctrl.relatedObj = undefined
const cqp = ctrl.getCQP()
searches.kwicSearch(cqp)
Expand Down
13 changes: 2 additions & 11 deletions app/scripts/controllers/word_picture_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { RootScope } from "@/root-scope.types"
import { LocationService } from "@/urlparams"
import { KorpResponse, ProgressReport } from "@/backend/types"
import { WordPictureDefItem } from "@/settings/app-settings.types"
import { SearchesService } from "@/services/searches"
import "@/services/searches"
import { TabHashScope } from "@/directives/tab-hash"

type WordpicCtrlScope = TabHashScope & {
Expand Down Expand Up @@ -69,14 +67,7 @@ angular.module("korpApp").directive("wordpicCtrl", () => ({
"$rootScope",
"$location",
"$timeout",
"searches",
(
$scope: WordpicCtrlScope,
$rootScope: RootScope,
$location: LocationService,
$timeout: ITimeoutService,
searches: SearchesService
) => {
($scope: WordpicCtrlScope, $rootScope: RootScope, $location: LocationService, $timeout: ITimeoutService) => {
const s = $scope
s.tabindex = 3
s.proxy = lemgramProxyFactory.create()
Expand Down Expand Up @@ -115,7 +106,7 @@ angular.module("korpApp").directive("wordpicCtrl", () => ({
s.onProgress = (progressObj) => (s.progress = Math.round(progressObj["stats"]))

s.makeRequest = () => {
const search = searches.activeSearch
const search = $rootScope.activeSearch
if (!s.wordPic || !search || (search.type !== "lemgram" && search.val.includes(" "))) {
s.resetView()
return
Expand Down
5 changes: 5 additions & 0 deletions app/scripts/root-scope.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { CompareResult, MapRequestResult } from "@/backend/backend"
/** Extends the Angular Root Scope interface with properties used by this app. */
export type RootScope = IRootScopeService & {
_settings: Settings
activeSearch: {
/** "word", "lemgram" or "cqp" */
type: string
val: string
} | null
extendedCQP: string | null
globalFilter: CqpQuery | null
globalFilterDef: IDeferred<never>
Expand Down
16 changes: 5 additions & 11 deletions app/scripts/services/searches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import angular, { IDeferred, IQService, ITimeoutService } from "angular"
import "@/services/search-history"

export type SearchesService = {
activeSearch: {
/** "word", "lemgram" or "cqp" */
type: string
val: string
} | null
/** is resolved when parallel search controller is loaded */
langDef: IDeferred<never>
kwicSearch: (cqp: string) => void
Expand Down Expand Up @@ -41,19 +36,18 @@ angular.module("korpApp").factory("searches", [
searchHistory: SearchHistoryService
): SearchesService {
const searches: SearchesService = {
activeSearch: null,
langDef: $q.defer(),

/** Tell result controllers (kwic/statistics/word picture) to send their requests. */
kwicSearch(cqp: string) {
$rootScope.$emit("make_request", cqp, this.activeSearch)
$rootScope.$emit("make_request", cqp, $rootScope.activeSearch)
},

getCqpExpr(): string {
if (!this.activeSearch) return ""
if (this.activeSearch.type === "word" || this.activeSearch.type === "lemgram")
if (!$rootScope.activeSearch) return ""
if ($rootScope.activeSearch.type === "word" || $rootScope.activeSearch.type === "lemgram")
return $rootScope.simpleCQP || ""
return this.activeSearch.val
return $rootScope.activeSearch.val
},

triggerSearch(): void {
Expand Down Expand Up @@ -87,7 +81,7 @@ angular.module("korpApp").factory("searches", [
}
// Update stored search query
if (["cqp", "word", "lemgram"].includes(type)) {
searches.activeSearch = { type, val: value }
$rootScope.activeSearch = { type, val: value }
}

// For Extended/Advanced search, merge with global filters and trigger API requests
Expand Down

0 comments on commit ae8fab7

Please sign in to comment.