summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/ModernTimeline/src/TimelinePresenter.php
blob: b0d006894163999137e868e2c30bf90a28265277 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

declare( strict_types = 1 );

namespace ModernTimeline;

use ModernTimeline\ResultFacade\ResultSimplifier;
use ModernTimeline\SlidePresenter\SimpleSlidePresenter;
use ModernTimeline\SlidePresenter\SlidePresenter;
use ModernTimeline\SlidePresenter\TemplateSlidePresenter;
use ParamProcessor\ProcessedParam;
use SMWOutputs;
use SMWQueryResult;

class TimelinePresenter {

	private $id;
	private $parameters;

	/**
	 * @param ProcessedParam[] $parameters
	 */
	public function __construct( array $parameters ) {
		$this->parameters = $parameters;
		$this->id = $this->newTimelineId();
	}

	private function newTimelineId(): string {
		static $timelineNumber = 0;
		return 'modern_timeline_' . ++$timelineNumber;
	}

	public function getResult( SMWQueryResult $result ): string {
		SMWOutputs::requireResource( 'ext.modern.timeline' );

		$json = $this->createJsonString( $result );

		SMWOutputs::requireHeadItem(
			$this->id,
			$this->createJs( $json )
		);

		return $this->createDiv( $json );
	}

	private function createJsonString( SMWQueryResult $result ) {
		$preJson = ( new JsonBuilder( $this->getSlidePresenter() ) )->buildTimelineJson(
			( new ResultSimplifier() )->newSubjectCollection( $result )
		);

		$preJson['options'] = TimelineOptions::processedParamsToJson( $this->parameters );

		return json_encode( $preJson );
	}

	private function getSlidePresenter(): SlidePresenter {
		if ( $this->getTemplateName() === '' ) {
			return new SimpleSlidePresenter();
		}

		return new TemplateSlidePresenter( $this->getTemplateName() );
	}

	private function getTemplateName(): string {
		return $this->parameters['template']->getValue();
	}

	private function createJs( string $json ): string {
		return \Html::rawElement(
			'script',
			[
				'type' => 'text/javascript'
			],
			"if (!window.hasOwnProperty('modernTimeline')) {window.modernTimeline = {};}"
			. "\n window.modernTimeline.{$this->id} = $json;"
		);
	}

	private function createDiv( string $json ): string {
		$width = $this->parameters[TimelineOptions::PARAM_WIDTH]->getValue();
		$height = $this->parameters[TimelineOptions::PARAM_HEIGHT]->getValue();

		return \Html::rawElement(
			'div',
			[
				'id' => $this->id,
				'style' => "width: $width; height: $height",
				'class' => 'modern_timeline_outer_div'
			],
			\Html::element(
				'div',
				[
					'class' => 'modern_timeline_inner_div',
					'style' => 'width: 100%; height: calc(100% - 10px); background-color: rgba(0, 0, 0, 0.05); margin-top: 5px; margin-bottom: 5px;'
				]
			)
		)
			. \Html::element( // TODO: remove when system tests can test head items
				'div',
				[ 'style' => 'display:none' ],
				$json
			);
	}

}