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
10 * Mixin for title widgets
16 * @param {Object} [config] Configuration options
17 * @cfg {number} [limit=10] Number of results to show
18 * @cfg {number} [namespace] Namespace to prepend to queries
19 * @cfg {number} [maxLength=255] Maximum query length
20 * @cfg {boolean} [relative=true] If a namespace is set, display titles 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 {boolean} [validateTitle=true] Whether the input must be a valid title (if set to true,
27 * the widget will marks itself red for invalid inputs, including an empty query).
28 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
30 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
33 // Config initialization
40 this.limit
= config
.limit
;
41 this.maxLength
= config
.maxLength
;
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.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
50 this.cache
= config
.cache
;
53 this.$element
.addClass( 'mw-widget-titleWidget' );
54 this.interwikiPrefixes
= [];
55 this.interwikiPrefixesPromise
= new mw
.Api().get( {
58 siprop
: 'interwikimap'
59 } ).done( function ( data
) {
60 $.each( data
.query
.interwikimap
, function ( index
, interwiki
) {
61 widget
.interwikiPrefixes
.push( interwiki
.prefix
);
68 OO
.initClass( mw
.widgets
.TitleWidget
);
73 * Get the current value of the search query
76 * @return {string} Search query
78 mw
.widgets
.TitleWidget
.prototype.getQueryValue
= null;
81 * Get the namespace to prepend to titles in suggestions, if any.
83 * @return {number|null} Namespace number
85 mw
.widgets
.TitleWidget
.prototype.getNamespace = function () {
86 return this.namespace;
90 * Set the namespace to prepend to titles in suggestions, if any.
92 * @param {number|null} namespace Namespace number
94 mw
.widgets
.TitleWidget
.prototype.setNamespace = function ( namespace ) {
95 this.namespace = namespace;
99 * Get a promise which resolves with an API repsonse for suggested
100 * links for the current query.
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 this.interwikiPrefixesPromise
.then( function () {
113 interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
115 interwiki
&& interwiki
!== '' &&
116 widget
.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
)
205 titles
.push( redirects
[ i
] );
209 titles
.sort( function ( a
, b
) {
210 return pageData
[ a
].index
- pageData
[ b
].index
;
213 // If not found, run value through mw.Title to avoid treating a match as a
214 // mismatch where normalisation would make them matching (bug 48476)
216 pageExistsExact
= titles
.indexOf( this.getQueryValue() ) !== -1;
217 pageExists
= pageExistsExact
|| (
218 titleObj
&& titles
.indexOf( titleObj
.getPrefixedText() ) !== -1
222 pageData
[ this.getQueryValue() ] = {
223 missing
: true, redirect
: false, disambiguation
: false,
224 description
: mw
.msg( 'mw-widgets-titleinput-description-new-page' )
229 this.cache
.set( pageData
);
232 // Offer the exact text as a suggestion if the page exists
233 if ( pageExists
&& !pageExistsExact
) {
234 titles
.unshift( this.getQueryValue() );
236 // Offer the exact text as a new page if the title is valid
237 if ( this.showRedlink
&& !pageExists
&& titleObj
) {
238 titles
.push( this.getQueryValue() );
240 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
241 page
= pageData
[ titles
[ i
] ] || {};
242 items
.push( new mw
.widgets
.TitleOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
249 * Get menu option widget data from the title and page data
251 * @param {string} title Title object
252 * @param {Object} data Page data
253 * @return {Object} Data for option widget
255 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
256 var mwTitle
= new mw
.Title( title
);
258 data
: this.namespace !== null && this.relative
259 ? mwTitle
.getRelativeText( this.namespace )
261 url
: mwTitle
.getUrl(),
262 imageUrl
: this.showImages
? data
.imageUrl
: null,
263 description
: this.showDescriptions
? data
.description
: null,
264 missing
: data
.missing
,
265 redirect
: data
.redirect
,
266 disambiguation
: data
.disambiguation
,
267 query
: this.getQueryValue()
272 * Get title object corresponding to given value, or #getQueryValue if not given.
274 * @param {string} [value] Value to get a title for
275 * @return {mw.Title|null} Title object, or null if value is invalid
277 mw
.widgets
.TitleWidget
.prototype.getTitle = function ( value
) {
278 var title
= value
!== undefined ? value
: this.getQueryValue(),
279 // mw.Title doesn't handle null well
280 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
286 * Check if the query is valid
288 * @return {boolean} The query is valid
290 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
291 return this.validateTitle
? !!this.getTitle() : true;
294 }( jQuery
, mediaWiki
) );