EditPage: Show a different label for the button on create vs. modify
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.CategoryCapsuleItemWidget.js
1 /*!
2 * MediaWiki Widgets - CategoryCapsuleItemWidget class.
3 *
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7 ( function ( $, mw ) {
8
9 /**
10 * @class mw.widgets.PageExistenceCache
11 * @private
12 * @param {mw.Api} [api]
13 */
14 function PageExistenceCache( api ) {
15 this.api = api || new mw.Api();
16 this.processExistenceCheckQueueDebounced = OO.ui.debounce( this.processExistenceCheckQueue );
17 this.currentRequest = null;
18 this.existenceCache = {};
19 this.existenceCheckQueue = {};
20 }
21
22 /**
23 * Check for existence of pages in the queue.
24 *
25 * @private
26 */
27 PageExistenceCache.prototype.processExistenceCheckQueue = function () {
28 var queue, titles,
29 cache = this;
30 if ( this.currentRequest ) {
31 // Don't fire off a million requests at the same time
32 this.currentRequest.always( function () {
33 cache.currentRequest = null;
34 cache.processExistenceCheckQueueDebounced();
35 } );
36 return;
37 }
38 queue = this.existenceCheckQueue;
39 this.existenceCheckQueue = {};
40 titles = Object.keys( queue ).filter( function ( title ) {
41 if ( cache.existenceCache.hasOwnProperty( title ) ) {
42 queue[ title ].resolve( cache.existenceCache[ title ] );
43 }
44 return !cache.existenceCache.hasOwnProperty( title );
45 } );
46 if ( !titles.length ) {
47 return;
48 }
49 this.currentRequest = this.api.get( {
50 formatversion: 2,
51 action: 'query',
52 prop: [ 'info' ],
53 titles: titles
54 } ).done( function ( response ) {
55 $.each( response.query.pages, function ( index, page ) {
56 var title = new ForeignTitle( page.title ).getPrefixedText();
57 cache.existenceCache[ title ] = !page.missing;
58 queue[ title ].resolve( cache.existenceCache[ title ] );
59 } );
60 } );
61 };
62
63 /**
64 * Register a request to check whether a page exists.
65 *
66 * @private
67 * @param {mw.Title} title
68 * @return {jQuery.Promise} Promise resolved with true if the page exists or false otherwise
69 */
70 PageExistenceCache.prototype.checkPageExistence = function ( title ) {
71 var key = title.getPrefixedText();
72 if ( !this.existenceCheckQueue[ key ] ) {
73 this.existenceCheckQueue[ key ] = $.Deferred();
74 }
75 this.processExistenceCheckQueueDebounced();
76 return this.existenceCheckQueue[ key ].promise();
77 };
78
79 /**
80 * @class mw.widgets.ForeignTitle
81 * @private
82 * @extends mw.Title
83 *
84 * @constructor
85 * @inheritdoc
86 */
87 function ForeignTitle( title, namespace ) {
88 // We only need to handle categories here... but we don't know the target language.
89 // So assume that any namespace-like prefix is the 'Category' namespace...
90 title = title.replace( /^(.+?)_*:_*(.*)$/, 'Category:$2' ); // HACK
91 ForeignTitle.parent.call( this, title, namespace );
92 }
93 OO.inheritClass( ForeignTitle, mw.Title );
94 ForeignTitle.prototype.getNamespacePrefix = function () {
95 // We only need to handle categories here...
96 return 'Category:'; // HACK
97 };
98
99 /**
100 * @class mw.widgets.CategoryCapsuleItemWidget
101 *
102 * Category selector capsule item widget. Extends OO.ui.CapsuleItemWidget with the ability to link
103 * to the given page, and to show its existence status (i.e., whether it is a redlink).
104 *
105 * @uses mw.Api
106 * @extends OO.ui.CapsuleItemWidget
107 *
108 * @constructor
109 * @param {Object} config Configuration options
110 * @cfg {mw.Title} title Page title to use (required)
111 * @cfg {string} [apiUrl] API URL, if not the current wiki's API
112 */
113 mw.widgets.CategoryCapsuleItemWidget = function MWWCategoryCapsuleItemWidget( config ) {
114 var widget = this;
115 // Parent constructor
116 mw.widgets.CategoryCapsuleItemWidget.parent.call( this, $.extend( {
117 data: config.title.getMainText(),
118 label: config.title.getMainText()
119 }, config ) );
120
121 // Properties
122 this.title = config.title;
123 this.apiUrl = config.apiUrl || '';
124 this.$link = $( '<a>' )
125 .text( this.label )
126 .attr( 'target', '_blank' )
127 .on( 'click', function ( e ) {
128 // CapsuleMultiselectWidget really wants to prevent you from clicking the link, don't let it
129 e.stopPropagation();
130 } );
131
132 // Initialize
133 this.setMissing( false );
134 this.$label.replaceWith( this.$link );
135 this.setLabelElement( this.$link );
136
137 /*jshint -W024*/
138 if ( !this.constructor.static.pageExistenceCaches[ this.apiUrl ] ) {
139 this.constructor.static.pageExistenceCaches[ this.apiUrl ] =
140 new PageExistenceCache( new mw.ForeignApi( this.apiUrl ) );
141 }
142 this.constructor.static.pageExistenceCaches[ this.apiUrl ]
143 .checkPageExistence( new ForeignTitle( this.title.getPrefixedText() ) )
144 .done( function ( exists ) {
145 widget.setMissing( !exists );
146 } );
147 /*jshint +W024*/
148 };
149
150 /* Setup */
151
152 OO.inheritClass( mw.widgets.CategoryCapsuleItemWidget, OO.ui.CapsuleItemWidget );
153
154 /* Static Properties */
155
156 /*jshint -W024*/
157 /**
158 * Map of API URLs to PageExistenceCache objects.
159 *
160 * @static
161 * @inheritable
162 * @property {Object}
163 */
164 mw.widgets.CategoryCapsuleItemWidget.static.pageExistenceCaches = {
165 '': new PageExistenceCache()
166 };
167 /*jshint +W024*/
168
169 /* Methods */
170
171 /**
172 * Update label link href and CSS classes to reflect page existence status.
173 *
174 * @private
175 * @param {boolean} missing Whether the page is missing (does not exist)
176 */
177 mw.widgets.CategoryCapsuleItemWidget.prototype.setMissing = function ( missing ) {
178 var
179 title = new ForeignTitle( this.title.getPrefixedText() ), // HACK
180 prefix = this.apiUrl.replace( '/w/api.php', '' ); // HACK
181
182 this.missing = missing;
183
184 if ( !missing ) {
185 this.$link
186 .attr( 'href', prefix + title.getUrl() )
187 .attr( 'title', title.getPrefixedText() )
188 .removeClass( 'new' );
189 } else {
190 this.$link
191 .attr( 'href', prefix + title.getUrl( { action: 'edit', redlink: 1 } ) )
192 .attr( 'title', mw.msg( 'red-link-title', title.getPrefixedText() ) )
193 .addClass( 'new' );
194 }
195 };
196
197 }( jQuery, mediaWiki ) );