Using the mw alias for mediaWiki in core. Saves bandwidth and makes sense (there...
[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 */
5
6 if ( typeof wgAjaxWatch === 'undefined' || !wgAjaxWatch ) {
7 window.wgAjaxWatch = { };
8 }
9
10 wgAjaxWatch.setLinkText = function( $link, action ) {
11 if ( action == 'watch' || action == 'unwatch' ) {
12 // save the accesskey from the title
13 var keyCommand = $link.attr( 'title' ).match( /\[.*?\]$/ ) ? $link.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
14 $link.attr( 'title', mw.msg( 'tooltip-ca-' + action ) + ' ' + keyCommand );
15 }
16 if ( $link.data( 'icon' ) ) {
17 $link.attr( 'alt', mw.msg( action ) );
18 if ( action == 'watching' || action == 'unwatching' ) {
19 $link.addClass( 'loading' );
20 } else {
21 $link.removeClass( 'loading' );
22 }
23 } else {
24 $link.html( mw.msg( action ) );
25 }
26 };
27
28 wgAjaxWatch.processResult = function( response, $link ) {
29 response = response.watch;
30
31 // To ensure we set the same status for all watch links with the
32 // same target we trigger a custom event on *all* watch links.
33 if( response.watched !== undefined ) {
34 wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'watch', $link] );
35 } else if ( response.unwatched !== undefined ) {
36 wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch', $link] );
37 } else {
38 // Either we got an error code or it just plain broke.
39 window.location.href = $link[0].href;
40 return;
41 }
42
43 mw.util.jsMessage( response.message, 'watch' );
44
45 // Bug 12395 - update the watch checkbox on edit pages when the
46 // page is watched or unwatched via the tab.
47 if( response.watched !== undefined ) {
48 $( '#wpWatchthis' ).attr( 'checked', 'checked' );
49 } else {
50 $( '#wpWatchthis' ).removeAttr( 'checked' );
51 }
52 };
53
54 $( document ).ready( function() {
55 var $links = $( '.mw-watchlink a, a.mw-watchlink' );
56 // BC with older skins
57 $links = $links
58 .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' +
59 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' );
60 // allowing people to add inline animated links is a little scary
61 $links = $links.filter( ':not( #bodyContent *, #content * )' );
62
63 $links.each( function() {
64 var $link = $( this );
65 var link = this;
66 $link
67 .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) )
68 .data( 'action', mw.util.getParamValue( 'action', link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
69 var title = mw.util.getParamValue( 'title', link.href );
70 $link.data( 'target', title.replace( /_/g, ' ' ) );
71 });
72
73 $links.click( function( event ) {
74 var $link = $( this );
75
76 if( wgAjaxWatch.supported === false || !mw.config.get( 'wgEnableWriteAPI' ) ) {
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
86 var reqData = {
87 'action': 'watch',
88 'format': 'json',
89 'title': $link.data( 'target' )
90 };
91 if ( $link.data( 'action' ) == 'unwatch' ) {
92 reqData['unwatch'] = '';
93 }
94 $.getJSON( mw.config.get( 'wgScriptPath' )
95 + '/api' + mw.config.get( 'wgScriptExtension' ),
96 reqData,
97 function( data, textStatus, xhr ) {
98 wgAjaxWatch.processResult( data, $link );
99 }
100 );
101
102 return false;
103 });
104
105 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
106 // on *all* watch links, so they can be updated if necessary
107 $links.bind( 'mw-ajaxwatch', function( event, target, action, $link ) {
108 var foo = $link.data( 'target' );
109 if( $link.data( 'target' ) == target ) {
110 var otheraction = action == 'watch'
111 ? 'unwatch'
112 : 'watch';
113
114 $link.data( 'action', otheraction );
115 wgAjaxWatch.setLinkText( $link, otheraction );
116 $link.attr( 'href', $link.attr( 'href' ).replace( '&action=' + action , '&action=' + otheraction ) );
117 if( $link.closest( 'li' ).attr( 'id' ) == 'ca-' + action ) {
118 $link.closest( 'li' ).attr( 'id', 'ca-' + otheraction );
119 // update the link text with the new message
120 $link.text( mw.msg( otheraction ) );
121 }
122 }
123
124 return false;
125 });
126
127 wgAjaxWatch.$links = $links;
128 });