From 841623c3fcc487d624e273060b6490d7ae039435 Mon Sep 17 00:00:00 2001 From: Krinkle Date: Thu, 9 Dec 2010 23:29:26 +0000 Subject: [PATCH] Ported jsMsg to mw.util; Fixing bugs and modernising mediawiki.action.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() --- resources/Resources.php | 2 +- .../mediawiki.action.watch.ajax.js | 17 ++++----- resources/mediawiki.util/mediawiki.util.js | 37 +++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/resources/Resources.php b/resources/Resources.php index 5b2cced264..c1c09793b2 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -343,7 +343,7 @@ return array( ), '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', diff --git a/resources/mediawiki.action/mediawiki.action.watch.ajax.js b/resources/mediawiki.action/mediawiki.action.watch.ajax.js index b4bef9eebf..0ef5b85c18 100644 --- a/resources/mediawiki.action/mediawiki.action.watch.ajax.js +++ b/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 ) { @@ -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' ); } @@ -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. @@ -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 ) { @@ -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 ) ); } diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index f7beea40fe..1db83842ca 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -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 = $( '
' ); + 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; } }; -- 2.20.1