Create mediawiki.widgets and mw.widgets.TitleInputWidget in it
authorJames D. Forrester <jforrester@wikimedia.org>
Fri, 24 Apr 2015 16:33:41 +0000 (09:33 -0700)
committerBartosz Dziewoński <matma.rex@gmail.com>
Sun, 31 May 2015 18:01:06 +0000 (20:01 +0200)
Change-Id: I43fb412ff65271fd411b14352784cb92f1f5487b

.jshintrc
resources/Resources.php
resources/src/mediawiki.widgets/AUTHORS.txt [new file with mode: 0644]
resources/src/mediawiki.widgets/LICENSE.txt [new file with mode: 0644]
resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.css [new file with mode: 0644]
resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js [new file with mode: 0644]
resources/src/mediawiki.widgets/mw.widgets.js [new file with mode: 0644]

index 4bb2440..d72c31d 100644 (file)
--- a/.jshintrc
+++ b/.jshintrc
@@ -21,6 +21,7 @@
        "globals": {
                "mediaWiki": true,
                "JSON": true,
+               "OO": true,
                "jQuery": false,
                "QUnit": false,
                "sinon": false
index 3943aae..362ee37 100644 (file)
@@ -1700,6 +1700,23 @@ return array(
                'targets' => array( 'desktop', 'mobile' ),
        ),
 
+       'mediawiki.widgets' => array(
+               'scripts' => array(
+                       'resources/src/mediawiki.widgets/mw.widgets.js',
+                       'resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js',
+               ),
+               'skinStyles' => array(
+                       'default' => 'resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.css',
+               ),
+               'dependencies' => array(
+                       'oojs-ui',
+               ),
+               'messages' => array(
+                       // …
+               ),
+               'targets' => array( 'desktop', 'mobile' ),
+       ),
+
        /* es5-shim */
        'es5-shim' => array(
                'scripts' => array(
diff --git a/resources/src/mediawiki.widgets/AUTHORS.txt b/resources/src/mediawiki.widgets/AUTHORS.txt
new file mode 100644 (file)
index 0000000..10064b2
--- /dev/null
@@ -0,0 +1,10 @@
+Authors (alphabetically)
+
+Alex Monk <krenair@wikimedia.org>
+Bartosz Dziewoński <bdziewonski@wikimedia.org>
+Ed Sanders <esanders@wikimedia.org>
+James D. Forrester <jforrester@wikimedia.org>
+Roan Kattouw <roan@wikimedia.org>
+Sucheta Ghoshal <sghoshal@wikimedia.org>
+Timo Tijhof <timo@wikimedia.org>
+Trevor Parscal <trevor@wikimedia.org>
diff --git a/resources/src/mediawiki.widgets/LICENSE.txt b/resources/src/mediawiki.widgets/LICENSE.txt
new file mode 100644 (file)
index 0000000..b03ca80
--- /dev/null
@@ -0,0 +1,25 @@
+Copyright (c) 2011-2015 MediaWiki Widgets Team and others under the
+terms of The MIT License (MIT), as follows:
+
+This software consists of voluntary contributions made by many
+individuals (AUTHORS.txt) For exact contribution history, see the
+revision history and logs, available at https://gerrit.wikimedia.org
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.css b/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.css
new file mode 100644 (file)
index 0000000..0065f70
--- /dev/null
@@ -0,0 +1,10 @@
+/*!
+ * MediaWiki Widgets – TitleInputWidget styles.
+ *
+ * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
+ * @license The MIT License (MIT); see LICENSE.txt
+ */
+
+.mw-widget-TitleInputWidget {
+       width: 30em;
+}
diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js b/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js
new file mode 100644 (file)
index 0000000..bfedb6b
--- /dev/null
@@ -0,0 +1,132 @@
+/*!
+ * MediaWiki Widgets – TitleInputWidget class.
+ *
+ * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
+ * @license The MIT License (MIT); see LICENSE.txt
+ */
+( function ( $, mw ) {
+       /**
+        * Creates an mw.widgets.TitleInputWidget object.
+        *
+        * @class
+        * @extends OO.ui.TextInputWidget
+        * @mixins OO.ui.LookupElement
+        *
+        * @constructor
+        * @param {Object} [config] Configuration options
+        * @cfg {number} [namespace] Namespace to prepend to queries
+        */
+       mw.widgets.TitleInputWidget = function MWWTitleInputWidget( config ) {
+               // Config initialization
+               config = config || {};
+
+               // Parent constructor
+               OO.ui.TextInputWidget.call( this, config );
+
+               // Mixin constructors
+               OO.ui.LookupElement.call( this, config );
+
+               // Properties
+               this.namespace = config.namespace || null;
+
+               // Initialization
+               this.$element.addClass( 'mw-widget-TitleInputWidget' );
+               this.lookupMenu.$element.addClass( 'mw-widget-TitleInputWidget-menu' );
+       };
+
+       /* Inheritance */
+
+       OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget );
+
+       OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.LookupElement );
+
+       /* Methods */
+
+       /**
+        * @inheritdoc
+        */
+       mw.widgets.TitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
+               this.closeLookupMenu();
+               this.setLookupsDisabled( true );
+               this.setValue( item.getData() );
+               this.setLookupsDisabled( false );
+       };
+
+       /**
+        * @inheritdoc
+        */
+       mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
+               var value = this.value;
+
+               // Prefix with default namespace name
+               if ( this.namespace !== null && mw.Title.newFromText( value, this.namespace ) ) {
+                       value = mw.Title.newFromText( value, this.namespace ).getPrefixedText();
+               }
+
+               // Dont send leading ':' to open search
+               if ( value.charAt( 0 ) === ':' ) {
+                       value = value.slice( 1 );
+               }
+
+               return new mw.Api().get( {
+                       action: 'opensearch',
+                       search: value,
+                       suggest: ''
+               } );
+       };
+
+       /**
+        * @inheritdoc
+        */
+       mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) {
+               return data[1] || [];
+       };
+
+       /**
+        * @inheritdoc
+        */
+       mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
+               var i, len, title, value,
+                       items = [],
+                       matchingPages = data;
+
+               // Matching pages
+               if ( matchingPages && matchingPages.length ) {
+                       for ( i = 0, len = matchingPages.length; i < len; i++ ) {
+                               title = new mw.Title( matchingPages[i] );
+                               if ( this.namespace !== null ) {
+                                       value = title.getRelativeText( this.namespace );
+                               } else {
+                                       value = title.getPrefixedText();
+                               }
+                               items.push( new OO.ui.MenuOptionWidget( {
+                                       data: value,
+                                       label: value
+                               } ) );
+                       }
+               }
+
+               return items;
+       };
+
+       /**
+        * Get title object corresponding to #getValue
+        *
+        * @returns {mw.Title|null} Title object, or null if value is invalid
+        */
+       mw.widgets.TitleInputWidget.prototype.getTitle = function () {
+               var title = this.getValue(),
+                       // mw.Title doesn't handle null well
+                       titleObj = mw.Title.newFromText( title, this.namespace !== null ? this.namespace : undefined );
+
+               return titleObj;
+       };
+
+       /**
+        * @inheritdoc
+        */
+       mw.widgets.TitleInputWidget.prototype.isValid = function () {
+               return $.Deferred().resolve( !!this.getTitle() ).promise();
+       };
+
+}( jQuery, mediaWiki ) );
diff --git a/resources/src/mediawiki.widgets/mw.widgets.js b/resources/src/mediawiki.widgets/mw.widgets.js
new file mode 100644 (file)
index 0000000..dc8b0cf
--- /dev/null
@@ -0,0 +1 @@
+mediaWiki.widgets = {};