2 * Legacy emulation for the now depricated skins/common/ajaxwatch.js
4 * AJAX functionality for the watch/unwatch link
6 * @depends mw.legacy.jsMsg() from mw.legacy.wikibits.js
13 $.extend( true, mw
.legacy
, {
15 /* Global Variables */
19 /* Global Variables */
22 'unwatchMsg': 'Unwatch',
23 'watchingMsg': 'Watching...',
24 'unwatchingMsg': 'Unwatching...',
25 'tooltip-ca-watchMsg': 'Add this page to your watchlist',
26 'tooltip-ca-unwatchMsg': 'Remove this page from your watchlist',
31 * Sets the text of the watch/unwatch link
33 * @param object link DOM node or jQuery selection of link to set text of
34 * @param string action message to use ('watch', 'unwatch', 'watching' or 'unwatching')
36 'setLinkText': function( link
, action
) {
37 var $link
= $( link
);
38 if ( action
== 'watch' || action
== 'unwatch' ) {
39 // save the accesskey from the title
40 var keyCommand
= $link
.attr( 'title' ).match( /\[.*?\]$/ ) ?
41 $link
.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
42 $link
.attr( 'title', wgAjaxWatch
['tooltip-ca-' + action
+ 'Msg'] + ' ' + keyCommand
);
44 if ( $link
.data( 'icon' ) ) {
45 $link
.attr( 'alt', wgAjaxWatch
[action
+ 'Msg'] );
46 if ( action
== 'watching' || action
== 'unwatching' ) {
47 $link
.addClass( 'loading' );
49 $link
.removeClass( 'loading' );
52 $link
.html( wgAjaxWatch
[action
+'Msg'] );
56 * Processes responses from the server
58 * @param object response data from server
60 'processResult': function( response
) {
61 response
= response
.watch
;
63 // To ensure we set the same status for all watch links with the same target we trigger a custom event on
65 if ( response
.watched
!== undefined ) {
66 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'watch'] );
67 } else if ( response
.unwatched
!== undefined ){
68 wgAjaxWatch
.$links
.trigger( 'mw-ajaxwatch', [response
.title
, 'unwatch'] );
70 // Either we got an error code or it just plain broke.
71 window
.location
.href
= $link
.attr( 'href' );
74 mw
.legacy
.jsMsg( response
.message
, 'watch' );
75 // Bug 12395 - update the watch checkbox on edit pages when the page is watched or unwatched via the tab.
76 if ( response
.watched
!== undefined ) {
77 $j( '#wpWatchthis' ).attr( 'checked', '1' );
79 $j( '#wpWatchthis' ).removeAttr( 'checked' );
87 $( document
).ready( function() {
88 var $links
= $( '.mw-watchlink a, a.mw-watchlink' );
89 // BC with older skins...
91 .add( $( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1' ) )
92 .add( $( 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' ) );
93 // ...allowing people to add inline animated links is a little scary
94 $links
= $links
.filter( ':not( #bodyContent *, #content * )' );
95 $links
.each( function() {
98 .data( 'icon', $link
.parent().hasClass( 'icon' ) )
99 .data( 'action', $link
.attr( 'href' ).match( /[\?\&]action=unwatch/i ) ? 'unwatch' : 'watch' );
100 var title
= $link
.attr( 'href' ).match( /[\?\&]title=(.*?)&/i )[1];
101 $link
.data( 'target', decodeURIComponent( title
).replace( /_
/g
, ' ' ) );
103 $links
.click( function( event
) {
105 if ( mw
.legacy
.wgAjaxWatch
.supported
=== false || !mw
.legacy
.wgEnableWriteAPI
|| !mw
.legacy
.wfSupportsAjax() ) {
106 // Lazy initialization so we don't toss up ActiveX warnings on initial page load for IE 6 users with
107 // security settings.
108 mw
.legacy
.wgAjaxWatch
.$links
.unbind( 'click' );
111 mw
.legacy
.wgAjaxWatch
.setLinkText( $link
, $link
.data( 'action' ) + 'ing' );
112 var url
= mw
.legacy
.wgScriptPath
+ '/api' + mw
.legacy
.wgScriptExtension
+ '?action=watch&format=json&title='
113 + encodeURIComponent( $link
.data( 'target' ) ) + ( $link
.data( 'action' ) == 'unwatch' ? '&unwatch' : '' );
114 $.get( url
, {}, mw
.legacy
.wgAjaxWatch
.processResult
, 'json' );
117 // When a request returns, a custom event 'mw-ajaxwatch' is triggered on *all* watch links, so they can be updated
119 $links
.bind( 'mw-ajaxwatch', function( event
, target
, action
) {
121 var foo
= $link
.data( 'target' );
122 if ( $link
.data( 'target' ) == target
) {
123 var otheraction
= action
== 'watch' ? 'unwatch' : 'watch';
124 $link
.data( 'action', otheraction
);
125 wgAjaxWatch
.setLinkText( $link
, otheraction
);
126 $link
.attr( 'href', $link
.attr( 'href' ).replace( '/&action=' + action
+ '/', '&action=' + otheraction
) );
127 if ( $link
.parent().attr( 'id' ) == 'ca-' + action
){
128 $link
.parent().attr( 'id', 'ca-' + otheraction
);
133 mw
.legacy
.wgAjaxWatch
.$links
= $links
;
136 } )( jQuery
, mediaWiki
);