X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.widgets%2Fmw.widgets.TitleInputWidget.js;h=3697a1c633beb11ced7d80fe319c3e12e2ea93d7;hb=a7c124f2e96a13464e51cb2ec9885f2d42357f57;hp=ca8c400fcb5bfd3a270b7fb57f123f57e4944a32;hpb=612fbbf536c818686ab32a55911b77cc812394d5;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js b/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js index ca8c400fcb..3697a1c633 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js @@ -5,6 +5,7 @@ * @license The MIT License (MIT); see LICENSE.txt */ ( function ( $, mw ) { + /** * Creates an mw.widgets.TitleInputWidget object. * @@ -17,6 +18,7 @@ * @cfg {number} [limit=10] Number of results to show * @cfg {number} [namespace] Namespace to prepend to queries * @cfg {boolean} [relative=true] If a namespace is set, return a title relative to it + * @cfg {boolean} [suggestions=true] Display search suggestions * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects * @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist * @cfg {boolean} [showImages] Show page images @@ -27,18 +29,20 @@ var widget = this; // Config initialization - config = config || {}; + config = $.extend( { maxLength: 255 }, config ); // Parent constructor - OO.ui.TextInputWidget.call( this, $.extend( {}, config, { autocomplete: false } ) ); + mw.widgets.TitleInputWidget.parent.call( this, $.extend( {}, config, { autocomplete: false } ) ); // Mixin constructors OO.ui.mixin.LookupElement.call( this, config ); // Properties this.limit = config.limit || 10; - this.namespace = config.namespace || null; + this.maxLength = config.maxLength; + this.namespace = config.namespace !== undefined ? config.namespace : null; this.relative = config.relative !== undefined ? config.relative : true; + this.suggestions = config.suggestions !== undefined ? config.suggestions : true; this.showRedirectTargets = config.showRedirectTargets !== false; this.showRedlink = !!config.showRedlink; this.showImages = !!config.showImages; @@ -54,6 +58,7 @@ if ( this.showDescriptions ) { this.lookupMenu.$element.addClass( 'mw-widget-titleInputWidget-menu-withDescriptions' ); } + this.setLookupsDisabled( !this.suggestions ); this.interwikiPrefixes = []; this.interwikiPrefixesPromise = new mw.Api().get( { @@ -67,10 +72,9 @@ } ); }; - /* Inheritance */ + /* Setup */ OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget ); - OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.mixin.LookupElement ); /* Methods */ @@ -82,7 +86,7 @@ this.closeLookupMenu(); this.setLookupsDisabled( true ); this.setValue( item.getData() ); - this.setLookupsDisabled( false ); + this.setLookupsDisabled( !this.suggestions ); }; /** @@ -95,9 +99,9 @@ this.setLookupsDisabled( true ); // Parent method - retval = OO.ui.TextInputWidget.prototype.focus.apply( this, arguments ); + retval = mw.widgets.TitleInputWidget.parent.prototype.focus.apply( this, arguments ); - this.setLookupsDisabled( false ); + this.setLookupsDisabled( !this.suggestions ); return retval; }; @@ -164,10 +168,10 @@ * Get lookup cache item from server response data. * * @method - * @param {Mixed} data Response from server + * @param {Mixed} response Response from server */ - mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) { - return data.query || {}; + mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) { + return response.query || {}; }; /** @@ -201,7 +205,12 @@ imageUrl: OO.getProp( suggestionPage, 'thumbnail', 'source' ), description: OO.getProp( suggestionPage, 'terms', 'description' ) }; - titles.push( suggestionPage.title ); + + // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true + // and we encounter a cross-namespace redirect. + if ( this.namespace === null || this.namespace === suggestionPage.ns ) { + titles.push( suggestionPage.title ); + } redirects = redirectsTo[suggestionPage.title] || []; for ( i = 0, len = redirects.length; i < len; i++ ) { @@ -263,6 +272,7 @@ data: this.namespace !== null && this.relative ? mwTitle.getRelativeText( this.namespace ) : title, + title: mwTitle, imageUrl: this.showImages ? data.imageUrl : null, description: this.showDescriptions ? data.description : null, missing: data.missing, @@ -273,18 +283,31 @@ }; /** - * Get title object corresponding to #getValue + * Get title object corresponding to given value, or #getValue if not given. * + * @param {string} [value] Value to get a title for * @returns {mw.Title|null} Title object, or null if value is invalid */ - mw.widgets.TitleInputWidget.prototype.getTitle = function () { - var title = this.getValue(), + mw.widgets.TitleInputWidget.prototype.getTitle = function ( value ) { + var title = value !== undefined ? value : 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.cleanUpValue = function ( value ) { + var widget = this; + value = mw.widgets.TitleInputWidget.parent.prototype.cleanUpValue.call( this, value ); + return $.fn.byteLimit.trimValueForByteLength( this.value, value, this.maxLength, function ( value ) { + var title = widget.getTitle( value ); + return title ? title.getMain() : value; + } ).newVal; + }; + /** * @inheritdoc */