2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than navigating to a different URI.
5 ( function ( $, mw, undefined ) {
8 * Update the link text, link href attribute and (if applicable)
11 * @param $link {jQuery} Anchor tag of (un)watch link
12 * @param action {String} One of 'watch', 'unwatch'.
13 * @param state {String} [optional] 'idle' or 'loading'. Default is 'idle'.
15 function updateWatchLink( $link, action, state ) {
16 // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
17 var msgKey = state === 'loading' ? action + 'ing' : action,
18 accesskeyTip = $link.attr( 'title' ).match( mw.util.tooltipAccessKeyRegexp ),
19 $li = $link.closest( 'li' );
22 .text( mw.msg( msgKey ) )
23 .attr( 'title', mw.msg( 'tooltip-ca-' + action ) +
24 ( accesskeyTip ? ' ' + accesskeyTip[0] : '' )
26 .attr( 'href', mw.util.wikiScript() + '?' + $.param({
27 title: mw.config.get( 'wgPageName' ),
32 // Special case for vector icon
33 if ( $li.hasClass( 'icon' ) ) {
34 if ( state === 'loading' ) {
35 $link.addClass( 'loading' );
37 $link.removeClass( 'loading' );
43 * @todo This should be moved somewhere more accessible.
45 * @return {String} The extracted action, defaults to 'view'.
47 function mwUriGetAction( url ) {
48 var actionPaths = mw.config.get( 'wgActionPaths' ),
49 key, parts, m, action;
51 // @todo: Does MediaWiki give action path or query param
52 // precedence ? If the former, move this to the bottom
53 action = mw.util.getParamValue( 'action', url );
54 if ( action !== null ) {
58 for ( key in actionPaths ) {
59 if ( actionPaths.hasOwnProperty( key ) ) {
60 parts = actionPaths[key].split( '$1' );
61 for ( i = 0; i < parts.length; i += 1 ) {
62 parts[i] = $.escapeRE( parts[i] );
64 m = new RegExp( parts.join( '(.+)' ) ).exec( url );
75 $( document ).ready( function() {
76 var $links = $( '.mw-watchlink a, a.mw-watchlink, ' +
77 '#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
78 '#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
80 // Allowing people to add inline animated links is a little scary
81 $links = $links.filter( ':not( #bodyContent *, #content * )' );
83 $links.click( function( e ) {
85 action = mwUriGetAction( this.href );
87 if ( action !== 'watch' && action !== 'unwatch' ) {
88 // Could not extract target action from link url,
89 // let native browsing handle it further
97 updateWatchLink( $link, action, 'loading' );
101 mw.config.get( 'wgPageName' ),
103 function( watchResponse ) {
104 var otherAction = action === 'watch' ? 'unwatch' : 'watch',
105 $li = $link.closest( 'li' );
107 mw.util.jsMessage( watchResponse.message, 'ajaxwatch' );
109 // Set link to opposite
110 updateWatchLink( $link, otherAction );
112 // Most common ID style
113 if ( $li.prop( 'id' ) === 'ca-' + otherAction || $li.prop( 'id' ) === 'ca-' + action ) {
114 $li.prop( 'id', 'ca-' + otherAction );
117 // Bug 12395 - update the watch checkbox on edit pages when the
118 // page is watched or unwatched via the tab.
119 if ( watchResponse.watched !== undefined ) {
120 $( '#wpWatchthis' ).prop( 'checked', true );
122 $( '#wpWatchthis' ).removeProp( 'checked' );
128 // Reset link to non-loading mode
129 updateWatchLink( $link, action );
131 // Format error message
132 var cleanTitle = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
133 var link = mw.html.element(
135 'href': mw.util.wikiGetlink( mw.config.get( 'wgPageName' ) ),
139 var html = mw.msg( 'watcherrortext', link );
141 // Report to user about the error
142 mw.util.jsMessage( html, 'ajaxwatch' );
150 })( jQuery, mediaWiki );