Skip to content

Commit

Permalink
Ported jsMsg to mw.util; Fixing bugs and modernising mediawiki.action…
Browse files Browse the repository at this point in the history
….watch.ajax.js a little more

* Using the raw element instead of jQuery object to get href. This way it's the complete url instead of what could potentially be a relative path (window.location.href is best passed a complete url)
* Adding 'mediawiki.legacy.ajax' as dependency for mediawiki.action.watch.ajax as it is and has been for a while.
* Ported jsMsg (legacy.wikibits) to mw.util.jsMessage()
  • Loading branch information
Krinkle committed Dec 9, 2010
1 parent b5561e1 commit 841623c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion resources/Resources.php
Expand Up @@ -343,7 +343,7 @@
),
'mediawiki.action.watch.ajax' => array(
'scripts' => 'resources/mediawiki.action/mediawiki.action.watch.ajax.js',
'dependencies' => 'mediawiki.util',
'dependencies' => array( 'mediawiki.util', 'mediawiki.legacy.ajax' ),
),
'mediawiki.special.preferences' => array(
'scripts' => 'resources/mediawiki.special/mediawiki.special.preferences.js',
Expand Down
17 changes: 8 additions & 9 deletions resources/mediawiki.action/mediawiki.action.watch.ajax.js
@@ -1,7 +1,6 @@
/**
* Animate watch/unwatch links to use asynchronous API requests to
* watch pages, rather than clicking on links. Requires jQuery.
* Uses jsMsg() from wikibits.js.
*/

if ( typeof wgAjaxWatch === 'undefined' || !wgAjaxWatch ) {
Expand Down Expand Up @@ -37,16 +36,16 @@ wgAjaxWatch.processResult = function( response, $link ) {
wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch', $link] );
} else {
// Either we got an error code or it just plain broke.
window.location.href = $link.attr( 'href' );
window.location.href = $link[0].href;
return;
}

jsMsg( response.message, 'watch' );
mw.util.jsMessage( response.message, 'watch' );

// Bug 12395 - update the watch checkbox on edit pages when the
// page is watched or unwatched via the tab.
if( response.watched !== undefined ) {
$( '#wpWatchthis' ).attr( 'checked', '1' );
$( '#wpWatchthis' ).attr( 'checked', 'checked' );
} else {
$( '#wpWatchthis' ).removeAttr( 'checked' );
}
Expand Down Expand Up @@ -74,7 +73,7 @@ $( document ).ready( function() {
$links.click( function( event ) {
var $link = $( this );

if( wgAjaxWatch.supported === false || !wgEnableWriteAPI || !wfSupportsAjax() ) {
if( wgAjaxWatch.supported === false || !mw.config.get( 'wgEnableWriteAPI' ) || !wfSupportsAjax() ) {
// Lazy initialization so we don't toss up
// ActiveX warnings on initial page load
// for IE 6 users with security settings.
Expand All @@ -83,8 +82,8 @@ $( document ).ready( function() {
}

wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
$.getJSON( wgScriptPath
+ '/api' + wgScriptExtension + '?action=watch&format=json&title='
$.getJSON( mw.config.get( 'wgScriptPath' )
+ '/api' + mw.config.get( 'wgScriptExtension' ) + '?action=watch&format=json&title='
+ encodeURIComponent( $link.data( 'target' ) )
+ ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
function( data, textStatus, xhr ) {
Expand All @@ -107,8 +106,8 @@ $( document ).ready( function() {
$link.data( 'action', otheraction );
wgAjaxWatch.setLinkText( $link, otheraction );
$link.attr( 'href', $link.attr( 'href' ).replace( '&action=' + action , '&action=' + otheraction ) );
if( $link.parents( 'li' ).attr( 'id' ) == 'ca-' + action ) {
$link.parents( 'li' ).attr( 'id', 'ca-' + otheraction );
if( $link.closest( 'li' ).attr( 'id' ) == 'ca-' + action ) {
$link.closest( 'li' ).attr( 'id', 'ca-' + otheraction );
// update the link text with the new message
$link.text( mediaWiki.msg( otheraction ) );
}
Expand Down
37 changes: 37 additions & 0 deletions resources/mediawiki.util/mediawiki.util.js
Expand Up @@ -314,6 +314,43 @@

return $item.get( 0 );
}
},

/**
* Add a little box at the top of the screen to inform the user of
* something, replacing any previous message.
*
* @param message mixed DOM-element or HTML to be put inside the message box
* @param className string Used in adding a class; should be different for each
* call to allow CSS/JS to hide different boxes. null = no class used.
* @return Boolean True on success, false on failure
*/
'jsMessage' : function( message, className ) {
// We special-case skin structures provided by the software. Skins that
// choose to abandon or significantly modify our formatting can just define
// an mw-js-message div to start with.
var $messageDiv = $( '#mw-js-message' );
if ( !$messageDiv.length ) {
$messageDiv = $( '<div id="mw-js-message">' );
if ( mw.util.$content.parent().length ) {
mw.util.$content.parent().prepend( $messageDiv );
} else {
return false;
}
}

$messageDiv.show();
if ( className ) {
$messageDiv.attr( 'class', 'mw-js-message-' + className );
}

if ( typeof message === 'object' ) {
$messageDiv.empty();
$messageDiv.append( message ); // Append new content
} else {
$messageDiv.html( message );
}
return true;
}

};
Expand Down

0 comments on commit 841623c

Please sign in to comment.