6 * Generic widget for buttons.
8 class ButtonWidget extends Widget {
15 use TabIndexedElement;
16 use AccessKeyedElement;
18 /* Static Properties */
20 public static $tagName = 'span';
25 * Whether button is active.
29 protected $active = false;
32 * Hyperlink to visit when clicked.
36 protected $href = null;
39 * Target to open hyperlink in.
43 protected $target = null;
46 * Search engine traversal hint.
48 * True if search engines should avoid following this hyperlink.
52 protected $noFollow = true;
55 * @param array $config Configuration options
56 * @param bool $config['active'] Whether button should be shown as active (default: false)
57 * @param string $config['href'] Hyperlink to visit when clicked
58 * @param string $config['target'] Target to open hyperlink in
59 * @param bool $config['noFollow'] Search engine traversal hint (default: true)
61 public function __construct( array $config = [] ) {
63 parent::__construct( $config );
66 $this->initializeButtonElement( $config );
67 $this->initializeIconElement( $config );
68 $this->initializeIndicatorElement( $config );
69 $this->initializeLabelElement( $config );
70 $this->initializeTitledElement(
71 array_merge( $config, [ 'titled' => $this->button ] ) );
72 $this->initializeFlaggedElement( $config );
73 $this->initializeTabIndexedElement(
74 array_merge( $config, [ 'tabIndexed' => $this->button ] ) );
75 $this->initializeAccessKeyedElement(
76 array_merge( $config, [ 'accessKeyed' => $this->button ] ) );
79 $this->button->appendContent( $this->icon, $this->label, $this->indicator );
81 ->addClasses( [ 'oo-ui-buttonWidget' ] )
82 ->appendContent( $this->button );
84 $this->setActive( isset( $config['active'] ) ? $config['active'] : false );
85 $this->setHref( isset( $config['href'] ) ? $config['href'] : null );
86 $this->setTarget( isset( $config['target'] ) ? $config['target'] : null );
87 $this->setNoFollow( isset( $config['noFollow'] ) ? $config['noFollow'] : true );
91 * Get hyperlink location.
93 * @return string Hyperlink location
95 public function getHref() {
100 * Get hyperlink target.
102 * @return string Hyperlink target
104 public function getTarget() {
105 return $this->target;
109 * Get search engine traversal hint.
111 * @return bool Whether search engines should avoid traversing this hyperlink
113 public function getNoFollow() {
114 return $this->noFollow;
118 * Set hyperlink location.
120 * @param string|null $href Hyperlink location, null to remove
123 public function setHref( $href ) {
124 $this->href = is_string( $href ) ? $href : null;
132 * Update the href attribute, in case of changes to href or disabled
137 public function updateHref() {
138 if ( $this->href !== null && !$this->isDisabled() ) {
139 $this->button->setAttributes( [ 'href' => $this->href ] );
141 $this->button->removeAttributes( [ 'href' ] );
147 * Set hyperlink target.
149 * @param string|null $target Hyperlink target, null to remove
152 public function setTarget( $target ) {
153 $this->target = is_string( $target ) ? $target : null;
155 if ( $this->target !== null ) {
156 $this->button->setAttributes( [ 'target' => $target ] );
158 $this->button->removeAttributes( [ 'target' ] );
165 * Set search engine traversal hint.
167 * @param bool $noFollow True if search engines should avoid traversing this hyperlink
170 public function setNoFollow( $noFollow ) {
171 $this->noFollow = is_bool( $noFollow ) ? $noFollow : true;
173 if ( $this->noFollow ) {
174 $this->button->setAttributes( [ 'rel' => 'nofollow' ] );
176 $this->button->removeAttributes( [ 'rel' ] );
183 * Toggle active state.
185 * A button should be marked as active when clicking it would only refresh the page.
187 * @param bool $active Make button active
190 public function setActive( $active = null ) {
191 $this->active = !!$active;
192 $this->toggleClasses( [ 'oo-ui-buttonElement-active' ], $this->active );
197 * Check if button is active.
199 * @return bool Button is active
201 public function isActive() {
202 return $this->active;
205 public function getConfig( &$config ) {
206 if ( $this->active !== false ) {
207 $config['active'] = $this->active;
209 if ( $this->href !== null ) {
210 $config['href'] = $this->href;
212 if ( $this->target !== null ) {
213 $config['target'] = $this->target;
215 if ( $this->noFollow !== true ) {
216 $config['noFollow'] = $this->noFollow;
218 return parent::getConfig( $config );