Undo browser defaults (underline/strike) for the <del> and <ins> DairikiDiff inserts...
[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 ( function( $ ) {
6 var $links;
7
8 var setLinkText = function( $link, action ) {
9 if ( action == 'watch' || action == 'unwatch' ) {
10 // save the accesskey from the title
11 var keyCommand = $link.attr( 'title' ).match( /\[.*?\]$/ ) ? $link.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
12 $link.attr( 'title', mw.msg( 'tooltip-ca-' + action ) + ' ' + keyCommand );
13 }
14 if ( $link.data( 'icon' ) ) {
15 $link.attr( 'alt', mw.msg( action ) );
16 if ( action == 'watching' || action == 'unwatching' ) {
17 $link.addClass( 'loading' );
18 } else {
19 $link.removeClass( 'loading' );
20 }
21 } else {
22 $link.html( mw.msg( action ) );
23 }
24 };
25
26 var errorHandler = function( $link ) {
27
28 // Reset link text to whatever it was before we switching it to the '(un)watch'+ing message.
29 setLinkText( $link, $link.data( 'action' ) );
30
31 // Format error message
32 var cleanTitle = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
33 var link = mw.html.element(
34 'a', {
35 'href': mw.util.wikiGetlink( mw.config.get( 'wgPageName' ) ),
36 'title': cleanTitle
37 }, cleanTitle
38 );
39 var msg = mw.msg( 'watcherrortext', link );
40
41 // Report to user about the error
42 mw.util.jsMessage( msg, 'watch' );
43 };
44
45 /**
46 * Process the result of the API watch action.
47 *
48 * @param response Data object from API request.
49 * @param $link jQuery object of the watch link.
50 * @return Boolean true on success, false otherwise.
51 */
52 var processResult = function( response, $link ) {
53
54 if ( ( 'error' in response ) || !response.watch ) {
55 errorHandler( $link );
56 return false;
57 }
58
59 var watchResponse = response.watch;
60
61 // To ensure we set the same status for all watch links with the
62 // same target we trigger a custom event on *all* watch links.
63 if ( watchResponse.watched !== undefined ) {
64 $links.trigger( 'mw-ajaxwatch', [watchResponse.title, 'watch', $link] );
65 } else if ( watchResponse.unwatched !== undefined ) {
66 $links.trigger( 'mw-ajaxwatch', [watchResponse.title, 'unwatch', $link] );
67 } else {
68 // Either we got an error code or it just plain broke.
69 window.location.href = $link[0].href;
70 return false;
71 }
72
73 mw.util.jsMessage( watchResponse.message, 'watch' );
74
75 // Bug 12395 - update the watch checkbox on edit pages when the
76 // page is watched or unwatched via the tab.
77 if ( watchResponse.watched !== undefined ) {
78 $( '#wpWatchthis' ).attr( 'checked', 'checked' );
79 } else {
80 $( '#wpWatchthis' ).removeAttr( 'checked' );
81 }
82 return true;
83 };
84
85 $( document ).ready( function() {
86 $links = $( '.mw-watchlink a, a.mw-watchlink' );
87 // BC with older skins
88 $links = $links
89 .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' +
90 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' );
91 // allowing people to add inline animated links is a little scary
92 $links = $links.filter( ':not( #bodyContent *, #content * )' );
93
94 $links.each( function() {
95 var $link = $( this );
96 var link = this;
97 $link
98 .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) )
99 .data( 'action', mw.util.getParamValue( 'action', link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
100 var title = mw.util.getParamValue( 'title', link.href );
101 $link.data( 'target', title.replace( /_/g, ' ' ) );
102 });
103
104 $links.click( function( event ) {
105 var $link = $( this );
106
107 if ( !mw.config.get( 'wgEnableWriteAPI' ) ) {
108 // Lazy initialization so we don't toss up
109 // ActiveX warnings on initial page load
110 // for IE 6 users with security settings.
111 $links.unbind( 'click' );
112 return true;
113 }
114
115 setLinkText( $link, $link.data( 'action' ) + 'ing' );
116
117 var reqData = {
118 'action': 'watch',
119 'format': 'json',
120 'title': $link.data( 'target' ),
121 'token': mw.user.tokens.get( 'watchToken' ),
122 // API return contains a localized data.watch.message string.
123 'uselang': mw.config.get( 'wgUserLanguage' )
124 };
125
126 if ( $link.data( 'action' ) == 'unwatch' ) {
127 reqData.unwatch = '';
128 }
129
130 $.ajax({
131 url: mw.util.wikiScript( 'api' ),
132 dataType: 'json',
133 type: 'POST',
134 data: reqData,
135 success: function( data, textStatus, xhr ) {
136 processResult( data, $link );
137 },
138 error: function(){
139 processResult( {}, $link );
140 }
141 });
142
143 return false;
144 });
145
146 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
147 // on *all* watch links, so they can be updated if necessary
148 $links.bind( 'mw-ajaxwatch', function( event, target, action, $link ) {
149 var foo = $link.data( 'target' );
150 if ( $link.data( 'target' ) == target ) {
151 var otheraction = action == 'watch'
152 ? 'unwatch'
153 : 'watch';
154
155 $link.data( 'action', otheraction );
156 setLinkText( $link, otheraction );
157 $link.attr( 'href',
158 mw.config.get( 'wgScript' )
159 + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) )
160 + '&action=' + otheraction
161 );
162 if ( $link.closest( 'li' ).attr( 'id' ) == 'ca-' + action ) {
163 $link.closest( 'li' ).attr( 'id', 'ca-' + otheraction );
164 // update the link text with the new message
165 $link.text( mw.msg( otheraction ) );
166 }
167 }
168
169 return false;
170 });
171
172 });
173
174 })( jQuery );