summaryrefslogtreecommitdiff
blob: 0ec2a97517944764e62c935c0ee47ff1ca8cbe40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * 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-2.0+, CC-BY-SA-3.0
 */
/*global mw:false*/
jQuery( document ).ready( function ( $ ) {
	'use strict';

	var $allChildRows, $allTogglesCache, $toggleAllButton,
		$translateTable = $( '.mw-sp-translate-table' ),
		$metaRows = $( 'tr.AggregateMessageGroup', $translateTable );

	// Quick return
	if ( !$metaRows.size() ) {
		return;
	}

	$metaRows.each( function () {
		var $toggler,
			$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 ( !$children.size() ) {
			return;
		}

		// Build toggle link
		$toggler = $( '<span class="groupexpander collapsed">[</span>' )
			.append( $( '<a href="#"></a>' )
				.text( mw.msg( 'translate-langstats-expand' ) ) )
			.append( ']' )
			.click( function ( e ) {
				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' ) );
				}

				e.preventDefault();
			} );

		// 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
	$allChildRows = $( 'tr[data-parentgroup]', $translateTable );
	$allTogglesCache = null;
	$toggleAllButton = $( '<span class="collapsed">[</span>' )
		.append( $( '<a href="#"></a>' )
			.text( mw.msg( 'translate-langstats-expandall' ) ) )
		.append( ']' )
		.click( function ( e ) {
			var $el = $( this ),
				$allToggles = !!$allTogglesCache ? $allTogglesCache : $( '.groupexpander', $translateTable );

			// Switch the state and toggle the rows
			// and update the local toggles too
			if ( $el.hasClass( 'collapsed' ) ) {
				$allChildRows.show();
				$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( 'collapsed' ).removeClass( 'expanded' );
				$el.find( '> a' ).text( mw.msg( 'translate-langstats-expandall' ) );
				$allToggles.find( '> a' ).text( mw.msg( 'translate-langstats-expand' ) );
			}

			e.preventDefault();
		} );

	// Initially hide them
	$allChildRows.hide();

	// 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 ( $ ) {
		// It is possible that the first event we get is hover-out, in
		// which case the colors will get stuck wrong. Ignore it.
		var seenHoverIn = false;

		$( '.mw-sp-translate-table.wikitable tr' ).hover( function () {
			seenHoverIn = true;
			$( '> td.hover-color', this )
				// 30% more brightness
				.css( 'background-color', function ( i, val ) {
					// @codingStandardsIgnoreStart Bug in CodeSniffer?
					return $.colorUtil.getColorBrightness( val, +0.3 );
					// codingStandardsIgnoreEnd
				} );
		}, function () {
			if ( !seenHoverIn ) {
				return;
			}
			$( '> td.hover-color', this )
				// 30% less brightness
				.css( 'background-color', function ( i, val ) {
					return $.colorUtil.getColorBrightness( val, -0.3 );
				} );
		} );
	} );
} );

(function ( mw, $ ) {
	'use strict';
	$( document ).ready( function () {
		var index,
			sort = {},
			re = /#sortable:(\d+)=(asc|desc)/,
			match = re.exec( window.location.hash ),
			$tables = $( '.statstable' );

		if ( match ) {
			index = parseInt( match[1], 10 );
			sort[index] = match[2];
		}
		$tables.tablesorter( {sortList: [sort]} );

		$tables.on( 'sortEnd.tablesorter', function () {
			var $table = $( this );
			$table.find( '.headerSortDown, .headerSortUp' ).each( function () {
				var index = $table.find( 'th' ).index( $( this ) ),
					dir = $( this ).hasClass( 'headerSortUp' ) ? 'desc' : 'asc';
				window.location.hash = 'sortable:' + index + '=' + dir;
			} );
		} );
	} );
}( mediaWiki, jQuery ) );