Ported jsMsg to mw.util; Fixing bugs and modernising mediawiki.action.watch.ajax...
authorKrinkle <krinkle@users.mediawiki.org>
Thu, 9 Dec 2010 23:29:26 +0000 (23:29 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Thu, 9 Dec 2010 23:29:26 +0000 (23:29 +0000)
* 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
resources/mediawiki.action/mediawiki.action.watch.ajax.js
resources/mediawiki.util/mediawiki.util.js

index 5b2cced..c1c0979 100644 (file)
@@ -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',
index b4bef9e..0ef5b85 100644 (file)
@@ -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 ) );
                        }
index f7beea4..1db8384 100644 (file)
 
                                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;
                }
 
        };