summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/Presentation/GeoJsonMapPageUi.php
blob: 89440f1f6b146e84c866a9786073a19dda399138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

declare( strict_types = 1 );

namespace Maps\Presentation;

use Html;

class GeoJsonMapPageUi {

	private $json;

	public static function forExistingPage( string $mapJson ): self {
		return new self( $mapJson );
	}

	private function __construct( ?string $json ) {
		$this->json = $json;
	}

	public function addToOutput( OutputFacade $output ) {
		$leafletPath = $GLOBALS['wgScriptPath'] . '/extensions/Maps/resources/lib/leaflet';

		$output->addHeadItem(
			'MapsGeoJsonHeadItem',
			Html::linkedStyle( "$leafletPath/leaflet.css" ) . Html::linkedScript( "$leafletPath/leaflet.js" )
		);

		$output->addHTML( $this->getJavascript() . $this->getHtml() );
		$output->addModules( 'ext.maps.geojson.page' );
	}

	private function getJavascript(): string {
		return Html::element(
			'script',
			[],
			$this->getJsonJs()
		);
	}

	private function getJsonJs(): string {
		return 'var GeoJson ='
			. $this->json
			. ';';
	}

	private function getHtml(): string {
		return $this->wrapHtmlInThumbDivs(
			Html::rawElement(
				'div',
				[
					'id' => 'GeoJsonMap',
					'style' => 'width: 100%; height: 600px; background-color: #eeeeee; overflow: hidden;',
					'class' => 'maps-map maps-leaflet maps-geojson-editor'
				],
				Html::element(
					'div',
					[
						'class' => 'maps-loading-message'
					],
					wfMessage( 'maps-loading-map' )->inContentLanguage()->text()
				)
			)
		);
	}

	private function wrapHtmlInThumbDivs( string $html ): string {
		return Html::rawElement(
			'div',
			[
				'class' => 'thumb'
			],
			Html::rawElement(
				'div',
				[
					'class' => 'thumbinner'
				],
				$html
			)
		);
	}

}