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' );
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 msgKey
, $li
, otherAction
;
22 // A valid but empty jQuery object shouldn't throw a TypeError
23 if ( !$link
.length
) {
27 // Invalid actions shouldn't silently turn the page in an unrecoverable state
28 if ( action
!== 'watch' && action
!== 'unwatch' ) {
29 throw new Error( 'Invalid action' );
32 // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
33 msgKey
= state
=== 'loading' ? action
+ 'ing' : action
;
34 otherAction
= action
=== 'watch' ? 'unwatch' : 'watch';
35 $li
= $link
.closest( 'li' );
37 // Trigger a 'watchpage' event for this List item.
38 // Announce the otherAction value as the first param.
39 // Used to monitor the state of watch link.
40 // TODO: Revise when system wide hooks are implemented
41 if ( state
=== undefined ) {
42 $li
.trigger( 'watchpage.mw', otherAction
);
46 .text( mw
.msg( msgKey
) )
47 .attr( 'title', mw
.msg( 'tooltip-ca-' + action
) )
48 .updateTooltipAccessKeys()
49 .attr( 'href', mw
.util
.getUrl( title
, { action
: action
} ) );
51 // Most common ID style
52 if ( $li
.prop( 'id' ) === 'ca-' + otherAction
) {
53 $li
.prop( 'id', 'ca-' + action
);
56 if ( state
=== 'loading' ) {
57 $link
.addClass( 'loading' );
59 $link
.removeClass( 'loading' );
64 * TODO: This should be moved somewhere more accessible.
68 * @return {string} The extracted action, defaults to 'view'
70 function mwUriGetAction( url
) {
71 var action
, actionPaths
, key
, i
, m
, parts
;
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 actionPaths
= mw
.config
.get( 'wgActionPaths' );
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
] = mw
.RegExp
.escape( 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 // Restrict to core interfaces, ignore user-generated content
106 $links
= $links
.filter( ':not( #bodyContent *, #content * )' );
108 $links
.click( function ( e
) {
109 var mwTitle
, action
, api
, $link
;
111 mwTitle
= mw
.Title
.newFromText( title
);
112 action
= mwUriGetAction( this.href
);
114 if ( !mwTitle
|| ( action
!== 'watch' && action
!== 'unwatch' ) ) {
115 // Let native browsing handle the link
123 if ( $link
.hasClass( 'loading' ) ) {
127 updateWatchLink( $link
, action
, 'loading' );
129 // Preload the notification module for mw.notify
130 mw
.loader
.load( 'mediawiki.notification' );
134 api
[ action
]( title
)
135 .done( function ( watchResponse
) {
136 var message
, otherAction
= action
=== 'watch' ? 'unwatch' : 'watch';
138 if ( mwTitle
.getNamespaceId() > 0 && mwTitle
.getNamespaceId() % 2 === 1 ) {
139 message
= action
=== 'watch' ? 'addedwatchtext-talk' : 'removedwatchtext-talk';
141 message
= action
=== 'watch' ? 'addedwatchtext' : 'removedwatchtext';
144 mw
.notify( mw
.message( message
, mwTitle
.getPrefixedText() ).parseDom(), {
148 // Set link to opposite
149 updateWatchLink( $link
, otherAction
);
151 // Update the "Watch this page" checkbox on action=edit when the
152 // page is watched or unwatched via the tab (bug 12395).
153 $( '#wpWatchthis' ).prop( 'checked', watchResponse
.watched
=== true );
158 // Reset link to non-loading mode
159 updateWatchLink( $link
, action
);
161 // Format error message
162 link
= mw
.html
.element(
164 href
: mw
.util
.getUrl( title
),
165 title
: mwTitle
.getPrefixedText()
166 }, mwTitle
.getPrefixedText()
168 msg
= mw
.message( 'watcherrortext', link
);
170 // Report to user about the error
179 }( mediaWiki
, jQuery
) );