]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - vendor/oojs/oojs-ui/php/mixins/ButtonElement.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / mixins / ButtonElement.php
diff --git a/vendor/oojs/oojs-ui/php/mixins/ButtonElement.php b/vendor/oojs/oojs-ui/php/mixins/ButtonElement.php
new file mode 100644 (file)
index 0000000..eaf0651
--- /dev/null
@@ -0,0 +1,79 @@
+<?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 boolean
+        */
+       protected $framed = false;
+
+       /**
+        * @var Tag
+        */
+       protected $button;
+
+       /**
+        * @param array $config Configuration options
+        * @param 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 = isset( $config['button'] ) ? $config['button'] : new Tag( 'a' );
+               $this->button = $target;
+
+               // Initialization
+               $this->addClasses( [ 'oo-ui-buttonElement' ] );
+               $this->button->addClasses( [ 'oo-ui-buttonElement-button' ] );
+               $this->toggleFramed( isset( $config['framed'] ) ? $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 $framed Make button framed, omit to toggle
+        * @return $this
+        */
+       public function toggleFramed( $framed = null ) {
+               $this->framed = $framed !== null ? !!$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;
+       }
+}