File: ButtonElement.php

package info (click to toggle)
mediawiki 1%3A1.39.13-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,416 kB
  • sloc: php: 815,516; javascript: 601,264; sql: 11,218; python: 4,863; xml: 3,080; sh: 990; ruby: 82; makefile: 78
file content (93 lines) | stat: -rw-r--r-- 2,188 bytes parent folder | download | duplicates (3)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

namespace OOUI;

/**
 * Element with a button.
 *
 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
 * and access keys for accessibility purposes.
 *
 * @abstract
 */
trait ButtonElement {

	/**
	 * Button is framed.
	 *
	 * @var bool
	 */
	protected $framed = false;

	/**
	 * @var Tag
	 */
	protected $button;

	/**
	 * @param array $config Configuration options
	 *      - bool $config['framed'] Render button with a frame (default: true)
	 */
	public function initializeButtonElement( array $config = [] ) {
		// Properties
		if ( !$this instanceof Element ) {
			throw new Exception( "ButtonElement trait can only be used on Element instances" );
		}
		$target = $config['button'] ?? new Tag( 'a' );
		$this->button = $target;

		// Initialization
		$this->addClasses( [ 'oo-ui-buttonElement' ] );
		$this->button->addClasses( [ 'oo-ui-buttonElement-button' ] );
		$this->toggleFramed( $config['framed'] ?? true );

		// Add `role="button"` on `<a>` elements, where it's needed
		if ( strtolower( $this->button->getTag() ) === 'a' ) {
			$this->button->setAttributes( [
				'role' => 'button',
			] );
		}

		$this->registerConfigCallback( function ( &$config ) {
			if ( $this->framed !== true ) {
				$config['framed'] = $this->framed;
			}
		} );
	}

	/**
	 * Toggle frame.
	 *
	 * @param bool|null $framed Make button framed, omit to toggle
	 * @return $this
	 */
	public function toggleFramed( $framed = null ) {
		$this->framed = $framed !== null ? (bool)$framed : !$this->framed;
		$this->toggleClasses( [ 'oo-ui-buttonElement-framed' ], $this->framed );
		$this->toggleClasses( [ 'oo-ui-buttonElement-frameless' ], !$this->framed );
		return $this;
	}

	/**
	 * Check if button has a frame.
	 *
	 * @return bool Button is framed
	 */
	public function isFramed() {
		return $this->framed;
	}

	/**
	 * Toggle CSS classes.
	 *
	 * @param array $classes List of classes to add
	 * @param bool|null $toggle Add classes
	 * @return $this
	 */
	abstract public function toggleClasses( array $classes, $toggle = null );

	/**
	 * @param callable $func
	 */
	abstract public function registerConfigCallback( callable $func );
}