summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php
blob: 72a89b04e9134a0b084f7676cf97f0d103ae61ba (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
<?php

namespace Maps\MediaWiki\Content;

use Html;
use ParserOptions;
use ParserOutput;
use Title;

class GeoJsonContent extends \JsonContent {

	public const CONTENT_MODEL_ID = 'GeoJSON';

	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
		parent::__construct( $text, $modelId );
	}

	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
		$generateHtml, ParserOutput &$output ) {

		if ( $generateHtml && $this->isValid() ) {
			$output->setText( $this->getMapHtml( $this->beautifyJSON() ) );
			$output->addModules( 'ext.maps.leaflet.editor' );
		} else {
			$output->setText( '' );
		}
	}

	private function getMapHtml( string $jsonString ): string {
		return
			Html::element(
				'div',
				[
					'id' => 'GeoJsonMap',
					'class' => 'GeoJsonMap',
				]
			)
			. '<style>'
			. '.GeoJsonMap {width: "100%"; height: 600px; display: "inline-block"}'
			. '</style>'
			.
			Html::element(
				'script',
				[],
				'var GeoJson =' . $jsonString . ';'
			);
	}

}