jenkins-bot has submitted this change and it was merged. Change subject: Remove UploadWizardUtil, replace with other things ......................................................................
Remove UploadWizardUtil, replace with other things Change-Id: I2913e2b218cb3c206c1b677f7c8d87dcc9cbcf76 Phabricator: http://fab.wmflabs.org/T452 --- M UploadWizardHooks.php M resources/mw.UploadWizard.js M resources/mw.UploadWizardDetails.js M resources/mw.UploadWizardUpload.js D resources/mw.UploadWizardUtil.js A tests/qunit/mw.UploadWizardUpload.test.js 6 files changed, 133 insertions(+), 120 deletions(-) Approvals: Gilles: Looks good to me, approved jenkins-bot: Verified diff --git a/UploadWizardHooks.php b/UploadWizardHooks.php index 97cae53..b8e100f 100644 --- a/UploadWizardHooks.php +++ b/UploadWizardHooks.php @@ -61,7 +61,6 @@ // workhorse libraries 'resources/mw.DestinationChecker.js', - 'resources/mw.UploadWizardUtil.js', // firefogg support libraries 'resources/mw.Firefogg.js', @@ -660,6 +659,7 @@ $testModules['qunit']['ext.uploadWizard.unit.tests'] = array( 'scripts' => array( 'tests/qunit/mw.UploadWizard.test.js', + 'tests/qunit/mw.UploadWizardUpload.test.js', 'tests/qunit/mw.UploadWizardLicenseInput.test.js', 'tests/qunit/mw.FlickrChecker.test.js', ), diff --git a/resources/mw.UploadWizard.js b/resources/mw.UploadWizard.js index 953b881..006da09 100644 --- a/resources/mw.UploadWizard.js +++ b/resources/mw.UploadWizard.js @@ -653,10 +653,17 @@ removeUpload: function( upload ) { // remove the div that passed along the trigger var $div = $( upload.ui.div ); + $div.unbind(); // everything $div.remove(); // and do what we in the wizard need to do after an upload is removed - mw.UploadWizardUtil.removeItem( this.uploads, upload ); + this.uploads = $.grep( + this.uploads, + function ( u ) { + return u !== upload; + } + ); + this.updateFileCounts(); }, diff --git a/resources/mw.UploadWizardDetails.js b/resources/mw.UploadWizardDetails.js index fefffd4..c1f7386 100644 --- a/resources/mw.UploadWizardDetails.js +++ b/resources/mw.UploadWizardDetails.js @@ -399,7 +399,7 @@ } ); */ - mw.UploadWizardUtil.makeToggler( + this.makeToggler( moreDetailsCtrlDiv, moreDetailsDiv, 'mwe-upwiz-more-options' @@ -775,7 +775,7 @@ ) ); - mw.UploadWizardUtil.makeToggler( + this.makeToggler( this.copyMetadataCtrlDiv, $copyMetadataDiv, 'mwe-upwiz-copy-metadata' @@ -1062,7 +1062,14 @@ */ removeDescription: function( description ) { $( description.div ).remove(); - mw.UploadWizardUtil.removeItem( this.descriptions, description ); + + this.descriptions = $.grep( + this.descriptions, + function ( d ) { + return d !== description; + } + ); + this.recountDescriptions(); this.updateCopyMsgs(); }, @@ -1606,6 +1613,49 @@ .addClass( 'mwe-upwiz-status-' + statusStr ); }, + /** + * Simple 'more options' toggle that opens more of a form. + * + * @param {jQuery} $toggleDiv the div which has the control to open and shut custom options + * @param {jQuery} $moreDiv the div containing the custom options + * @param {string} msg the UI message key to use for the toggler + * (with mwe-upwiz- prefix for UploadWizard messages) + */ + makeToggler: function ( $toggleDiv, $moreDiv, msg ) { + function toggle() { + var isOpen = $toggleLink.hasClass( 'mwe-upwiz-toggler-open' ); + if ( isOpen ) { + // hide the extra options + $moreDiv.slideUp( 250 ); + /* when closed, show control to open */ + $toggleLink.removeClass( 'mwe-upwiz-toggler-open' ); + } else { + // show the extra options + $moreDiv.slideDown( 250 ); + /* when open, show control to close */ + $toggleLink.addClass( 'mwe-upwiz-toggler-open' ); + } + } + + var $toggleLink, actualMsg; + + if ( typeof msg === 'object' ) { + actualMsg = mw.message.apply( this, msg ).text(); + } else { + actualMsg = mw.message( msg ).text(); + } + $toggleLink = $( '<a>' ) + .addClass( 'mwe-upwiz-toggler mwe-upwiz-more-options' ) + .text( actualMsg ); + $toggleDiv.append( $toggleLink ); + + $moreDiv.hide(); + + $toggleLink.click( function( e ) { e.stopPropagation(); toggle(); } ); + + $moreDiv.addClass( 'mwe-upwiz-toggled' ); + }, + dateInputCount: 0, /** diff --git a/resources/mw.UploadWizardUpload.js b/resources/mw.UploadWizardUpload.js index 364155c..4d878da 100644 --- a/resources/mw.UploadWizardUpload.js +++ b/resources/mw.UploadWizardUpload.js @@ -335,6 +335,23 @@ }, /** + * Get the basename of a path. + * For error conditions, returns the empty string. + * + * @param {string} path + * @return {string} basename + */ + getBasename: function ( path ) { + if ( path === undefined || path === null ) { + return ''; + } + + // find index of last path separator in the path, add 1. (If no separator found, yields 0) + // then take the entire string after that. + return path.substr( Math.max( path.lastIndexOf( '/' ), path.lastIndexOf( '\\' ) ) + 1 ); + }, + + /** * Called when the file is entered into the file input, bound to its change() event. * Checks for file validity, then extracts metadata. * Error out if filename or its contents are determined to be unacceptable @@ -359,7 +376,7 @@ // Check if filename is acceptable // TODO sanitize filename - basename = mw.UploadWizardUtil.getBasename( filename ), + basename = this.getBasename( filename ), tooManyFiles = files.length + this.wizard.uploads.length > mw.UploadWizard.config.maxUploads; function finishCallback() { @@ -680,24 +697,6 @@ 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 ); - if ( !extension ) { - if ( this.imageinfo.mimetype ) { - if ( mw.UploadWizardUtil.mimetypeToExtension[ this.imageinfo.mimetype ] ) { - extension = mw.UploadWizardUtil.mimetypeToExtension[ this.imageinfo.mimetype ]; - } - } - } - - } - */ }, /** diff --git a/resources/mw.UploadWizardUtil.js b/resources/mw.UploadWizardUtil.js deleted file mode 100644 index fba14ce..0000000 --- a/resources/mw.UploadWizardUtil.js +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Miscellaneous utilities - */ -( function ( mw, $ ) { -mw.UploadWizardUtil = { - - /** - * Simple 'more options' toggle that opens more of a form. - * - * @param toggleDiv the div which has the control to open and shut custom options - * @param moreDiv the div containing the custom options - * @param msg the UI message key to use for the toggler - * (with mwe-upwiz- prefix for UploadWizard messages) - */ - makeToggler: function ( toggleDiv, moreDiv, msg ) { - function toggle() { - var isOpen = $toggleLink.hasClass( 'mwe-upwiz-toggler-open' ); - if ( isOpen ) { - // hide the extra options - moreDiv.slideUp( 250 ); - /* when closed, show control to open */ - $toggleLink.removeClass( 'mwe-upwiz-toggler-open' ); - } else { - // show the extra options - moreDiv.slideDown( 250 ); - /* when open, show control to close */ - $toggleLink.addClass( 'mwe-upwiz-toggler-open' ); - } - } - - var $toggleLink, actualMsg; - - if ( typeof msg === 'object' ) { - actualMsg = mw.message.apply( this, msg ).text(); - } else { - actualMsg = mw.message( msg ).text(); - } - $toggleLink = $( '<a>' ) - .addClass( 'mwe-upwiz-toggler mwe-upwiz-more-options' ) - .text( actualMsg ); - $( toggleDiv ).append( $toggleLink ); - - moreDiv.hide(); - - $toggleLink.click( function( e ) { e.stopPropagation(); toggle(); } ); - - $( moreDiv ).addClass( 'mwe-upwiz-toggled' ); - }, - - /** - * remove an item from an array. Tests for === identity to remove the item - * XXX the entire rationale for this function may be wrong. - * XXX The jQuery way would be to query the DOM for objects, not to keep a separate array hanging around - * @param items the array where we want to remove an item - * @param item the item to remove - */ - removeItem: function( items, item ) { - for ( var i = 0; i < items.length; i++ ) { - if ( items[i] === item ) { - items.splice( i, 1 ); - break; - } - } - }, - - /** - * Get the basename of a path. - * For error conditions, returns the empty string. - * - * @param {String} path - * @return {String} basename - */ - getBasename: function( path ) { - if ( path === undefined || path === null ) { - return ''; - } - - // find index of last path separator in the path, add 1. (If no separator found, yields 0) - // then take the entire string after that. - return path.substr( Math.max( path.lastIndexOf( '/' ), path.lastIndexOf( '\\' ) ) + 1 ); - }, - - - - /** - * Last resort to guess a proper extension - */ - mimetypeToExtension: { - 'image/jpeg': 'jpg', - 'image/gif': 'gif' - // fill as needed - } - - -}; -}( mediaWiki, jQuery ) ); diff --git a/tests/qunit/mw.UploadWizardUpload.test.js b/tests/qunit/mw.UploadWizardUpload.test.js new file mode 100644 index 0000000..52eba11 --- /dev/null +++ b/tests/qunit/mw.UploadWizardUpload.test.js @@ -0,0 +1,53 @@ +/* + * This file is part of the MediaWiki extension UploadWizard. + * + * UploadWizard is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * UploadWizard is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with UploadWizard. If not, see <http://www.gnu.org/licenses/>. + */ + +( function ( mw, $ ) { + QUnit.module( 'mw.UploadWizardUpload', QUnit.newMwEnvironment() ); + + function createUpload() { + var upload, + oldconf = mw.UploadWizard.config; + + mw.UploadWizard.config = {}; + + upload = new mw.UploadWizardUpload( { + api: { + defaults: { + ajax: {} + } + } + }, $( '<div>' ) ); + + mw.UploadWizard.config = oldconf; + + return upload; + } + + QUnit.test( 'constructor sanity test', 1, function ( assert ) { + var upload = createUpload(); + + assert.ok( upload ); + } ); + + QUnit.test( 'getBasename', 3, function ( assert ) { + var upload = createUpload(); + + assert.strictEqual( upload.getBasename( 'path/to/filename.png' ), 'filename.png', 'Path is stripped' ); + assert.strictEqual( upload.getBasename( 'filename.png' ), 'filename.png', 'Only filename is left alone' ); + assert.strictEqual( upload.getBasename( '///////////' ), '', 'Nonsensical path is just removed' ); + } ); +}( mediaWiki, jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/146140 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I2913e2b218cb3c206c1b677f7c8d87dcc9cbcf76 Gerrit-PatchSet: 9 Gerrit-Project: mediawiki/extensions/UploadWizard Gerrit-Branch: master Gerrit-Owner: MarkTraceur <[email protected]> Gerrit-Reviewer: Gilles <[email protected]> Gerrit-Reviewer: MarkTraceur <[email protected]> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
