SearchInputWidget.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace MediaWiki\Widget;
  3. /**
  4. * Search input widget.
  5. *
  6. * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
  7. * @license MIT
  8. */
  9. class SearchInputWidget extends TitleInputWidget {
  10. protected $performSearchOnClick = true;
  11. protected $validateTitle = false;
  12. protected $highlightFirst = false;
  13. protected $dataLocation = 'header';
  14. protected $showDescriptions = false;
  15. /**
  16. * @param array $config Configuration options
  17. * - bool|null $config['performSearchOnClick'] If true, the script will start a search
  18. * whenever a user hits a suggestion. If false, the text of the suggestion is inserted into
  19. * the text field only (default: true)
  20. * - string $config['dataLocation'] Where the search input field will be
  21. * used (header or content, default: header)
  22. */
  23. public function __construct( array $config = [] ) {
  24. $config = array_merge( [
  25. 'maxLength' => null,
  26. 'icon' => 'search',
  27. ], $config );
  28. parent::__construct( $config );
  29. // Properties, which are ignored in PHP and just shipped back to JS
  30. if ( isset( $config['performSearchOnClick'] ) ) {
  31. $this->performSearchOnClick = $config['performSearchOnClick'];
  32. }
  33. if ( isset( $config['dataLocation'] ) ) {
  34. // identifies the location of the search bar for tracking purposes
  35. $this->dataLocation = $config['dataLocation'];
  36. }
  37. if ( !empty( $config['showDescriptions'] ) ) {
  38. $this->showDescriptions = true;
  39. }
  40. // Initialization
  41. $this->addClasses( [ 'mw-widget-searchInputWidget' ] );
  42. }
  43. protected function getInputElement( $config ) {
  44. return ( new \OOUI\Tag( 'input' ) )->setAttributes( [ 'type' => 'search' ] );
  45. }
  46. protected function getJavaScriptClassName() {
  47. return 'mw.widgets.SearchInputWidget';
  48. }
  49. public function getConfig( &$config ) {
  50. $config['performSearchOnClick'] = $this->performSearchOnClick;
  51. if ( $this->dataLocation ) {
  52. $config['dataLocation'] = $this->dataLocation;
  53. }
  54. if ( $this->showDescriptions ) {
  55. $config['showDescriptions'] = true;
  56. }
  57. $config['$overlay'] = true;
  58. return parent::getConfig( $config );
  59. }
  60. }