93aa29c9085384674218f2dac2236135a7b55049
2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than clicking on links. Requires jQuery.
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
);
14 if ( $link
.data( 'icon' ) ) {
15 $link
.attr( 'alt', mw
.msg( action
) );
16 if ( action
== 'watching' || action
== 'unwatching' ) {
17 $link
.addClass( 'loading' );
19 $link
.removeClass( 'loading' );
22 $link
.html( mw
.msg( action
) );
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(
35 'href': mw
.util
.wikiGetlink( mw
.config
.get( 'wgPageName' ) ),
39 var msg
= mw
.msg( 'watcherrortext', link
);
41 // Report to user about the error
42 mw
.util
.jsMessage( msg
, 'watch' );
46 * Process the result of the API watch action.
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.
52 var processResult = function( response
, $link
) {
54 if ( ( 'error' in response
) || !response
.watch
) {
55 errorHandler( $link
);
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
] );
68 // Either we got an error code or it just plain broke.
69 window
.location
.href
= $link
[0].href
;
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' );
80 $( '#wpWatchthis' ).removeAttr( 'checked' );
85 $( document
).ready( function() {
86 $links
= $( '.mw-watchlink a, a.mw-watchlink' );
87 // BC with older skins
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 );
98 .data( 'icon', $link
.closest( 'li' ).hasClass( 'icon' ) )
99 .data( 'action', mw
.util
.getParamValue( 'action', link
.href
) == 'unwatch' ? 'unwatch' : 'watch' );
100 var title
= mw
.util
.getParamValue( 'title', link
.href
);
101 $link
.data( 'target', title
.replace( /_
/g
, ' ' ) );
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' );
115 setLinkText( $link
, $link
.data( 'action' ) + 'ing' );
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' )
126 if ( $link
.data( 'action' ) == 'unwatch' ) {
127 reqData
.unwatch
= '';
131 url
: mw
.util
.wikiScript( 'api' ),
135 success: function( data
, textStatus
, xhr
) {
136 processResult( data
, $link
);
139 processResult( {}, $link
);
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'
155 $link
.data( 'action', otheraction
);
156 setLinkText( $link
, otheraction
);
158 mw
.config
.get( 'wgScript' )
159 + '?title=' + mw
.util
.wikiUrlencode( mw
.config
.get( 'wgPageName' ) )
160 + '&action=' + otheraction
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
) );