Completed
Push — master ( 07ef30...c256ab )
by Jeroen De
03:52
created

GeoJsonPageOutput::addToOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Presentation;
6
7
use Html;
8
9
class GeoJsonPageOutput {
10
11
	private $json;
12
13
	public static function forNewPage(): self {
14
		return new self( null );
15
	}
16
17
	public static function forExistingPage( string $mapJson ): self {
18
		return new self( $mapJson );
19
	}
20
21
	private function __construct( ?string $json ) {
22
		$this->json = $json;
23
	}
24
25
	public function addToOutput( OutputFacade $output ) {
26
		$leafletPath = $GLOBALS['wgScriptPath'] . '/extensions/Maps/resources/leaflet/leaflet';
27
28
		$output->addHeadItem(
29
			'MapsGeoJsonHeadItem',
30
			Html::linkedStyle( "$leafletPath/leaflet.css" ) . Html::linkedScript( "$leafletPath/leaflet.js" )
31
		);
32
33
		$output->addHTML( $this->getJavascript() . $this->getHtml() );
34
		$output->addModules( 'ext.maps.leaflet.editor' );
35
	}
36
37
	private function getJavascript(): string {
38
		return Html::element(
39
			'script',
40
			[],
41
			$this->getJsonJs()
42
		);
43
	}
44
45
	private function getJsonJs(): string {
46
		return 'var GeoJson ='
47
			. ( $this->json ?? 'null' )
48
			. ';';
49
	}
50
51
	private function getHtml(): string {
52
		if ( $this->isExistingPage() ) {
53
			return $this->getMapHtml();
54
		}
55
56
		return $this->getNewPageHtml();
57
	}
58
59
	private function isExistingPage(): bool {
60
		return $this->json !== null;
61
	}
62
63
	private function getMapHtml(): string {
64
		return $this->wrapHtmlInThumbDivs(
65
			Html::rawElement(
66
				'div',
67
				[
68
					'id' => 'GeoJsonMap',
69
					'style' => 'width: 100%; height: 600px; background-color: #eeeeee; overflow: hidden;',
70
					'class' => 'maps-map maps-leaflet maps-geojson-editor'
71
				],
72
				Html::element(
73
					'div',
74
					[
75
						'class' => 'maps-loading-message'
76
					],
77
					wfMessage( 'maps-loading-map' )->inContentLanguage()->text()
78
				)
79
			)
80
		);
81
	}
82
83
	private function wrapHtmlInThumbDivs( string $html ): string {
84
		return Html::rawElement(
85
			'div',
86
			[
87
				'class' => 'thumb'
88
			],
89
			Html::rawElement(
90
				'div',
91
				[
92
					'class' => 'thumbinner'
93
				],
94
				$html
95
			)
96
		);
97
	}
98
99
	private function getNewPageHtml(): string {
100
		return
101
			Html::element(
102
				'button',
103
				[
104
					'id' => 'maps-geojson-new'
105
				],
106
				wfMessage( 'maps-geo-json-create-page-button' )->inContentLanguage()->text()
107
			)
108
			. Html::rawElement(
109
				'div',
110
				[
111
					'style' => 'display: none;',
112
					'id' => 'maps-geojson-map-wrapper'
113
				],
114
				$this->getMapHtml()
115
			);
116
	}
117
118
}
119