Follow-up per r65570 CR, also check wgEnableWriteAPI.
[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.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, ' ' ) );
80
81 });
82
83 $links.click( function(event){
84 var $link = $j(this);
85
86 if( wgAjaxWatch.supported === false || !wgEnableWriteAPI || !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' );
91 return true;
92 }
93
94 wgAjaxWatch.setLinkText( $link, $link.data('action')+'ing' );
95 $j.get( wgScriptPath
96 + '/api' + wgScriptExtension + '?action=watch&format=json&title='
97 + encodeURIComponent( $link.data('target') )
98 + ( $link.data('action')=='unwatch' ? '&unwatch' : '' ),
99 {},
100 wgAjaxWatch.processResult,
101 'json'
102 );
103
104 return false;
105 });
106
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'
114 ? 'unwatch'
115 : 'watch';
116
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 );
122 }
123 };
124 return false;
125 });
126
127 wgAjaxWatch.$links = $links;
128 });