Conditions | 1 |
Paths | 1 |
Total Lines | 97 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | /** |
||
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 |
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.