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
) {
10 unwatchMsg
: 'Unwatch',
11 watchingMsg
: 'Watching...',
12 unwatchingMsg
: 'Unwatching...',
13 'tooltip-ca-watchMsg': 'Add this page to your watchlist',
14 'tooltip-ca-unwatchMsg': 'Remove this page from your watchlist'
18 wgAjaxWatch
.setLinkText = function( $link
, action
) {
19 if( action
== 'watch' || action
== 'unwatch' ) {
20 // save the accesskey from the title
21 var keyCommand
= $link
.attr( 'title' ).match( /\[.*?\]$/ )
22 ? $link
.attr( 'title' ).match( /\[.*?\]$/ )[0]
24 $link
.attr( 'title', wgAjaxWatch
['tooltip-ca-' + action
+ 'Msg'] + ' ' + keyCommand
);
26 if( $link
.data( 'icon' ) ) {
27 $link
.attr( 'alt', wgAjaxWatch
[action
+ 'Msg'] );
28 if ( action
== 'watching' || action
== 'unwatching' ) {
29 $link
.addClass( 'loading' );
31 $link
.removeClass( 'loading' );
34 $link
.html( wgAjaxWatch
[action
+ 'Msg'] );
38 wgAjaxWatch
.processResult = function( response
) {
39 response
= response
.watch
;
40 var $link
= $j( this );
41 // To ensure we set the same status for all watch links with the
42 // same target we trigger a custom event on *all* watch links.
43 if( response
.watched
!== undefined ) {
44 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'watch'] );
45 } else if ( response
.unwatched
!== undefined ) {
46 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'unwatch'] );
48 // Either we got an error code or it just plain broke.
49 window
.location
.href
= $link
.attr( 'href' );
53 jsMsg( response
.message
, 'watch' );
55 // Bug 12395 - update the watch checkbox on edit pages when the
56 // page is watched or unwatched via the tab.
57 if( response
.watched
!== undefined ) {
58 $j( '#wpWatchthis' ).attr( 'checked', '1' );
60 $j( '#wpWatchthis' ).removeAttr( 'checked' );
64 $j( document
).ready( function() {
65 var $links
= $j( '.mw-watchlink a, a.mw-watchlink' );
66 // BC with older skins
68 .add( $j( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1' ) )
69 .add( $j( 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' ) );
70 // allowing people to add inline animated links is a little scary
71 $links
= $links
.filter( ':not( #bodyContent *, #content * )' );
73 $links
.each( function() {
74 var $link
= $j( this );
76 .data( 'icon', $link
.parents( 'li' ).hasClass( 'icon' ) )
77 .data( 'action', $link
.attr( 'href' ).match( /[\?\&]action=unwatch/i ) ? 'unwatch' : 'watch' );
78 var title
= $link
.attr( 'href' ).match( /[\?\&]title=(.*?)&/i )[1];
79 $link
.data( 'target', decodeURIComponent( title
).replace( /_
/g
, ' ' ) );
82 $links
.click( function( event
) {
83 var $link
= $j( this );
85 if( wgAjaxWatch
.supported
=== false || !wgEnableWriteAPI
|| !wfSupportsAjax() ) {
86 // Lazy initialization so we don't toss up
87 // ActiveX warnings on initial page load
88 // for IE 6 users with security settings.
89 wgAjaxWatch
.$links
.unbind( 'click' );
93 wgAjaxWatch
.setLinkText( $link
, $link
.data( 'action' ) + 'ing' );
95 + '/api' + wgScriptExtension
+ '?action=watch&format=json&title='
96 + encodeURIComponent( $link
.data( 'target' ) )
97 + ( $link
.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
99 wgAjaxWatch
.processResult
,
106 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
107 // on *all* watch links, so they can be updated if necessary
108 $links
.bind( 'mw-ajaxwatch', function( event
, target
, action
) {
109 var $link
= $j( this );
110 var foo
= $link
.data( 'target' );
111 if( $link
.data( 'target' ) == target
) {
112 var otheraction
= action
== 'watch'
116 $link
.data( 'action', otheraction
);
117 wgAjaxWatch
.setLinkText( $link
, otheraction
);
118 $link
.attr( 'href', $link
.attr( 'href' ).replace( '/&action=' + action
+ '/', '&action=' + otheraction
) );
119 if( $link
.parents( 'li' ).attr( 'id' ) == 'ca-' + action
) {
120 $link
.parents( 'li' ).attr( 'id', 'ca-' + otheraction
);
126 wgAjaxWatch
.$links
= $links
;