c9c9f031ee4c3a0f0b46716a09faf2e7fcaa7472
2 * MediaWiki Widgets - TitleInputWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * Creates an mw.widgets.TitleInputWidget object.
13 * @extends OO.ui.TextInputWidget
14 * @mixins OO.ui.mixin.LookupElement
17 * @param {Object} [config] Configuration options
18 * @cfg {number} [limit=10] Number of results to show
19 * @cfg {number} [namespace] Namespace to prepend to queries
20 * @cfg {boolean} [relative=true] If a namespace is set, return a title relative to it
21 * @cfg {boolean} [suggestions=true] Display search suggestions
22 * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
23 * @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist
24 * @cfg {boolean} [showImages] Show page images
25 * @cfg {boolean} [showDescriptions] Show page descriptions
26 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
28 mw
.widgets
.TitleInputWidget
= function MwWidgetsTitleInputWidget( config
) {
31 // Config initialization
32 config
= $.extend( { maxLength
: 255 }, config
);
35 mw
.widgets
.TitleInputWidget
.parent
.call( this, $.extend( {}, config
, { autocomplete
: false } ) );
38 OO
.ui
.mixin
.LookupElement
.call( this, config
);
41 this.limit
= config
.limit
|| 10;
42 this.maxLength
= config
.maxLength
;
43 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
44 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
45 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
46 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
47 this.showRedlink
= !!config
.showRedlink
;
48 this.showImages
= !!config
.showImages
;
49 this.showDescriptions
= !!config
.showDescriptions
;
50 this.cache
= config
.cache
;
53 this.$element
.addClass( 'mw-widget-titleInputWidget' );
54 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu' );
55 if ( this.showImages
) {
56 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu-withImages' );
58 if ( this.showDescriptions
) {
59 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu-withDescriptions' );
61 this.setLookupsDisabled( !this.suggestions
);
63 this.interwikiPrefixes
= [];
64 this.interwikiPrefixesPromise
= new mw
.Api().get( {
67 siprop
: 'interwikimap'
68 } ).done( function ( data
) {
69 $.each( data
.query
.interwikimap
, function ( index
, interwiki
) {
70 widget
.interwikiPrefixes
.push( interwiki
.prefix
);
77 OO
.inheritClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.TextInputWidget
);
78 OO
.mixinClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.mixin
.LookupElement
);
83 * Get the namespace to prepend to titles in suggestions, if any.
85 * @return {number|null} Namespace number
87 mw
.widgets
.TitleInputWidget
.prototype.getNamespace = function () {
88 return this.namespace;
92 * Set the namespace to prepend to titles in suggestions, if any.
94 * @param {number|null} namespace Namespace number
96 mw
.widgets
.TitleInputWidget
.prototype.setNamespace = function ( namespace ) {
97 this.namespace = namespace;
98 this.lookupCache
= {};
99 this.closeLookupMenu();
105 mw
.widgets
.TitleInputWidget
.prototype.onLookupMenuItemChoose = function ( item
) {
106 this.closeLookupMenu();
107 this.setLookupsDisabled( true );
108 this.setValue( item
.getData() );
109 this.setLookupsDisabled( !this.suggestions
);
115 mw
.widgets
.TitleInputWidget
.prototype.focus = function () {
118 // Prevent programmatic focus from opening the menu
119 this.setLookupsDisabled( true );
122 retval
= mw
.widgets
.TitleInputWidget
.parent
.prototype.focus
.apply( this, arguments
);
124 this.setLookupsDisabled( !this.suggestions
);
132 mw
.widgets
.TitleInputWidget
.prototype.getLookupRequest = function () {
135 promiseAbortObject
= { abort: function () {
136 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
139 if ( mw
.Title
.newFromText( this.value
) ) {
140 return this.interwikiPrefixesPromise
.then( function () {
142 interwiki
= widget
.value
.substring( 0, widget
.value
.indexOf( ':' ) );
144 interwiki
&& interwiki
!== '' &&
145 widget
.interwikiPrefixes
.indexOf( interwiki
) !== -1
147 return $.Deferred().resolve( { query
: {
151 } } ).promise( promiseAbortObject
);
155 generator
: 'prefixsearch',
156 gpssearch
: widget
.value
,
157 gpsnamespace
: widget
.namespace !== null ? widget
.namespace : undefined,
158 gpslimit
: widget
.limit
,
159 ppprop
: 'disambiguation'
161 props
= [ 'info', 'pageprops' ];
162 if ( widget
.showRedirectTargets
) {
163 params
.redirects
= '1';
165 if ( widget
.showImages
) {
166 props
.push( 'pageimages' );
167 params
.pithumbsize
= 80;
168 params
.pilimit
= widget
.limit
;
170 if ( widget
.showDescriptions
) {
171 props
.push( 'pageterms' );
172 params
.wbptterms
= 'description';
174 params
.prop
= props
.join( '|' );
175 req
= new mw
.Api().get( params
);
176 promiseAbortObject
.abort
= req
.abort
.bind( req
); // todo: ew
179 } ).promise( promiseAbortObject
);
181 // Don't send invalid titles to the API.
182 // Just pretend it returned nothing so we can show the 'invalid title' section
183 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
188 * Get lookup cache item from server response data.
191 * @param {Mixed} response Response from server
193 mw
.widgets
.TitleInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
194 return response
.query
|| {};
198 * Get list of menu items from a server response.
200 * @param {Object} data Query result
201 * @returns {OO.ui.MenuOptionWidget[]} Menu items
203 mw
.widgets
.TitleInputWidget
.prototype.getLookupMenuOptionsFromData = function ( data
) {
204 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
207 titleObj
= mw
.Title
.newFromText( this.value
),
211 if ( data
.redirects
) {
212 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
213 redirect
= data
.redirects
[i
];
214 redirectsTo
[redirect
.to
] = redirectsTo
[redirect
.to
] || [];
215 redirectsTo
[redirect
.to
].push( redirect
.from );
219 for ( index
in data
.pages
) {
220 suggestionPage
= data
.pages
[index
];
221 pageData
[suggestionPage
.title
] = {
222 missing
: suggestionPage
.missing
!== undefined,
223 redirect
: suggestionPage
.redirect
!== undefined,
224 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
225 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
226 description
: OO
.getProp( suggestionPage
, 'terms', 'description' )
229 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
230 // and we encounter a cross-namespace redirect.
231 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
232 titles
.push( suggestionPage
.title
);
235 redirects
= redirectsTo
[suggestionPage
.title
] || [];
236 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
237 pageData
[redirects
[i
]] = {
240 disambiguation
: false,
241 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
)
243 titles
.push( redirects
[i
] );
247 // If not found, run value through mw.Title to avoid treating a match as a
248 // mismatch where normalisation would make them matching (bug 48476)
250 pageExistsExact
= titles
.indexOf( this.value
) !== -1;
251 pageExists
= pageExistsExact
|| (
252 titleObj
&& titles
.indexOf( titleObj
.getPrefixedText() ) !== -1
256 pageData
[this.value
] = {
257 missing
: true, redirect
: false, disambiguation
: false,
258 description
: mw
.msg( 'mw-widgets-titleinput-description-new-page' )
263 this.cache
.set( pageData
);
266 // Offer the exact text as a suggestion if the page exists
267 if ( pageExists
&& !pageExistsExact
) {
268 titles
.unshift( this.value
);
270 // Offer the exact text as a new page if the title is valid
271 if ( this.showRedlink
&& !pageExists
&& titleObj
) {
272 titles
.push( this.value
);
274 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
275 page
= pageData
[titles
[i
]] || {};
276 items
.push( new mw
.widgets
.TitleOptionWidget( this.getOptionWidgetData( titles
[i
], page
) ) );
283 * Get menu option widget data from the title and page data
285 * @param {mw.Title} title Title object
286 * @param {Object} data Page data
287 * @return {Object} Data for option widget
289 mw
.widgets
.TitleInputWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
290 var mwTitle
= new mw
.Title( title
);
292 data
: this.namespace !== null && this.relative
293 ? mwTitle
.getRelativeText( this.namespace )
296 imageUrl
: this.showImages
? data
.imageUrl
: null,
297 description
: this.showDescriptions
? data
.description
: null,
298 missing
: data
.missing
,
299 redirect
: data
.redirect
,
300 disambiguation
: data
.disambiguation
,
306 * Get title object corresponding to given value, or #getValue if not given.
308 * @param {string} [value] Value to get a title for
309 * @returns {mw.Title|null} Title object, or null if value is invalid
311 mw
.widgets
.TitleInputWidget
.prototype.getTitle = function ( value
) {
312 var title
= value
!== undefined ? value
: this.getValue(),
313 // mw.Title doesn't handle null well
314 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
322 mw
.widgets
.TitleInputWidget
.prototype.cleanUpValue = function ( value
) {
324 value
= mw
.widgets
.TitleInputWidget
.parent
.prototype.cleanUpValue
.call( this, value
);
325 return $.trimByteLength( this.value
, value
, this.maxLength
, function ( value
) {
326 var title
= widget
.getTitle( value
);
327 return title
? title
.getMain() : value
;
334 mw
.widgets
.TitleInputWidget
.prototype.isValid = function () {
335 return $.Deferred().resolve( !!this.getTitle() ).promise();
338 }( jQuery
, mediaWiki
) );