d2d24fafd3cc8fb4d38242c617763f3689e3173c
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
;
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' );
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(){
76 .data( 'icon', $link
.parent().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
, ' ' ) );
83 $links
.click( function(event
){
86 if( wgAjaxWatch
.supported
=== false || !wfSupportsAjax() ) {
87 // Lazy initialization so we don't toss up
88 // ActiveX warnings on initial page load
89 // for IE 6 users with security settings.
90 wgAjaxWatch
.$links
.unbind( 'click' );
94 wgAjaxWatch
.setLinkText( $link
, $link
.data('action')+'ing' );
96 + '/api.php?action=watch&format=json&title='
97 + $link
.data('target')
98 + ( $link
.data('action')=='unwatch' ? '&unwatch' : '' ),
100 wgAjaxWatch
.processResult
,
107 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
108 // on *all* watch links, so they can be updated if necessary
109 $links
.bind( 'mw-ajaxwatch', function( event
, target
, action
){
110 var $link
= $j(this);
111 var foo
= $link
.data( 'target' );
112 if( $link
.data( 'target' ) == target
){
113 var otheraction
= action
== 'watch'
117 $link
.data( 'action', otheraction
);
118 wgAjaxWatch
.setLinkText( $link
, otheraction
);
119 $link
.attr( 'href', $link
.attr('href').replace( '/&action='+action
+'/', '&action='+otheraction
) );
120 if( $link
.parent().attr('id') == 'ca-'+action
){
121 $link
.parent().attr( 'id', 'ca-'+otheraction
);
127 wgAjaxWatch
.$links
= $links
;