Skip to content

Commit f4c2bed

Browse files
committed
WIP: Trying to dynamically import map component
1 parent 129ca39 commit f4c2bed

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

app/index.ts

-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ require("./styles/ui_mods.css")
1919
require("./styles/jquery.tooltip.css")
2020
require("rickshaw/rickshaw.css")
2121

22-
require("leaflet/dist/leaflet.css")
23-
require("leaflet.markercluster/dist/MarkerCluster.css")
2422
require("./styles/_bootstrap-custom.scss")
2523

2624
require("./styles/tailwind.scss")
@@ -49,9 +47,6 @@ require("slickgrid/slick.interactions.js")
4947

5048
require("./scripts/jq_extensions.js")
5149

52-
require("leaflet")
53-
require("leaflet.markercluster")
54-
require("leaflet-providers")
5550
require("angular-filter/index.js")
5651

5752
require("./lib/jquery.tooltip.pack.js")

app/scripts/components/result-map.ts

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import "@/../styles/map.scss"
88
import { AppSettings } from "@/settings/app-settings.types"
99
import { WithinParameters } from "@/backend/types"
1010
import { Point } from "@/map_services"
11+
import "leaflet/dist/leaflet.css"
12+
import "leaflet.markercluster"
13+
import "leaflet.markercluster/dist/MarkerCluster.css"
14+
import "leaflet-providers"
1115

1216
type ResultMapController = IController & {
1317
center: AppSettings["map_center"]

app/scripts/components/results-map.ts

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/** @format */
22
import _ from "lodash"
3-
import angular, { IController, IScope, ITimeoutService } from "angular"
3+
import angular, { ICompileService, IController, IScope, ITimeoutService } from "angular"
44
import settings from "@/settings"
55
import { html, regescape } from "@/util"
66
import { MapTab, RootScope } from "@/root-scope.types"
77
import { AppSettings } from "@/settings/app-settings.types"
88
import { MapRequestResult } from "@/backend/backend"
99
import { MapResult } from "@/map_services"
1010
import { WithinParameters } from "@/backend/types"
11-
import { Marker, MarkerEvent, MarkerGroup } from "@/components/result-map"
11+
import type { Marker, MarkerEvent, MarkerGroup } from "@/components/result-map"
1212
import "@/components/korp-error"
13-
import "@/components/result-map"
1413

1514
type ResultsMapController = IController & {
1615
active: boolean
@@ -22,6 +21,7 @@ type ResultsMapController = IController & {
2221
type ResultsMapScope = IScope & {
2322
center: AppSettings["map_center"]
2423
error?: string
24+
hasComponent?: boolean
2525
selectedGroups: string[]
2626
markerGroups?: Record<string, MarkerGroup>
2727
numResults: number
@@ -57,14 +57,19 @@ angular.module("korpApp").component("resultsMap", {
5757
</label>
5858
</div>
5959
</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>
6873
</div>
6974
</div>`,
7075
bindings: {
@@ -81,19 +86,22 @@ angular.module("korpApp").component("resultsMap", {
8186
const $ctrl = this as ResultsMapController
8287

8388
$scope.center = settings["map_center"]
89+
$scope.hasComponent = undefined
8490
$scope.selectedGroups = []
8591
$scope.markerGroups = {}
8692
$scope.numResults = 0
8793
$scope.useClustering = false
8894

95+
const componentPromise = import(/* webpackChunkName: "map" */ "@/components/result-map")
8996
const rickshawPromise = import(/* webpackChunkName: "rickshaw" */ "rickshaw")
9097

9198
$ctrl.$onInit = () => {
9299
$ctrl.setProgress(true, 0)
93100

94-
Promise.all([rickshawPromise, $ctrl.promise]).then(
95-
([Rickshaw, result]) => {
101+
Promise.all([$ctrl.promise, componentPromise, rickshawPromise]).then(
102+
([result, component, Rickshaw]) => {
96103
$scope.$apply(($scope: ResultsMapScope) => {
104+
$scope.hasComponent = true
97105
$ctrl.setProgress(false, 100)
98106
$scope.numResults = 20
99107
$scope.markerGroups = result ? getMarkerGroups(Rickshaw, result) : undefined
@@ -204,3 +212,34 @@ angular.module("korpApp").component("resultsMap", {
204212
},
205213
],
206214
})
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

Comments
 (0)