MarkTraceur has uploaded a new change for review.
https://gerrit.wikimedia.org/r/86789
Change subject: jshint fixes for mw.UploadWizardUpload.js
......................................................................
jshint fixes for mw.UploadWizardUpload.js
Bug: 47161
Change-Id: I1c54a2c2f82dcddf8bdfa63ff80b320744aa6b2f
---
M resources/mw.UploadWizardUpload.js
1 file changed, 257 insertions(+), 256 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UploadWizard
refs/changes/89/86789/1
diff --git a/resources/mw.UploadWizardUpload.js
b/resources/mw.UploadWizardUpload.js
index a199207..04c6ad6 100644
--- a/resources/mw.UploadWizardUpload.js
+++ b/resources/mw.UploadWizardUpload.js
@@ -1,3 +1,4 @@
+/* jshint nomen: false, camelcase: false */
/**
* Represents the upload -- in its local and remote state. (Possibly those
could be separate objects too...)
* This is our 'model' object if we are thinking MVC. Needs to be better
factored, lots of feature envy with the UploadWizard
@@ -78,29 +79,28 @@
// increments with each upload
count: 0,
- acceptDeed: function( deed ) {
- var _this = this;
- _this.deed.applyDeed( _this );
+ acceptDeed: function() {
+ this.deed.applyDeed( this );
},
/**
* start
*/
start: function() {
- var _this = this;
+ var upload = this;
if ( mw.UploadWizard.config.startImmediately === true ) {
- _this.wizard.hideFileEndButtons();
+ this.wizard.hideFileEndButtons();
$('#mwe-upwiz-stepdiv-file .mwe-upwiz-buttons').hide();
- _this.wizard.startProgressBar();
- _this.wizard.allowCloseWindow = mw.confirmCloseWindow( {
- message: function() { return mw.message(
'mwe-upwiz-prevent-close', _this.wizard.uploads.length ).escaped(); },
- test: function() { return
!_this.wizard.isComplete() && _this.wizard.uploads.length > 0; }
+ this.wizard.startProgressBar();
+ this.wizard.allowCloseWindow = mw.confirmCloseWindow( {
+ message: function() { return mw.message(
'mwe-upwiz-prevent-close', upload.wizard.uploads.length ).escaped(); },
+ test: function() { return
!upload.wizard.isComplete() && upload.wizard.uploads.length > 0; }
} );
}
- _this.setTransportProgress(0.0);
- //_this.ui.start();
- _this.handler.start();
+ this.setTransportProgress(0.0);
+ //this.ui.start();
+ this.handler.start();
},
/**
@@ -135,10 +135,9 @@
* @param fraction
*/
setTransportProgress: function ( fraction ) {
- var _this = this;
- _this.state = 'transporting';
- _this.transportProgress = fraction;
- $( _this.ui.div ).trigger( 'transportProgressEvent' );
+ this.state = 'transporting';
+ this.transportProgress = fraction;
+ $( this.ui.div ).trigger( 'transportProgressEvent' );
},
/**
@@ -163,13 +162,18 @@
* @param result the API result in parsed JSON form
*/
setTransported: function( result ) {
- var _this = this;
- if ( _this.state == 'aborted' ) {
+ if ( this.state === 'aborted' ) {
return;
}
+ function rmErrs( theCode ) {
+ upload.removeErrors( theCode );
+ }
+
// default error state
- var code = 'unknown',
+ var comma, warnCode, $override,
+ upload = this,
+ code = 'unknown',
info = 'unknown';
if ( result.error ) {
@@ -179,7 +183,7 @@
}
if ( code === 'filetype-banned' &&
result.error.blacklisted ) {
code = 'filetype-banned-type';
- var comma = mw.message( 'comma-separator'
).escaped();
+ comma = mw.message( 'comma-separator'
).escaped();
info = [
result.error.blacklisted.join( comma ),
result.error.allowed.join( comma ),
@@ -192,8 +196,6 @@
this.setError( code, info );
return;
}
-
- var warnCode;
result.upload = result.upload || {};
result.upload.warnings = result.upload.warnings || {};
@@ -210,20 +212,18 @@
break;
case 'duplicate':
code = warnCode;
- _this.setError( warnCode,
_this.duplicateErrorInfo( warnCode, result.upload.warnings[warnCode] ) );
+ this.setError( warnCode,
this.duplicateErrorInfo( warnCode, result.upload.warnings[warnCode] ) );
break;
case 'duplicate-archive':
// This is the case where the
file did exist, but it was deleted.
// We should definitely tell
the user, but let them override.
// If they already have, then
don't execute any of this.
code = warnCode;
- _this.setError( warnCode,
_this.duplicateErrorInfo( warnCode, result.upload.warnings[warnCode] ) );
- var $override = $( '<a></a>' )
- .attr( 'href',
'javascript:' )
+ this.setError( warnCode,
this.duplicateErrorInfo( warnCode, result.upload.warnings[warnCode] ) );
+ $override = $( '<a></a>' )
+ .attr( 'href', '#' )
.text( mw.message(
'mwe-upwiz-override' ).text() )
- .click( ( function (
theCode ) {
-
this.removeErrors( theCode );
- } ).bind( this,
warnCode ) );
+ .click( rmErrs );
$(
'.mwe-upwiz-file-status-line-item', this.ui.visibleFilenameDiv )
.first()
.append( ' ' );
@@ -238,7 +238,7 @@
} else {
info =
result.upload.warnings[warnCode];
}
- _this.setError( code, info );
+ this.setError( code, info );
break;
}
}
@@ -247,14 +247,14 @@
if ( this.state !== 'error' ) {
if ( result.upload && result.upload.result ===
'Success' ) {
if ( result.upload.imageinfo ) {
- _this.setSuccess( result );
+ this.setSuccess( result );
} else {
- _this.setError( 'noimageinfo', info );
+ this.setError( 'noimageinfo', info );
}
} else if ( result.upload && result.upload.result ===
'Warning' ) {
throw new Error( 'Your browser got back a
Warning result from the server. Please file a bug.' );
} else {
- _this.setError( code, info );
+ this.setError( code, info );
}
}
},
@@ -265,26 +265,7 @@
* @param {Object} portion of the API error result listing duplicates
*/
duplicateErrorInfo: function( code, resultDuplicate ) {
- var _this = this;
- var duplicates;
- if ( typeof resultDuplicate === 'object' ) {
- duplicates = resultDuplicate;
- } else if ( typeof resultDuplicate === 'string' ) {
- duplicates = [ resultDuplicate ];
- }
- var $ul = $( '<ul></ul>' );
- $.each( duplicates, function( i, filename ) {
- var $a = $( '<a/>' ).append( filename );
- try {
- var href = new mw.Title( filename, fileNsId
).getUrl();
- $a.attr( { 'href': href, 'target': '_blank' } );
- } catch ( e ) {
- $a.click( function() { alert('could not parse
filename=' + filename ); } );
- $a.attr( 'href', '#' );
- }
- $ul.append( $( '<li></li>' ).append( $a ) );
- } );
- var dialogFn = function(e) {
+ function dialogFn(e) {
$( '<div></div>' )
.html( $ul )
.dialog( {
@@ -295,7 +276,31 @@
modal : true
} );
e.preventDefault();
- };
+ }
+
+ var duplicates,
+ $ul = $( '<ul>' );
+
+ if ( typeof resultDuplicate === 'object' ) {
+ duplicates = resultDuplicate;
+ } else if ( typeof resultDuplicate === 'string' ) {
+ duplicates = [ resultDuplicate ];
+ }
+
+ $.each( duplicates, function( i, filename ) {
+ var href,
+ $a = $( '<a/>' ).append( filename );
+
+ try {
+ href = new mw.Title( filename, fileNsId
).getUrl();
+ $a.attr( { 'href': href, 'target': '_blank' } );
+ } catch ( e ) {
+ $a.click( function() { window.alert('could not
parse filename=' + filename ); } );
+ $a.attr( 'href', '#' );
+ }
+ $ul.append( $( '<li></li>' ).append( $a ) );
+ } );
+
return [ duplicates.length, dialogFn ];
},
@@ -304,26 +309,25 @@
* @param {Mixed} result -- result of AJAX call
*/
setSuccess: function( result ) {
- var _this = this;
- _this.state = 'transported';
- _this.transportProgress = 1;
+ this.state = 'transported';
+ this.transportProgress = 1;
- _this.ui.setStatus( 'mwe-upwiz-getting-metadata' );
+ this.ui.setStatus( 'mwe-upwiz-getting-metadata' );
+
if ( result.upload ) {
- _this.extractUploadInfo( result.upload );
- if ( !_this.fromURL ) {
- _this.deedPreview.setup();
+ this.extractUploadInfo( result.upload );
+ if ( !this.fromURL ) {
+ this.deedPreview.setup();
}
- _this.details.populate();
- _this.state = 'stashed';
- _this.ui.showStashed();
- $.publishReady( 'thumbnails.' + _this.index, 'api' );
+ this.details.populate();
+ this.state = 'stashed';
+ this.ui.showStashed();
+ $.publishReady( 'thumbnails.' + this.index, 'api' );
// check all uploads, if they're complete, show the
next button
- //_this.wizard.showNext( 'file', 'stashed' ); See bug
39852
+ //this.wizard.showNext( 'file', 'stashed' ); See bug
39852
} else {
- _this.setError( 'noimageinfo' );
+ this.setError( 'noimageinfo' );
}
-
},
/**
@@ -344,31 +348,32 @@
* @param {Function()} callback when resetting FileInput
*/
checkFile: function( filename, files, fileNameOk, fileNameErr,
resetFileInput ) {
- var _this = this;
- var fileErrors = {};
+ var totalSize, duplicate, extension, hasError, errorIndex,
+ actualMaxSize, binReader,
+ upload = this,
+ fileErrors = {},
- function finishCallback () {
- if ( _this && _this.ui ) {
+ // Check if filename is acceptable
+ // TODO sanitize filename
+ basename = mw.UploadWizardUtil.getBasename( filename ),
+ tooManyFiles = files.length +
this.wizard.uploads.length > mw.UploadWizard.config.maxUploads;
+
+ function finishCallback() {
+ if ( upload && upload.ui ) {
fileNameOk();
} else {
setTimeout( finishCallback, 200 );
}
}
- // Check if filename is acceptable
- // TODO sanitize filename
- var basename = mw.UploadWizardUtil.getBasename( filename );
-
- var tooManyFiles = files.length + _this.wizard.uploads.length >
mw.UploadWizard.config.maxUploads;
if ( tooManyFiles ) {
- _this.showTooManyFilesWarning( files.length );
+ this.showTooManyFilesWarning( files.length );
resetFileInput();
return;
}
if ( files.length > 1 ) {
-
- var totalSize = 0;
+ totalSize = 0;
$.each( files, function( i, file ) {
totalSize += file.size;
});
@@ -376,16 +381,16 @@
// Local previews are slow due to the data URI
insertion into the DOM; for batches we
// don't generate them if the size of the batch exceeds
10 MB
if ( totalSize > 10000000 ) {
- _this.wizard.makePreviewsFlag = false;
+ this.wizard.makePreviewsFlag = false;
}
- _this.reservedIndex = _this.wizard.uploads.length;
+ this.reservedIndex = this.wizard.uploads.length;
}
// check to see if the file has already been selected for
upload.
- var duplicate = false;
- $.each( this.wizard.uploads, function ( i, upload ) {
- if ( upload !== undefined && _this !== upload &&
filename === upload.filename ) {
+ duplicate = false;
+ $.each( this.wizard.uploads, function ( i, thisupload ) {
+ if ( thisupload !== undefined && upload !== thisupload
&& filename === thisupload.filename ) {
duplicate = true;
return false;
}
@@ -404,7 +409,7 @@
}
// Check if extension is acceptable
- var extension = this.title.getExtension();
+ extension = this.title.getExtension();
if ( mw.isEmpty( extension ) ) {
fileErrors.noext = true;
fileNameErr( 'noext', null, fileErrors );
@@ -416,8 +421,8 @@
// Split this into a separate case, if the error above
got ignored,
// we want to still trudge forward.
if ( !fileErrors.ext ) {
- var hasError = false;
- for ( var errorIndex in fileErrors ) {
+ hasError = false;
+ for ( errorIndex in fileErrors ) {
if ( fileErrors[errorIndex] ) {
hasError = true;
break;
@@ -436,7 +441,6 @@
// will accept. Otherwise we're bound
by PHP's limits.
// NOTE: Because we don't know until
runtime if the browser supports chunked
// uploading, we can't determine this
server-side.
- var actualMaxSize;
if (
mw.UploadWizard.config.enableChunked && mw.fileApi.isFormDataAvailable() ) {
actualMaxSize =
mw.UploadWizard.config.maxMwUploadSize;
} else {
@@ -448,10 +452,10 @@
// make sure the file isn't too large
// XXX need a way to find the size of
the Flickr image
- if ( !_this.fromURL ){
+ if ( !this.fromURL ){
this.transportWeight =
this.file.size;
if ( this.transportWeight >
actualMaxSize ) {
-
_this.showMaxSizeWarning( this.transportWeight, actualMaxSize );
+
this.showMaxSizeWarning( this.transportWeight, actualMaxSize );
return;
}
}
@@ -469,36 +473,35 @@
// TODO: This should be refactored.
if ( this.file.type === 'image/jpeg' ) {
- var binReader = new
FileReader();
+ binReader = new FileReader();
binReader.onload = function() {
- var binStr;
+ var binStr, arr, i,
meta;
if ( typeof
binReader.result === 'string' ) {
binStr =
binReader.result;
} else {
// Array
buffer; convert to binary string for the library.
- var arr = new
Uint8Array( binReader.result );
+ arr = new
Uint8Array( binReader.result );
binStr = '';
- for ( var i =
0; i < arr.byteLength; i++ ) {
+ for ( i = 0; i
< arr.byteLength; i++ ) {
binStr
+= String.fromCharCode( arr[i] );
}
}
- var meta;
try {
- meta =
mw.libs.jpegmeta( binStr, _this.file.fileName );
+ meta =
mw.libs.jpegmeta( binStr, upload.file.fileName );
meta._binary_data = null;
} catch ( e ) {
meta = null;
}
-
_this.extractMetadataFromJpegMeta( meta );
- _this.filename =
filename;
+
upload.extractMetadataFromJpegMeta( meta );
+ upload.filename =
filename;
if ( hasError === false
) {
finishCallback();
}
};
if ( 'readAsBinaryString' in
binReader ) {
-
binReader.readAsBinaryString( _this.file );
+
binReader.readAsBinaryString( upload.file );
} else if ( 'readAsArrayBuffer'
in binReader ) {
-
binReader.readAsArrayBuffer( _this.file );
+
binReader.readAsArrayBuffer( upload.file );
} else {
// We should never get
here. :P
throw new Error(
'Cannot read thumbnail as binary string or array buffer.' );
@@ -514,13 +517,11 @@
files = files.slice( 1 );
if ( files.length > 0 ) {
$.each( files, function( i,
file ) {
-
// NOTE: By running
newUpload we will end up calling checkfile() again.
- var upload =
_this.wizard.newUpload( file, _this.reservedIndex + i + 1 );
+
upload.wizard.newUpload( file, upload.reservedIndex + i + 1 );
} );
- _this.wizard.updateFileCounts();
+ this.wizard.updateFileCounts();
}
-
} else {
this.filename = filename;
if ( hasError === false ) {
@@ -541,7 +542,7 @@
{
text: mw.message( 'mwe-upwiz-file-too-large-ok'
).escaped(),
click: function() {
- $( this ).dialog( "close" );
+ $( this ).dialog( 'close' );
}
}
];
@@ -608,12 +609,13 @@
this.imageinfo.metadata.orientation =
meta.tiff.Orientation.value;
}
if ( meta.general ) {
- var pixelHeightDim = 'height';
- var pixelWidthDim = 'width';
- // this must be called after orientation is set
above. If no orientation set, defaults to 0
- var degrees = this.getOrientationDegrees();
+ var pixelHeightDim = 'height',
+ pixelWidthDim = 'width',
+ // this must be called after
orientation is set above. If no orientation set, defaults to 0
+ degrees = this.getOrientationDegrees();
+
// jpegmeta reports pixelHeight & width
- if ( degrees == 90 || degrees == 270 ) {
+ if ( degrees === 90 || degrees === 270 ) {
pixelHeightDim = 'width';
pixelWidthDim = 'height';
}
@@ -633,7 +635,6 @@
* @param result The JSON object from a successful API upload result.
*/
extractUploadInfo: function( resultUpload ) {
-
if ( resultUpload.filekey ) {
this.fileKey = resultUpload.filekey;
}
@@ -653,36 +654,40 @@
* @param imageinfo JSON object obtained from API result.
*/
extractImageInfo: function( imageinfo ) {
- var _this = this;
- for ( var key in imageinfo ) {
- // we get metadata as list of key-val pairs; convert to
object for easier lookup. Assuming that EXIF fields are unique.
- if ( key == 'metadata' ) {
- if ( _this.imageinfo.metadata === undefined ) {
- _this.imageinfo.metadata = {};
- }
- if ( imageinfo.metadata &&
imageinfo.metadata.length ) {
- $.each( imageinfo.metadata, function(
i, pair ) {
- if ( pair !== undefined ) {
-
_this.imageinfo.metadata[pair.name.toLowerCase()] = pair.value;
- }
- } );
- }
- } else {
- _this.imageinfo[key] = imageinfo[key];
+ var key,
+ upload = this;
+
+ function setMetadata( i, pair ) {
+ if ( pair !== undefined ) {
+
upload.imageinfo.metadata[pair.name.toLowerCase()] = pair.value;
}
}
- /*
+ for ( key in imageinfo ) {
+ // we get metadata as list of key-val pairs; convert to
object for easier lookup. Assuming that EXIF fields are unique.
+ if ( key === 'metadata' ) {
+ if ( this.imageinfo.metadata === undefined ) {
+ this.imageinfo.metadata = {};
+ }
+ if ( imageinfo.metadata &&
imageinfo.metadata.length ) {
+ $.each( imageinfo.metadata, setMetadata
);
+ }
+ } else {
+ this.imageinfo[key] = imageinfo[key];
+ }
+ }
+
+ /* BEFORE YOU UNCOMMENT: declare the extension variable at the
top of this function.
if ( _this.title.getExtension() === null ) {
// 1;
// TODO v1.1 what if we don't have an extension? Should
be impossible as it is currently impossible to upload without extension, but you
// never know... theoretically there is no restriction
on extensions if we are uploading to the stash, but the check is performed
anyway.
- var extension = mw.UploadWizardUtil.getExtension(
_this.imageinfo.url );
+ var extension = mw.UploadWizardUtil.getExtension(
this.imageinfo.url );
if ( !extension ) {
- if ( _this.imageinfo.mimetype ) {
- if (
mw.UploadWizardUtil.mimetypeToExtension[ _this.imageinfo.mimetype ] ) {
- extension =
mw.UploadWizardUtil.mimetypeToExtension[ _this.imageinfo.mimetype ];
+ if ( this.imageinfo.mimetype ) {
+ if (
mw.UploadWizardUtil.mimetypeToExtension[ this.imageinfo.mimetype ] ) {
+ extension =
mw.UploadWizardUtil.mimetypeToExtension[ this.imageinfo.mimetype ];
}
}
}
@@ -700,7 +705,19 @@
* @param {Number} optional, height of thumbnail. Will force 'url' to
be added to props
*/
getStashImageInfo: function( callback, props, width, height ) {
- var _this = this;
+ function ok( data ) {
+ if ( !data || !data.query || !data.query.stashimageinfo
) {
+
mw.log('mw.UploadWizardUpload::getStashImageInfo> No data? ');
+ callback( null );
+ return;
+ }
+ callback( data.query.stashimageinfo );
+ }
+
+ function err( code ) {
+ mw.log( 'mw.UploadWizardUpload::getStashImageInfo>
error: ' + code, 'debug' );
+ callback( null );
+ }
if ( props === undefined ) {
props = [];
@@ -708,7 +725,7 @@
var params = {
'prop': 'stashimageinfo',
- 'siifilekey': _this.fileKey,
+ 'siifilekey': this.fileKey,
'siiprop': props.join( '|' )
};
@@ -724,20 +741,6 @@
}
}
- var ok = function( data ) {
- if ( !data || !data.query || !data.query.stashimageinfo
) {
-
mw.log("mw.UploadWizardUpload::getStashImageInfo> No data? ");
- callback( null );
- return;
- }
- callback( data.query.stashimageinfo );
- };
-
- var err = function( code, result ) {
- mw.log( 'mw.UploadWizardUpload::getStashImageInfo>
error: ' + code, 'debug' );
- callback( null );
- };
-
this.api.get( params, { ok: ok, err: err } );
},
@@ -751,30 +754,7 @@
* @param {Number} optional, height of thumbnail. Will force 'url' to
be added to props
*/
getImageInfo: function( callback, props, width, height ) {
- var _this = this;
- if ( props === undefined ) {
- props = [];
- }
- var requestedTitle = _this.title.getPrefixedText();
- var params = {
- 'prop': 'imageinfo',
- 'titles': requestedTitle,
- 'iiprop': props.join( '|' )
- };
-
- if ( width !== undefined || height !== undefined ) {
- if ( ! $.inArray( 'url', props ) ) {
- props.push( 'url' );
- }
- if ( width !== undefined ) {
- params.iiurlwidth = width;
- }
- if ( height !== undefined ) {
- params.iiurlheight = height;
- }
- }
-
- var ok = function( data ) {
+ function ok( data ) {
if ( data && data.query && data.query.pages ) {
var found = false;
$.each( data.query.pages, function( pageId,
page ) {
@@ -788,14 +768,37 @@
return;
}
}
- mw.log("mw.UploadWizardUpload::getImageInfo> No data
matching " + requestedTitle + " ? ");
+ mw.log('mw.UploadWizardUpload::getImageInfo> No data
matching ' + requestedTitle + ' ? ');
callback( null );
- };
+ }
- var err = function( code, result ) {
+ function err( code ) {
mw.log( 'mw.UploadWizardUpload::getImageInfo> error: '
+ code, 'debug' );
callback( null );
- };
+ }
+
+ if ( props === undefined ) {
+ props = [];
+ }
+
+ var requestedTitle = this.title.getPrefixedText(),
+ params = {
+ 'prop': 'imageinfo',
+ 'titles': requestedTitle,
+ 'iiprop': props.join( '|' )
+ };
+
+ if ( width !== undefined || height !== undefined ) {
+ if ( ! $.inArray( 'url', props ) ) {
+ props.push( 'url' );
+ }
+ if ( width !== undefined ) {
+ params.iiurlwidth = width;
+ }
+ if ( height !== undefined ) {
+ params.iiurlheight = height;
+ }
+ }
this.api.get( params, { ok: ok, err: err } );
},
@@ -833,70 +836,70 @@
* @param height - (optional) maximum height of thumbnail
*/
getAndPublishApiThumbnail: function( key, width, height ) {
- var _this = this;
+ function thumbnailPublisher( thumbnails ) {
+ if ( thumbnails === null ) {
+ // the api call failed somehow, no thumbnail
data.
+ $.publishReady( key, null );
+ } else {
+ // ok, the api callback has returned us
information on where the thumbnail(s) ARE, but that doesn't mean
+ // they are actually there yet. Keep trying to
set the source ( which should trigger "error" or "load" event )
+ // on the image. If it loads publish the event
with the image. If it errors out too many times, give up and publish
+ // the event with a null.
+ $.each( thumbnails, function( i, thumb ) {
+ if ( thumb.thumberror || ( ! (
thumb.thumburl && thumb.thumbwidth && thumb.thumbheight ) ) ) {
+ mw.log(
'mw.UploadWizardUpload::getThumbnail> thumbnail error or missing information' );
+ $.publishReady( key, null );
+ return;
+ }
+
+ // try to load this image with
exponential backoff
+ // if the delay goes past 8 seconds, it
gives up and publishes the event with null
+ var timeoutMs = 100,
+ image = document.createElement(
'img' );
+ image.width = thumb.thumbwidth;
+ image.height = thumb.thumbheight;
+ $( image )
+ .load( function() {
+ // cache this thumbnail
+ upload.thumbnails[key]
= image;
+ // publish the image to
anyone who wanted it
+ $.publishReady( key,
image );
+ } )
+ .error( function() {
+ // retry with
exponential backoff
+ if ( timeoutMs < 8000 )
{
+ setTimeout(
function() {
+
timeoutMs = timeoutMs * 2 + Math.round( Math.random() * ( timeoutMs / 10 ) );
+
setSrc();
+ }, timeoutMs );
+ } else {
+ $.publishReady(
key, null );
+ }
+ } );
+
+ // executing this should cause a
.load() or .error() event on the image
+ function setSrc() {
+ image.src = thumb.thumburl;
+ }
+
+ // and, go!
+ setSrc();
+ } );
+ }
+ }
+
+ var upload = this;
if ( mw.isEmpty( height ) ) {
height = -1;
}
- if ( _this.thumbnailPublishers[key] === undefined ) {
- var thumbnailPublisher = function( thumbnails ) {
- if ( thumbnails === null ) {
- // the api call failed somehow, no
thumbnail data.
- $.publishReady( key, null );
- } else {
- // ok, the api callback has returned us
information on where the thumbnail(s) ARE, but that doesn't mean
- // they are actually there yet. Keep
trying to set the source ( which should trigger "error" or "load" event )
- // on the image. If it loads publish
the event with the image. If it errors out too many times, give up and publish
- // the event with a null.
- $.each( thumbnails, function( i, thumb
) {
- if ( thumb.thumberror || ( ! (
thumb.thumburl && thumb.thumbwidth && thumb.thumbheight ) ) ) {
- mw.log(
"mw.UploadWizardUpload::getThumbnail> thumbnail error or missing information" );
- $.publishReady( key,
null );
- return;
- }
-
- // try to load this image with
exponential backoff
- // if the delay goes past 8
seconds, it gives up and publishes the event with null
- var timeoutMs = 100;
- var image =
document.createElement( 'img' );
- image.width = thumb.thumbwidth;
- image.height =
thumb.thumbheight;
- $( image )
- .load( function() {
- // cache this
thumbnail
-
_this.thumbnails[key] = image;
- // publish the
image to anyone who wanted it
- $.publishReady(
key, image );
- } )
- .error( function() {
- // retry with
exponential backoff
- if ( timeoutMs
< 8000 ) {
-
setTimeout( function() {
-
timeoutMs = timeoutMs * 2 + Math.round( Math.random() * ( timeoutMs / 10 ) );
-
setSrc();
- },
timeoutMs );
- } else {
-
$.publishReady( key, null );
- }
- } );
-
- // executing this should cause
a .load() or .error() event on the image
- function setSrc() {
- image.src =
thumb.thumburl;
- }
-
- // and, go!
- setSrc();
- } );
- }
- };
-
- _this.thumbnailPublishers[key] = thumbnailPublisher;
- if ( _this.state !== 'complete' ) {
- _this.getStashImageInfo( thumbnailPublisher, [
'url' ], width, height );
+ if ( this.thumbnailPublishers[key] === undefined ) {
+ this.thumbnailPublishers[key] = thumbnailPublisher;
+ if ( this.state !== 'complete' ) {
+ this.getStashImageInfo( thumbnailPublisher, [
'url' ], width, height );
} else {
- _this.getImageInfo( thumbnailPublisher, [ 'url'
], width, height );
+ this.getImageInfo( thumbnailPublisher, [ 'url'
], width, height );
}
}
@@ -920,7 +923,6 @@
case 6:
orientation = 270; // 'top left' ->
'right top'
break;
- case 1:
default:
orientation = 0; // 'top left' ->
'top left'
break;
@@ -957,18 +959,18 @@
* @return {HTMLCanvasElement}
*/
getTransformedCanvasElement: function( image, constraints ) {
-
- var rotation = 0;
+ var angle, scaleConstraints, scaling, width, height,
+ dx, dy, x, y, $canvas, ctx,
+ rotation = 0;
// if this wiki can rotate images to match their EXIF metadata,
// we should do the same in our preview
if ( mw.config.get( 'wgFileCanRotate' ) ) {
- var angle = this.getOrientationDegrees();
+ angle = this.getOrientationDegrees();
rotation = angle ? 360 - angle : 0;
}
// swap scaling constraints if needed by rotation...
- var scaleConstraints;
if ( rotation === 90 || rotation === 270 ) {
scaleConstraints = {
width: constraints.height,
@@ -981,15 +983,14 @@
};
}
- var scaling = this.getScalingFromConstraints( image,
constraints );
+ scaling = this.getScalingFromConstraints( image, constraints );
- var width = image.width * scaling;
- var height = image.height * scaling;
+ width = image.width * scaling;
+ height = image.height * scaling;
// Determine the offset required to center the image
- var dx = (constraints.width - width) / 2;
- var dy = (constraints.height - height) / 2;
- var x, y;
+ dx = (constraints.width - width) / 2;
+ dy = (constraints.height - height) / 2;
switch ( rotation ) {
// If a rotation is applied, the direction of the axis
@@ -1008,15 +1009,14 @@
x = dx - constraints.width;
y = dy;
break;
- case 0:
default:
x = dx;
y = dy;
break;
}
- var $canvas = $( '<canvas></canvas>' ).attr( constraints );
- var ctx = $canvas[0].getContext( '2d' );
+ $canvas = $( '<canvas></canvas>' ).attr( constraints );
+ ctx = $canvas[0].getContext( '2d' );
ctx.clearRect( 0, 0, width, height );
ctx.rotate( rotation / 180 * Math.PI );
ctx.drawImage( image, x, y, width, height );
@@ -1073,14 +1073,14 @@
* @param boolean add lightbox large preview when ready
*/
setThumbnail: function( selector, width, height, isLightBox ) {
- var _this = this;
+ var _this = this,
+ placed = false;
/**
* This callback will add an image to the selector, using
in-browser scaling if necessary
* @param {HTMLImageElement}
*/
- var placed = false;
- var placeImageCallback = function( image ) {
+ function placeImageCallback( image ) {
if ( image === null ) {
$( selector ).addClass(
'mwe-upwiz-file-preview-broken' );
_this.ui.setStatus(
'mwe-upwiz-thumbnail-failed' );
@@ -1092,11 +1092,11 @@
.css( { background: 'none' } )
.html(
$( '<a/></a>' )
- .addClass(
"mwe-upwiz-thumbnail-link" )
+ .addClass(
'mwe-upwiz-thumbnail-link' )
.append( elm )
);
placed = true;
- };
+ }
// Listen for even which says some kind of thumbnail is
available.
// The argument is an either an ImageHtmlElement ( if we could
get the thumbnail locally ) or the string 'api' indicating you
@@ -1132,8 +1132,9 @@
* @param selector
*/
setLightBox: function( selector ) {
- var _this = this;
- var $imgDiv = $( '<div></div>' ).css( 'text-align', 'center' );
+ var _this = this,
+ $imgDiv = $( '<div></div>' ).css( 'text-align',
'center' );
+
$( selector )
.click( function() {
// get large preview image
--
To view, visit https://gerrit.wikimedia.org/r/86789
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1c54a2c2f82dcddf8bdfa63ff80b320744aa6b2f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UploadWizard
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits