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
|
( function () {
var RenameDropdown;
$( function () {
var windowManager, renameDialog;
RenameDropdown.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' ) ) {
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 ) {
$( '.smg-rename-actions' ).hide();
$element.show().addClass( 'loading' );
} else {
$( '.smg-rename-actions' ).show();
$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' )
.text( mw.msg( 'translate-smg-rename-new' ) )
),
$addAsNew = $( '<li>' ).append(
$( '<button>' )
.attr( 'type', 'button' )
.addClass( 'smg-rename-rename-action' )
.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 );
// show all the li's
$renameMenu.find( 'li' ).show();
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().hide();
return this;
}
return {
init: init,
appendTo: appendTo,
show: show,
hide: hide,
getData: getData,
hideOption: hideOption
};
}() );
}() );
|