X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.special%2Fmediawiki.special.upload.js;h=57578a647ebae05c0e56ea7adb6b700ff5177193;hb=ea5c2f5d2d36af57ff793e5c3db186642c584a0e;hp=214ee60a0bff6850aa91c7569f81b6cb706bb217;hpb=9e9e3c798f1155e19c20dff9440c62282c85d0a1;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki.special/mediawiki.special.upload.js b/resources/src/mediawiki.special/mediawiki.special.upload.js index 214ee60a0b..57578a647e 100644 --- a/resources/src/mediawiki.special/mediawiki.special.upload.js +++ b/resources/src/mediawiki.special/mediawiki.special.upload.js @@ -6,11 +6,10 @@ * @singleton */ -/* eslint-disable no-use-before-define */ /* global Uint8Array */ ( function ( mw, $ ) { - var uploadWarning, uploadLicense, + var uploadWarning, uploadTemplatePreview, ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ), $license = $( '#wpLicense' ); @@ -61,7 +60,7 @@ timeout: function () { var $spinnerDestCheck, title; - if ( !ajaxUploadDestCheck || this.nameToCheck === '' ) { + if ( !ajaxUploadDestCheck || this.nameToCheck.trim() === '' ) { return; } $spinnerDestCheck = $.createSpinner().insertAfter( '#wpDestFile' ); @@ -71,9 +70,11 @@ formatversion: 2, action: 'query', // If title is empty, user input is invalid, the API call will produce details about why - titles: title ? title.getPrefixedText() : this.nameToCheck, + titles: [ title ? title.getPrefixedText() : this.nameToCheck ], prop: 'imageinfo', - iiprop: 'uploadwarning' + iiprop: 'uploadwarning', + errorformat: 'html', + errorlang: mw.config.get( 'wgUserLanguage' ) } ).done( function ( result ) { var resultOut = '', @@ -81,7 +82,7 @@ if ( page.imageinfo ) { resultOut = page.imageinfo[ 0 ].html; } else if ( page.invalidreason ) { - resultOut = mw.html.escape( page.invalidreason ); + resultOut = page.invalidreason.html; } uploadWarning.processResult( resultOut, uploadWarning.nameToCheck ); } ).always( function () { @@ -113,44 +114,44 @@ } }; - uploadLicense = { + window.wgUploadTemplatePreviewObj = uploadTemplatePreview = { responseCache: { '': '' }, - fetchPreview: function ( license ) { - var $spinnerLicense; - if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) { - return; - } - if ( this.responseCache.hasOwnProperty( license ) ) { - this.showPreview( this.responseCache[ license ] ); + /** + * @param {jQuery} $element The element whose .val() will be previewed + * @param {jQuery} $previewContainer The container to display the preview in + */ + getPreview: function ( $element, $previewContainer ) { + var template = $element.val(), + $spinner; + + if ( this.responseCache.hasOwnProperty( template ) ) { + this.showPreview( this.responseCache[ template ], $previewContainer ); return; } - $spinnerLicense = $.createSpinner().insertAfter( '#wpLicense' ); + $spinner = $.createSpinner().insertAfter( $element ); - ( new mw.Api() ).get( { - formatversion: 2, - action: 'parse', - text: '{{' + license + '}}', + ( new mw.Api() ).parse( '{{' + template + '}}', { title: $( '#wpDestFile' ).val() || 'File:Sample.jpg', prop: 'text', pst: true, uselang: mw.config.get( 'wgUserLanguage' ) } ).done( function ( result ) { - uploadLicense.processResult( result, license ); + uploadTemplatePreview.processResult( result, template, $previewContainer ); } ).always( function () { - $spinnerLicense.remove(); + $spinner.remove(); } ); }, - processResult: function ( result, license ) { - this.responseCache[ license ] = result.parse.text; - this.showPreview( this.responseCache[ license ] ); + processResult: function ( result, template, $previewContainer ) { + this.responseCache[ template ] = result; + this.showPreview( this.responseCache[ template ], $previewContainer ); }, - showPreview: function ( preview ) { - $( '#mw-license-preview' ).html( preview ); + showPreview: function ( preview, $previewContainer ) { + $previewContainer.html( preview ); } }; @@ -178,7 +179,7 @@ // License selector check $license.change( function () { // We might show a preview - uploadLicense.fetchPreview( $license.val() ); + uploadTemplatePreview.getPreview( $license, $( '#mw-license-preview' ) ); } ); // License selector table row @@ -191,7 +192,7 @@ } // fillDestFile setup - $.each( mw.config.get( 'wgUploadSourceIds' ), function ( index, sourceId ) { + mw.config.get( 'wgUploadSourceIds' ).forEach( function ( sourceId ) { $( '#' + sourceId ).change( function () { var path, slash, backslash, fname; if ( !mw.config.get( 'wgUploadAutoFill' ) ) { @@ -219,17 +220,14 @@ if ( mw.config.get( 'wgCheckFileExtensions' ) && mw.config.get( 'wgStrictFileExtensions' ) && - mw.config.get( 'wgFileExtensions' ) && + Array.isArray( mw.config.get( 'wgFileExtensions' ) ) && $( this ).attr( 'id' ) !== 'wpUploadFileURL' ) { if ( fname.lastIndexOf( '.' ) === -1 || - $.inArray( - fname.slice( fname.lastIndexOf( '.' ) + 1 ).toLowerCase(), - $.map( mw.config.get( 'wgFileExtensions' ), function ( element ) { - return element.toLowerCase(); - } ) - ) === -1 + mw.config.get( 'wgFileExtensions' ).map( function ( element ) { + return element.toLowerCase(); + } ).indexOf( fname.slice( fname.lastIndexOf( '.' ) + 1 ).toLowerCase() ) === -1 ) { // Not a valid extension // Clear the upload and set mw-upload-permitted to error @@ -291,7 +289,7 @@ function fileIsPreviewable( file ) { var known = [ 'image/png', 'image/gif', 'image/jpeg', 'image/svg+xml' ], tooHuge = 10 * 1024 * 1024; - return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge; + return ( known.indexOf( file.type ) !== -1 ) && file.size > 0 && file.size < tooHuge; } /** @@ -311,6 +309,73 @@ return mw.msg( sizeMsgs[ 0 ], Math.round( s ) ); } + /** + * Start loading a file into memory; when complete, pass it as a + * data URL to the callback function. If the callbackBinary is set it will + * first be read as binary and afterwards as data URL. Useful if you want + * to do preprocessing on the binary data first. + * + * @param {File} file + * @param {Function} callback + * @param {Function} callbackBinary + */ + function fetchPreview( file, callback, callbackBinary ) { + var reader = new FileReader(); + if ( callbackBinary && 'readAsBinaryString' in reader ) { + // To fetch JPEG metadata we need a binary string; start there. + // TODO + reader.onload = function () { + callbackBinary( reader.result ); + + // Now run back through the regular code path. + fetchPreview( file, callback ); + }; + reader.readAsBinaryString( file ); + } else if ( callbackBinary && 'readAsArrayBuffer' in reader ) { + // readAsArrayBuffer replaces readAsBinaryString + // However, our JPEG metadata library wants a string. + // So, this is going to be an ugly conversion. + reader.onload = function () { + var i, + buffer = new Uint8Array( reader.result ), + string = ''; + for ( i = 0; i < buffer.byteLength; i++ ) { + string += String.fromCharCode( buffer[ i ] ); + } + callbackBinary( string ); + + // Now run back through the regular code path. + fetchPreview( file, callback ); + }; + reader.readAsArrayBuffer( file ); + } else if ( 'URL' in window && 'createObjectURL' in window.URL ) { + // Supported in Firefox 4.0 and above + // WebKit has it in a namespace for now but that's ok. ;) + // + // Lifetime of this URL is until document close, which is fine + // for Special:Upload -- if this code gets used on longer-running + // pages, add a revokeObjectURL() when it's no longer needed. + // + // Prefer this over readAsDataURL for Firefox 7 due to bug reading + // some SVG files from data URIs + callback( window.URL.createObjectURL( file ) ); + } else { + // This ends up decoding the file to base-64 and back again, which + // feels horribly inefficient. + reader.onload = function () { + callback( reader.result ); + }; + reader.readAsDataURL( file ); + } + } + + /** + * Clear the file upload preview area. + */ + function clearPreview() { + $( '#mw-upload-thumbnail' ).remove(); + } + /** * Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload * in browsers supporting HTML5 FileAPI. @@ -435,73 +500,6 @@ } : null ); } - /** - * Start loading a file into memory; when complete, pass it as a - * data URL to the callback function. If the callbackBinary is set it will - * first be read as binary and afterwards as data URL. Useful if you want - * to do preprocessing on the binary data first. - * - * @param {File} file - * @param {Function} callback - * @param {Function} callbackBinary - */ - function fetchPreview( file, callback, callbackBinary ) { - var reader = new FileReader(); - if ( callbackBinary && 'readAsBinaryString' in reader ) { - // To fetch JPEG metadata we need a binary string; start there. - // TODO - reader.onload = function () { - callbackBinary( reader.result ); - - // Now run back through the regular code path. - fetchPreview( file, callback ); - }; - reader.readAsBinaryString( file ); - } else if ( callbackBinary && 'readAsArrayBuffer' in reader ) { - // readAsArrayBuffer replaces readAsBinaryString - // However, our JPEG metadata library wants a string. - // So, this is going to be an ugly conversion. - reader.onload = function () { - var i, - buffer = new Uint8Array( reader.result ), - string = ''; - for ( i = 0; i < buffer.byteLength; i++ ) { - string += String.fromCharCode( buffer[ i ] ); - } - callbackBinary( string ); - - // Now run back through the regular code path. - fetchPreview( file, callback ); - }; - reader.readAsArrayBuffer( file ); - } else if ( 'URL' in window && 'createObjectURL' in window.URL ) { - // Supported in Firefox 4.0 and above - // WebKit has it in a namespace for now but that's ok. ;) - // - // Lifetime of this URL is until document close, which is fine - // for Special:Upload -- if this code gets used on longer-running - // pages, add a revokeObjectURL() when it's no longer needed. - // - // Prefer this over readAsDataURL for Firefox 7 due to bug reading - // some SVG files from data URIs - callback( window.URL.createObjectURL( file ) ); - } else { - // This ends up decoding the file to base-64 and back again, which - // feels horribly inefficient. - reader.onload = function () { - callback( reader.result ); - }; - reader.readAsDataURL( file ); - } - } - - /** - * Clear the file upload preview area. - */ - function clearPreview() { - $( '#mw-upload-thumbnail' ).remove(); - } - /** * Check if the file does not exceed the maximum size *