2 * MediaWiki Widgets - TitleWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
9 var interwikiPrefixesPromise
= new mw
.Api().get( {
12 siprop
: 'interwikimap'
13 } ).then( function ( data
) {
14 return $.map( data
.query
.interwikimap
, function ( interwiki
) {
15 return interwiki
.prefix
;
20 * Mixin for title widgets
26 * @param {Object} [config] Configuration options
27 * @cfg {number} [limit=10] Number of results to show
28 * @cfg {number} [namespace] Namespace to prepend to queries
29 * @cfg {number} [maxLength=255] Maximum query length
30 * @cfg {boolean} [relative=true] If a namespace is set, display titles relative to it
31 * @cfg {boolean} [suggestions=true] Display search suggestions
32 * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
33 * @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist
34 * @cfg {boolean} [showImages] Show page images
35 * @cfg {boolean} [showDescriptions] Show page descriptions
36 * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title (if set to true,
37 * the widget will marks itself red for invalid inputs, including an empty query).
38 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
40 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
41 // Config initialization
48 this.limit
= config
.limit
;
49 this.maxLength
= config
.maxLength
;
50 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
51 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
52 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
53 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
54 this.showRedlink
= !!config
.showRedlink
;
55 this.showImages
= !!config
.showImages
;
56 this.showDescriptions
= !!config
.showDescriptions
;
57 this.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
58 this.cache
= config
.cache
;
61 this.$element
.addClass( 'mw-widget-titleWidget' );
66 OO
.initClass( mw
.widgets
.TitleWidget
);
71 * Get the current value of the search query
74 * @return {string} Search query
76 mw
.widgets
.TitleWidget
.prototype.getQueryValue
= null;
79 * Get the namespace to prepend to titles in suggestions, if any.
81 * @return {number|null} Namespace number
83 mw
.widgets
.TitleWidget
.prototype.getNamespace = function () {
84 return this.namespace;
88 * Set the namespace to prepend to titles in suggestions, if any.
90 * @param {number|null} namespace Namespace number
92 mw
.widgets
.TitleWidget
.prototype.setNamespace = function ( namespace ) {
93 this.namespace = namespace;
97 * Get a promise which resolves with an API repsonse for suggested
98 * links for the current query.
100 * @return {jQuery.Promise} Suggestions promise
102 mw
.widgets
.TitleWidget
.prototype.getSuggestionsPromise = function () {
104 query
= this.getQueryValue(),
106 promiseAbortObject
= { abort: function () {
107 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
110 if ( mw
.Title
.newFromText( query
) ) {
111 return interwikiPrefixesPromise
.then( function ( interwikiPrefixes
) {
113 interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
115 interwiki
&& interwiki
!== '' &&
116 interwikiPrefixes
.indexOf( interwiki
) !== -1
118 return $.Deferred().resolve( { query
: {
122 } } ).promise( promiseAbortObject
);
126 prop
: [ 'info', 'pageprops' ],
127 generator
: 'prefixsearch',
129 gpsnamespace
: widget
.namespace !== null ? widget
.namespace : undefined,
130 gpslimit
: widget
.limit
,
131 ppprop
: 'disambiguation'
133 if ( widget
.showRedirectTargets
) {
134 params
.redirects
= true;
136 if ( widget
.showImages
) {
137 params
.prop
.push( 'pageimages' );
138 params
.pithumbsize
= 80;
139 params
.pilimit
= widget
.limit
;
141 if ( widget
.showDescriptions
) {
142 params
.prop
.push( 'pageterms' );
143 params
.wbptterms
= 'description';
145 req
= new mw
.Api().get( params
);
146 promiseAbortObject
.abort
= req
.abort
.bind( req
); // TODO ew
149 } ).promise( promiseAbortObject
);
151 // Don't send invalid titles to the API.
152 // Just pretend it returned nothing so we can show the 'invalid title' section
153 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
158 * Get option widgets from the server response
160 * @param {Object} data Query result
161 * @return {OO.ui.OptionWidget[]} Menu items
163 mw
.widgets
.TitleWidget
.prototype.getOptionsFromData = function ( data
) {
164 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
167 titleObj
= mw
.Title
.newFromText( this.getQueryValue() ),
171 if ( data
.redirects
) {
172 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
173 redirect
= data
.redirects
[ i
];
174 redirectsTo
[ redirect
.to
] = redirectsTo
[ redirect
.to
] || [];
175 redirectsTo
[ redirect
.to
].push( redirect
.from );
179 for ( index
in data
.pages
) {
180 suggestionPage
= data
.pages
[ index
];
181 pageData
[ suggestionPage
.title
] = {
182 missing
: suggestionPage
.missing
!== undefined,
183 redirect
: suggestionPage
.redirect
!== undefined,
184 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
185 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
186 description
: OO
.getProp( suggestionPage
, 'terms', 'description' ),
188 index
: suggestionPage
.index
191 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
192 // and we encounter a cross-namespace redirect.
193 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
194 titles
.push( suggestionPage
.title
);
197 redirects
= redirectsTo
[ suggestionPage
.title
] || [];
198 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
199 pageData
[ redirects
[ i
] ] = {
202 disambiguation
: false,
203 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
),
204 // Sort index, just below its target
205 index
: suggestionPage
.index
+ 0.5
207 titles
.push( redirects
[ i
] );
211 titles
.sort( function ( a
, b
) {
212 return pageData
[ a
].index
- pageData
[ b
].index
;
215 // If not found, run value through mw.Title to avoid treating a match as a
216 // mismatch where normalisation would make them matching (bug 48476)
218 pageExistsExact
= titles
.indexOf( this.getQueryValue() ) !== -1;
219 pageExists
= pageExistsExact
|| (
220 titleObj
&& titles
.indexOf( titleObj
.getPrefixedText() ) !== -1
224 pageData
[ this.getQueryValue() ] = {
225 missing
: true, redirect
: false, disambiguation
: false,
226 description
: mw
.msg( 'mw-widgets-titleinput-description-new-page' )
231 this.cache
.set( pageData
);
234 // Offer the exact text as a suggestion if the page exists
235 if ( pageExists
&& !pageExistsExact
) {
236 titles
.unshift( this.getQueryValue() );
238 // Offer the exact text as a new page if the title is valid
239 if ( this.showRedlink
&& !pageExists
&& titleObj
) {
240 titles
.push( this.getQueryValue() );
242 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
243 page
= pageData
[ titles
[ i
] ] || {};
244 items
.push( new mw
.widgets
.TitleOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
251 * Get menu option widget data from the title and page data
253 * @param {string} title Title object
254 * @param {Object} data Page data
255 * @return {Object} Data for option widget
257 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
258 var mwTitle
= new mw
.Title( title
);
260 data
: this.namespace !== null && this.relative
261 ? mwTitle
.getRelativeText( this.namespace )
263 url
: mwTitle
.getUrl(),
264 imageUrl
: this.showImages
? data
.imageUrl
: null,
265 description
: this.showDescriptions
? data
.description
: null,
266 missing
: data
.missing
,
267 redirect
: data
.redirect
,
268 disambiguation
: data
.disambiguation
,
269 query
: this.getQueryValue()
274 * Get title object corresponding to given value, or #getQueryValue if not given.
276 * @param {string} [value] Value to get a title for
277 * @return {mw.Title|null} Title object, or null if value is invalid
279 mw
.widgets
.TitleWidget
.prototype.getTitle = function ( value
) {
280 var title
= value
!== undefined ? value
: this.getQueryValue(),
281 // mw.Title doesn't handle null well
282 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
288 * Check if the query is valid
290 * @return {boolean} The query is valid
292 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
293 return this.validateTitle
? !!this.getTitle() : true;
296 }( jQuery
, mediaWiki
) );