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