Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/77266
Change subject: legacy upload.js: Clean up and remove from jshintignore
......................................................................
legacy upload.js: Clean up and remove from jshintignore
* Coding style:
- Braces
- Single quotes
- Dot instead of bracket access for properties
* Fixed various implied global/static variables, most significantly
the for-loop variable 'cached' etc.
* Changed == '' to ! since === '' will require it to be a string
and == '' returns true for almost anything that is falsy, no
good way to choose a strict alternative.
* Changed setAttribute('disabled', ..) to using .property instead.
* Using setTimeout as a global instead of as a window property.
* Using other globals from wikibits.js through window property,
as they are defined.
* Use a closure instead of string eval() in setTimeout().
* Made local equivelants for some global variables for access
inside the closure.
* Don't create functions in a loop. Moved the onchange function
outside the loop and re-used it for each one (safe in this case,
and more efficient).
Change-Id: I9df912ee48d30c189394bf07f5e49014220f36a3
---
M .jshintignore
M skins/common/upload.js
2 files changed, 161 insertions(+), 120 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/66/77266/1
diff --git a/.jshintignore b/.jshintignore
index ad5e959..f067ecd 100644
--- a/.jshintignore
+++ b/.jshintignore
@@ -29,7 +29,6 @@
skins/common/IEFixes.js
skins/common/config.js
skins/common/protect.js
-skins/common/upload.js
# github.com/jshint/jshint/issues/729
tests/qunit/suites/resources/mediawiki/mediawiki.jscompat.test.js
diff --git a/skins/common/upload.js b/skins/common/upload.js
index 4246e82..4ebde75 100644
--- a/skins/common/upload.js
+++ b/skins/common/upload.js
@@ -1,39 +1,48 @@
+/*global sajax_init_object, sajax_do_call */
+/*jshint camelcase:false */
( function ( mw, $ ) {
-var ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
+var licenseSelectorCheck, wgUploadWarningObj, wgUploadLicenseObj,
fillDestFilename,
+ ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
fileExtensions = mw.config.get( 'wgFileExtensions' );
-window.licenseSelectorCheck = function() {
- var selector = document.getElementById( "wpLicense" );
- var selection = selector.options[selector.selectedIndex].value;
- if( selector.selectedIndex > 0 ) {
- if( selection == "" ) {
+licenseSelectorCheck = window.licenseSelectorCheck = function () {
+ var selector = document.getElementById( 'wpLicense' ),
+ selection = selector.options[selector.selectedIndex].value;
+ if ( selector.selectedIndex > 0 ) {
+ if ( !selection ) {
// Option disabled, but browser is broken and doesn't
respect this
selector.selectedIndex = 0;
}
}
// We might show a preview
- wgUploadLicenseObj.fetchPreview( selection );
+ wgUploadWarningObj.fetchPreview( selection );
};
function uploadSetup() {
// Disable URL box if the URL copy upload source type is not selected
- var e = document.getElementById( 'wpSourceTypeurl' );
- if( e ) {
- if( !e.checked ) {
- var ein = document.getElementById( 'wpUploadFileURL' );
- if(ein)
- ein.setAttribute( 'disabled', 'disabled' );
+ var ein,
+ selector, ua, isMacIe, i,
+ optionsTable, row, td,
+ wpLicense, wpLicenseRow, wpLicenseTbody,
+ uploadSourceIds, len, onchange,
+ e = document.getElementById( 'wpSourceTypeurl' );
+ if ( e ) {
+ if ( !e.checked ) {
+ ein = document.getElementById( 'wpUploadFileURL' );
+ if ( ein ) {
+ ein.disabled = true;
+ }
}
}
// For MSIE/Mac: non-breaking spaces cause the <option> not to render.
// But for some reason, setting the text to itself works
- var selector = document.getElementById("wpLicense");
- if (selector) {
- var ua = navigator.userAgent;
- var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac")
!= -1);
- if (isMacIe) {
- for (var i = 0; i < selector.options.length; i++) {
+ selector = document.getElementById( 'wpLicense' );
+ if ( selector ) {
+ ua = navigator.userAgent;
+ isMacIe = ua.indexOf( 'MSIE' ) !== -1 && ua.indexOf( 'Mac' )
!== -1;
+ if ( isMacIe ) {
+ for ( i = 0; i < selector.options.length; i++ ) {
selector.options[i].text =
selector.options[i].text;
}
}
@@ -43,31 +52,31 @@
if ( ajaxUploadDestCheck ) {
// Insert an event handler that fetches upload warnings when
wpDestFile
// has been changed
- document.getElementById( 'wpDestFile' ).onchange = function ( e
) {
- wgUploadWarningObj.checkNow(this.value);
+ document.getElementById( 'wpDestFile' ).onchange = function () {
+ wgUploadWarningObj.checkNow( this.value );
};
// Insert a row where the warnings will be displayed just below
the
// wpDestFile row
- var optionsTable = document.getElementById(
'mw-htmlform-description' ).tBodies[0];
- var row = optionsTable.insertRow( 1 );
- var td = document.createElement( 'td' );
+ optionsTable = document.getElementById(
'mw-htmlform-description' ).tBodies[0];
+ row = optionsTable.insertRow( 1 );
+ td = document.createElement( 'td' );
td.id = 'wpDestFile-warning';
td.colSpan = 2;
row.appendChild( td );
}
- var wpLicense = document.getElementById( 'wpLicense' );
+ wpLicense = document.getElementById( 'wpLicense' );
if ( mw.config.get( 'wgAjaxLicensePreview' ) && wpLicense ) {
// License selector check
wpLicense.onchange = licenseSelectorCheck;
// License selector table row
- var wpLicenseRow = wpLicense.parentNode.parentNode;
- var wpLicenseTbody = wpLicenseRow.parentNode;
+ wpLicenseRow = wpLicense.parentNode.parentNode;
+ wpLicenseTbody = wpLicenseRow.parentNode;
- var row = document.createElement( 'tr' );
- var td = document.createElement( 'td' );
+ row = document.createElement( 'tr' );
+ td = document.createElement( 'td' );
row.appendChild( td );
td = document.createElement( 'td' );
td.id = 'mw-license-preview';
@@ -78,50 +87,60 @@
// fillDestFile setup
- var i,
- uploadSourceIds = mw.config.get( 'wgUploadSourceIds' ),
- len = uploadSourceIds.length;
- for ( i = 0; i < len; i += 1 )
- document.getElementById( uploadSourceIds[i] ).onchange =
function (e) {
- fillDestFilename( this.id );
- };
-};
+ uploadSourceIds = mw.config.get( 'wgUploadSourceIds' );
+ len = uploadSourceIds.length;
+ onchange = function () {
+ fillDestFilename( this.id );
+ };
+ for ( i = 0; i < len; i += 1 ) {
+ document.getElementById( uploadSourceIds[i] ).onchange =
onchange;
+ }
+}
+wgUploadWarningObj = window.wgUploadWarningObj = {
+ responseCache: { '' : ' ' },
+ nameToCheck: '',
+ typing: false,
+ delay: 500, // ms
+ timeoutID: false,
-window.wgUploadWarningObj = {
- 'responseCache' : { '' : ' ' },
- 'nameToCheck' : '',
- 'typing': false,
- 'delay': 500, // ms
- 'timeoutID': false,
+ keypress: function () {
+ var cached, destFile, warningElt;
- 'keypress': function () {
- if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
+ if ( !ajaxUploadDestCheck || !sajax_init_object() ) {
+ return;
+ }
// Find file to upload
- var destFile = document.getElementById('wpDestFile');
- var warningElt = document.getElementById( 'wpDestFile-warning'
);
- if ( !destFile || !warningElt ) return ;
+ destFile = document.getElementById( 'wpDestFile' );
+ warningElt = document.getElementById( 'wpDestFile-warning' );
+ if ( !destFile || !warningElt ) {
+ return;
+ }
- this.nameToCheck = destFile.value ;
+ this.nameToCheck = destFile.value;
// Clear timer
if ( this.timeoutID ) {
window.clearTimeout( this.timeoutID );
}
// Check response cache
- for (cached in this.responseCache) {
- if (this.nameToCheck == cached) {
+ for ( cached in this.responseCache ) {
+ if ( this.nameToCheck === cached ) {
this.setWarning(this.responseCache[this.nameToCheck]);
return;
}
}
- this.timeoutID = window.setTimeout(
'wgUploadWarningObj.timeout()', this.delay );
+ this.timeoutID = setTimeout( function () {
+ wgUploadWarningObj.timeout();
+ }, this.delay );
},
- 'checkNow': function (fname) {
- if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
+ checkNow: function ( fname ) {
+ if ( !ajaxUploadDestCheck || !sajax_init_object() ) {
+ return;
+ }
if ( this.timeoutID ) {
window.clearTimeout( this.timeoutID );
}
@@ -129,105 +148,119 @@
this.timeout();
},
- 'timeout' : function() {
- if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
- injectSpinner( document.getElementById( 'wpDestFile' ),
'destcheck' );
+ timeout: function () {
+ if ( !ajaxUploadDestCheck || !sajax_init_object() ) {
+ return;
+ }
+ window.injectSpinner( document.getElementById( 'wpDestFile' ),
'destcheck' );
// Get variables into local scope so that they will be
preserved for the
// anonymous callback. fileName is copied so that multiple
overlapping
// ajax requests can be supported.
- var obj = this;
- var fileName = this.nameToCheck;
+ var obj = this,
+ fileName = this.nameToCheck;
sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning',
[this.nameToCheck],
- function (result) {
- obj.processResult(result, fileName)
+ function ( result ) {
+ obj.processResult( result, fileName );
}
);
},
- 'processResult' : function (result, fileName) {
- removeSpinner( 'destcheck' );
+ processResult: function ( result, fileName ) {
+ window.removeSpinner( 'destcheck' );
this.setWarning(result.responseText);
this.responseCache[fileName] = result.responseText;
},
- 'setWarning' : function (warning) {
- var warningElt = document.getElementById( 'wpDestFile-warning'
);
- var ackElt = document.getElementsByName( 'wpDestFileWarningAck'
);
+ setWarning: function ( warning ) {
+ var warningElt = document.getElementById( 'wpDestFile-warning'
),
+ ackElt = document.getElementsByName(
'wpDestFileWarningAck' );
this.setInnerHTML(warningElt, warning);
// Set a value in the form indicating that the warning is
acknowledged and
// doesn't need to be redisplayed post-upload
- if ( warning == '' || warning == ' ' ) {
+ if ( !warning || warning === ' ' ) {
ackElt[0].value = '';
} else {
ackElt[0].value = '1';
}
},
- 'setInnerHTML' : function (element, text) {
+ setInnerHTML: function ( element, text ) {
// Check for no change to avoid flicker in IE 7
- if (element.innerHTML != text) {
+ if ( element.innerHTML !== text ) {
element.innerHTML = text;
}
}
};
-window.fillDestFilename = function(id) {
+fillDestFilename = window.fillDestFilename = function ( id ) {
+ var e, path, slash, backslash, fname,
+ found, ext, i,
+ destFile;
if ( !mw.config.get( 'wgUploadAutoFill' ) ) {
return;
}
- if (!document.getElementById) {
+ if ( !document.getElementById ) {
return;
}
// Remove any previously flagged errors
- var e = document.getElementById( 'mw-upload-permitted' );
- if( e ) e.className = '';
+ e = document.getElementById( 'mw-upload-permitted' );
+ if ( e ) {
+ e.className = '';
+ }
- var e = document.getElementById( 'mw-upload-prohibited' );
- if( e ) e.className = '';
+ e = document.getElementById( 'mw-upload-prohibited' );
+ if ( e ) {
+ e.className = '';
+ }
- var path = document.getElementById(id).value;
+ path = document.getElementById( id ).value;
// Find trailing part
- var slash = path.lastIndexOf('/');
- var backslash = path.lastIndexOf('\\');
- var fname;
- if (slash == -1 && backslash == -1) {
+ slash = path.lastIndexOf( '/');
+ backslash = path.lastIndexOf( '\\');
+ if ( slash === -1 && backslash === -1 ) {
fname = path;
- } else if (slash > backslash) {
- fname = path.substring(slash+1, 10000);
+ } else if ( slash > backslash ) {
+ fname = path.substring( slash + 1, 10000 );
} else {
- fname = path.substring(backslash+1, 10000);
+ fname = path.substring( backslash + 1, 10000 );
}
// Clear the filename if it does not have a valid extension.
// URLs are less likely to have a useful extension, so don't include
them in the
// extension check.
if ( mw.config.get( 'wgStrictFileExtensions' ) && fileExtensions && id
!== 'wpUploadFileURL' ) {
- var found = false;
+ found = false;
if ( fname.lastIndexOf( '.' ) !== -1 ) {
- var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
- for ( var i = 0; i < fileExtensions.length; i += 1 ) {
+ ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
+ for ( i = 0; i < fileExtensions.length; i += 1 ) {
if ( fileExtensions[i].toLowerCase() ===
ext.toLowerCase() ) {
found = true;
break;
}
}
}
- if( !found ) {
+ if ( !found ) {
// Not a valid extension
// Clear the upload and set mw-upload-permitted to error
- document.getElementById(id).value = '';
- var e = document.getElementById( 'mw-upload-permitted'
);
- if( e ) e.className = 'error';
+ document.getElementById( id ).value = '';
+ e = document.getElementById( 'mw-upload-permitted' );
+ if ( e ) {
+ e.className = 'error';
+ }
- var e = document.getElementById( 'mw-upload-prohibited'
);
- if( e ) e.className = 'error';
+ e = document.getElementById( 'mw-upload-prohibited' );
+ if ( e ) {
+ e.className = 'error';
+ }
// Clear wpDestFile as well
- var e = document.getElementById( 'wpDestFile' );
- if( e ) e.value = '';
+ e = document.getElementById( 'wpDestFile' );
+ if ( e ) {
+ e.value = '';
+ }
return false;
}
@@ -241,68 +274,77 @@
}
// Output result
- var destFile = document.getElementById( 'wpDestFile' );
+ destFile = document.getElementById( 'wpDestFile' );
if ( destFile ) {
// Call decodeURIComponent function to remove possible
URL-encoded characters
// from the file name (bug 30390). Especially likely with
upload-form-url.
// decodeURIComponent can throw an exception in input is
invalid utf-8
try {
destFile.value = decodeURIComponent( fname );
- } catch ( e ) {
+ } catch ( err ) {
destFile.value = fname;
}
wgUploadWarningObj.checkNow( fname );
}
};
-window.toggleFilenameFiller = function() {
- if(!document.getElementById) return;
- var upfield = document.getElementById('wpUploadFile');
- var destName = document.getElementById('wpDestFile').value;
- wgUploadAutoFill = ( destName == '' || destName == ' ' );
+window.toggleFilenameFiller = function () {
+ if ( !document.getElementById ) {
+ return;
+ }
+ var destName = document.getElementById( 'wpDestFile' ).value;
+ mw.config.set( 'wgUploadAutoFill', !destName );
};
-window.wgUploadLicenseObj = {
+wgUploadLicenseObj = window.wgUploadLicenseObj = {
- 'responseCache' : { '' : '' },
+ responseCache: { '' : '' },
- 'fetchPreview': function( license ) {
- if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) return;
- for (cached in this.responseCache) {
- if (cached == license) {
+ fetchPreview: function ( license ) {
+ var cached, title, url, req;
+ if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) {
+ return;
+ }
+ for ( cached in this.responseCache ) {
+ if ( cached === license ) {
this.showPreview( this.responseCache[license] );
return;
}
}
- injectSpinner( document.getElementById( 'wpLicense' ),
'license' );
+ window.injectSpinner( document.getElementById( 'wpLicense' ),
'license' );
- var title = document.getElementById('wpDestFile').value;
- if ( !title ) title = 'File:Sample.jpg';
+ title = document.getElementById( 'wpDestFile' ).value;
+ if ( !title ) {
+ title = 'File:Sample.jpg';
+ }
- var url = mw.util.wikiScript( 'api' )
+ url = mw.util.wikiScript( 'api' )
+ '?action=parse&text={{' + encodeURIComponent( license
) + '}}'
+ '&title=' + encodeURIComponent( title )
+ '&prop=text&pst&format=json';
- var req = sajax_init_object();
- req.onreadystatechange = function() {
- if ( req.readyState == 4 && req.status == 200 )
+ req = sajax_init_object();
+ req.onreadystatechange = function () {
+ /*jshint evil:true */
+ if ( req.readyState === 4 && req.status === 200 ) {
wgUploadLicenseObj.processResult( eval( '(' +
req.responseText + ')' ), license );
+ }
};
req.open( 'GET', url, true );
req.send( '' );
},
- 'processResult' : function( result, license ) {
- removeSpinner( 'license' );
- this.responseCache[license] = result['parse']['text']['*'];
+ processResult: function ( result, license ) {
+ window.removeSpinner( 'license' );
+ this.responseCache[license] = result.parse.text['*'];
this.showPreview( this.responseCache[license] );
},
- 'showPreview' : function( preview ) {
+ showPreview: function ( preview ) {
var previewPanel = document.getElementById(
'mw-license-preview' );
- if( previewPanel.innerHTML != preview )
+ if ( previewPanel.innerHTML !== preview ) {
previewPanel.innerHTML = preview;
+ }
}
};
--
To view, visit https://gerrit.wikimedia.org/r/77266
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9df912ee48d30c189394bf07f5e49014220f36a3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits