summaryrefslogtreecommitdiff
path: root/www/wiki/includes/widget/DateTimeInputWidget.php
blob: 21e3d7930daf390f5d07d83a40ed13339a0976df (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
<?php

namespace MediaWiki\Widget;

use OOUI\Tag;

/**
 * Date-time input widget.
 *
 * @copyright 2016 MediaWiki Widgets Team and others; see AUTHORS.txt
 * @license MIT
 */
class DateTimeInputWidget extends \OOUI\InputWidget {

	protected $type = null;
	protected $min = null;
	protected $max = null;
	protected $clearable = null;

	/**
	 * @param array $config Configuration options
	 *   - string $config['type'] 'date', 'time', or 'datetime'
	 *   - string $config['min'] Minimum date, time, or datetime
	 *   - string $config['max'] Maximum date, time, or datetime
	 *   - bool $config['clearable'] Whether to provide for blanking the value.
	 */
	public function __construct( array $config = [] ) {
		// We need $this->type set before calling the parent constructor
		if ( isset( $config['type'] ) ) {
			$this->type = $config['type'];
		} else {
			throw new \InvalidArgumentException( '$config[\'type\'] must be specified' );
		}

		parent::__construct( $config );

		// Properties, which are ignored in PHP and just shipped back to JS
		if ( isset( $config['min'] ) ) {
			$this->min = $config['min'];
		}
		if ( isset( $config['max'] ) ) {
			$this->max = $config['max'];
		}
		if ( isset( $config['clearable'] ) ) {
			$this->clearable = $config['clearable'];
		}

		// Initialization
		$this->addClasses( [ 'mw-widgets-datetime-dateTimeInputWidget' ] );
	}

	protected function getJavaScriptClassName() {
		return 'mw.widgets.datetime.DateTimeInputWidget';
	}

	public function getConfig( &$config ) {
		$config['type'] = $this->type;
		if ( $this->min !== null ) {
			$config['min'] = $this->min;
		}
		if ( $this->max !== null ) {
			$config['max'] = $this->max;
		}
		if ( $this->clearable !== null ) {
			$config['clearable'] = $this->clearable;
		}
		return parent::getConfig( $config );
	}

	protected function getInputElement( $config ) {
		return ( new Tag( 'input' ) )->setAttributes( [ 'type' => $this->type ] );
	}
}