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

Revision: 113616
Author:   nikerabbit
Date:     2012-03-12 12:14:48 +0000 (Mon, 12 Mar 2012)
Log Message:
-----------
Improved and cleaned up Special:LanguageStats JavaScript to support 
collapsing/expanding nested subgroups

Modified Paths:
--------------
    trunk/extensions/Translate/README
    trunk/extensions/Translate/resources/ext.translate.special.languagestats.css
    trunk/extensions/Translate/resources/ext.translate.special.languagestats.js
    trunk/extensions/Translate/specials/SpecialLanguageStats.php

Modified: trunk/extensions/Translate/README
===================================================================
--- trunk/extensions/Translate/README   2012-03-12 11:00:34 UTC (rev 113615)
+++ trunk/extensions/Translate/README   2012-03-12 12:14:48 UTC (rev 113616)
@@ -30,6 +30,8 @@
  https://translatewiki.net/docs/Translate/html/
 
 == Change log ==
+* 2012-03-12
+* Special:LanguageStats group collapsing now supports nested subgroups
 * 2012-03-11
 - Support for shared TTMServer databases was added
 - Suggestions from different TTMServers are now grouped

Modified: 
trunk/extensions/Translate/resources/ext.translate.special.languagestats.css
===================================================================
--- 
trunk/extensions/Translate/resources/ext.translate.special.languagestats.css    
    2012-03-12 11:00:34 UTC (rev 113615)
+++ 
trunk/extensions/Translate/resources/ext.translate.special.languagestats.css    
    2012-03-12 12:14:48 UTC (rev 113616)
@@ -2,20 +2,20 @@
        background: white;
 }
 
