Move action.watch to a more suitable place and expose updateWatchLink

method in mw.page.watch.  Update method to toggle watch, unwatch
list item id attribute.
Patchset 2- Expose method with less code duplication, restore
comments, cleanup whitespace.
Patchset 3-5 Whitespace cleanup
Patchset 6 - Trigger a watch event on the updated li
Patchset 7 - Only trigger watch event if not updating
state, announce opposite of icon action, to properly
indicate action taken.
Patchset 8 - Cleanup spacing and comments issues,
change event namespace.
Patchset 9 - actually add the change
Change-Id: I591f9f847db391c5d1477dc2ed41de54ec266261
This commit is contained in:
Rob Moen 2012-06-19 16:00:57 -07:00
parent 7d66961ad3
commit 2e089a4e58
3 changed files with 38 additions and 25 deletions

View file

@ -2470,7 +2470,7 @@ $templates
wfRunHooks( 'AjaxAddScript', array( &$this ) );
if( $wgAjaxWatch && $this->getUser()->isLoggedIn() ) {
$this->addModules( 'mediawiki.action.watch.ajax' );
$this->addModules( 'mediawiki.page.watch.ajax' );
}
if ( $wgEnableMWSuggest && !$this->getUser()->getOption( 'disablesuggest', false ) ) {

View file

@ -655,22 +655,6 @@ return array(
'mediawiki.action.view.rightClickEdit' => array(
'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js',
),
'mediawiki.action.watch.ajax' => array(
'scripts' => 'resources/mediawiki.action/mediawiki.action.watch.ajax.js',
'dependencies' => array(
'mediawiki.api.watch',
'mediawiki.util',
),
'messages' => array(
'watch',
'unwatch',
'watching',
'unwatching',
'tooltip-ca-watch',
'tooltip-ca-unwatch',
'watcherrortext',
),
),
/* MediaWiki Language */
@ -770,7 +754,23 @@ return array(
),
'position' => 'top',
),
'mediawiki.page.watch.ajax' => array(
'scripts' => 'resources/mediawiki.page/mediawiki.page.watch.ajax.js',
'dependencies' => array(
'mediawiki.page.startup',
'mediawiki.api.watch',
'mediawiki.util'
),
'messages' => array(
'watch',
'unwatch',
'watching',
'unwatching',
'tooltip-ca-watch',
'tooltip-ca-unwatch',
'watcherrortext',
),
),
/* MediaWiki Special pages */

View file

@ -3,12 +3,15 @@
* watch pages, rather than navigating to a different URI.
*/
( function ( $, mw, undefined ) {
/**
* The name of the page to watch or unwatch.
*/
var title = mw.config.get( 'wgRelevantPageName', mw.config.get( 'wgPageName' ) );
// Expose local methods
mw.page.watch = {
'updateWatchLink': updateWatchLink
};
/**
* Update the link text, link href attribute and (if applicable)
* "loading" class.
@ -18,12 +21,22 @@
* @param state {String} [optional] 'idle' or 'loading'. Default is 'idle'.
*/
function updateWatchLink( $link, action, state ) {
var accesskeyTip, msgKey, $li;
var accesskeyTip, msgKey, $li, otherAction;
// message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
msgKey = state === 'loading' ? action + 'ing' : action;
otherAction = action === 'watch' ? 'unwatch' : 'watch';
accesskeyTip = $link.attr( 'title' ).match( mw.util.tooltipAccessKeyRegexp );
$li = $link.closest( 'li' );
/**
* 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 );
}
$link
.text( mw.msg( msgKey ) )
@ -36,6 +49,11 @@
})
);
// Most common ID style
if ( $li.prop( 'id' ) === 'ca-' + otherAction ) {
$li.prop( 'id', 'ca-' + action );
}
// Special case for vector icon
if ( $li.hasClass( 'icon' ) ) {
if ( state === 'loading' ) {
@ -120,11 +138,6 @@
// Set link to opposite
updateWatchLink( $link, otherAction );
// Most common ID style
if ( $li.prop( 'id' ) === 'ca-' + otherAction || $li.prop( 'id' ) === 'ca-' + action ) {
$li.prop( 'id', 'ca-' + otherAction );
}
// Bug 12395 - update the watch checkbox on edit pages when the
// page is watched or unwatched via the tab.
if ( watchResponse.watched !== undefined ) {