Passing token paremeter in mw.action.watch.ajax since this is required as of r88522.
[mediawiki.git] / resources / mediawiki.action / mediawiki.action.watch.ajax.js
blob19e6d1e6f13daccfafbd21e33cfbdb86366bff2d
1 /**
2  * Animate watch/unwatch links to use asynchronous API requests to
3  * watch pages, rather than clicking on links. Requires jQuery.
4  */
5 ( function( $ ) {
6 var $links;
8 var setLinkText = function( $link, action ) {
9         if ( action == 'watch' || action == 'unwatch' ) {
10                 // save the accesskey from the title
11                 var keyCommand = $link.attr( 'title' ).match( /\[.*?\]$/ ) ? $link.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
12                 $link.attr( 'title', mw.msg( 'tooltip-ca-' + action ) + ' ' + keyCommand );
13         }
14         if ( $link.data( 'icon' ) ) {
15                 $link.attr( 'alt', mw.msg( action ) );
16                 if ( action == 'watching' || action == 'unwatching' ) {
17                         $link.addClass( 'loading' );
18                 } else {
19                         $link.removeClass( 'loading' );
20                 }
21         } else {
22                 $link.html( mw.msg( action ) );
23         }
26 var errorHandler = function( $link ) {
28         // Reset link text to whatever it was before we switching it to the '(un)watch'+ing message.
29         setLinkText( $link, $link.data( 'action' ) );
31         // Format error message
32         var cleanTitle = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
33         var link = mw.html.element(
34                 'a', {
35                         'href': mw.util.wikiGetlink( mw.config.get( 'wgPageName' ) ),
36                         'title': cleanTitle
37                 }, cleanTitle
38         );
39         var msg = mw.msg( 'watcherrortext', link );
41         // Report to user about the error
42         mw.util.jsMessage( msg, 'watch' );
45 /**
46  * Process the result of the API watch action.
47  *
48  * @param response Data object from API request.
49  * @param $link jQuery object of the watch link.
50  * @return Boolean true on success, false otherwise.
51  */
52 var processResult = function( response, $link ) {
54         if ( ( 'error' in response ) || !response.watch ) {
55                 errorHandler( $link );
56                 return false;
57         }
59         var watchResponse = response.watch;
61         // To ensure we set the same status for all watch links with the
62         // same target we trigger a custom event on *all* watch links.
63         if ( watchResponse.watched !== undefined ) {
64                 $links.trigger( 'mw-ajaxwatch', [watchResponse.title, 'watch', $link] );
65         } else if ( watchResponse.unwatched !== undefined ) {
66                 $links.trigger( 'mw-ajaxwatch', [watchResponse.title, 'unwatch', $link] );
67         } else {
68                 // Either we got an error code or it just plain broke.
69                 window.location.href = $link[0].href;
70                 return false;
71         }
73         mw.util.jsMessage( watchResponse.message, 'watch' );
75         // Bug 12395 - update the watch checkbox on edit pages when the
76         // page is watched or unwatched via the tab.
77         if ( watchResponse.watched !== undefined ) {
78                 $( '#wpWatchthis' ).attr( 'checked', 'checked' );
79         } else {
80                 $( '#wpWatchthis' ).removeAttr( 'checked' );
81         }
82         return true;
85 $( document ).ready( function() {
86         $links = $( '.mw-watchlink a, a.mw-watchlink' );
87         // BC with older skins
88         $links = $links
89                 .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' +
90                         'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' );
91         // allowing people to add inline animated links is a little scary
92         $links = $links.filter( ':not( #bodyContent *, #content * )' );
94         $links.each( function() {
95                 var $link = $( this );
96                 var link = this;
97                 $link
98                         .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) )
99                         .data( 'action', mw.util.getActionFrom( link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
100                 var title = mw.util.getTitleFrom( link.href );
101                 $link.data( 'target', title.replace( /_/g, ' ' ) );
102         });
104         $links.click( function( event ) {
105                 var $link = $( this );
107                 if ( !mw.config.get( 'wgEnableWriteAPI' ) ) {
108                         // Lazy initialization so we don't toss up
109                         // ActiveX warnings on initial page load
110                         // for IE 6 users with security settings.
111                         $links.unbind( 'click' );
112                         return true;
113                 }
115                 setLinkText( $link, $link.data( 'action' ) + 'ing' );
117                 var reqData = {
118                         'action': 'watch',
119                         'format': 'json',
120                         'title': $link.data( 'target' ),
121                         'token': mw.user.tokens.get( 'watchToken' ),
122                         // API return contains a localized data.watch.message string.
123                         'uselang': mw.config.get( 'wgUserLanguage' )
124                 };
126                 if ( $link.data( 'action' ) == 'unwatch' ) {
127                         reqData.unwatch = '';
128                 }
130                 $.ajax({
131                         url: mw.util.wikiScript( 'api' ),
132                         dataType: 'json',
133                         type: 'POST',
134                         data: reqData,
135                         success: function( data, textStatus, xhr ) {
136                                 processResult( data, $link );
137                         },
138                         error: function(){
139                                 processResult( {}, $link );
140                         }
141                 });
143                 return false;
144         });
146         // When a request returns, a custom event 'mw-ajaxwatch' is triggered
147         // on *all* watch links, so they can be updated if necessary
148         $links.bind( 'mw-ajaxwatch', function( event, target, action, $link ) {
149                 var foo = $link.data( 'target' );
150                 if ( $link.data( 'target' ) == target ) {
151                         var otheraction = action == 'watch'
152                                 ? 'unwatch'
153                                 : 'watch';
155                         $link.data( 'action', otheraction );
156                         setLinkText( $link, otheraction );
157                         $link.attr( 'href',
158                                 mw.config.get( 'wgScript' )
159                                 + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) )
160                                 + '&action=' + otheraction
161                         );
162                         if ( $link.closest( 'li' ).attr( 'id' ) == 'ca-' + action ) {
163                                 $link.closest( 'li' ).attr( 'id', 'ca-' + otheraction );
164                                 // update the link text with the new message
165                                 $link.text( mw.msg( otheraction ) );
166                         }
167                 }
169                 return false;
170         });
174 })( jQuery );