Introduce TagMultiselectWidget.php
authorThalia <thalia.e.chan@googlemail.com>
Wed, 12 Dec 2018 15:57:57 +0000 (15:57 +0000)
committerThalia <thalia.e.chan@googlemail.com>
Fri, 14 Dec 2018 15:18:11 +0000 (15:18 +0000)
TitlesMultiselectWidget and UsersMultiselectWidget share
a lot of functionality, so implement a common base class.

This also adds some things to UsersMultiselectWidget:
* shows a pending element to users with JavaScript
* makes the input configurable

Change-Id: Ie6649b476c64e795254f457e3863fa7f14aa05ac

autoload.php
includes/specials/SpecialBlock.php
includes/specials/SpecialPreferences.php
includes/widget/TagMultiselectWidget.php [new file with mode: 0644]
includes/widget/TitlesMultiselectWidget.php
includes/widget/UsersMultiselectWidget.php
resources/Resources.php
resources/src/mediawiki.widgets/mw.widgets.TagMultiselectWidget.base.css [new file with mode: 0644]
resources/src/mediawiki.widgets/mw.widgets.TitlesMultiselectWidget.base.css [deleted file]

index c411948..4dcc20e 100644 (file)
@@ -940,6 +940,7 @@ $wgAutoloadLocalClasses = [
        'MediaWiki\\Widget\\Search\\SimpleSearchResultWidget' => __DIR__ . '/includes/widget/search/SimpleSearchResultWidget.php',
        'MediaWiki\\Widget\\SelectWithInputWidget' => __DIR__ . '/includes/widget/SelectWithInputWidget.php',
        'MediaWiki\\Widget\\SizeFilterWidget' => __DIR__ . '/includes/widget/SizeFilterWidget.php',
+       'MediaWiki\\Widget\\TagMultiselectWidget' => __DIR__ . '/includes/widget/TagMultiselectWidget.php',
        'MediaWiki\\Widget\\TitleInputWidget' => __DIR__ . '/includes/widget/TitleInputWidget.php',
        'MediaWiki\\Widget\\TitlesMultiselectWidget' => __DIR__ . '/includes/widget/TitlesMultiselectWidget.php',
        'MediaWiki\\Widget\\UserInputWidget' => __DIR__ . '/includes/widget/UserInputWidget.php',
index 78b8f8a..8bc61d8 100644 (file)
@@ -394,7 +394,7 @@ class SpecialBlock extends FormSpecialPage {
         * @return string
         */
        protected function preText() {
-               $this->getOutput()->addModuleStyles( 'mediawiki.widgets.TitlesMultiselectWidget.styles' );
+               $this->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
                $this->getOutput()->addModules( [ 'mediawiki.special.block' ] );
 
                $blockCIDRLimit = $this->getConfig()->get( 'BlockCIDRLimit' );
index 04be22b..cc7ed55 100644 (file)
@@ -53,7 +53,10 @@ class SpecialPreferences extends SpecialPage {
                }
 
                $out->addModules( 'mediawiki.special.preferences.ooui' );
-               $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
+               $out->addModuleStyles( [
+                       'mediawiki.special.preferences.styles.ooui',
+                       'mediawiki.widgets.TagMultiselectWidget.styles',
+               ] );
                $out->addModuleStyles( 'oojs-ui-widgets.styles' );
 
                $session = $this->getRequest()->getSession();
diff --git a/includes/widget/TagMultiselectWidget.php b/includes/widget/TagMultiselectWidget.php
new file mode 100644 (file)
index 0000000..43e184c
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+
+namespace MediaWiki\Widget;
+
+use OOUI\MultilineTextInputWidget;
+
+/**
+ * Abstract base class for widgets to select multiple users, titles,
+ * namespaces, etc.
+ *
+ * @copyright 2017 MediaWiki Widgets Team and others; see AUTHORS.txt
+ * @license MIT
+ */
+abstract class TagMultiselectWidget extends \OOUI\Widget {
+
+       protected $selectedArray = [];
+       protected $inputName = null;
+       protected $inputPlaceholder = null;
+       protected $tagLimit = null;
+
+       /**
+        * @param array $config Configuration options
+        *   - array $config['default'] Array of items to use as preset data
+        *   - array $config['name'] Name attribute (used in forms)
+        *   - array $config['placeholder'] Placeholder message for input
+        *   - array $config['input'] Config options for the input widget
+        *   - number $config['tagLimit'] Maximum number of selected items
+        */
+       public function __construct( array $config = [] ) {
+               parent::__construct( $config );
+
+               // Properties
+               if ( isset( $config['default'] ) ) {
+                       $this->selectedArray = $config['default'];
+               }
+               if ( isset( $config['name'] ) ) {
+                       $this->inputName = $config['name'];
+               }
+               if ( isset( $config['placeholder'] ) ) {
+                       $this->inputPlaceholder = $config['placeholder'];
+               }
+               if ( isset( $config['input'] ) ) {
+                       $this->input = $config['input'];
+               } else {
+                       $this->input = [];
+               }
+               if ( isset( $config['tagLimit'] ) ) {
+                       $this->tagLimit = $config['tagLimit'];
+               }
+
+               $textarea = new MultilineTextInputWidget( array_merge( [
+                       'name' => $this->inputName,
+                       'value' => implode( "\n", $this->selectedArray ),
+                       'rows' => 10,
+                       'classes' => [
+                               'mw-widgets-tagMultiselectWidget-multilineTextInputWidget'
+                       ],
+               ], $this->input ) );
+
+               $pending = new PendingTextInputWidget();
+
+               $this->appendContent( $textarea, $pending );
+               $this->addClasses( [ 'mw-widgets-tagMultiselectWidget' ] );
+       }
+
+       public function getConfig( &$config ) {
+               if ( $this->selectedArray !== null ) {
+                       $config['selected'] = $this->selectedArray;
+               }
+               if ( $this->inputName !== null ) {
+                       $config['name'] = $this->inputName;
+               }
+               if ( $this->inputPlaceholder !== null ) {
+                       $config['placeholder'] = $this->inputPlaceholder;
+               }
+               if ( $this->input !== null ) {
+                       $config['input'] = $this->input;
+               }
+               if ( $this->tagLimit !== null ) {
+                       $config['tagLimit'] = $this->tagLimit;
+               }
+
+               $config['$overlay'] = true;
+               return parent::getConfig( $config );
+       }
+
+}
index f62ee83..3246e7d 100644 (file)
@@ -2,66 +2,28 @@
 
 namespace MediaWiki\Widget;
 
-use OOUI\MultilineTextInputWidget;
-
 /**
  * Widget to select multiple titles.
  *
  * @copyright 2017 MediaWiki Widgets Team and others; see AUTHORS.txt
  * @license MIT
  */
-class TitlesMultiselectWidget extends \OOUI\Widget {
+class TitlesMultiselectWidget extends TagMultiselectWidget {
 
-       protected $titlesArray = [];
-       protected $inputName = null;
-       protected $inputPlaceholder = null;
-       protected $tagLimit = null;
        protected $showMissing = null;
 
        /**
         * @param array $config Configuration options
-        *   - array $config['default'] Array of titles to use as preset data
-        *   - array $config['placeholder'] Placeholder message for input
-        *   - array $config['name'] Name attribute (used in forms)
-        *   - number $config['tagLimit'] Maximum number of selected titles
         *   - bool $config['showMissing'] Show missing pages
-        *   - array $config['input'] Config options for the input widget
         */
        public function __construct( array $config = [] ) {
                parent::__construct( $config );
 
                // Properties
-               if ( isset( $config['default'] ) ) {
-                       $this->titlesArray = $config['default'];
-               }
-               if ( isset( $config['name'] ) ) {
-                       $this->inputName = $config['name'];
-               }
-               if ( isset( $config['placeholder'] ) ) {
-                       $this->inputPlaceholder = $config['placeholder'];
-               }
-               if ( isset( $config['tagLimit'] ) ) {
-                       $this->tagLimit = $config['tagLimit'];
-               }
                if ( isset( $config['showMissing'] ) ) {
                        $this->showMissing = $config['showMissing'];
                }
-               if ( isset( $config['input'] ) ) {
-                       $this->input = $config['input'];
-               }
-
-               $textarea = new MultilineTextInputWidget( array_merge( [
-                       'name' => $this->inputName,
-                       'value' => implode( "\n", $this->titlesArray ),
-                       'rows' => 10,
-                       'classes' => [
-                               'mw-widgets-titlesMultiselectWidget-multilineTextInputWidget'
-                       ],
-               ], $this->input ) );
 
-               $pending = new PendingTextInputWidget();
-
-               $this->appendContent( $textarea, $pending );
                $this->addClasses( [ 'mw-widgets-titlesMultiselectWidget' ] );
        }
 
@@ -70,26 +32,10 @@ class TitlesMultiselectWidget extends \OOUI\Widget {
        }
 
        public function getConfig( &$config ) {
-               if ( $this->titlesArray !== null ) {
-                       $config['selected'] = $this->titlesArray;
-               }
-               if ( $this->inputName !== null ) {
-                       $config['name'] = $this->inputName;
-               }
-               if ( $this->inputPlaceholder !== null ) {
-                       $config['placeholder'] = $this->inputPlaceholder;
-               }
-               if ( $this->tagLimit !== null ) {
-                       $config['tagLimit'] = $this->tagLimit;
-               }
                if ( $this->showMissing !== null ) {
                        $config['showMissing'] = $this->showMissing;
                }
-               if ( $this->input !== null ) {
-                       $config['input'] = $this->input;
-               }
 
-               $config['$overlay'] = true;
                return parent::getConfig( $config );
        }
 
index aaa46ae..066a2f3 100644 (file)
@@ -2,46 +2,19 @@
 
 namespace MediaWiki\Widget;
 
-use OOUI\MultilineTextInputWidget;
-
 /**
  * Widget to select multiple users.
  *
  * @copyright 2017 MediaWiki Widgets Team and others; see AUTHORS.txt
  * @license MIT
  */
-class UsersMultiselectWidget extends \OOUI\Widget {
-
-       protected $usersArray = [];
-       protected $inputName = null;
-       protected $inputPlaceholder = null;
+class UsersMultiselectWidget extends TagMultiselectWidget {
 
        /**
         * @param array $config Configuration options
-        *   - array $config['users'] Array of usernames to use as preset data
-        *   - array $config['placeholder'] Placeholder message for input
-        *   - array $config['name'] Name attribute (used in forms)
         */
        public function __construct( array $config = [] ) {
                parent::__construct( $config );
-
-               // Properties
-               if ( isset( $config['default'] ) ) {
-                       $this->usersArray = $config['default'];
-               }
-               if ( isset( $config['name'] ) ) {
-                       $this->inputName = $config['name'];
-               }
-               if ( isset( $config['placeholder'] ) ) {
-                       $this->inputPlaceholder = $config['placeholder'];
-               }
-
-               $textarea = new MultilineTextInputWidget( [
-                       'name' => $this->inputName,
-                       'value' => implode( "\n", $this->usersArray ),
-                       'rows' => 10,
-               ] );
-               $this->prependContent( $textarea );
        }
 
        protected function getJavaScriptClassName() {
@@ -49,17 +22,6 @@ class UsersMultiselectWidget extends \OOUI\Widget {
        }
 
        public function getConfig( &$config ) {
-               if ( $this->usersArray !== null ) {
-                       $config['selected'] = $this->usersArray;
-               }
-               if ( $this->inputName !== null ) {
-                       $config['name'] = $this->inputName;
-               }
-               if ( $this->inputPlaceholder !== null ) {
-                       $config['placeholder'] = $this->inputPlaceholder;
-               }
-
-               $config['$overlay'] = true;
                return parent::getConfig( $config );
        }
 
index 317385d..ef8d974 100644 (file)
@@ -2735,8 +2735,8 @@ return [
                ],
                'targets' => [ 'desktop', 'mobile' ],
        ],
-       'mediawiki.widgets.TitlesMultiselectWidget.styles' => [
-               'styles' => 'resources/src/mediawiki.widgets/mw.widgets.TitlesMultiselectWidget.base.css',
+       'mediawiki.widgets.TagMultiselectWidget.styles' => [
+               'styles' => 'resources/src/mediawiki.widgets/mw.widgets.TagMultiselectWidget.base.css',
        ],
        'mediawiki.widgets.SearchInputWidget' => [
                'scripts' => [
diff --git a/resources/src/mediawiki.widgets/mw.widgets.TagMultiselectWidget.base.css b/resources/src/mediawiki.widgets/mw.widgets.TagMultiselectWidget.base.css
new file mode 100644 (file)
index 0000000..86e7ecd
--- /dev/null
@@ -0,0 +1,11 @@
+/*!
+ * MediaWiki Widgets - base TagMultiselectWidget styles.
+ *
+ * @copyright 2011-2018 MediaWiki Widgets Team and others; see AUTHORS.txt
+ * @license The MIT License (MIT); see LICENSE.txt
+ */
+
+.client-nojs .mw-widgets-tagMultiselectWidget .mw-widgets-pendingTextInputWidget,
+.client-js .mw-widgets-tagMultiselectWidget .mw-widgets-tagMultiselectWidget-multilineTextInputWidget {
+       display: none;
+}
diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitlesMultiselectWidget.base.css b/resources/src/mediawiki.widgets/mw.widgets.TitlesMultiselectWidget.base.css
deleted file mode 100644 (file)
index de43fb3..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-/*!
- * MediaWiki Widgets - base TitlesMultiselectWidget styles.
- *
- * @copyright 2011-2018 MediaWiki Widgets Team and others; see AUTHORS.txt
- * @license The MIT License (MIT); see LICENSE.txt
- */
-
-.client-nojs .mw-widgets-titlesMultiselectWidget .mw-widgets-pendingTextInputWidget,
-.client-js .mw-widgets-titlesMultiselectWidget .mw-widgets-titlesMultiselectWidget-multilineTextInputWidget {
-       display: none;
-}