https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112849

Revision: 112849
Author:   krinkle
Date:     2012-03-01 23:04:44 +0000 (Thu, 01 Mar 2012)
Log Message:
-----------
[CategoryTree] clean up JS, fix undefined bug
* Use dashes versions of data attributes to make it easier to find them (now 
the data attributes as found in PHP where no where to be found in any JS file, 
appeared to be bogus)
* Don't create a new <div> element ($parentTag) only to store a single string 
in it's data object, using local variables for ctTitle, ctMode and ctOptions 
instead.

Modified Paths:
--------------
    trunk/extensions/CategoryTree/modules/ext.categoryTree.js

Modified: trunk/extensions/CategoryTree/modules/ext.categoryTree.js
===================================================================
--- trunk/extensions/CategoryTree/modules/ext.categoryTree.js   2012-03-01 
23:03:40 UTC (rev 112848)
+++ trunk/extensions/CategoryTree/modules/ext.categoryTree.js   2012-03-01 
23:04:44 UTC (rev 112849)
@@ -27,7 +27,7 @@
         */
        handleNode: function( e ) {
                var $link = $( this );
-               if ( $link.data( 'ctState' ) === 'collapsed' ) {
+               if ( $link.data( 'ct-state' ) === 'collapsed' ) {
                        categoryTree.expandNode( $link );
                } else {
                        categoryTree.collapseNode( $link );
@@ -48,9 +48,9 @@
                $link
                        .html( mw.msg( 'categorytree-collapse-bullet' ) )
                        .attr( 'title', mw.msg( 'categorytree-collapse' ) )
-                       .data( 'ctState', 'expanded' );
+                       .data( 'ct-state', 'expanded' );
 
-               if ( !$link.data( 'ctLoaded' ) ) {
+               if ( !$link.data( 'ct-loaded' ) ) {
                        categoryTree.loadChildren( $link, $children );
                }
        },
@@ -68,7 +68,7 @@
                $link
                        .html( mw.msg( 'categorytree-expand-bullet' ) )
                        .attr( 'title', mw.msg( 'categorytree-expand' ) )
-                       .data( 'ctState', 'collapsed' );
+                       .data( 'ct-state', 'collapsed' );
        },
 
        /**
@@ -78,26 +78,53 @@
         * @param {jQuery} $children
         */
        loadChildren: function( $link, $children ) {
-               $link.data( 'ctLoaded', true );
+               var $linkParentCTTag, ctTitle, ctMode, ctOptions;
+
+               /**
+                * Error callback
+                */
+               function error() {
+                       var $retryLink;
+
+                       $retryLink = $( '<a>' )
+                               .text( mw.msg( 'categorytree-retry' ) )
+                               .attr( 'href', '#' )
+                               .click( function() {
+                                       categoryTree.loadChildren( $link, 
$children );
+                               } );
+
+                       $children
+                               .text( mw.msg( 'categorytree-error' ) )
+                               .append( $retryLink );
+               }
+
+               $link.data( 'ct-loaded', true );
+
                $children.html(
                        $( '<i class="CategoryTreeNotice"></i>' )
                                .text( mw.msg( 'categorytree-loading' ) )
                );
 
-               var $parentTag = $link.parents( '.CategoryTreeTag' );
+               $linkParentCTTag = $link.parents( '.CategoryTreeTag' );
 
-               if ( $parentTag.length === 0 ) {
-                       // Probably a CategoryPage
-                       $parentTag = $( '<div />' )
-                               .hide()
-                               .data( 'ctOptions', mw.config.get( 
'wgCategoryTreePageCategoryOptions' ) );
+               // Element may not have a .CategoryTreeTag parent, fallback to 
defauls
+               // Probably a CategoryPage (@todo: based on what?)
+               ctTitle = $link.data( 'ct-title' );
+               ctMode = $linkParentCTTag.data( 'ct-mode' ) || undefined;
+               ctOptions = $linkParentCTTag.data( 'ct-options' ) || 
mw.config.get( 'wgCategoryTreePageCategoryOptions' );
+
+               // Mode and options have defaults or fallbacks, title does not.
+               // Don't make a request if there is no title.
+               if ( typeof ctTitle !== 'string' ) {
+                       error();
+                       return;
                }
 
                $.get(
                        mw.util.wikiScript(), {
                                action: 'ajax',
                                rs: 'efCategoryTreeAjaxWrapper',
-                               rsargs: [$link.data( 'ctTitle' ), 
$parentTag.data( 'ctOptions' ), 'json'] // becomes 
&rsargs[]=arg1&rsargs[]=arg2...
+                               rsargs: [ctTitle, ctOptions, 'json'] // becomes 
&rsargs[]=arg1&rsargs[]=arg2...
                        }
                )
                        .success( function ( data ) {
@@ -105,7 +132,7 @@
                                data = data.replace(/##LOAD##/g, mw.msg( 
'categorytree-expand' ) );
 
                                if ( data === '' ) {
-                                       switch ( $parentTag.data( 'ctMode' ) ) {
+                                       switch ( ctMode ) {
                                                case 0:
                                                        data = mw.msg( 
'categorytree-no-subcategories' );
                                                        break;
@@ -128,15 +155,7 @@
                                                .click( categoryTree.handleNode 
);
                                categoryTree.showToggles();
                        } )
-                       .error( function() {
-                               var $retryLink = $( '<a />' )
-                                       .text( mw.msg( 'categorytree-retry' ) )
-                                       .attr( 'href', '#' )
-                                       .click( function() { 
categoryTree.loadChildren( $link, $children ); } );
-                               $children
-                                       .text( mw.msg( 'categorytree-error' ) )
-                                       .append( $retryLink );
-                       } );
+                       .error( error );
        }
 };
 


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to