-.mw-sp-langstats-toggleall {
+.groupexpander-all {
        text-align: right;
 }
 
-.mw-sp-langstats-toggle {
+.groupexpander {
        float: right;
 }
 
-.mw-sp-langstats-collapser,
-.mw-sp-langstats-collapser a {
+.expanded,
+.expanded a {
        cursor: n-resize;
 }
 
-.mw-sp-langstats-expander,
-.mw-sp-langstats-expander a {
+.collapsed,
+.collapsed a {
        cursor: s-resize;
-}
\ No newline at end of file
+}

Modified: 
trunk/extensions/Translate/resources/ext.translate.special.languagestats.js
===================================================================
--- trunk/extensions/Translate/resources/ext.translate.special.languagestats.js 
2012-03-12 11:00:34 UTC (rev 113615)
+++ trunk/extensions/Translate/resources/ext.translate.special.languagestats.js 
2012-03-12 12:14:48 UTC (rev 113616)
@@ -1,78 +1,104 @@
 /**
  * Collapsing script for Special:LanguageStats in MediaWiki Extension:Translate
  * @author Krinkle <krinklemail (at) gmail (dot) com>
+ * @author Niklas Laxström, 2012
  * @created January 3, 2011
  * @license GPL v2, CC-BY-SA-3.0
  */
+/*global mw:false*/
 jQuery( document ).ready( function( $ ) {
+       "use strict";
 
-       var     $translateTable =  $( '.mw-sp-translate-table' ),
-               $metaRows = $( 'tr[data-ismeta=1]', $translateTable );
+       var
+               $translateTable = $( '.mw-sp-translate-table' ),
+               $metaRows = $( 'tr.AggregateMessageGroup', $translateTable );
 
-       // Only do stuff if there are any meta group rows on this pages
-       if ( $metaRows.size() ) {
+       // Quick return
+       if ( !$metaRows.size() ) {
+               return;
+       }
 
-               $metaRows.each( function() {
-                       // Get info and cache selectors
-                       var     $thisGroup = $(this),
-                               thisGroupId = $thisGroup.attr( 'data-groupid' ),
-                               $thisChildRows = $( 'tr[data-parentgroups~="' + 
thisGroupId + '"]', $translateTable );
+       $metaRows.each( function() {
+               var
+                       $parent = $( this ),
+                       thisGroupId = $parent.attr( 'data-groupid' ),
+                       $children = $( 'tr[data-parentgroup="' + thisGroupId + 
'"]', $translateTable );
 
-                       // Only do the collapse stuff if this Meta-group 
actually has children on this page
-                       if ( $thisChildRows.size() ) {
+               // Only do the collapse stuff if this Meta-group actually has 
children on this page
+               if ( !$children.size() ) {
+                       return;
+               }
 
-                               // Build toggle link
-                               var $toggler = $( '<span 
class="mw-sp-langstats-toggle mw-sp-langstats-expander">[</span>' ).append( $( 
'<a href="#" onclick="return false;"></a>' ).text( mw.msg( 
'translate-langstats-expand' ) ) ).append( ']' ).click( function() {
-                                       var $el = $( this );
-                                       // Switch the state and toggle the rows
-                                       if ( $el.hasClass( 
'mw-sp-langstats-expander' ) ) {
-                                               $thisChildRows.fadeIn();
-                                               $el.removeClass( 
'mw-sp-langstats-expander' ).addClass( 'mw-sp-langstats-collapser' )
-                                                  .find( '> a' ).text( mw.msg( 
'translate-langstats-collapse' ) );
-                                       } else {
-                                               $thisChildRows.fadeOut();
-                                               $el.addClass( 
'mw-sp-langstats-expander' ).removeClass( 'mw-sp-langstats-collapser' )
-                                                  .find( '> a' ).text( mw.msg( 
'translate-langstats-expand' ) );
-                                       }
-                               } );
+               // Build toggle link
+               var $toggler = $( '<span class="groupexpander 
collapsed">[</span>' )
+                       .append( $( '<a href="#" onclick="return false;"></a>' )
+                       .text( mw.msg( 'translate-langstats-expand' ) ) )
+                       .append( ']' )
+                       .click( function() {
+                               var $el = $( this );
+                               // Switch the state and toggle the rows
+                               if ( $el.hasClass( 'collapsed' ) ) {
+                                       $children.fadeIn().trigger( 'show' );
+                                       $el.removeClass( 'collapsed' 
).addClass( 'expanded' );
+                                       $el.find( '> a' ).text( mw.msg( 
'translate-langstats-collapse' ) );
+                               } else {
+                                       $children.fadeOut().trigger( 'hide' );
+                                       $el.addClass( 'collapsed' 
).removeClass( 'expanded' );
+                                       $el.find( '> a' ).text( mw.msg( 
'translate-langstats-expand' ) );
+                               }
+                       } );
 
-                               // Add the toggle link to the first cell of the 
meta group table-row
-                               $thisGroup.find( ' > td:first' ).append( 
$toggler );
+               // Add the toggle link to the first cell of the meta group 
table-row
+               $parent.find( ' > td:first' ).append( $toggler );
+               
+               // Handle hide/show recursively, so that collapsing parent group
+               // hides all sub groups regardless of nesting level
+               $parent.on( 'hide show', function( event ) {
+                       // Reuse $toggle, $parent and $children from parent 
scope
+                       if ( $toggler.hasClass( 'expanded' ) ) {
+                               $children.trigger( event.type )[event.type]();
                        }
                } );
+       } );
 
-               // Create, bind and append the toggle-all button
-               var $allChildRows = $( 'tr[data-parentgroups]', $translateTable 
),
-                       $allToggles_cache = null,
-                       $toggleAllButton = $( '<span 
class="mw-sp-langstats-expander">[</span>' ).append( $( '<a href="#" 
onclick="return false;"></a>' ).text( mw.msg( 'translate-langstats-expandall' ) 
) ).append( ']' ).click( function() {
-                               var     $el = $( this ),
-                                       $allToggles = !!$allToggles_cache ? 
$allToggles_cache : $( '.mw-sp-langstats-toggle', $translateTable );
+       // Create, bind and append the toggle-all button
+       var
+               $allChildRows = $( 'tr[data-parentgroup]', $translateTable ),
+               $allToggles_cache = null,
+               $toggleAllButton = $( '<span class="collapsed">[</span>' )
+                       .append( $( '<a href="#" onclick="return false;"></a>' )
+                       .text( mw.msg( 'translate-langstats-expandall' ) ) )
+                       .append( ']' )
+                       .click( function() {
+                               var
+                                       $el = $( this ),
+                                       $allToggles = !!$allToggles_cache ? 
$allToggles_cache : $( '.groupexpander', $translateTable );
                                // Switch the state and toggle the rows
                                // and update the local toggles too
-                               if ( $el.hasClass( 'mw-sp-langstats-expander' ) 
) {
+                               if ( $el.hasClass( 'collapsed' ) ) {
                                        $allChildRows.show();
-                                       $el.add( $allToggles ).removeClass( 
'mw-sp-langstats-expander' ).addClass( 'mw-sp-langstats-collapser' );
+                                       $el.add( $allToggles ).removeClass( 
'collapsed' ).addClass( 'expanded' );
                                        $el.find( '> a' ).text( mw.msg( 
'translate-langstats-collapseall' ) );
                                        $allToggles.find( '> a' ).text( mw.msg( 
'translate-langstats-collapse' ) );
                                } else {
                                        $allChildRows.hide();
-                                       $el.add( $allToggles ).addClass( 
'mw-sp-langstats-expander' ).removeClass( 'mw-sp-langstats-collapser' );
+                                       $el.add( $allToggles ).addClass( 
'collapsed' ).removeClass( 'expanded' );
                                        $el.find( '> a' ).text( mw.msg( 
'translate-langstats-expandall' ) );
                                        $allToggles.find( '> a' ).text( mw.msg( 
'translate-langstats-expand' ) );
                                }
                        } );
 
-               // Initially hide them
-               $allChildRows.hide();
+       // Initially hide them
+       $allChildRows.hide();
 
-               // Add the toggle-all button above the table
-               $( '<p class="mw-sp-langstats-toggleall"></p>' ).append( 
$toggleAllButton ).insertBefore( $translateTable );
-       }
+       // Add the toggle-all button above the table
+       $( '<p class="groupexpander-all"></p>' ).append( $toggleAllButton 
).insertBefore( $translateTable );
 } );
 
 // When hovering a row, adjust brightness of the last two custom-colored cells 
as well
 // See also translate.langstats.css for the highlighting for the other normal 
rows
 mw.loader.using( 'jquery.colorUtil' , function() {
+       "use strict";
        jQuery( document ).ready( function( $ ) {
                $( '.mw-sp-translate-table.wikitable tr' ).hover( function() {
                        $( '> td.hover-color', this )

Modified: trunk/extensions/Translate/specials/SpecialLanguageStats.php
===================================================================
--- trunk/extensions/Translate/specials/SpecialLanguageStats.php        
2012-03-12 11:00:34 UTC (rev 113615)
+++ trunk/extensions/Translate/specials/SpecialLanguageStats.php        
2012-03-12 12:14:48 UTC (rev 113616)
@@ -327,35 +327,39 @@
        }
 
        /**
-        * @param $item
-        * @param $cache
-        * @param $parent string
+        * Creates a html table row for given (top-level) message group.
+        * If $item is an array, meaning that the first group is an
+        * AggregateMessageGroup and the latter are its children, it will 
recurse
+        * and create rows for them too.
+        * @param $item Array|MessageGroup
+        * @param $cache Array Cache as returned by 
MessageGroupStats::forLanguage
+        * @param $parent MessageGroup (do not use, used internally only)
         * @return string
         */
-       protected function makeGroupGroup( $item, $cache, $parent = '' ) {
+       protected function makeGroupGroup( $item, array $cache, MessageGroup 
$parent = null ) {
                if ( !is_array( $item ) ) {
-                       return $this->makeGroupRow( $item, $cache, $parent === 
'' ? false : $parent );
+                       return $this->makeGroupRow( $item, $cache, $parent );
                }
 
+               // The first group in the array is the parent 
AggregateMessageGroup
                $out = '';
                $top = array_shift( $item );
-               $out .= $this->makeGroupRow( $top, $cache, $parent === '' ? 
true : $parent );
+               $out .= $this->makeGroupRow( $top, $cache, $parent );
 
+               // Rest are children
                foreach ( $item as $subgroup ) {
-                       $parents = trim( $parent . ' ' . $top->getId() );
-                       $out .= $this->makeGroupGroup( $subgroup, $cache, 
$parents );
+                       $out .= $this->makeGroupGroup( $subgroup, $cache, $top 
);
                }
 
                return $out;
        }
 
        /**
-        * @param $group
-        * @param $cache
-        * @param $parent bool
+        * Actually creates the table for single message group, unless it
+        * is blacklisted or hidden by filters.
         * @return string
         */
-       protected function makeGroupRow( $group, $cache, $parent = false ) {
+       protected function makeGroupRow( MessageGroup $group, array $cache, 
MessageGroup $parent = null ) {
                $groupId = $group->getId();
 
                if ( $this->table->isBlacklisted( $groupId, $this->target ) !== 
null ) {
@@ -394,11 +398,9 @@
 
                $rowParams = array();
                $rowParams['data-groupid'] = $groupId;
-
-               if ( is_string( $parent ) ) {
-                       $rowParams['data-parentgroups'] = $parent;
-               } elseif ( $parent === true ) {
-                       $rowParams['data-ismeta'] = '1';
+               $rowParams['class'] = get_class( $group );
+               if ( $parent ) {
+                       $rowParams['data-parentgroup'] = $parent->getId();
                }
 
                $out  = "\t" . Html::openElement( 'tr', $rowParams );


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

Reply via email to