2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than clicking on links. Requires jQuery.
4 * Uses jsMsg() from wikibits.js.
7 if ( typeof wgAjaxWatch
=== 'undefined' || !wgAjaxWatch
) {
8 window
.wgAjaxWatch
= { };
11 wgAjaxWatch
.setLinkText = function( $link
, action
) {
12 if ( action
== 'watch' || action
== 'unwatch' ) {
13 // save the accesskey from the title
14 var keyCommand
= $link
.attr( 'title' ).match( /\[.*?\]$/ ) ? $link
.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
15 $link
.attr( 'title', mediaWiki
.msg( 'tooltip-ca-' + action
) + ' ' + keyCommand
);
17 if ( $link
.data( 'icon' ) ) {
18 $link
.attr( 'alt', mediaWiki
.msg( action
) );
19 if ( action
== 'watching' || action
== 'unwatching' ) {
20 $link
.addClass( 'loading' );
22 $link
.removeClass( 'loading' );
25 $link
.html( mediaWiki
.msg( action
) );
29 wgAjaxWatch
.processResult = function( response
, $link
) {
30 response
= response
.watch
;
32 // To ensure we set the same status for all watch links with the
33 // same target we trigger a custom event on *all* watch links.
34 if( response
.watched
!== undefined ) {
35 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'watch', $link
] );
36 } else if ( response
.unwatched
!== undefined ) {
37 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'unwatch', $link
] );
39 // Either we got an error code or it just plain broke.
40 window
.location
.href
= $link
.attr( 'href' );
44 jsMsg( response
.message
, 'watch' );
46 // Bug 12395 - update the watch checkbox on edit pages when the
47 // page is watched or unwatched via the tab.
48 if( response
.watched
!== undefined ) {
49 $( '#wpWatchthis' ).attr( 'checked', '1' );
51 $( '#wpWatchthis' ).removeAttr( 'checked' );
55 $( document
).ready( function() {
56 var $links
= $( '.mw-watchlink a, a.mw-watchlink' );
57 // BC with older skins
59 .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' +
60 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' );
61 // allowing people to add inline animated links is a little scary
62 $links
= $links
.filter( ':not( #bodyContent *, #content * )' );
64 $links
.each( function() {
65 var $link
= $( this );
68 .data( 'icon', $link
.closest( 'li' ).hasClass( 'icon' ) )
69 .data( 'action', mw
.util
.getParamValue( 'action', link
.href
) == 'unwatch' ? 'unwatch' : 'watch' );
70 var title
= mw
.util
.getParamValue( 'title', link
.href
);
71 $link
.data( 'target', title
.replace( /_
/g
, ' ' ) );
74 $links
.click( function( event
) {
75 var $link
= $( this );
77 if( wgAjaxWatch
.supported
=== false || !wgEnableWriteAPI
|| !wfSupportsAjax() ) {
78 // Lazy initialization so we don't toss up
79 // ActiveX warnings on initial page load
80 // for IE 6 users with security settings.
81 wgAjaxWatch
.$links
.unbind( 'click' );
85 wgAjaxWatch
.setLinkText( $link
, $link
.data( 'action' ) + 'ing' );
86 $.getJSON( wgScriptPath
87 + '/api' + wgScriptExtension
+ '?action=watch&format=json&title='
88 + encodeURIComponent( $link
.data( 'target' ) )
89 + ( $link
.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
90 function( data
, textStatus
, xhr
) {
91 wgAjaxWatch
.processResult( data
, $link
);
98 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
99 // on *all* watch links, so they can be updated if necessary
100 $links
.bind( 'mw-ajaxwatch', function( event
, target
, action
, $link
) {
101 var foo
= $link
.data( 'target' );
102 if( $link
.data( 'target' ) == target
) {
103 var otheraction
= action
== 'watch'
107 $link
.data( 'action', otheraction
);
108 wgAjaxWatch
.setLinkText( $link
, otheraction
);
109 $link
.attr( 'href', $link
.attr( 'href' ).replace( '&action=' + action
, '&action=' + otheraction
) );
110 if( $link
.parents( 'li' ).attr( 'id' ) == 'ca-' + action
) {
111 $link
.parents( 'li' ).attr( 'id', 'ca-' + otheraction
);
112 // update the link text with the new message
113 $link
.text( mediaWiki
.msg( otheraction
) );
120 wgAjaxWatch
.$links
= $links
;