aboutsummaryrefslogtreecommitdiffstats
path: root/includes/widget/SpinnerWidget.php
blob: 71f26f7521afd3c697eeaeb70609da349fa86f14 (plain) (blame)
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
<?php

namespace MediaWiki\Widget;

/**
 * PHP version of jquery.spinner.
 *
 * If used with jquery.spinner.styles, can be used to show a
 * spinner before JavaScript has loaded.
 *
 * @copyright 2011-2020 MediaWiki Widgets Team and others; see AUTHORS.txt
 * @license MIT
 */
class SpinnerWidget {

	private $attributes;
	private $content;

	/**
	 * @param array $config Configuration options
	 */
	public function __construct( array $config = [] ) {
		$size = $config['size'] ?? 'small';
		$type = $config['type'] ?? 'inline';

		$this->attributes = [];

		if ( isset( $config['id'] ) ) {
			$this->attributes['id'] = $config['id'];
		}

		// Initialization
		$this->attributes['class'] = [
			'mw-spinner',
			$size === 'small' ? 'mw-spinner-small' : 'mw-spinner-large',
			$type === 'inline' ? 'mw-spinner-inline' : 'mw-spinner-block',
		];

		$this->content =
			'<div class="mw-spinner-container">' .
				str_repeat( '<div></div>', 12 ) .
			'</div>';
	}

	/**
	 * Render element into HTML.
	 * @return string HTML serialization
	 */
	public function toString() {
		return \MediaWiki\Html\Html::rawElement( 'div', $this->attributes, $this->content );
	}

	/**
	 * Magic method implementation.
	 *
	 * Copied from OOUI\Tag
	 *
	 * @return string
	 */
	public function __toString() {
		try {
			return $this->toString();
		} catch ( \Exception $ex ) {
			trigger_error( (string)$ex, E_USER_ERROR );
		}
	}
}