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} [showRedirectTargets=true] Show the targets of redirects
22 * @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist
23 * @cfg {boolean} [showImages] Show page images
24 * @cfg {boolean} [showDescriptions] Show page descriptions
25 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
27 mw
.widgets
.TitleInputWidget
= function MwWidgetsTitleInputWidget( config
) {
30 // Config initialization
31 config
= config
|| {};
34 mw
.widgets
.TitleInputWidget
.parent
.call( this, $.extend( {}, config
, { autocomplete
: false } ) );
37 OO
.ui
.mixin
.LookupElement
.call( this, config
);
40 this.limit
= config
.limit
|| 10;
41 this.namespace = config
.namespace || null;
42 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
43 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
44 this.showRedlink
= !!config
.showRedlink
;
45 this.showImages
= !!config
.showImages
;
46 this.showDescriptions
= !!config
.showDescriptions
;
47 this.cache
= config
.cache
;
50 this.$element
.addClass( 'mw-widget-titleInputWidget' );
51 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu' );
52 if ( this.showImages
) {
53 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu-withImages' );
55 if ( this.showDescriptions
) {
56 this.lookupMenu
.$element
.addClass( 'mw-widget-titleInputWidget-menu-withDescriptions' );
59 this.interwikiPrefixes
= [];
60 this.interwikiPrefixesPromise
= new mw
.Api().get( {
63 siprop
: 'interwikimap'
64 } ).done( function ( data
) {
65 $.each( data
.query
.interwikimap
, function ( index
, interwiki
) {
66 widget
.interwikiPrefixes
.push( interwiki
.prefix
);
73 OO
.inheritClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.TextInputWidget
);
74 OO
.mixinClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.mixin
.LookupElement
);
81 mw
.widgets
.TitleInputWidget
.prototype.onLookupMenuItemChoose = function ( item
) {
82 this.closeLookupMenu();
83 this.setLookupsDisabled( true );
84 this.setValue( item
.getData() );
85 this.setLookupsDisabled( false );
91 mw
.widgets
.TitleInputWidget
.prototype.focus = function () {
94 // Prevent programmatic focus from opening the menu
95 this.setLookupsDisabled( true );
98 retval
= mw
.widgets
.TitleInputWidget
.parent
.prototype.focus
.apply( this, arguments
);
100 this.setLookupsDisabled( false );
108 mw
.widgets
.TitleInputWidget
.prototype.getLookupRequest = function () {
111 promiseAbortObject
= { abort: function () {
112 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
115 if ( mw
.Title
.newFromText( this.value
) ) {
116 return this.interwikiPrefixesPromise
.then( function () {
118 interwiki
= widget
.value
.substring( 0, widget
.value
.indexOf( ':' ) );
120 interwiki
&& interwiki
!== '' &&
121 widget
.interwikiPrefixes
.indexOf( interwiki
) !== -1
123 return $.Deferred().resolve( { query
: {
127 } } ).promise( promiseAbortObject
);
131 generator
: 'prefixsearch',
132 gpssearch
: widget
.value
,
133 gpsnamespace
: widget
.namespace !== null ? widget
.namespace : undefined,
134 gpslimit
: widget
.limit
,
135 ppprop
: 'disambiguation'
137 props
= [ 'info', 'pageprops' ];
138 if ( widget
.showRedirectTargets
) {
139 params
.redirects
= '1';
141 if ( widget
.showImages
) {
142 props
.push( 'pageimages' );
143 params
.pithumbsize
= 80;
144 params
.pilimit
= widget
.limit
;
146 if ( widget
.showDescriptions
) {
147 props
.push( 'pageterms' );
148 params
.wbptterms
= 'description';
150 params
.prop
= props
.join( '|' );
151 req
= new mw
.Api().get( params
);
152 promiseAbortObject
.abort
= req
.abort
.bind( req
); // todo: ew
155 } ).promise( promiseAbortObject
);
157 // Don't send invalid titles to the API.
158 // Just pretend it returned nothing so we can show the 'invalid title' section
159 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
164 * Get lookup cache item from server response data.
167 * @param {Mixed} response Response from server
169 mw
.widgets
.TitleInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
170 return response
.query
|| {};
174 * Get list of menu items from a server response.
176 * @param {Object} data Query result
177 * @returns {OO.ui.MenuOptionWidget[]} Menu items
179 mw
.widgets
.TitleInputWidget
.prototype.getLookupMenuOptionsFromData = function ( data
) {
180 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
183 titleObj
= mw
.Title
.newFromText( this.value
),
187 if ( data
.redirects
) {
188 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
189 redirect
= data
.redirects
[i
];
190 redirectsTo
[redirect
.to
] = redirectsTo
[redirect
.to
] || [];
191 redirectsTo
[redirect
.to
].push( redirect
.from );
195 for ( index
in data
.pages
) {
196 suggestionPage
= data
.pages
[index
];
197 pageData
[suggestionPage
.title
] = {
198 missing
: suggestionPage
.missing
!== undefined,
199 redirect
: suggestionPage
.redirect
!== undefined,
200 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
201 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
202 description
: OO
.getProp( suggestionPage
, 'terms', 'description' )
204 titles
.push( suggestionPage
.title
);
206 redirects
= redirectsTo
[suggestionPage
.title
] || [];
207 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
208 pageData
[redirects
[i
]] = {
211 disambiguation
: false,
212 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
)
214 titles
.push( redirects
[i
] );
218 // If not found, run value through mw.Title to avoid treating a match as a
219 // mismatch where normalisation would make them matching (bug 48476)
221 pageExistsExact
= titles
.indexOf( this.value
) !== -1;
222 pageExists
= pageExistsExact
|| (
223 titleObj
&& titles
.indexOf( titleObj
.getPrefixedText() ) !== -1
227 pageData
[this.value
] = {
228 missing
: true, redirect
: false, disambiguation
: false,
229 description
: mw
.msg( 'mw-widgets-titleinput-description-new-page' )
234 this.cache
.set( pageData
);
237 // Offer the exact text as a suggestion if the page exists
238 if ( pageExists
&& !pageExistsExact
) {
239 titles
.unshift( this.value
);
241 // Offer the exact text as a new page if the title is valid
242 if ( this.showRedlink
&& !pageExists
&& titleObj
) {
243 titles
.push( this.value
);
245 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
246 page
= pageData
[titles
[i
]] || {};
247 items
.push( new mw
.widgets
.TitleOptionWidget( this.getOptionWidgetData( titles
[i
], page
) ) );
254 * Get menu option widget data from the title and page data
256 * @param {mw.Title} title Title object
257 * @param {Object} data Page data
258 * @return {Object} Data for option widget
260 mw
.widgets
.TitleInputWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
261 var mwTitle
= new mw
.Title( title
);
263 data
: this.namespace !== null && this.relative
264 ? mwTitle
.getRelativeText( this.namespace )
267 imageUrl
: this.showImages
? data
.imageUrl
: null,
268 description
: this.showDescriptions
? data
.description
: null,
269 missing
: data
.missing
,
270 redirect
: data
.redirect
,
271 disambiguation
: data
.disambiguation
,
277 * Get title object corresponding to #getValue
279 * @returns {mw.Title|null} Title object, or null if value is invalid
281 mw
.widgets
.TitleInputWidget
.prototype.getTitle = function () {
282 var title
= this.getValue(),
283 // mw.Title doesn't handle null well
284 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
292 mw
.widgets
.TitleInputWidget
.prototype.isValid = function () {
293 return $.Deferred().resolve( !!this.getTitle() ).promise();
296 }( jQuery
, mediaWiki
) );