| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Generic widget for buttons. |
| 7 |
*/ |
| 8 |
class SelectFileInputWidget extends InputWidget { |
| 9 |
use RequiredElement; |
| 10 |
|
| 11 |
/* Static Properties */ |
| 12 |
|
| 13 |
/** @var string[]|null */ |
| 14 |
protected $accept; |
| 15 |
/** @var bool */ |
| 16 |
protected $multiple; |
| 17 |
/** @var string|null */ |
| 18 |
protected $placeholder; |
| 19 |
/** @var array|null */ |
| 20 |
protected $button; |
| 21 |
/** @var string|null */ |
| 22 |
protected $icon; |
| 23 |
/** @var bool */ |
| 24 |
protected $droppable; |
| 25 |
/** @var bool */ |
| 26 |
protected $buttonOnly; |
| 27 |
/** @var bool */ |
| 28 |
protected $showDropTarget; |
| 29 |
/** @var float */ |
| 30 |
protected $thumbnailSizeLimit; |
| 31 |
|
| 32 |
/** |
| 33 |
* @param array $config Configuration options |
| 34 |
* - string[]|null $config['accept'] MIME types to accept. null accepts all types. |
| 35 |
* (default: null) |
| 36 |
* - bool $config['multiple'] Allow multiple files to be selected. |
| 37 |
* - string $config['placeholder'] Text to display when no file is selected. |
| 38 |
* - array $config['button'] Config to pass to select file button. |
| 39 |
* - string $config['icon'] Icon to show next to file info |
| 40 |
* and show a preview (for performance). |
| 41 |
* - boolean $config['droppable'] Whether to accept files by drag and drop. (default: true) |
| 42 |
* - boolean $config['buttonOnly'] Show only the select file button, no info field. Requires |
| 43 |
* showDropTarget to be false. |
| 44 |
* - boolean $config['showDropTarget'] Whether to show a drop target. Requires droppable to be |
| 45 |
* true. |
| 46 |
* - number $config['thumbnailSizeLimit'] File size limit in MiB above which to not try and show a |
| 47 |
* preview (for performance). (default: 20) |
| 48 |
*/ |
| 49 |
public function __construct( array $config = [] ) { |
| 50 |
// Parent constructor |
| 51 |
parent::__construct( $config ); |
| 52 |
|
| 53 |
// Properties |
| 54 |
$this->accept = $config['accept'] ?? null; |
| 55 |
$this->multiple = $config['multiple'] ?? false; |
| 56 |
$this->placeholder = $config['placeholder'] ?? null; |
| 57 |
$this->button = $config['button'] ?? null; |
| 58 |
$this->icon = $config['icon'] ?? null; |
| 59 |
$this->droppable = $config['droppable'] ?? true; |
| 60 |
$this->buttonOnly = $config['buttonOnly'] ?? false; |
| 61 |
$this->showDropTarget = $config['showDropTarget'] ?? false; |
| 62 |
$this->thumbnailSizeLimit = $config['thumbnailSizeLimit'] ?? 20; |
| 63 |
|
| 64 |
// Traits |
| 65 |
$this->initializeRequiredElement( |
| 66 |
array_merge( [ 'indicatorElement' => null ], $config ) |
| 67 |
); |
| 68 |
|
| 69 |
// Initialization |
| 70 |
$this->addClasses( [ 'oo-ui-selectFileInputWidget' ] ); |
| 71 |
$this->input->setAttributes( [ |
| 72 |
'type' => 'file' |
| 73 |
] ); |
| 74 |
if ( $this->multiple ) { |
| 75 |
$this->input->setAttributes( [ |
| 76 |
'multiple' => '' |
| 77 |
] ); |
| 78 |
} |
| 79 |
if ( $this->accept ) { |
| 80 |
$this->input->setAttributes( [ |
| 81 |
'accept' => implode( ',', $this->accept ) |
| 82 |
] ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** @inheritDoc */ |
| 87 |
public function getConfig( &$config ) { |
| 88 |
if ( $this->accept !== null ) { |
| 89 |
$config['accept'] = $this->accept; |
| 90 |
} |
| 91 |
if ( $this->multiple !== null ) { |
| 92 |
$config['multiple'] = $this->multiple; |
| 93 |
} |
| 94 |
if ( $this->placeholder !== null ) { |
| 95 |
$config['placeholder'] = $this->placeholder; |
| 96 |
} |
| 97 |
if ( $this->button !== null ) { |
| 98 |
$config['button'] = $this->button; |
| 99 |
} |
| 100 |
if ( $this->icon !== null ) { |
| 101 |
$config['icon'] = $this->icon; |
| 102 |
} |
| 103 |
if ( $this->droppable !== null ) { |
| 104 |
$config['droppable'] = $this->droppable; |
| 105 |
} |
| 106 |
if ( $this->buttonOnly !== null ) { |
| 107 |
$config['buttonOnly'] = $this->buttonOnly; |
| 108 |
} |
| 109 |
if ( $this->showDropTarget !== null ) { |
| 110 |
$config['showDropTarget'] = $this->showDropTarget; |
| 111 |
} |
| 112 |
if ( $this->thumbnailSizeLimit !== null ) { |
| 113 |
$config['thumbnailSizeLimit'] = $this->thumbnailSizeLimit; |
| 114 |
} |
| 115 |
return parent::getConfig( $config ); |
| 116 |
} |
| 117 |
} |