X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.widgets%2Fmw.widgets.TitleInputWidget.js;h=d5a7abc68f0a594785a26dbefd1eafa9287d304b;hp=66b009f3c59b404b46e36d402c57262baed8ae18;hb=f8659ae6ea90d7bc8ce28bfc1caa56153878836f;hpb=0ea03a7f56070956838ab360a5dccc2a506fc4ff diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js b/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js index 66b009f3c5..d5a7abc68f 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js @@ -18,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 @@ -28,7 +29,10 @@ var widget = this; // Config initialization - config = config || {}; + config = $.extend( { + maxLength: 255, + limit: 10 + }, config ); // Parent constructor mw.widgets.TitleInputWidget.parent.call( this, $.extend( {}, config, { autocomplete: false } ) ); @@ -37,9 +41,11 @@ OO.ui.mixin.LookupElement.call( this, config ); // Properties - this.limit = config.limit || 10; + this.limit = config.limit; + 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; @@ -55,6 +61,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( { @@ -75,6 +82,26 @@ /* Methods */ + /** + * Get the namespace to prepend to titles in suggestions, if any. + * + * @return {number|null} Namespace number + */ + mw.widgets.TitleInputWidget.prototype.getNamespace = function () { + return this.namespace; + }; + + /** + * Set the namespace to prepend to titles in suggestions, if any. + * + * @param {number|null} namespace Namespace number + */ + mw.widgets.TitleInputWidget.prototype.setNamespace = function ( namespace ) { + this.namespace = namespace; + this.lookupCache = {}; + this.closeLookupMenu(); + }; + /** * @inheritdoc */ @@ -82,7 +109,7 @@ this.closeLookupMenu(); this.setLookupsDisabled( true ); this.setValue( item.getData() ); - this.setLookupsDisabled( false ); + this.setLookupsDisabled( !this.suggestions ); }; /** @@ -97,7 +124,7 @@ // Parent method retval = mw.widgets.TitleInputWidget.parent.prototype.focus.apply( this, arguments ); - this.setLookupsDisabled( false ); + this.setLookupsDisabled( !this.suggestions ); return retval; }; @@ -121,9 +148,9 @@ widget.interwikiPrefixes.indexOf( interwiki ) !== -1 ) { return $.Deferred().resolve( { query: { - pages: [{ + pages: [ { title: widget.value - }] + } ] } } ).promise( promiseAbortObject ); } else { params = { @@ -186,15 +213,15 @@ if ( data.redirects ) { for ( i = 0, len = data.redirects.length; i < len; i++ ) { - redirect = data.redirects[i]; - redirectsTo[redirect.to] = redirectsTo[redirect.to] || []; - redirectsTo[redirect.to].push( redirect.from ); + redirect = data.redirects[ i ]; + redirectsTo[ redirect.to ] = redirectsTo[ redirect.to ] || []; + redirectsTo[ redirect.to ].push( redirect.from ); } } for ( index in data.pages ) { - suggestionPage = data.pages[index]; - pageData[suggestionPage.title] = { + suggestionPage = data.pages[ index ]; + pageData[ suggestionPage.title ] = { missing: suggestionPage.missing !== undefined, redirect: suggestionPage.redirect !== undefined, disambiguation: OO.getProp( suggestionPage, 'pageprops', 'disambiguation' ) !== undefined, @@ -208,15 +235,15 @@ titles.push( suggestionPage.title ); } - redirects = redirectsTo[suggestionPage.title] || []; + redirects = redirectsTo[ suggestionPage.title ] || []; for ( i = 0, len = redirects.length; i < len; i++ ) { - pageData[redirects[i]] = { + pageData[ redirects[ i ] ] = { missing: false, redirect: true, disambiguation: false, description: mw.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage.title ) }; - titles.push( redirects[i] ); + titles.push( redirects[ i ] ); } } @@ -229,7 +256,7 @@ ); if ( !pageExists ) { - pageData[this.value] = { + pageData[ this.value ] = { missing: true, redirect: false, disambiguation: false, description: mw.msg( 'mw-widgets-titleinput-description-new-page' ) }; @@ -248,8 +275,8 @@ titles.push( this.value ); } for ( i = 0, len = titles.length; i < len; i++ ) { - page = pageData[titles[i]] || {}; - items.push( new mw.widgets.TitleOptionWidget( this.getOptionWidgetData( titles[i], page ) ) ); + page = pageData[ titles[ i ] ] || {}; + items.push( new mw.widgets.TitleOptionWidget( this.getOptionWidgetData( titles[ i ], page ) ) ); } return items; @@ -279,18 +306,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 $.trimByteLength( this.value, value, this.maxLength, function ( value ) { + var title = widget.getTitle( value ); + return title ? title.getMain() : value; + } ).newVal; + }; + /** * @inheritdoc */