2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than navigating to a different URI.
7 * The name of the page to watch or unwatch.
9 var title
= mw
.config
.get( 'wgRelevantPageName', mw
.config
.get( 'wgPageName' ) );
12 * Update the link text, link href attribute and (if applicable)
15 * @param $link {jQuery} Anchor tag of (un)watch link.
16 * @param action {String} One of 'watch', 'unwatch'.
17 * @param state {String} [optional] 'idle' or 'loading'. Default is 'idle'.
19 function updateWatchLink( $link
, action
, state
) {
20 var accesskeyTip
, msgKey
, $li
, otherAction
;
22 // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
23 msgKey
= state
=== 'loading' ? action
+ 'ing' : action
;
24 otherAction
= action
=== 'watch' ? 'unwatch' : 'watch';
25 accesskeyTip
= $link
.attr( 'title' ).match( mw
.util
.tooltipAccessKeyRegexp
);
26 $li
= $link
.closest( 'li' );
28 * Trigger a 'watchpage' event for this List item.
29 * Announce the otherAction value as the first param.
30 * Used to monitor the state of watch link.
31 * TODO: Revise when system wide hooks are implemented
33 if( state
=== undefined ) {
34 $li
.trigger( 'watchpage.mw', otherAction
);
38 .text( mw
.msg( msgKey
) )
39 .attr( 'title', mw
.msg( 'tooltip-ca-' + action
) +
40 ( accesskeyTip
? ' ' + accesskeyTip
[0] : '' )
42 .attr( 'href', mw
.util
.wikiScript() + '?' + $.param({
48 // Most common ID style
49 if ( $li
.prop( 'id' ) === 'ca-' + otherAction
) {
50 $li
.prop( 'id', 'ca-' + action
);
53 // Special case for vector icon
54 if ( $li
.hasClass( 'icon' ) ) {
55 if ( state
=== 'loading' ) {
56 $link
.addClass( 'loading' );
58 $link
.removeClass( 'loading' );
64 * @todo This should be moved somewhere more accessible.
66 * @return {String} The extracted action, defaults to 'view'.
68 function mwUriGetAction( url
) {
69 var action
, actionPaths
, key
, i
, m
, parts
;
71 actionPaths
= mw
.config
.get( 'wgActionPaths' );
73 // @todo: Does MediaWiki give action path or query param
74 // precedence ? If the former, move this to the bottom
75 action
= mw
.util
.getParamValue( 'action', url
);
76 if ( action
!== null ) {
80 for ( key
in actionPaths
) {
81 if ( actionPaths
.hasOwnProperty( key
) ) {
82 parts
= actionPaths
[key
].split( '$1' );
83 for ( i
= 0; i
< parts
.length
; i
+= 1 ) {
84 parts
[i
] = $.escapeRE( parts
[i
] );
86 m
= new RegExp( parts
.join( '(.+)' ) ).exec( url
);
97 // Expose local methods
99 'updateWatchLink': updateWatchLink
102 $( document
).ready( function () {
103 var $links
= $( '.mw-watchlink a, a.mw-watchlink, ' +
104 '#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
105 '#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
107 // Allowing people to add inline animated links is a little scary
108 $links
= $links
.filter( ':not( #bodyContent *, #content * )' );
110 $links
.click( function ( e
) {
111 var action
, api
, $link
;
113 action
= mwUriGetAction( this.href
);
115 if ( action
!== 'watch' && action
!== 'unwatch' ) {
116 // Could not extract target action from link url,
117 // let native browsing handle it further
125 updateWatchLink( $link
, action
, 'loading' );
131 function ( watchResponse
) {
132 var $li
, otherAction
;
134 otherAction
= action
=== 'watch' ? 'unwatch' : 'watch';
135 $li
= $link
.closest( 'li' );
137 mw
.notify( $.parseHTML( watchResponse
.message
), { tag
: 'watch-self' } );
139 // Set link to opposite
140 updateWatchLink( $link
, otherAction
);
142 // Bug 12395 - update the watch checkbox on edit pages when the
143 // page is watched or unwatched via the tab.
144 if ( watchResponse
.watched
!== undefined ) {
145 $( '#wpWatchthis' ).prop( 'checked', true );
147 $( '#wpWatchthis' ).removeProp( 'checked' );
152 var cleanTitle
, msg
, link
;
154 // Reset link to non-loading mode
155 updateWatchLink( $link
, action
);
157 // Format error message
158 cleanTitle
= title
.replace( /_
/g
, ' ' );
159 link
= mw
.html
.element(
161 href
: mw
.util
.wikiGetlink( title
),
165 msg
= mw
.messsage( 'watcherrortext', link
);
167 // Report to user about the error
168 mw
.notify( msg
, { tag
: 'watch-self' } );
175 }( mediaWiki
, jQuery
) );