Pass archived page id to ArticleUndelete hook.
[lhc/web/wiklou.git] / resources / src / mediawiki.page / mediawiki.page.watch.ajax.js
1 /**
2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than navigating to a different URI.
4 *
5 * @class mw.page.watch.ajax
6 */
7 ( function ( mw, $ ) {
8 // The name of the page to watch or unwatch
9 var title = mw.config.get( 'wgRelevantPageName', mw.config.get( 'wgPageName' ) );
10
11 /**
12 * Update the link text, link href attribute and (if applicable)
13 * "loading" class.
14 *
15 * @param {jQuery} $link Anchor tag of (un)watch link
16 * @param {string} action One of 'watch', 'unwatch'
17 * @param {string} [state="idle"] 'idle' or 'loading'. Default is 'idle'
18 */
19 function updateWatchLink( $link, action, state ) {
20 var msgKey, $li, otherAction;
21
22 // A valid but empty jQuery object shouldn't throw a TypeError
23 if ( !$link.length ) {
24 return;
25 }
26
27 // Invalid actions shouldn't silently turn the page in an unrecoverable state
28 if ( action !== 'watch' && action !== 'unwatch' ) {
29 throw new Error( 'Invalid action' );
30 }
31
32 // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
33 msgKey = state === 'loading' ? action + 'ing' : action;
34 otherAction = action === 'watch' ? 'unwatch' : 'watch';
35 $li = $link.closest( 'li' );
36
37 // Trigger a 'watchpage' event for this List item.
38 // Announce the otherAction value as the first param.
39 // Used to monitor the state of watch link.
40 // TODO: Revise when system wide hooks are implemented
41 if ( state === undefined ) {
42 $li.trigger( 'watchpage.mw', otherAction );
43 }
44
45 $link
46 .text( mw.msg( msgKey ) )
47 .attr( 'title', mw.msg( 'tooltip-ca-' + action ) )
48 .updateTooltipAccessKeys()
49 .attr( 'href', mw.util.wikiScript() + '?' + $.param( {
50 title: title,
51 action: action
52 } )
53 );
54
55 // Most common ID style
56 if ( $li.prop( 'id' ) === 'ca-' + otherAction ) {
57 $li.prop( 'id', 'ca-' + action );
58 }
59
60 // Special case for vector icon
61 if ( $li.hasClass( 'icon' ) ) {
62 if ( state === 'loading' ) {
63 $link.addClass( 'loading' );
64 } else {
65 $link.removeClass( 'loading' );
66 }
67 }
68 }
69
70 /**
71 * TODO: This should be moved somewhere more accessible.
72 *
73 * @private
74 * @param {string} url
75 * @return {string} The extracted action, defaults to 'view'
76 */
77 function mwUriGetAction( url ) {
78 var action, actionPaths, key, i, m, parts;
79
80 // TODO: Does MediaWiki give action path or query param
81 // precedence? If the former, move this to the bottom
82 action = mw.util.getParamValue( 'action', url );
83 if ( action !== null ) {
84 return action;
85 }
86
87 actionPaths = mw.config.get( 'wgActionPaths' );
88 for ( key in actionPaths ) {
89 if ( actionPaths.hasOwnProperty( key ) ) {
90 parts = actionPaths[key].split( '$1' );
91 for ( i = 0; i < parts.length; i++ ) {
92 parts[i] = $.escapeRE( parts[i] );
93 }
94 m = new RegExp( parts.join( '(.+)' ) ).exec( url );
95 if ( m && m[1] ) {
96 return key;
97 }
98
99 }
100 }
101
102 return 'view';
103 }
104
105 // Expose public methods
106 mw.page.watch = {
107 updateWatchLink: updateWatchLink
108 };
109
110 $( function () {
111 var $links = $( '.mw-watchlink a, a.mw-watchlink, ' +
112 '#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
113 '#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
114
115 // Allowing people to add inline animated links is a little scary
116 $links = $links.filter( ':not( #bodyContent *, #content * )' );
117
118 $links.click( function ( e ) {
119 var action, api, $link;
120
121 // Start preloading the notification module (normally loaded by mw.notify())
122 mw.loader.load( ['mediawiki.notification'], null, true );
123
124 action = mwUriGetAction( this.href );
125
126 if ( action !== 'watch' && action !== 'unwatch' ) {
127 // Could not extract target action from link url,
128 // let native browsing handle it further
129 return true;
130 }
131 e.preventDefault();
132 e.stopPropagation();
133
134 $link = $( this );
135
136 updateWatchLink( $link, action, 'loading' );
137
138 api = new mw.Api();
139
140 api[action]( title )
141 .done( function ( watchResponse ) {
142 var otherAction = action === 'watch' ? 'unwatch' : 'watch';
143
144 mw.notify( $.parseHTML( watchResponse.message ), {
145 tag: 'watch-self'
146 } );
147
148 // Set link to opposite
149 updateWatchLink( $link, otherAction );
150
151 // Update the "Watch this page" checkbox on action=edit when the
152 // page is watched or unwatched via the tab (bug 12395).
153 $( '#wpWatchthis' ).prop( 'checked', watchResponse.watched !== undefined );
154 } )
155 .fail( function () {
156 var cleanTitle, msg, link;
157
158 // Reset link to non-loading mode
159 updateWatchLink( $link, action );
160
161 // Format error message
162 cleanTitle = title.replace( /_/g, ' ' );
163 link = mw.html.element(
164 'a', {
165 href: mw.util.getUrl( title ),
166 title: cleanTitle
167 }, cleanTitle
168 );
169 msg = mw.message( 'watcherrortext', link );
170
171 // Report to user about the error
172 mw.notify( msg, { tag: 'watch-self' } );
173 } );
174 } );
175 } );
176
177 }( mediaWiki, jQuery ) );