1
1
/** @format */
2
2
import _ from "lodash"
3
- import angular , { IController , IScope , ITimeoutService } from "angular"
3
+ import angular , { ICompileService , IController , IScope , ITimeoutService } from "angular"
4
4
import settings from "@/settings"
5
5
import { html , regescape } from "@/util"
6
6
import { MapTab , RootScope } from "@/root-scope.types"
7
7
import { AppSettings } from "@/settings/app-settings.types"
8
8
import { MapRequestResult } from "@/backend/backend"
9
9
import { MapResult } from "@/map_services"
10
10
import { WithinParameters } from "@/backend/types"
11
- import { Marker , MarkerEvent , MarkerGroup } from "@/components/result-map"
11
+ import type { Marker , MarkerEvent , MarkerGroup } from "@/components/result-map"
12
12
import "@/components/korp-error"
13
- import "@/components/result-map"
14
13
15
14
type ResultsMapController = IController & {
16
15
active : boolean
@@ -22,6 +21,7 @@ type ResultsMapController = IController & {
22
21
type ResultsMapScope = IScope & {
23
22
center : AppSettings [ "map_center" ]
24
23
error ?: string
24
+ hasComponent ?: boolean
25
25
selectedGroups : string [ ]
26
26
markerGroups ?: Record < string , MarkerGroup >
27
27
numResults : number
@@ -57,14 +57,19 @@ angular.module("korpApp").component("resultsMap", {
57
57
</ label >
58
58
</ div >
59
59
</ div >
60
- < result-map
61
- center ="center "
62
- markers ="markerGroups "
63
- marker-callback ="newKWICSearch "
64
- selected-groups ="selectedGroups "
65
- rest-color ="'#9b9fa5' "
66
- use-clustering ="useClustering "
67
- > </ result-map >
60
+ < div ng-if ="hasComponent ">
61
+ < dynamic-wrapper
62
+ name ="result-map "
63
+ payload ="{
64
+ center: center,
65
+ markers: markerGroups,
66
+ markerCallback: newKWICSearch,
67
+ selectedGroups: selectedGroups,
68
+ restColor: '#9b9fa5',
69
+ useClustering: useClustering,
70
+ } "
71
+ > </ dynamic-wrapper >
72
+ </ div >
68
73
</ div >
69
74
</ div > ` ,
70
75
bindings : {
@@ -81,19 +86,22 @@ angular.module("korpApp").component("resultsMap", {
81
86
const $ctrl = this as ResultsMapController
82
87
83
88
$scope . center = settings [ "map_center" ]
89
+ $scope . hasComponent = undefined
84
90
$scope . selectedGroups = [ ]
85
91
$scope . markerGroups = { }
86
92
$scope . numResults = 0
87
93
$scope . useClustering = false
88
94
95
+ const componentPromise = import ( /* webpackChunkName: "map" */ "@/components/result-map" )
89
96
const rickshawPromise = import ( /* webpackChunkName: "rickshaw" */ "rickshaw" )
90
97
91
98
$ctrl . $onInit = ( ) => {
92
99
$ctrl . setProgress ( true , 0 )
93
100
94
- Promise . all ( [ rickshawPromise , $ctrl . promise ] ) . then (
95
- ( [ Rickshaw , result ] ) => {
101
+ Promise . all ( [ $ctrl . promise , componentPromise , rickshawPromise ] ) . then (
102
+ ( [ result , component , Rickshaw ] ) => {
96
103
$scope . $apply ( ( $scope : ResultsMapScope ) => {
104
+ $scope . hasComponent = true
97
105
$ctrl . setProgress ( false , 100 )
98
106
$scope . numResults = 20
99
107
$scope . markerGroups = result ? getMarkerGroups ( Rickshaw , result ) : undefined
@@ -204,3 +212,34 @@ angular.module("korpApp").component("resultsMap", {
204
212
} ,
205
213
] ,
206
214
} )
215
+
216
+ // https://stackoverflow.com/a/53398323/1750365
217
+ type DynamicWrapperScope = IScope & {
218
+ name : string
219
+ payload : Record < string , unknown >
220
+ }
221
+ angular . module ( "korpApp" ) . component ( "dynamicWrapper" , {
222
+ bindings : {
223
+ name : "@" ,
224
+ payload : "=?" ,
225
+ } ,
226
+ controller : [
227
+ "$compile" ,
228
+ "$element" ,
229
+ "$scope" ,
230
+ function ( $compile : ICompileService , $element : JQLite , $scope : DynamicWrapperScope ) {
231
+ const $ctrl = this
232
+ $ctrl . $onInit = ( ) => {
233
+ const scope = $scope . $new ( )
234
+ for ( const key in $ctrl . payload || { } ) {
235
+ ; ( scope as unknown as Record < string , unknown > ) [ key ] = $ctrl . payload [ key ]
236
+ }
237
+ const attrs = Object . keys ( $ctrl . payload )
238
+ . map ( ( key ) => `${ key } ="${ key } "` )
239
+ . join ( " " )
240
+ const template = `<${ $ctrl . name } ${ attrs } ></${ $ctrl . name } >`
241
+ $element . append ( $compile ( template ) ( scope ) )
242
+ }
243
+ } ,
244
+ ] ,
245
+ } )
0 commit comments