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 ) {
30 response = response.watch;
31 var $link = $( this );
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'] );
36 } else if ( response.unwatched !== undefined ) {
37 wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch'] );
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 .add( $( '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 );
67 .data( 'icon', $link.parents( 'li' ).hasClass( 'icon' ) )
68 .data( 'action', $link.attr( 'href' ).match( /[\?&]action=unwatch/i ) ? 'unwatch' : 'watch' );
69 var title = $link.attr( 'href' ).match( /[\?&]title=(.*?)&/i )[1];
70 $link.data( 'target', decodeURIComponent( title ).replace( /_/g, ' ' ) );
73 $links.click( function( event ) {
74 var $link = $( this );
76 if( wgAjaxWatch.supported === false || !wgEnableWriteAPI || !wfSupportsAjax() ) {
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' );
86 + '/api' + wgScriptExtension + '?action=watch&format=json&title='
87 + encodeURIComponent( $link.data( 'target' ) )
88 + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
90 wgAjaxWatch.processResult,
97 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
98 // on *all* watch links, so they can be updated if necessary
99 $links.bind( 'mw-ajaxwatch', function( event, target, action ) {
100 var $link = $( this );
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 ) );
119 wgAjaxWatch.$links = $links;