summaryrefslogtreecommitdiff
blob: aeec9314da231a225fda78da9cd31df79386d039 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*!
 * Translate language statistics bar - jQuery plugin.
 *
 * @author Niklas Laxström
 * @author Santhosh Thottingal
 * @license GPL-2.0-or-later
 * @since 2012-11-30
 */

/*
 * Usage:
 *     $( '<div>' ).languagestatsbar( {
 *         language: 'fi',
 *         group: 'core'
 *     } );
 * The status bar will be rendered to the newly created div. Or use any container.
 */
( function () {
	'use strict';

	var LanguageStatsBar = function ( container, options ) {
		this.$container = $( container );
		this.group = options.group;
		this.language = options.language;
		this.$statsBar = null;
		this.elements = null;
		this.init();
	};

	LanguageStatsBar.prototype = {
		init: function () {
			if ( mw.translate.languagestats[ this.language ] ) {
				this.render();
			} else {
				mw.translate.loadLanguageStats( this.language )
					.done( this.render.bind( this ) );
			}
		},

		/**
		 * Listen for the change events and update the statsbar
		 */
		listen: function () {
			var i,
				statsbar = this,
				languageStats = mw.translate.languagestats[ this.language ];

			statsbar.$statsBar.on( 'change', function ( event, to, from ) {
				for ( i = 0; i < languageStats.length; i++ ) {
					if ( languageStats[ i ].group === statsbar.group ) {
						// Changing a proofread message does not create a new translation
						if ( to === 'translated' && from !== 'proofread' ) {
							languageStats[ i ].translated++;
						}
						if ( to === 'proofread' ) {
							languageStats[ i ].proofread++;
						}
						if ( to === 'fuzzy' ) {
							languageStats[ i ].fuzzy++;
						}

						if ( from === 'fuzzy' ) {
							languageStats[ i ].fuzzy--;
						}
						if ( from === 'proofread' ) {
							languageStats[ i ].proofread--;
						}
						// Proofreading a message does not remove translation
						if ( from === 'translated' && to !== 'proofread' ) {
							languageStats[ i ].translated--;
						}
						break;
					}
				}

				// Update the stats bar
				statsbar.update();
			} );

			statsbar.$container.on( {
				mouseenter: function () {
					statsbar.elements.$info.removeClass( 'hide' );
				},
				mouseleave: function () {
					statsbar.elements.$info.addClass( 'hide' );
				}
			} );
		},

		render: function () {
			this.$statsBar = $( '<div>' )
				.addClass( 'tux-statsbar' )
				.data( 'group', this.group );

			this.elements = {
				$proofread: $( '<span>' ).addClass( 'tux-proofread' ),
				$translated: $( '<span>' ).addClass( 'tux-translated' ),
				$fuzzy: $( '<span>' ).addClass( 'tux-fuzzy' ),
				$untranslated: $( '<span>' ).addClass( 'tux-untranslated' ),
				$info: $( '<div>' ).addClass( 'tux-statsbar-info hide' )
			};

			this.update();
			this.$statsBar.append( [
				// Append needs an array instead of an object
				this.elements.$proofread,
				this.elements.$translated,
				this.elements.$fuzzy,
				this.elements.$untranslated,
				this.elements.$info
			] );
			this.$container.append( this.$statsBar );

			this.listen();
		},

		update: function () {
			var proofread, translated, fuzzy, untranslated,
				stats = this.getStatsForGroup( this.group );

			proofread = 100 * stats.proofread / stats.total;
			// Proofread messages are also translated, so remove those for
			// the bar showing only translated count.
			translated = stats.translated - stats.proofread;
			translated = 100 * translated / stats.total;
			fuzzy = 100 * stats.fuzzy / stats.total;
			untranslated = 100 - proofread - translated - fuzzy;

			this.elements.$proofread[ 0 ].style.width = proofread + '%';
			this.elements.$translated[ 0 ].style.width = translated + '%';
			this.elements.$fuzzy[ 0 ].style.width = fuzzy + '%';
			this.elements.$untranslated[ 0 ].style.width = untranslated + '%';

			translated = !translated ? 0 : translated + proofread;
			proofread = !proofread ? 0 : proofread;

			if ( fuzzy ) {
				this.elements.$info
					.text( mw.msg( 'translate-statsbar-tooltip-with-fuzzy',
						translated.toFixed(), proofread.toFixed(),
						fuzzy.toFixed() ) );
			} else {
				this.elements.$info
					.text( mw.msg( 'translate-statsbar-tooltip',
						translated.toFixed(), proofread.toFixed() ) );
			}
		},

		getStatsForGroup: function ( group ) {
			var i,
				languageStats = mw.translate.languagestats[ this.language ];

			for ( i = 0; i < languageStats.length; i++ ) {
				if ( languageStats[ i ].group === group ) {
					return languageStats[ i ];
				}
			}

			return {
				proofread: 0,
				total: 0,
				fuzzy: 0,
				translated: 0
			};
		}
	};

	/*
	 * languagestatsbar PLUGIN DEFINITION
	 */

	$.fn.languagestatsbar = function ( options ) {
		return this.each( function () {
			var $this = $( this ),
				data = $this.data( 'languagestatsbar' );

			if ( !data ) {
				$this.data( 'languagestatsbar', ( data = new LanguageStatsBar( this, options ) ) );
			}
		} );
	};

	$.fn.languagestatsbar.Constructor = LanguageStatsBar;

	mw.translate = mw.translate || {};

}() );