Follow-up r78150, removed accidental left-over console.logs calls
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.watch.ajax.js
1 /**
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.
5 */
6
7 if ( typeof wgAjaxWatch === 'undefined' || !wgAjaxWatch ) {
8 window.wgAjaxWatch = { };
9 }
10
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 );
16 }
17 if ( $link.data( 'icon' ) ) {
18 $link.attr( 'alt', mediaWiki.msg( action ) );
19 if ( action == 'watching' || action == 'unwatching' ) {
20 $link.addClass( 'loading' );
21 } else {
22 $link.removeClass( 'loading' );
23 }
24 } else {
25 $link.html( mediaWiki.msg( action ) );
26 }
27 };
28
29 wgAjaxWatch.processResult = function( response, $link ) {
30 response = response.watch;
31
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', $link] );
36 } else if ( response.unwatched !== undefined ) {
37 wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch', $link] );
38 } else {
39 // Either we got an error code or it just plain broke.
40 window.location.href = $link.attr( 'href' );
41 return;
42 }
43
44 jsMsg( response.message, 'watch' );
45
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' );
50 } else {
51 $( '#wpWatchthis' ).removeAttr( 'checked' );
52 }
53 };
54
55 $( document ).ready( function() {
56 var $links = $( '.mw-watchlink a, a.mw-watchlink' );
57 // BC with older skins
58 $links = $links
59 .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' +
60 '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 * )' );
63
64 $links.each( function() {
65 var $link = $( this );
66 var link = this;
67 $link
68 .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) )
69 .data( 'action', mw.util.getParamValue( 'action', link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
70 var title = mw.util.getParamValue( 'title', link.href );
71 $link.data( 'target', title.replace( /_/g, ' ' ) );
72 });
73
74 $links.click( function( event ) {
75 var $link = $( this );
76
77 if( wgAjaxWatch.supported === false || !wgEnableWriteAPI || !wfSupportsAjax() ) {
78 // Lazy initialization so we don't toss up
79 // ActiveX warnings on initial page load
80 // for IE 6 users with security settings.
81 wgAjaxWatch.$links.unbind( 'click' );
82 return true;
83 }
84
85 wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
86 $.getJSON( wgScriptPath
87 + '/api' + wgScriptExtension + '?action=watch&format=json&title='
88 + encodeURIComponent( $link.data( 'target' ) )
89 + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
90 function( data, textStatus, xhr ) {
91 wgAjaxWatch.processResult( data, $link );
92 }
93 );
94
95 return false;
96 });
97
98 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
99 // on *all* watch links, so they can be updated if necessary
100 $links.bind( 'mw-ajaxwatch', function( event, target, action, $link ) {
101 var foo = $link.data( 'target' );
102 if( $link.data( 'target' ) == target ) {
103 var otheraction = action == 'watch'
104 ? 'unwatch'
105 : 'watch';
106
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 ) );
114 }
115 }
116
117 return false;
118 });
119
120 wgAjaxWatch.$links = $links;
121 });