summaryrefslogtreecommitdiff
blob: 9337ed5fc38f82dafbd8d7075495e187354e85d8 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
( function () {
	var RenameDropdown,
		GroupSynchronization;

	$( function () {
		var windowManager, renameDialog;

		RenameDropdown.init();
		GroupSynchronization.init();

		// Create and append a window manager.
		windowManager = new OO.ui.WindowManager();
		windowManager.$element.appendTo( document.body );

		// Create a new process dialog window.
		renameDialog = new mw.translate.MessageRenameDialog( {
			classes: [ 'smg-rename-dialog' ],
			size: 'large'
		}, function ( renameParams ) {
			return setRename( renameParams ).done( function ( data ) {
				if ( data.managemessagegroups && data.managemessagegroups.success ) {
					location.reload();
				}
			} ).fail( function ( code, result ) {
				if ( result.error ) {
					mw.notify( result.error.info, {
						type: 'error',
						tag: 'new-error'
					} );
				}
			} );
		} );

		// Add the window to window manager using the addWindows() method.
		windowManager.addWindows( [ renameDialog ] );

		/**
		 * Attach the click handler to display the rename dropdown.
		 */
		$( '#mw-content-text' ).on( 'click', '.smg-rename-actions', function ( event ) {
			var $target, $parentContainer;
			$target = $( event.target );
			$parentContainer = $target.parents( '.mw-translate-smg-change' );
			RenameDropdown.appendTo( event.target, $parentContainer, {
				groupId: $target.data( 'groupId' ),
				msgKey: $target.data( 'msgkey' ),
				msgTitle: $target.data( 'msgtitle' )
			} ).show();

			if ( $parentContainer.hasClass( 'smg-change-addition' ) ) {
				// For a new message, the "add as new" option is hidden.
				RenameDropdown.hideOption( '.smg-rename-new-action' );
			}
			event.preventDefault();
		} );

		$( document.documentElement ).on( 'click', function ( event ) {
			if ( !event.isDefaultPrevented() ) {
				RenameDropdown.hide();
			}
		} );

		/**
		 * Click handler triggered when "Add as rename" is clicked in the dropdown.
		 */
		$( '.smg-rename-rename-action' ).on( 'click', function () {
			var keyData = RenameDropdown.getData(),
				$renameButton = getRenameButton( $( event.target ) );
			toggleLoading( $renameButton, true );

			getRenames( keyData.groupId, keyData.msgKey )
				.done( function ( data ) {
				// Open the dialog, and display possible renames.
					windowManager.openWindow( renameDialog, {
						messages: data.managemessagegroups[ 0 ],
						title: mw.msg( 'translate-smg-rename-dialog-title', keyData.msgTitle ),
						groupId: keyData.groupId,
						targetKey: keyData.msgKey
					} );
				} ).fail( function ( code, result ) {
					if ( result.error ) {
						mw.notify( result.error.info, {
							type: 'error',
							tag: 'rename-error'
						} );
					}
				} ).always( function () {
					toggleLoading( $renameButton, false );
				} );
		} );

		/**
		 * Click handler triggered when "Add as new" is clicked in the dropdown.
		 */
		$( '.smg-rename-new-action' ).on( 'click', function () {
			var keyData = RenameDropdown.getData(),
				$renameButton = getRenameButton( $( event.target ) ),
				isReloading = false;
			toggleLoading( $renameButton, true );

			setAsNew( keyData.groupId, keyData.msgKey ).done( function ( data ) {
				if ( data.managemessagegroups && data.managemessagegroups.success ) {
					location.reload();
					isReloading = true;
				}
			} ).fail( function ( code, result ) {
				if ( result.error ) {
					mw.notify( result.error.info, {
						type: 'error',
						tag: 'new-error'
					} );
				}
			} ).always( function () {
				// If page is reloading, don't bother hiding the loader.
				if ( isReloading === false ) {
					toggleLoading( $renameButton, false );
				}
			} );
		} );
	} );

	function getRenameButton( $target ) {
		return $target.parents( '.mw-translate-smg-change' ).find( '.smg-rename-actions' );
	}

	function toggleLoading( $element, isLoading ) {
		if ( isLoading ) {
			// hide all the rename buttons, but show the current one with loading animation
			$( '.smg-rename-actions' ).addClass( 'mw-translate-hide' );
			$element.removeClass( 'mw-translate-hide' ).addClass( 'loading' );
		} else {
			$( '.smg-rename-actions' ).removeClass( 'mw-translate-hide' );
			$element.removeClass( 'loading' );
		}
	}

	/**
	 * Fetch the possible renames for a given message.
	 *
	 * @param {string} groupId
	 * @param {string} msgKey
	 * @return {jQuery.Promise}
	 */
	function getRenames( groupId, msgKey ) {
		var params, api = new mw.Api(), changesetName;
		params = {
			action: 'query',
			meta: 'managemessagegroups',
			formatversion: 2,
			mmggroupId: groupId,
			mmgmessageKey: msgKey
		};

		changesetName = getChangesetName();
		if ( changesetName !== null ) {
			params.mmgchangesetName = changesetName;
		}

		return api.get( params ).then( function ( result ) {
			return result.query;
		} );
	}

	/**
	 * Identifies and returns the group name from the URL
	 *
	 * @return {string}
	 */
	function getChangesetName() {
		var locationPaths, suffix, pageTitle;
		locationPaths = window.location.pathname.split( '/' );
		suffix = locationPaths.pop();
		pageTitle = $( '#smgPageTitle' ).val();

		if ( suffix && suffix.indexOf( pageTitle ) === -1 ) {
			return suffix;
		}

		return null;
	}

	function getChangesetModifiedTime() {
		var modifiedTime = $( '[name="changesetModifiedTime"]' ).val();
		modifiedTime = +modifiedTime;
		if ( isNaN( modifiedTime ) ) {
			return 0;
		}

		return modifiedTime;
	}

	/**
	 * Update the rename associated with a message
	 *
	 * @param {Object} renameParams
	 * @param {string} renameParams.groupId
	 * @param {string} renameParams.selectedKey Key to be matched to. This message will be renamed.
	 * @param {string} renameParams.targetKey Key from the source
	 * @return {jQuery.Promise}
	 */
	function setRename( renameParams ) {
		var params, api = new mw.Api(), changesetName;

		params = {
			action: 'managemessagegroups',
			groupId: renameParams.groupId,
			renameMessageKey: renameParams.selectedKey,
			messageKey: renameParams.targetKey,
			operation: 'rename',
			changesetModified: getChangesetModifiedTime(),
			assert: 'user',
			formatversion: 2
		};

		changesetName = getChangesetName();
		if ( changesetName !== null ) {
			params.changesetName = changesetName;
		}
		return api.postWithToken( 'csrf', params );
	}

	/**
	 * Mark the message as a new message
	 *
	 * @param {string} groupId
	 * @param {string} msgKey
	 * @return {jQuery.Promise}
	 */
	function setAsNew( groupId, msgKey ) {
		var params, api = new mw.Api(), changesetName;

		params = {
			action: 'managemessagegroups',
			groupId: groupId,
			messageKey: msgKey,
			operation: 'new',
			changesetModified: getChangesetModifiedTime(),
			assert: 'user',
			formatversion: 2
		};

		changesetName = getChangesetName();
		if ( changesetName !== null ) {
			params.changesetName = changesetName;
		}

		return api.postWithToken( 'csrf', params );
	}

	RenameDropdown = ( function () {
		var $renameMenu;

		/**
		 * Initialization function. Creates the elements for the rename dropdown
		 *
		 * @chainable
		 */
		function init() {
			$renameMenu = getRenameDropdown().appendTo( document.body );
			return this;
		}

		/**
		 * Returns the HTML element for the dropdown
		 *
		 * @return {jQuery}
		 */
		function getRenameDropdown() {
			var $addAsRename = $( '<li>' ).append(
					$( '<button>' )
						.attr( 'type', 'button' )
						.addClass( 'smg-rename-new-action mw-translate-hide' )
						.text( mw.msg( 'translate-smg-rename-new' ) )
				),
				$addAsNew = $( '<li>' ).append(
					$( '<button>' )
						.attr( 'type', 'button' )
						.addClass( 'smg-rename-rename-action mw-translate-hide' )
						.text( mw.msg( 'translate-smg-rename-rename' ) )
				);

			return $( '<ul>' ).addClass( 'smg-rename-dropdown-menu' ).append(
				$addAsRename,
				$addAsNew
			);
		}

		/**
		 * Displays the rename menu
		 *
		 * @chainable
		 */
		function show() {
			$renameMenu.addClass( 'show' );
			return this;
		}

		/**
		 * Hides the rename menu
		 *
		 * @chainable
		 */
		function hide() {
			$renameMenu.removeClass( 'show' );
			return this;
		}

		/**
		 * Appends the dropdown to a container element
		 *
		 * @param {jQuery} target Target trigger element
		 * @param {jQuery} $container Container to which to append the menu
		 * @param {Object} customData Custom data to be associated with the menu
		 * @chainable
		 */
		function appendTo( target, $container, customData ) {
			var $currentTarget = $( target );
			$container.append( $renameMenu );
			$renameMenu.css( {
				top: $currentTarget.position().top + $currentTarget.height(),
				left: $currentTarget.position().left - $renameMenu.width() + $currentTarget.width()
			} ).data( 'custom-data', customData );

			// When appending, show all the li's by default since based on the
			// message type (RENAME / NEW) some li's may be hidden previously
			$renameMenu.find( 'li' ).removeClass( 'mw-translate-hide' );
			return this;
		}

		/**
		 * Fetch the custom data associated with rename menu
		 *
		 * @return {Object}
		 */
		function getData() {
			return $renameMenu.data( 'custom-data' );
		}

		/**
		 * Hide a specific option in the dropdown
		 *
		 * @param {string} optSelector
		 * @chainable
		 */
		function hideOption( optSelector ) {
			$renameMenu.find( optSelector ).parent().addClass( 'mw-translate-hide' );
			return this;
		}

		return {
			init: init,
			appendTo: appendTo,
			show: show,
			hide: hide,
			getData: getData,
			hideOption: hideOption
		};
	}() );

	GroupSynchronization = ( function () {
		function init() {
			$( '.js-group-sync-message-resolve' ).on( 'click', markMessageAsResolved );
			$( '.js-group-sync-group-resolve' ).on( 'click', markGroupAsResolved );
		}

		function markMessageAsResolved() {
			var $target = $( this ),
				groupId = $target.data( 'groupId' ),
				messageTitle = $target.data( 'msgTitle' );

			showLoading( $target );

			markAsResolved( 'resolveMessage', groupId, messageTitle ).done( function ( response ) {
				var responseData = response.managegroupsynchronizationcache || null;
				if ( responseData && responseData.success ) {
					if ( responseData.data.groupRemainingMessageCount === 0 ) {
						removeParentGroupBlock( $target );
					} else {
						// Remove the message from the DOM
						$target.parents( '.js-group-sync-message-error' ).remove();
					}
				}
			} ).fail( function ( code, result ) {
				handleResolutionFailure( code, result, groupId, messageTitle );
			} ).always( function () {
				hideLoading( $target );
			} );
		}

		function markGroupAsResolved() {
			var $target = $( this ),
				groupId = $target.data( 'groupId' );

			showLoading( $target );

			markAsResolved( 'resolveGroup', groupId ).done( function ( response ) {
				var responseData = response.managegroupsynchronizationcache || null;
				if ( responseData && responseData.success ) {
					removeParentGroupBlock( $target );
				}
			} ).fail( function ( code, result ) {
				handleResolutionFailure( code, result, groupId );
			} ).always( function () {
				hideLoading( $target );
			} );
		}

		function markAsResolved( operation, groupId, messageTitle ) {
			var params, api = new mw.Api();

			params = {
				action: 'managegroupsynchronizationcache',
				group: groupId,
				operation: operation,
				assert: 'user',
				formatversion: 2
			};

			if ( messageTitle ) {
				params.title = messageTitle;
			}

			return api.postWithToken( 'csrf', params );
		}

		function removeParentGroupBlock( $child ) {
			// Remove the entire group block from DOM
			$child.parents( '.js-group-sync-group-errors' ).remove();
			// If all groups are resolved, remove the group sync error block
			if ( !$( '.js-group-sync-group-errors' ).length ) {
				$( '.js-group-sync-groups-with-error' ).remove();
			}
		}

		function handleResolutionFailure( code, result, groupId, messageTitle ) {
			var errorInfo = result && result.error ? result.error.info : null;
			if ( errorInfo ) {
				mw.notify( result.error.info, {
					type: 'error',
					tag: 'new-error'
				} );
			} else {
				// Unknown error
				mw.notify( mw.msg( 'translate-smg-unknown-error' ), {
					type: 'error',
					tag: 'new-error'
				} );
			}

			mw.log.error( 'Error while resolving group or message. Param: ' + JSON.stringify( {
				groupId: groupId,
				messageTitle: messageTitle,
				errorCode: code,
				errorInfo: errorInfo
			} ) );
		}

		function showLoading( $target ) {
			$target.addClass( 'loading' )
				.text( mw.msg( 'translate-smg-loading' ) )
				.removeAttr( 'href' );
		}

		function hideLoading( $target ) {
			$target.removeClass( 'loading' )
				.text( mw.msg( 'translate-smg-group-action-resolve' ) )
				.prop( 'href', '#' );
		}

		return {
			init: init
		};
	}() );
}() );