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
= 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.namespace = config
.namespace !== undefined ? config
.namespace : null;
43 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
44 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
45 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
46 this.showRedlink
= !!config
.showRedlink
;
47 this.showImages
= !!config
.showImages
;
48 this.showDescriptions
= !!config
.showDescriptions
;
49 this.cache
= config
.cache
;
52 this.$element
.addClass( 'mw-widget-titleInputWidget' );
53 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu' );
54 if ( this.showImages
) {
55 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu-withImages' );
57 if ( this.showDescriptions
) {
58 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu-withDescriptions' );
60 this.setLookupsDisabled( !this.suggestions
);
62 this.interwikiPrefixes
= [];
63 this.interwikiPrefixesPromise
= new mw
.Api().get( {
66 siprop
: 'interwikimap'
67 } ).done( function ( data
) {
68 $.each( data
.query
.interwikimap
, function ( index
, interwiki
) {
69 widget
.interwikiPrefixes
.push( interwiki
.prefix
);
76 OO
.inheritClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.TextInputWidget
);
77 OO
.mixinClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.mixin
.LookupElement
);
84 mw
.widgets
.TitleInputWidget
.prototype.onLookupMenuItemChoose = function ( item
) {
85 this.closeLookupMenu();
86 this.setLookupsDisabled( true );
87 this.setValue( item
.getData() );
88 this.setLookupsDisabled( !this.suggestions
);
94 mw
.widgets
.TitleInputWidget
.prototype.focus = function () {
97 // Prevent programmatic focus from opening the menu
98 this.setLookupsDisabled( true );
101 retval
= mw
.widgets
.TitleInputWidget
.parent
.prototype.focus
.apply( this, arguments
);
103 this.setLookupsDisabled( !this.suggestions
);
111 mw
.widgets
.TitleInputWidget
.prototype.getLookupRequest = function () {
114 promiseAbortObject
= { abort: function () {
115 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
118 if ( mw
.Title
.newFromText( this.value
) ) {
119 return this.interwikiPrefixesPromise
.then( function () {
121 interwiki
= widget
.value
.substring( 0, widget
.value
.indexOf( ':' ) );
123 interwiki
&& interwiki
!== '' &&
124 widget
.interwikiPrefixes
.indexOf( interwiki
) !== -1
126 return $.Deferred().resolve( { query
: {
130 } } ).promise( promiseAbortObject
);
134 generator
: 'prefixsearch',
135 gpssearch
: widget
.value
,
136 gpsnamespace
: widget
.namespace !== null ? widget
.namespace : undefined,
137 gpslimit
: widget
.limit
,
138 ppprop
: 'disambiguation'
140 props
= [ 'info', 'pageprops' ];
141 if ( widget
.showRedirectTargets
) {
142 params
.redirects
= '1';
144 if ( widget
.showImages
) {
145 props
.push( 'pageimages' );
146 params
.pithumbsize
= 80;
147 params
.pilimit
= widget
.limit
;
149 if ( widget
.showDescriptions
) {
150 props
.push( 'pageterms' );
151 params
.wbptterms
= 'description';
153 params
.prop
= props
.join( '|' );
154 req
= new mw
.Api().get( params
);
155 promiseAbortObject
.abort
= req
.abort
.bind( req
); // todo: ew
158 } ).promise( promiseAbortObject
);
160 // Don't send invalid titles to the API.
161 // Just pretend it returned nothing so we can show the 'invalid title' section
162 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
167 * Get lookup cache item from server response data.
170 * @param {Mixed} response Response from server
172 mw
.widgets
.TitleInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
173 return response
.query
|| {};
177 * Get list of menu items from a server response.
179 * @param {Object} data Query result
180 * @returns {OO.ui.MenuOptionWidget[]} Menu items
182 mw
.widgets
.TitleInputWidget
.prototype.getLookupMenuOptionsFromData = function ( data
) {
183 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
186 titleObj
= mw
.Title
.newFromText( this.value
),
190 if ( data
.redirects
) {
191 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
192 redirect
= data
.redirects
[i
];
193 redirectsTo
[redirect
.to
] = redirectsTo
[redirect
.to
] || [];
194 redirectsTo
[redirect
.to
].push( redirect
.from );
198 for ( index
in data
.pages
) {
199 suggestionPage
= data
.pages
[index
];
200 pageData
[suggestionPage
.title
] = {
201 missing
: suggestionPage
.missing
!== undefined,
202 redirect
: suggestionPage
.redirect
!== undefined,
203 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
204 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
205 description
: OO
.getProp( suggestionPage
, 'terms', 'description' )
208 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
209 // and we encounter a cross-namespace redirect.
210 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
211 titles
.push( suggestionPage
.title
);
214 redirects
= redirectsTo
[suggestionPage
.title
] || [];
215 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
216 pageData
[redirects
[i
]] = {
219 disambiguation
: false,
220 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
)
222 titles
.push( redirects
[i
] );
226 // If not found, run value through mw.Title to avoid treating a match as a
227 // mismatch where normalisation would make them matching (bug 48476)
229 pageExistsExact
= titles
.indexOf( this.value
) !== -1;
230 pageExists
= pageExistsExact
|| (
231 titleObj
&& titles
.indexOf( titleObj
.getPrefixedText() ) !== -1
235 pageData
[this.value
] = {
236 missing
: true, redirect
: false, disambiguation
: false,
237 description
: mw
.msg( 'mw-widgets-titleinput-description-new-page' )
242 this.cache
.set( pageData
);
245 // Offer the exact text as a suggestion if the page exists
246 if ( pageExists
&& !pageExistsExact
) {
247 titles
.unshift( this.value
);
249 // Offer the exact text as a new page if the title is valid
250 if ( this.showRedlink
&& !pageExists
&& titleObj
) {
251 titles
.push( this.value
);
253 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
254 page
= pageData
[titles
[i
]] || {};
255 items
.push( new mw
.widgets
.TitleOptionWidget( this.getOptionWidgetData( titles
[i
], page
) ) );
262 * Get menu option widget data from the title and page data
264 * @param {mw.Title} title Title object
265 * @param {Object} data Page data
266 * @return {Object} Data for option widget
268 mw
.widgets
.TitleInputWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
269 var mwTitle
= new mw
.Title( title
);
271 data
: this.namespace !== null && this.relative
272 ? mwTitle
.getRelativeText( this.namespace )
275 imageUrl
: this.showImages
? data
.imageUrl
: null,
276 description
: this.showDescriptions
? data
.description
: null,
277 missing
: data
.missing
,
278 redirect
: data
.redirect
,
279 disambiguation
: data
.disambiguation
,
285 * Get title object corresponding to #getValue
287 * @returns {mw.Title|null} Title object, or null if value is invalid
289 mw
.widgets
.TitleInputWidget
.prototype.getTitle = function () {
290 var title
= this.getValue(),
291 // mw.Title doesn't handle null well
292 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
300 mw
.widgets
.TitleInputWidget
.prototype.isValid = function () {
301 return $.Deferred().resolve( !!this.getTitle() ).promise();
304 }( jQuery
, mediaWiki
) );