2 * MediaWiki Widgets - CategoryCapsuleItemWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * @class mw.widgets.CategoryCapsuleItemWidget
13 var processExistenceCheckQueueDebounced
,
15 currentRequest
= null,
17 existenceCheckQueue
= {};
19 // The existence checking code really could be refactored into a separate class.
24 function processExistenceCheckQueue() {
26 if ( currentRequest
) {
27 // Don't fire off a million requests at the same time
28 currentRequest
.always( function () {
29 currentRequest
= null;
30 processExistenceCheckQueueDebounced();
34 queue
= existenceCheckQueue
;
35 existenceCheckQueue
= {};
36 titles
= Object
.keys( queue
).filter( function ( title
) {
37 if ( existenceCache
.hasOwnProperty( title
) ) {
38 queue
[ title
].resolve( existenceCache
[ title
] );
40 return !existenceCache
.hasOwnProperty( title
);
42 if ( !titles
.length
) {
45 currentRequest
= api
.get( {
49 } ).done( function ( response
) {
50 var index
, curr
, title
;
51 for ( index
in response
.query
.pages
) {
52 curr
= response
.query
.pages
[ index
];
53 title
= mw
.Title
.newFromText( curr
.title
).getPrefixedText();
54 existenceCache
[ title
] = curr
.missing
=== undefined;
55 queue
[ title
].resolve( existenceCache
[ title
] );
60 processExistenceCheckQueueDebounced
= OO
.ui
.debounce( processExistenceCheckQueue
);
63 * Register a request to check whether a page exists.
66 * @param {mw.Title} title
67 * @return {jQuery.Promise} Promise resolved with true if the page exists or false otherwise
69 function checkPageExistence( title
) {
70 var key
= title
.getPrefixedText();
71 if ( !existenceCheckQueue
[ key
] ) {
72 existenceCheckQueue
[ key
] = $.Deferred();
74 processExistenceCheckQueueDebounced();
75 return existenceCheckQueue
[ key
].promise();
79 * Category selector capsule item widget. Extends OO.ui.CapsuleItemWidget with the ability to link
80 * to the given page, and to show its existence status (i.e., whether it is a redlink).
83 * @extends OO.ui.CapsuleItemWidget
86 * @param {Object} config Configuration options
87 * @cfg {mw.Title} title Page title to use (required)
89 mw
.widgets
.CategoryCapsuleItemWidget
= function MWWCategoryCapsuleItemWidget( config
) {
91 mw
.widgets
.CategoryCapsuleItemWidget
.parent
.call( this, $.extend( {
92 data
: config
.title
.getMainText(),
93 label
: config
.title
.getMainText()
97 this.title
= config
.title
;
98 this.$link
= $( '<a>' )
100 .attr( 'target', '_blank' )
101 .on( 'click', function ( e
) {
102 // CapsuleMultiSelectWidget really wants to prevent you from clicking the link, don't let it
107 this.setMissing( false );
108 this.$label
.replaceWith( this.$link
);
109 this.setLabelElement( this.$link
);
110 checkPageExistence( this.title
).done( function ( exists
) {
111 this.setMissing( !exists
);
117 OO
.inheritClass( mw
.widgets
.CategoryCapsuleItemWidget
, OO
.ui
.CapsuleItemWidget
);
122 * Update label link href and CSS classes to reflect page existence status.
125 * @param {boolean} missing Whether the page is missing (does not exist)
127 mw
.widgets
.CategoryCapsuleItemWidget
.prototype.setMissing = function ( missing
) {
130 .attr( 'href', this.title
.getUrl() )
131 .removeClass( 'new' );
134 .attr( 'href', this.title
.getUrl( { action
: 'edit', redlink
: 1 } ) )
139 }( jQuery
, mediaWiki
) );