DateTimeInputWidget.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace MediaWiki\Widget;
  3. use OOUI\Tag;
  4. /**
  5. * Date-time input widget.
  6. *
  7. * @copyright 2016 MediaWiki Widgets Team and others; see AUTHORS.txt
  8. * @license MIT
  9. */
  10. class DateTimeInputWidget extends \OOUI\InputWidget {
  11. protected $type = null;
  12. protected $min = null;
  13. protected $max = null;
  14. protected $clearable = null;
  15. /**
  16. * @param array $config Configuration options
  17. * - string $config['type'] 'date', 'time', or 'datetime'
  18. * - string $config['min'] Minimum date, time, or datetime
  19. * - string $config['max'] Maximum date, time, or datetime
  20. * - bool $config['clearable'] Whether to provide for blanking the value.
  21. */
  22. public function __construct( array $config = [] ) {
  23. // We need $this->type set before calling the parent constructor
  24. if ( isset( $config['type'] ) ) {
  25. $this->type = $config['type'];
  26. } else {
  27. throw new \InvalidArgumentException( '$config[\'type\'] must be specified' );
  28. }
  29. parent::__construct( $config );
  30. // Properties, which are ignored in PHP and just shipped back to JS
  31. if ( isset( $config['min'] ) ) {
  32. $this->min = $config['min'];
  33. }
  34. if ( isset( $config['max'] ) ) {
  35. $this->max = $config['max'];
  36. }
  37. if ( isset( $config['clearable'] ) ) {
  38. $this->clearable = $config['clearable'];
  39. }
  40. // Initialization
  41. $this->addClasses( [ 'mw-widgets-datetime-dateTimeInputWidget' ] );
  42. }
  43. protected function getJavaScriptClassName() {
  44. return 'mw.widgets.datetime.DateTimeInputWidget';
  45. }
  46. public function getConfig( &$config ) {
  47. $config['type'] = $this->type;
  48. if ( $this->min !== null ) {
  49. $config['min'] = $this->min;
  50. }
  51. if ( $this->max !== null ) {
  52. $config['max'] = $this->max;
  53. }
  54. if ( $this->clearable !== null ) {
  55. $config['clearable'] = $this->clearable;
  56. }
  57. return parent::getConfig( $config );
  58. }
  59. protected function getInputElement( $config ) {
  60. return ( new Tag( 'input' ) )->setAttributes( [ 'type' => $this->type ] );
  61. }
  62. }