ajaxwatch.js: coding style tweaks
[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 var wgAjaxWatch = {
9 watchMsg: 'Watch',
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'
15 };
16 }
17
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]
23 : '';
24 $link.attr( 'title', wgAjaxWatch['tooltip-ca-' + action + 'Msg'] + ' ' + keyCommand );
25 }
26 if( $link.data( 'icon' ) ) {
27 $link.attr( 'alt', wgAjaxWatch[action + 'Msg'] );
28 if ( action == 'watching' || action == 'unwatching' ) {
29 $link.addClass( 'loading' );
30 } else {
31 $link.removeClass( 'loading' );
32 }
33 } else {
34 $link.html( wgAjaxWatch[action + 'Msg'] );
35 }
36 };
37
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'] );
47 } else {
48 // Either we got an error code or it just plain broke.
49 window.location.href = $link.attr( 'href' );
50 return;
51 }
52
53 jsMsg( response.message, 'watch' );
54
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' );
59 } else {
60 $j( '#wpWatchthis' ).removeAttr( 'checked' );
61 }
62 };
63
64 $j( document ).ready( function() {
65 var $links = $j( '.mw-watchlink a, a.mw-watchlink' );
66 // BC with older skins
67 $links = $links
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 * )' );
72
73 $links.each( function() {
74 var $link = $j( this );
75 $link
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, ' ' ) );
80 });
81
82 $links.click( function( event ) {
83 var $link = $j( this );
84
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' );
90 return true;
91 }
92
93 wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
94 $j.get( wgScriptPath
95 + '/api' + wgScriptExtension + '?action=watch&format=json&title='
96 + encodeURIComponent( $link.data( 'target' ) )
97 + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
98 {},
99 wgAjaxWatch.processResult,
100 'json'
101 );
102
103 return false;
104 });
105
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'
113 ? 'unwatch'
114 : 'watch';
115
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 );
121 }
122 };
123 return false;
124 });
125
126 wgAjaxWatch.$links = $links;
127 });