@@ -2,8 +2,6 @@ import $ from 'jquery';
22import { AJAX } from '../modules/ajax.ts' ;
33import { escapeHtml } from '../modules/functions/escape.ts' ;
44
5- let openLayersData : any [ ] | null = null ;
6-
75/**
86 * @fileoverview functions used for visualizing GIS data
97 *
@@ -418,15 +416,50 @@ class SvgVisualization extends GisVisualization {
418416class OlVisualization extends GisVisualization {
419417 private olMap : any = undefined ;
420418
421- private initFn : ( HTMLElement ) => any ;
419+ private data : any [ ] ;
422420
423421 /**
424422 * @param {function(HTMLElement): ol.Map } initFn
425423 */
426- constructor ( target : HTMLElement , initFn ) {
424+ constructor ( target : HTMLElement , data : any [ ] ) {
427425 super ( target ) ;
428426
429- this . initFn = initFn ;
427+ this . data = data ;
428+ }
429+
430+ drawOpenLayers ( ) {
431+ if ( typeof window . ol === 'undefined' ) {
432+ return undefined ;
433+ }
434+
435+ $ ( 'head' ) . append ( '<link rel="stylesheet" type="text/css" href="js/vendor/openlayers/theme/ol.css">' ) ;
436+
437+ const vectorSource = new window . ol . source . Vector ( {
438+ features : getFeaturesFromOpenLayersData ( this . data ) ,
439+ } ) ;
440+ const map = new window . ol . Map ( {
441+ target : this . target ,
442+ layers : [
443+ new window . ol . layer . Tile ( { source : new window . ol . source . OSM ( ) } ) ,
444+ new window . ol . layer . Vector ( { source : vectorSource } ) ,
445+ ] ,
446+ view : new window . ol . View ( { center : [ 0 , 0 ] , zoom : 4 } ) ,
447+ controls : [
448+ new window . ol . control . MousePosition ( {
449+ coordinateFormat : window . ol . coordinate . createStringXY ( 4 ) ,
450+ projection : 'EPSG:4326'
451+ } ) ,
452+ new window . ol . control . Zoom ,
453+ new window . ol . control . Attribution
454+ ]
455+ } ) ;
456+
457+ const extent = vectorSource . getExtent ( ) ;
458+ if ( ! window . ol . extent . isEmpty ( extent ) ) {
459+ map . getView ( ) . fit ( extent , { padding : [ 20 , 20 , 20 , 20 ] } ) ;
460+ }
461+
462+ return map ;
430463 }
431464
432465 show ( ) {
@@ -435,8 +468,7 @@ class OlVisualization extends GisVisualization {
435468 if ( this . olMap ) {
436469 this . olMap . updateSize ( ) ;
437470 } else {
438- const initFn = this . initFn ;
439- this . olMap = initFn ( this . target ) ;
471+ this . olMap = this . drawOpenLayers ( ) ;
440472 }
441473 }
442474
@@ -513,54 +545,18 @@ function getFeaturesFromOpenLayersData (geometries: any[]): any[] {
513545 return features ;
514546}
515547
516- function drawOpenLayers ( target : HTMLElement ) {
517- if ( typeof window . ol === 'undefined' ) {
518- return undefined ;
519- }
520-
521- $ ( 'head' ) . append ( '<link rel="stylesheet" type="text/css" href="js/vendor/openlayers/theme/ol.css">' ) ;
522-
523- const vectorSource = new window . ol . source . Vector ( { } ) ;
524- const map = new window . ol . Map ( {
525- target : target ,
526- layers : [
527- new window . ol . layer . Tile ( { source : new window . ol . source . OSM ( ) } ) ,
528- new window . ol . layer . Vector ( { source : vectorSource } )
529- ] ,
530- view : new window . ol . View ( { center : [ 0 , 0 ] , zoom : 4 } ) ,
531- controls : [
532- new window . ol . control . MousePosition ( {
533- coordinateFormat : window . ol . coordinate . createStringXY ( 4 ) ,
534- projection : 'EPSG:4326'
535- } ) ,
536- new window . ol . control . Zoom ,
537- new window . ol . control . Attribution
538- ]
539- } ) ;
540-
541- openLayersData = openLayersData ?? JSON . parse ( $ ( '#visualization-placeholder' ) . attr ( 'data-ol-data' ) ) ;
542- const features = getFeaturesFromOpenLayersData ( openLayersData ) ;
543- for ( const feature of features ) {
544- vectorSource . addFeature ( feature ) ;
545- }
546-
547- const extent = vectorSource . getExtent ( ) ;
548- if ( ! window . ol . extent . isEmpty ( extent ) ) {
549- map . getView ( ) . fit ( extent , { padding : [ 20 , 20 , 20 , 20 ] } ) ;
550- }
551-
552- return map ;
553- }
554-
555548class GisVisualizationController {
556549 private svgVis : SvgVisualization | undefined = undefined ;
557550
558551 private olVis : OlVisualization | undefined = undefined ;
559552
560553 private boundOnChoiceChange : any ;
561554
562- constructor ( ) {
555+ private olData : any [ ] ;
556+
557+ constructor ( olData : any [ ] ) {
563558 this . boundOnChoiceChange = this . onChoiceChange . bind ( this ) ;
559+ this . olData = olData ;
564560
565561 $ ( document ) . on ( 'click' , '#useOsmAsBaseLayerSwitch' , this . boundOnChoiceChange ) ;
566562
@@ -591,14 +587,16 @@ class GisVisualizationController {
591587 if ( ! this . olVis ) {
592588 this . olVis = new OlVisualization (
593589 $ ( '#visualization-placeholder > .visualization-target-ol' ) . get ( 0 ) ,
594- drawOpenLayers
590+ this . olData ,
595591 ) ;
596592 }
597593
598594 newVis = this . olVis ;
599595 } else {
600596 if ( ! this . svgVis ) {
601- this . svgVis = new SvgVisualization ( $ ( '#visualization-placeholder > .visualization-target-svg' ) . get ( 0 ) ) ;
597+ this . svgVis = new SvgVisualization (
598+ $ ( '#visualization-placeholder > .visualization-target-svg' ) . get ( 0 ) ,
599+ ) ;
602600 }
603601
604602 newVis = this . svgVis ;
@@ -621,10 +619,6 @@ class GisVisualizationController {
621619 this . olVis . dispose ( ) ;
622620 }
623621 }
624-
625- public setOpenLayersData ( olData : any [ ] ) : void {
626- openLayersData = olData ;
627- }
628622}
629623
630624declare global {
@@ -658,6 +652,8 @@ AJAX.registerOnload('table/gis_visualization.js', function () {
658652 // If we are in GIS visualization, initialize it
659653
660654 if ( $ ( '#gis_div' ) . length > 0 ) {
661- visualizationController = new GisVisualizationController ( ) ;
655+ visualizationController = new GisVisualizationController (
656+ JSON . parse ( $ ( '#visualization-placeholder' ) . attr ( 'data-ol-data' ) )
657+ ) ;
662658 }
663659} ) ;
0 commit comments