Completed
Pull Request — master (#7)
by Mark A.
02:37
created

$.done   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 9.3191
c 0
b 0
f 0
cc 2
nc 2
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * File containing the SimpleBatchUpload class
3
 *
4
 * @copyright (C) 2016, Stephan Gambke
5
 * @license   GNU General Public License, version 2 (or any later version)
6
 *
7
 * This software is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * @file
19
 * @ingroup SimpleBatchUpload
20
 */
21
22
/** global: mediaWiki */
23
/** global: jQuery */
24
25
/* Load the RL module mediawiki.api.messages and when loaded and the page is ready; */
26
$.when( mw.loader.using(["mediawiki.api.messages", "mediawiki.jqueryMsg"]), $.ready ).done( function() {
0 ignored issues
show
Bug introduced by
The variable mw seems to be never declared. If this is a global, consider adding a /** global: mw */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
27
	/* Load the messages that you need if they are not yet loaded, then do stuff with them */
28
	new mw.Api().loadMessagesIfMissing( ['simplebatchupload-comment'  ] ).done(
0 ignored issues
show
Bug introduced by
The variable mw seems to be never declared. If this is a global, consider adding a /** global: mw */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
29
		function ( $, mw, undefined ) {
30
31
			'use strict';
32
33
			$( function () {
34
				$( '#fileupload' )
35
36
					.on( 'change', function ( /* e, data */ ) { $( '#fileupload-results' ).empty(); } )
37
					.on( 'drop', function ( /* e, data */ ) { $( '#fileupload-results' ).empty(); } )
38
39
					.fileupload( {
40
						dataType: 'json',
41
						dropZone: $( '#fileupload-dropzone' ),
42
						progressInterval: 100,
43
44
45
						add: function ( e, data ) {
46
47
							var that = this;
48
							data.id = Date.now();
49
50
							var status = $('<li>')
51
								.attr( 'id', data.id )
52
								.text( data.files[0].name );
53
54
							$( '#fileupload-results' ).append( status );
55
56
							var api = new mw.Api();
57
58
							var tokenType = 'csrf';
59
60
							if ( mw.config.get( 'wgVersion' ) < '1.27.0' ) {
61
								tokenType = 'edit';
62
							}
63
64
							// invalidate cached token; always request a new one
65
							api.badToken( tokenType );
66
67
							api.getToken( tokenType )
68
								.then(
69
									function ( token ) {
70
71
										data.formData = {
72
											format: 'json',
73
											action: 'upload',
74
											token: token,
75
											ignorewarnings: 1,
76
											text: $( that ).fileupload( 'option', 'text' ),
77
											comment: $( that ).fileupload( 'option', 'comment' ),
78
											filename: data.files[ 0 ].name
79
										};
80
81
										data.submit()
82
											.success( function ( result /*, textStatus, jqXHR */ ) {
83
84
												if ( result.error !== undefined ) {
85
86
													status.text( status.text() + " ERROR: " + result.error.info ).addClass( 'ful-error' );
87
88
												} else {
89
													var link = $( '<a>' );
90
													link
91
														.attr( 'href', mw.Title.newFromFileName( result.upload.filename ).getUrl() )
92
														.text( result.upload.filename );
93
94
													status
95
														.addClass( 'ful-success' )
96
														.text( ' OK' )
97
														.prepend( link );
98
												}
99
100
											} )
101
											.error( function ( /* jqXHR, textStatus, errorThrown */ ) {
102
												status.text( status.text() + " ERROR" ).addClass( 'ful-error' );
103
											} );
104
									},
105
									function () {
106
										status.text( status.text() + " ERROR" ).addClass( 'ful-error' );
107
									}
108
								);
109
110
						},
111
112
						progress: function (e, data) {
113
							if ( data.loaded !== data.total ) {
114
								$( '#' + data.id )
115
									.text( data.files[0].name + ' ' + parseInt(data.loaded / data.total * 100, 10) + '%' );
116
							}
117
						}
118
					} );
119
120
				$( document ).bind( 'drop dragover', function ( e ) {
121
					e.preventDefault();
122
				} );
123
			} );
124
125
		}( jQuery, mediaWiki ));} );
126