e051229b412e331dd7a15ec3ceb6f67adb90fa25
[lhc/web/wiklou.git] / skins / common / ajaxwatch.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.message( 'tooltip-ca-' + action ) + ' ' + keyCommand );
16 }
17 if ( $link.data( 'icon' ) ) {
18 $link.attr( 'alt', mediaWiki.message( action ) );
19 if ( action == 'watching' || action == 'unwatching' ) {
20 $link.addClass( 'loading' );
21 } else {
22 $link.removeClass( 'loading' );
23 }
24 } else {
25 $link.html( mediaWiki.message( action ) );
26 }
27 };
28
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'] );
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 .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 * )' );
63
64 $links.each( function() {
65 var $link = $( this );
66 $link
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, ' ' ) );
71 });
72
73 $links.click( function( event ) {
74 var $link = $( this );
75
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' );
81 return true;
82 }
83
84 wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
85 $.get( wgScriptPath
86 + '/api' + wgScriptExtension + '?action=watch&format=json&title='
87 + encodeURIComponent( $link.data( 'target' ) )
88 + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
89 {},
90 wgAjaxWatch.processResult,
91 'json'
92 );
93
94 return false;
95 });
96
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'
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.message( otheraction ) );
114 }
115 };
116 return false;
117 });
118
119 wgAjaxWatch.$links = $links;
120 });