2010-04-27 15:09:04 +00:00
|
|
|
/**
|
|
|
|
|
* Animate watch/unwatch links to use asynchronous API requests to
|
2011-12-27 01:21:56 +00:00
|
|
|
* watch pages, rather than navigating to a different URI.
|
2010-04-27 15:09:04 +00:00
|
|
|
*/
|
2011-12-27 01:21:56 +00:00
|
|
|
( function ( $, mw, undefined ) {
|
2012-03-15 06:04:20 +00:00
|
|
|
/**
|
|
|
|
|
* The name of the page to watch or unwatch.
|
|
|
|
|
*/
|
|
|
|
|
var title = mw.config.get( 'wgRelevantPageName', mw.config.get( 'wgPageName' ) );
|
|
|
|
|
|
2012-06-19 23:00:57 +00:00
|
|
|
// Expose local methods
|
|
|
|
|
mw.page.watch = {
|
|
|
|
|
'updateWatchLink': updateWatchLink
|
|
|
|
|
};
|
2012-03-15 06:04:20 +00:00
|
|
|
/**
|
|
|
|
|
* Update the link text, link href attribute and (if applicable)
|
|
|
|
|
* "loading" class.
|
|
|
|
|
*
|
|
|
|
|
* @param $link {jQuery} Anchor tag of (un)watch link.
|
|
|
|
|
* @param action {String} One of 'watch', 'unwatch'.
|
|
|
|
|
* @param state {String} [optional] 'idle' or 'loading'. Default is 'idle'.
|
|
|
|
|
*/
|
|
|
|
|
function updateWatchLink( $link, action, state ) {
|
2012-06-19 23:00:57 +00:00
|
|
|
var accesskeyTip, msgKey, $li, otherAction;
|
2012-03-15 06:04:20 +00:00
|
|
|
|
|
|
|
|
// message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
|
|
|
|
|
msgKey = state === 'loading' ? action + 'ing' : action;
|
2012-06-19 23:00:57 +00:00
|
|
|
otherAction = action === 'watch' ? 'unwatch' : 'watch';
|
2012-03-15 06:04:20 +00:00
|
|
|
accesskeyTip = $link.attr( 'title' ).match( mw.util.tooltipAccessKeyRegexp );
|
2011-12-27 01:21:56 +00:00
|
|
|
$li = $link.closest( 'li' );
|
2012-06-19 23:00:57 +00:00
|
|
|
/**
|
|
|
|
|
* Trigger a 'watch' event for this List item.
|
|
|
|
|
* Announce the otherAction value as the first param.
|
|
|
|
|
* Used to monitor the state of watch link.
|
|
|
|
|
* TODO: Revise when system wide hooks are implemented
|
|
|
|
|
*/
|
|
|
|
|
if( state === undefined ) {
|
|
|
|
|
$li.trigger( 'watch.mw', otherAction );
|
|
|
|
|
}
|
2011-12-27 01:21:56 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
$link
|
|
|
|
|
.text( mw.msg( msgKey ) )
|
|
|
|
|
.attr( 'title', mw.msg( 'tooltip-ca-' + action ) +
|
|
|
|
|
( accesskeyTip ? ' ' + accesskeyTip[0] : '' )
|
|
|
|
|
)
|
|
|
|
|
.attr( 'href', mw.util.wikiScript() + '?' + $.param({
|
|
|
|
|
title: title,
|
|
|
|
|
action: action
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2012-06-19 23:00:57 +00:00
|
|
|
// Most common ID style
|
|
|
|
|
if ( $li.prop( 'id' ) === 'ca-' + otherAction ) {
|
|
|
|
|
$li.prop( 'id', 'ca-' + action );
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
// Special case for vector icon
|
|
|
|
|
if ( $li.hasClass( 'icon' ) ) {
|
|
|
|
|
if ( state === 'loading' ) {
|
|
|
|
|
$link.addClass( 'loading' );
|
|
|
|
|
} else {
|
|
|
|
|
$link.removeClass( 'loading' );
|
|
|
|
|
}
|
2007-06-22 17:36:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-05-21 18:33:48 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
/**
|
|
|
|
|
* @todo This should be moved somewhere more accessible.
|
|
|
|
|
* @param url {String}
|
|
|
|
|
* @return {String} The extracted action, defaults to 'view'.
|
|
|
|
|
*/
|
|
|
|
|
function mwUriGetAction( url ) {
|
|
|
|
|
var action, actionPaths, key, i, m, parts;
|
|
|
|
|
|
|
|
|
|
actionPaths = mw.config.get( 'wgActionPaths' );
|
|
|
|
|
|
|
|
|
|
// @todo: Does MediaWiki give action path or query param
|
|
|
|
|
// precedence ? If the former, move this to the bottom
|
|
|
|
|
action = mw.util.getParamValue( 'action', url );
|
|
|
|
|
if ( action !== null ) {
|
|
|
|
|
return action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( key in actionPaths ) {
|
|
|
|
|
if ( actionPaths.hasOwnProperty( key ) ) {
|
|
|
|
|
parts = actionPaths[key].split( '$1' );
|
|
|
|
|
for ( i = 0; i < parts.length; i += 1 ) {
|
|
|
|
|
parts[i] = $.escapeRE( parts[i] );
|
|
|
|
|
}
|
|
|
|
|
m = new RegExp( parts.join( '(.+)' ) ).exec( url );
|
|
|
|
|
if ( m && m[1] ) {
|
|
|
|
|
return key;
|
|
|
|
|
}
|
2011-05-21 18:33:48 +00:00
|
|
|
|
Revert r107309, r113601, r113704, r113742, r113792, r113838, r113859, r113893, r113894, r113952, r114047, r114252, r114256, r114257. This reverts the remaining 'new' revisions in core.
All of these revisions are tagged with 'gerritmigration' and will be resubmitted into Gerrit after the Gerrit switchover. See also http://lists.wikimedia.org/pipermail/wikitech-l/2012-March/059124.html
2012-03-21 00:16:50 +00:00
|
|
|
}
|
2011-12-27 01:21:56 +00:00
|
|
|
}
|
2012-03-15 06:04:20 +00:00
|
|
|
|
|
|
|
|
return 'view';
|
2006-12-26 23:53:34 +00:00
|
|
|
}
|
2010-07-16 14:57:30 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
$( document ).ready( function () {
|
|
|
|
|
var $links = $( '.mw-watchlink a, a.mw-watchlink, ' +
|
|
|
|
|
'#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
|
|
|
|
|
'#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
|
2006-12-26 23:53:34 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
// Allowing people to add inline animated links is a little scary
|
|
|
|
|
$links = $links.filter( ':not( #bodyContent *, #content * )' );
|
2010-07-16 14:57:30 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
$links.click( function ( e ) {
|
|
|
|
|
var action, api, $link;
|
2010-07-16 14:57:30 +00:00
|
|
|
|
2011-12-27 01:21:56 +00:00
|
|
|
action = mwUriGetAction( this.href );
|
2006-12-26 23:53:34 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
if ( action !== 'watch' && action !== 'unwatch' ) {
|
|
|
|
|
// Could not extract target action from link url,
|
|
|
|
|
// let native browsing handle it further
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2010-07-16 14:57:30 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
$link = $( this );
|
Revert r107309, r113601, r113704, r113742, r113792, r113838, r113859, r113893, r113894, r113952, r114047, r114252, r114256, r114257. This reverts the remaining 'new' revisions in core.
All of these revisions are tagged with 'gerritmigration' and will be resubmitted into Gerrit after the Gerrit switchover. See also http://lists.wikimedia.org/pipermail/wikitech-l/2012-March/059124.html
2012-03-21 00:16:50 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
updateWatchLink( $link, action, 'loading' );
|
2012-03-15 06:04:20 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
api = new mw.Api();
|
|
|
|
|
api[action](
|
|
|
|
|
title,
|
|
|
|
|
// Success
|
|
|
|
|
function ( watchResponse ) {
|
|
|
|
|
var $li, otherAction;
|
Revert r107309, r113601, r113704, r113742, r113792, r113838, r113859, r113893, r113894, r113952, r114047, r114252, r114256, r114257. This reverts the remaining 'new' revisions in core.
All of these revisions are tagged with 'gerritmigration' and will be resubmitted into Gerrit after the Gerrit switchover. See also http://lists.wikimedia.org/pipermail/wikitech-l/2012-March/059124.html
2012-03-21 00:16:50 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
otherAction = action === 'watch' ? 'unwatch' : 'watch';
|
|
|
|
|
$li = $link.closest( 'li' );
|
2011-05-21 11:06:52 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
mw.util.jsMessage( watchResponse.message, 'ajaxwatch' );
|
|
|
|
|
|
|
|
|
|
// Set link to opposite
|
|
|
|
|
updateWatchLink( $link, otherAction );
|
|
|
|
|
|
|
|
|
|
// Bug 12395 - update the watch checkbox on edit pages when the
|
|
|
|
|
// page is watched or unwatched via the tab.
|
|
|
|
|
if ( watchResponse.watched !== undefined ) {
|
|
|
|
|
$( '#wpWatchthis' ).prop( 'checked', true );
|
|
|
|
|
} else {
|
|
|
|
|
$( '#wpWatchthis' ).removeProp( 'checked' );
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// Error
|
|
|
|
|
function () {
|
|
|
|
|
var cleanTitle, html, link;
|
|
|
|
|
|
|
|
|
|
// Reset link to non-loading mode
|
|
|
|
|
updateWatchLink( $link, action );
|
|
|
|
|
|
|
|
|
|
// Format error message
|
|
|
|
|
cleanTitle = title.replace( /_/g, ' ' );
|
|
|
|
|
link = mw.html.element(
|
|
|
|
|
'a', {
|
|
|
|
|
href: mw.util.wikiGetlink( title ),
|
|
|
|
|
title: cleanTitle
|
|
|
|
|
}, cleanTitle
|
|
|
|
|
);
|
|
|
|
|
html = mw.msg( 'watcherrortext', link );
|
|
|
|
|
|
|
|
|
|
// Report to user about the error
|
|
|
|
|
mw.util.jsMessage( html, 'ajaxwatch' );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
Revert r107309, r113601, r113704, r113742, r113792, r113838, r113859, r113893, r113894, r113952, r114047, r114252, r114256, r114257. This reverts the remaining 'new' revisions in core.
All of these revisions are tagged with 'gerritmigration' and will be resubmitted into Gerrit after the Gerrit switchover. See also http://lists.wikimedia.org/pipermail/wikitech-l/2012-March/059124.html
2012-03-21 00:16:50 +00:00
|
|
|
|
2012-03-15 06:04:20 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|