2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than clicking on links. Requires jQuery.
6 if ( typeof wgAjaxWatch
=== 'undefined' || !wgAjaxWatch
) {
7 window
.wgAjaxWatch
= { };
10 wgAjaxWatch
.setLinkText = function( $link
, action
) {
11 if ( action
== 'watch' || action
== 'unwatch' ) {
12 // save the accesskey from the title
13 var keyCommand
= $link
.attr( 'title' ).match( /\[.*?\]$/ ) ? $link
.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
14 $link
.attr( 'title', mw
.msg( 'tooltip-ca-' + action
) + ' ' + keyCommand
);
16 if ( $link
.data( 'icon' ) ) {
17 $link
.attr( 'alt', mw
.msg( action
) );
18 if ( action
== 'watching' || action
== 'unwatching' ) {
19 $link
.addClass( 'loading' );
21 $link
.removeClass( 'loading' );
24 $link
.html( mw
.msg( action
) );
28 wgAjaxWatch
.processResult = function( response
, $link
) {
29 response
= response
.watch
;
31 // To ensure we set the same status for all watch links with the
32 // same target we trigger a custom event on *all* watch links.
33 if( response
.watched
!== undefined ) {
34 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'watch', $link
] );
35 } else if ( response
.unwatched
!== undefined ) {
36 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'unwatch', $link
] );
38 // Either we got an error code or it just plain broke.
39 window
.location
.href
= $link
[0].href
;
43 mw
.util
.jsMessage( response
.message
, 'watch' );
45 // Bug 12395 - update the watch checkbox on edit pages when the
46 // page is watched or unwatched via the tab.
47 if( response
.watched
!== undefined ) {
48 $( '#wpWatchthis' ).attr( 'checked', 'checked' );
50 $( '#wpWatchthis' ).removeAttr( 'checked' );
54 $( document
).ready( function() {
55 var $links
= $( '.mw-watchlink a, a.mw-watchlink' );
56 // BC with older skins
58 .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' +
59 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' );
60 // allowing people to add inline animated links is a little scary
61 $links
= $links
.filter( ':not( #bodyContent *, #content * )' );
63 $links
.each( function() {
64 var $link
= $( this );
67 .data( 'icon', $link
.closest( 'li' ).hasClass( 'icon' ) )
68 .data( 'action', mw
.util
.getParamValue( 'action', link
.href
) == 'unwatch' ? 'unwatch' : 'watch' );
69 var title
= mw
.util
.getParamValue( 'title', link
.href
);
70 $link
.data( 'target', title
.replace( /_
/g
, ' ' ) );
73 $links
.click( function( event
) {
74 var $link
= $( this );
76 if( wgAjaxWatch
.supported
=== false || !mw
.config
.get( 'wgEnableWriteAPI' ) ) {
77 // Lazy initialization so we don't toss up
78 // ActiveX warnings on initial page load
79 // for IE 6 users with security settings.
80 wgAjaxWatch
.$links
.unbind( 'click' );
84 wgAjaxWatch
.setLinkText( $link
, $link
.data( 'action' ) + 'ing' );
89 'title': $link
.data( 'target' )
91 if ( $link
.data( 'action' ) == 'unwatch' ) {
92 reqData
['unwatch'] = '';
94 $.getJSON( mw
.config
.get( 'wgScriptPath' )
95 + '/api' + mw
.config
.get( 'wgScriptExtension' ),
97 function( data
, textStatus
, xhr
) {
98 wgAjaxWatch
.processResult( data
, $link
);
105 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
106 // on *all* watch links, so they can be updated if necessary
107 $links
.bind( 'mw-ajaxwatch', function( event
, target
, action
, $link
) {
108 var foo
= $link
.data( 'target' );
109 if( $link
.data( 'target' ) == target
) {
110 var otheraction
= action
== 'watch'
114 $link
.data( 'action', otheraction
);
115 wgAjaxWatch
.setLinkText( $link
, otheraction
);
116 $link
.attr( 'href', $link
.attr( 'href' ).replace( '&action=' + action
, '&action=' + otheraction
) );
117 if( $link
.closest( 'li' ).attr( 'id' ) == 'ca-' + action
) {
118 $link
.closest( 'li' ).attr( 'id', 'ca-' + otheraction
);
119 // update the link text with the new message
120 $link
.text( mw
.msg( otheraction
) );
127 wgAjaxWatch
.$links
= $links
;