2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than navigating to a different URI.
5 * @class mw.page.watch.ajax
8 // 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 {jQuery} $link Anchor tag of (un)watch link
16 * @param {string} action One of 'watch', 'unwatch'
17 * @param {string} [state="idle"] '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
32 if ( state
=== undefined ) {
33 $li
.trigger( 'watchpage.mw', otherAction
);
37 .text( mw
.msg( msgKey
) )
38 .attr( 'title', mw
.msg( 'tooltip-ca-' + action
) +
39 ( accesskeyTip
? ' ' + accesskeyTip
[0] : '' )
41 .attr( 'href', mw
.util
.wikiScript() + '?' + $.param( {
47 // Most common ID style
48 if ( $li
.prop( 'id' ) === 'ca-' + otherAction
) {
49 $li
.prop( 'id', 'ca-' + action
);
52 // Special case for vector icon
53 if ( $li
.hasClass( 'icon' ) ) {
54 if ( state
=== 'loading' ) {
55 $link
.addClass( 'loading' );
57 $link
.removeClass( 'loading' );
63 * TODO: This should be moved somewhere more accessible.
67 * @return {string} The extracted action, defaults to 'view'
69 function mwUriGetAction( url
) {
70 var action
, actionPaths
, key
, i
, m
, parts
;
72 actionPaths
= mw
.config
.get( 'wgActionPaths' );
74 // TODO: Does MediaWiki give action path or query param
75 // precedence? If the former, move this to the bottom
76 action
= mw
.util
.getParamValue( 'action', url
);
77 if ( action
!== null ) {
81 for ( key
in actionPaths
) {
82 if ( actionPaths
.hasOwnProperty( key
) ) {
83 parts
= actionPaths
[key
].split( '$1' );
84 for ( i
= 0; i
< parts
.length
; i
++ ) {
85 parts
[i
] = $.escapeRE( parts
[i
] );
87 m
= new RegExp( parts
.join( '(.+)' ) ).exec( url
);
98 // Expose public methods
100 updateWatchLink
: updateWatchLink
104 var $links
= $( '.mw-watchlink a, a.mw-watchlink, ' +
105 '#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
106 '#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
108 // Allowing people to add inline animated links is a little scary
109 $links
= $links
.filter( ':not( #bodyContent *, #content * )' );
111 $links
.click( function ( e
) {
112 var action
, api
, $link
;
114 // Start preloading the notification module (normally loaded by mw.notify())
115 mw
.loader
.load( ['mediawiki.notification'], null, true );
117 action
= mwUriGetAction( this.href
);
119 if ( action
!== 'watch' && action
!== 'unwatch' ) {
120 // Could not extract target action from link url,
121 // let native browsing handle it further
129 updateWatchLink( $link
, action
, 'loading' );
134 .done( function ( watchResponse
) {
135 var otherAction
= action
=== 'watch' ? 'unwatch' : 'watch';
137 mw
.notify( $.parseHTML( watchResponse
.message
), {
141 // Set link to opposite
142 updateWatchLink( $link
, otherAction
);
144 // Update the "Watch this page" checkbox on action=edit when the
145 // page is watched or unwatched via the tab (bug 12395).
146 $( '#wpWatchthis' ).prop( 'checked', watchResponse
.watched
!== undefined );
149 var cleanTitle
, msg
, link
;
151 // Reset link to non-loading mode
152 updateWatchLink( $link
, action
);
154 // Format error message
155 cleanTitle
= title
.replace( /_
/g
, ' ' );
156 link
= mw
.html
.element(
158 href
: mw
.util
.getUrl( title
),
162 msg
= mw
.message( 'watcherrortext', link
);
164 // Report to user about the error
165 mw
.notify( msg
, { tag
: 'watch-self' } );
170 }( mediaWiki
, jQuery
) );