Matthias Mullie has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/385997 )
Change subject: [WIP] Refactor deeds ...................................................................... [WIP] Refactor deeds The existing deed code was pretty hard to follow. OwnWork & ThirdParty were mostly alright, even though some of the code for ThirdParty was in the generic UploadWizardDeed (and none of the other places that used UploadWizardDeed used that) But it got mighty complex when dealing with custom deeds, where methods got overridden in places you wouldn't even expect. Change-Id: I444a61586b03f3f5391394750267476a2e1830b1 --- M extension.json M resources/controller/uw.controller.Deed.js A resources/deed/uw.deed.Abstract.js A resources/deed/uw.deed.Custom.js A resources/deed/uw.deed.External.js A resources/deed/uw.deed.OwnWork.js A resources/deed/uw.deed.ThirdParty.js A resources/deed/uw.deed.base.js M resources/details/uw.DeedChooserDetailsWidget.js M resources/mw.UploadWizard.js D resources/mw.UploadWizardDeed.js D resources/mw.UploadWizardDeedOwnWork.js D resources/mw.UploadWizardDeedThirdParty.js 13 files changed, 951 insertions(+), 686 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UploadWizard refs/changes/97/385997/1 diff --git a/extension.json b/extension.json index 214c711..3beab2b 100644 --- a/extension.json +++ b/extension.json @@ -205,6 +205,10 @@ "uw.controller.Thanks", "uw.controller.Tutorial", "uw.controller.Upload", + "uw.deed.OwnWork", + "uw.deed.ThirdParty", + "uw.deed.Custom", + "uw.deed.External", "uw.units", "oojs", "oojs-ui", @@ -242,9 +246,6 @@ "resources/mw.QuickTitleChecker.js", "resources/mw.FlickrChecker.js", "resources/mw.GroupProgressBar.js", - "resources/mw.UploadWizardDeed.js", - "resources/mw.UploadWizardDeedOwnWork.js", - "resources/mw.UploadWizardDeedThirdParty.js", "resources/mw.UploadWizardDeedChooser.js", "resources/mw.UploadWizardLicenseInput.js", "resources/mw.UploadWizard.js", @@ -316,49 +317,9 @@ "mwe-upwiz-finished", "mwe-upwiz-deeds-macro-prompt", "mwe-upwiz-source-ownwork", - "mwe-upwiz-source-ownwork-assert-any-license", - "mwe-upwiz-source-ownwork-assert", - "mwe-upwiz-source-ownwork-assert-custom", - "mwe-upwiz-source-ownwork-assert-note", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-4.0", - "mwe-upwiz-source-ownwork-cc-by-sa-4.0-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-at", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-at-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-de", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-de-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-ee", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-ee-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-es", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-es-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-hr", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-hr-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-lu", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-lu-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-nl", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-nl-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-no", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-no-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-pl", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-pl-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-ro", - "mwe-upwiz-source-ownwork-cc-by-sa-3.0-ro-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-4.0", - "mwe-upwiz-source-ownwork-cc-by-4.0-explain", - "mwe-upwiz-source-ownwork-assert-cc-by-3.0", - "mwe-upwiz-source-ownwork-cc-by-3.0-explain", - "mwe-upwiz-source-ownwork-assert-cc-zero", - "mwe-upwiz-source-ownwork-cc-zero-explain", - "mwe-upwiz-source-ownwork-assert-generic", - "mwe-upwiz-source-ownwork-generic-explain", "disclaimerpage", "mwe-upwiz-source-permission", "mwe-upwiz-source-thirdparty", - "mwe-upwiz-source-thirdparty-intro", - "mwe-upwiz-source-thirdparty-custom-multiple-intro", - "mwe-upwiz-source-thirdparty-cases", - "mwe-upwiz-source-thirdparty-accept", "mwe-upwiz-source-custom", "mwe-upwiz-more-options", "mwe-upwiz-desc", @@ -377,10 +338,8 @@ "mwe-upwiz-copyright-info", "mwe-upwiz-objref-pick-image", "mwe-upwiz-objref-notice-existing-image", - "mwe-upwiz-author", "mwe-upwiz-autoconverted", "mwe-upwiz-other", - "mwe-upwiz-source", "mwe-upwiz-upload-error-bad-filename-extension", "mwe-upwiz-upload-error-bad-filename-no-extension", "mwe-upwiz-upload-error-duplicate-filename-error", @@ -402,9 +361,6 @@ "mwe-upwiz-previous", "mwe-upwiz-home", "mwe-upwiz-upload-another", - "mwe-upwiz-tooltip-author", - "mwe-upwiz-tooltip-source", - "mwe-upwiz-tooltip-sign", "mwe-upwiz-tooltip-title", "mwe-upwiz-tooltip-description", "mwe-upwiz-tooltip-date", @@ -415,15 +371,10 @@ "mwe-upwiz-file-need-file", "mwe-upwiz-deeds-need-deed", "mwe-upwiz-deeds-need-license", - "mwe-upwiz-license-show-all", - "mwe-upwiz-license-show-recommended", - "mwe-upwiz-error-signature-blank", "mwe-upwiz-error-latitude", "mwe-upwiz-error-longitude", "mwe-upwiz-error-altitude", "mwe-upwiz-error-heading", - "mwe-upwiz-error-signature-too-long", - "mwe-upwiz-error-signature-too-short", "mwe-upwiz-error-blank", "mwe-upwiz-error-date-license-mismatch", "mwe-upwiz-error-date-license-unlikely", @@ -793,6 +744,127 @@ "targets": [ "desktop", "mobile" ], "group": "ext.uploadWizard" }, + "uw.deed.base": { + "scripts": [ + "resources/deed/uw.deed.base.js" + ], + "dependencies": [ + "uw.base" + ], + "targets": [ "desktop", "mobile" ], + "group": "ext.uploadWizard" + }, + "uw.deed.Abstract": { + "scripts": [ + "resources/deed/uw.deed.Abstract.js" + ], + "dependencies": [ + "uw.deed.base" + ], + "targets": [ "desktop", "mobile" ], + "group": "ext.uploadWizard" + }, + "uw.deed.OwnWork": { + "scripts": [ + "resources/deed/uw.deed.OwnWork.js" + ], + "dependencies": [ + "oojs", + "uw.deed.Abstract" + ], + "messages": [ + "mwe-upwiz-tooltip-sign", + "mwe-upwiz-source-ownwork-assert-any-license", + "mwe-upwiz-source-ownwork-assert", + "mwe-upwiz-source-ownwork-assert-custom", + "mwe-upwiz-source-ownwork-assert-note", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-4.0", + "mwe-upwiz-source-ownwork-cc-by-sa-4.0-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-at", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-at-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-de", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-de-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-ee", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-ee-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-es", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-es-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-hr", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-hr-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-lu", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-lu-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-nl", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-nl-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-no", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-no-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-pl", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-pl-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-sa-3.0-ro", + "mwe-upwiz-source-ownwork-cc-by-sa-3.0-ro-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-4.0", + "mwe-upwiz-source-ownwork-cc-by-4.0-explain", + "mwe-upwiz-source-ownwork-assert-cc-by-3.0", + "mwe-upwiz-source-ownwork-cc-by-3.0-explain", + "mwe-upwiz-source-ownwork-assert-cc-zero", + "mwe-upwiz-source-ownwork-cc-zero-explain", + "mwe-upwiz-source-ownwork-assert-generic", + "mwe-upwiz-source-ownwork-generic-explain", + "mwe-upwiz-error-signature-blank", + "mwe-upwiz-error-signature-too-long", + "mwe-upwiz-error-signature-too-short", + "mwe-upwiz-license-show-all", + "mwe-upwiz-license-show-recommended" + ], + "targets": [ "desktop", "mobile" ], + "group": "ext.uploadWizard" + }, + "uw.deed.ThirdParty": { + "scripts": [ + "resources/deed/uw.deed.ThirdParty.js" + ], + "dependencies": [ + "oojs", + "uw.deed.Abstract" + ], + "messages": [ + "mwe-upwiz-error-blank", + "mwe-upwiz-error-too-short", + "mwe-upwiz-error-too-long", + "mwe-upwiz-source", + "mwe-upwiz-author", + "mwe-upwiz-tooltip-author", + "mwe-upwiz-tooltip-source", + "mwe-upwiz-source-thirdparty-cases", + "mwe-upwiz-source-thirdparty-custom-multiple-intro", + "mwe-upwiz-source-thirdparty-intro", + "mwe-upwiz-source-thirdparty-accept" + ], + "targets": [ "desktop", "mobile" ], + "group": "ext.uploadWizard" + }, + "uw.deed.Custom": { + "scripts": [ + "resources/deed/uw.deed.Custom.js" + ], + "dependencies": [ + "oojs", + "uw.deed.Abstract" + ], + "targets": [ "desktop", "mobile" ], + "group": "ext.uploadWizard" + }, + "uw.deed.External": { + "scripts": [ + "resources/deed/uw.deed.External.js" + ], + "dependencies": [ + "oojs", + "uw.deed.Custom" + ], + "targets": [ "desktop", "mobile" ], + "group": "ext.uploadWizard" + }, "uw.units": { "scripts": [ "resources/uw.units.js" diff --git a/resources/controller/uw.controller.Deed.js b/resources/controller/uw.controller.Deed.js index fc8c33e..8e8d144 100644 --- a/resources/controller/uw.controller.Deed.js +++ b/resources/controller/uw.controller.Deed.js @@ -114,14 +114,12 @@ previousDeed = this.deedChooser.getSerialized(); } - this.deeds = mw.UploadWizard.getLicensingDeeds( this.uploads.length, this.config ); + this.deeds = mw.UploadWizard.getLicensingDeeds( this.uploads, this.config ); // if we have multiple uploads, also give them the option to set // licenses individually if ( this.uploads.length > 1 && this.shouldShowIndividualDeed( this.config ) ) { - customDeed = $.extend( new mw.UploadWizardDeed(), { - name: 'custom' - } ); + customDeed = new uw.deed.Custom(); this.deeds[ customDeed.name ] = customDeed; } diff --git a/resources/deed/uw.deed.Abstract.js b/resources/deed/uw.deed.Abstract.js new file mode 100644 index 0000000..37430f0 --- /dev/null +++ b/resources/deed/uw.deed.Abstract.js @@ -0,0 +1,96 @@ +/* + * 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, uw ) { + /** + * Represents a generic deed. + * + * @class uw.deed.Abstract + * @constructor + * @param {string} name The name of this step + */ + uw.deed.Abstract = function UWDeedInterface( name ) { + this.name = name; + uw.deed.Abstract.prototype.instanceCount++; + this.instanceCount = uw.deed.Abstract.prototype.instanceCount; + }; + + /** + * @type {number} + */ + uw.deed.Abstract.prototype.instanceCount = 0; + + /** + * @returns {number} + */ + uw.deed.Abstract.prototype.getInstanceCount = function () { + return this.instanceCount; + }; + + /** + * @return {uw.FieldLayout[]} Fields that need validation + */ + uw.deed.Abstract.prototype.getFields = function () { + return []; + }; + + /** + * @param {jQuery} $selector + */ + uw.deed.Abstract.prototype.setFormFields = function ( $selector ) {}; + + /** + * @return {string} + */ + uw.deed.Abstract.prototype.getSourceWikiText = function () { + throw new Error( 'Not implemented.' ); + }; + + /** + * @return {string} + */ + uw.deed.Abstract.prototype.getAuthorWikiText = function () { + throw new Error( 'Not implemented.' ); + }; + + /** + * Get wikitext representing the licenses selected in the license object + * + * @return {string} wikitext of all applicable license templates. + */ + uw.deed.Abstract.prototype.getLicenseWikiText = function () { + throw new Error( 'Not implemented.' ); + }; + + /** + * @return {Object} + */ + uw.deed.Abstract.prototype.getSerialized = function () { + return { + name: this.name + }; + }; + + /** + * @param {Object} serialized + */ + uw.deed.Abstract.prototype.setSerialized = function ( serialized ) { + if ( serialized.name ) { + this.name = serialized.name; + } + }; +}( mediaWiki, mediaWiki.uploadWizard ) ); diff --git a/resources/deed/uw.deed.Custom.js b/resources/deed/uw.deed.Custom.js new file mode 100644 index 0000000..6be6439 --- /dev/null +++ b/resources/deed/uw.deed.Custom.js @@ -0,0 +1,56 @@ +/* + * 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, uw, $, OO ) { + /** + * @param {mw.UploadWizardUpload} upload Uploads that this deed refers to + * @class uw.deed.Custom + * @constructor + */ + uw.deed.Custom = function UWDeedCustom( upload ) { + uw.deed.Abstract.call( this, 'custom' ); + + this.upload = upload; + }; + + OO.inheritClass( uw.deed.Custom, uw.deed.Abstract ); + + /** + * @return {string} + */ + uw.deed.Custom.prototype.getSourceWikiText = function () { + if ( typeof this.upload.file.sourceURL !== 'undefined' ) { + return this.upload.file.sourceURL; + } else { + return this.upload.file.url; + } + }; + + /** + * @return {string} + */ + uw.deed.Custom.prototype.getAuthorWikiText = function () { + return this.upload.file.author; + }; + + /** + * @return {string} + */ + uw.deed.Custom.prototype.getLicenseWikiText = function () { + return this.upload.file.licenseValue; + }; +}( mediaWiki, mediaWiki.uploadWizard, jQuery, OO ) ); diff --git a/resources/deed/uw.deed.External.js b/resources/deed/uw.deed.External.js new file mode 100644 index 0000000..aadf033 --- /dev/null +++ b/resources/deed/uw.deed.External.js @@ -0,0 +1,78 @@ +/* + * 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, uw, $, OO ) { + /** + * @param {mw.UploadWizardUpload} upload Uploads that this deed refers to + * @param {Object} config + * @class uw.deed.External + * @constructor + */ + uw.deed.External = function UWDeedExternal( upload, config ) { + uw.deed.Custom.call( this, upload ); + + this.licenseInput = new mw.UploadWizardLicenseInput( + undefined, + config, + 1, + upload.api + ); + this.licenseInput.$element.addClass( 'mwe-upwiz-External-deed' ); + this.licenseInputField = new uw.FieldLayout( this.licenseInput ); + this.licenseInput.setDefaultValues(); + }; + + OO.inheritClass( uw.deed.External, uw.deed.Custom ); + + /** + * @return {uw.FieldLayout[]} Fields that need validation + */ + uw.deed.External.prototype.getFields = function () { + return [ this.licenseInputField ]; + }; + + /** + * @return {uw.FieldLayout[]} Fields that need validation + */ + uw.deed.External.prototype.getLicenseWikiText = function () { + if ( this.upload.file.licenseValue ) { + return this.upload.file.licenseValue + this.licenseInput.getWikiText(); + } else { + return this.licenseInput.getWikiText(); + } + }; + + /** + * @return {Object} + */ + uw.deed.External.prototype.getSerialized = function () { + return $.extend( uw.deed.Custom.prototype.getSerialized.call( this ), { + license: this.licenseInput.getSerialized() + } ); + }; + + /** + * @param {Object} serialized + */ + uw.deed.External.prototype.setSerialized = function ( serialized ) { + uw.deed.Custom.prototype.setSerialized.call( this, serialized ); + + if ( serialized.license ) { + this.licenseInput.setSerialized( serialized.license ); + } + }; +}( mediaWiki, mediaWiki.uploadWizard, jQuery, OO ) ); diff --git a/resources/deed/uw.deed.OwnWork.js b/resources/deed/uw.deed.OwnWork.js new file mode 100644 index 0000000..bdaf744 --- /dev/null +++ b/resources/deed/uw.deed.OwnWork.js @@ -0,0 +1,314 @@ +/* + * 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, uw, $, OO ) { + /** + * Set up the form and deed object for the deed option that says these uploads are all the user's own work. + * + * @class uw.deed.OwnWork + * @constructor + * @param {mw.UploadWizardUpload[]} uploads Array of uploads that this deed refers to + * @param {mw.Api} api API object - useful for doing previews + * @param {Object} config The UW config + */ + uw.deed.OwnWork = function UWDeedOwnWork( uploads, api, config ) { + var deed = this; + + uw.deed.Abstract.call( this, 'ownwork' ); + + this.config = config; + this.uploadCount = uploads.length; + + // copyright holder + this.authorInput = new OO.ui.TextInputWidget( { + name: 'author', + title: mw.message( 'mwe-upwiz-tooltip-sign' ).text(), + value: mw.config.get( 'wgUserName' ), + classes: [ 'mwe-upwiz-sign' ] + } ); + this.fakeAuthorInput = new OO.ui.TextInputWidget( { + readOnly: true, + value: mw.config.get( 'wgUserName' ), + classes: [ 'mwe-upwiz-sign' ] + } ); + this.authorInput.on( 'change', function () { + deed.fakeAuthorInput.setValue( deed.authorInput.getValue() ); + } ); + + // "use a different license" + this.showCustomDiv = this.config.licensing.ownWork.licenses.length > 1; + if ( this.showCustomDiv ) { + this.licenseInput = new mw.UploadWizardLicenseInput( + undefined, + this.config.licensing.ownWork, + this.uploadCount, + api + ); + this.licenseInput.$element.addClass( 'mwe-upwiz-deed-license' ); + this.licenseInputField = new uw.FieldLayout( this.licenseInput ); + } + }; + + OO.inheritClass( uw.deed.OwnWork, uw.deed.Abstract ); + + /** + * @return {uw.FieldLayout[]} Fields that need validation + */ + uw.deed.OwnWork.prototype.getFields = function () { + var fields = [ this.authorInputField ]; + if ( this.showCustomDiv ) { + fields.push( this.licenseInputField ); + } + return fields; + }; + + uw.deed.OwnWork.prototype.setFormFields = function ( $selector ) { + var $customDiv, $formFields, $toggler, crossfaderWidget, defaultLicense, + defaultLicenseURL, defaultLicenseMsg, defaultLicenseExplainMsg, + defaultLicenseLink, $standardDiv, $crossfader, deed, languageCode; + + this.$selector = $selector; + deed = this; + languageCode = mw.config.get( 'wgUserLanguage' ); + + defaultLicense = this.getDefaultLicense(); + + defaultLicenseURL = this.config.licenses[ defaultLicense ].url === undefined ? + '#missing license URL' : + this.config.licenses[ defaultLicense ].url + 'deed.' + languageCode; + defaultLicenseMsg = 'mwe-upwiz-source-ownwork-assert-' + defaultLicense; + defaultLicenseExplainMsg = 'mwe-upwiz-source-ownwork-' + defaultLicense + '-explain'; + defaultLicenseLink = $( '<a>' ).attr( { target: '_blank', href: defaultLicenseURL } ); + + this.$form = $( '<form>' ); + + $standardDiv = $( '<div class="mwe-upwiz-standard" />' ).append( + $( '<p>' ).msg( + defaultLicenseMsg, + this.uploadCount, + this.authorInput.$element, + defaultLicenseLink, + mw.user + ), + $( '<p class="mwe-small-print"></p>' ).msg( + defaultLicenseExplainMsg, + this.uploadCount + ) + ); + $crossfader = $( '<div class="mwe-upwiz-crossfader" />' ).append( $standardDiv ); + + if ( this.showCustomDiv ) { + $customDiv = $( '<div class="mwe-upwiz-custom" />' ).append( + $( '<p>' ).msg( 'mwe-upwiz-source-ownwork-assert-custom', + this.uploadCount, + this.fakeAuthorInput.$element ) + ); + + $crossfader.append( $customDiv ); + } + + crossfaderWidget = new OO.ui.Widget(); + crossfaderWidget.$element.append( $crossfader ); + // See uw.DetailsWidget + crossfaderWidget.getErrors = function () { + var + errors = [], + minLength = deed.config.minAuthorLength, + maxLength = deed.config.maxAuthorLength, + text = deed.authorInput.getValue().trim(); + + if ( text === '' ) { + errors.push( mw.message( 'mwe-upwiz-error-signature-blank' ) ); + } else if ( text.length < minLength ) { + errors.push( mw.message( 'mwe-upwiz-error-signature-too-short', minLength ) ); + } else if ( text.length > maxLength ) { + errors.push( mw.message( 'mwe-upwiz-error-signature-too-long', maxLength ) ); + } + + return $.Deferred().resolve( errors ).promise(); + }; + // See uw.DetailsWidget + crossfaderWidget.getWarnings = function () { + return $.Deferred().resolve( [] ).promise(); + }; + this.authorInputField = new uw.FieldLayout( crossfaderWidget ); + // Aggregate 'change' event + this.authorInput.on( 'change', OO.ui.debounce( function () { + crossfaderWidget.emit( 'change' ); + }, 500 ) ); + + $formFields = $( '<div class="mwe-upwiz-deed-form-internal" />' ) + .append( + this.authorInputField.$element, + this.showCustomDiv ? this.licenseInputField.$element.hide() : '' + ); + + $toggler = $( '<p class="mwe-more-options" style="text-align: right"></p>' ) + .append( $( '<a />' ) + .msg( 'mwe-upwiz-license-show-all' ) + .click( function () { + if ( $crossfader.data( 'crossfadeDisplay' ).get( 0 ) === $customDiv.get( 0 ) ) { + deed.standardLicense(); + } else { + deed.customLicense(); + } + } ) ); + + if ( this.showCustomDiv ) { + $formFields.append( $toggler ); + } + + this.$form.append( $formFields ).appendTo( $selector ); + + // done after added to the DOM, so there are true heights + $crossfader.morphCrossfader(); + + if ( this.showCustomDiv ) { + // choose default licenses + this.licenseInput.setDefaultValues(); + } + + $.each( this.config.licensing.ownWork.licenses, function ( i, license ) { + if ( license === defaultLicense ) { + $( '#license1_' + i ).prop( 'checked', true ); + return false; + } + } ); + }; + + uw.deed.OwnWork.prototype.getSourceWikiText = function () { + return '{{own}}'; + }; + + // XXX do we need to escape authorInput, or is wikitext a feature here? + // what about scripts? + uw.deed.OwnWork.prototype.getAuthorWikiText = function () { + var author = this.authorInput.getValue(); + + if ( author.indexOf( '[' ) >= 0 || author.indexOf( '{' ) >= 0 ) { + return author; + } + + return '[[User:' + mw.config.get( 'wgUserName' ) + '|' + author + ']]'; + }; + + uw.deed.OwnWork.prototype.getLicenseWikiText = function () { + if ( this.showCustomDiv && this.licenseInput.getWikiText() !== '' ) { + return this.licenseInput.getWikiText(); + } else { + return '{{' + + this.config.licensing.ownWork.template + + '|' + + this.getDefaultLicense() + + '}}'; + } + }; + + /** + * @return {Object} + */ + uw.deed.OwnWork.prototype.getSerialized = function () { + var serialized = $.extend( uw.deed.Abstract.prototype.getSerialized.call( this ), { + author: this.authorInput.getValue() + } ); + + if ( this.showCustomDiv ) { + serialized.license = this.licenseInput.getSerialized(); + } + + return serialized; + }; + + /** + * @param {Object} serialized + */ + uw.deed.OwnWork.prototype.setSerialized = function ( serialized ) { + uw.deed.Abstract.prototype.setSerialized.call( this, serialized ); + + if ( serialized.author ) { + this.authorInput.setValue( serialized.author ); + } + + if ( this.showCustomDiv && serialized.license ) { + // only need to set license if it's not the default license + if ( !( this.getDefaultLicense() in serialized.license ) ) { + // expand licenses container + this.customLicense(); + this.licenseInput.setSerialized( serialized.license ); + } + } + }; + + uw.deed.OwnWork.prototype.swapNodes = function ( a, b ) { + var + parentA = a.parentNode, + parentB = b.parentNode, + nextA = a.nextSibling, + nextB = b.nextSibling; + + // This is not correct if a and b are siblings, or if one is a child of the + // other, or if they're detached, or maybe in other cases, but we don't care + parentA[ nextA ? 'insertBefore' : 'appendChild' ]( b, nextA ); + parentB[ nextB ? 'insertBefore' : 'appendChild' ]( a, nextB ); + }; + + uw.deed.OwnWork.prototype.getDefaultLicense = function () { + if ( this.config.licensing.defaultType === 'ownwork' ) { + return this.config.licensing.ownWork.defaults; + } else { + return this.config.licensing.ownWork.licenses[ 0 ]; + } + }; + + uw.deed.OwnWork.prototype.standardLicense = function () { + var deed = this, + $crossfader = this.$selector.find( '.mwe-upwiz-crossfader' ), + $standardDiv = this.$selector.find( '.mwe-upwiz-standard' ), + $toggler = this.$selector.find( '.mwe-more-options a' ); + + this.licenseInput.setDefaultValues(); + + $crossfader.morphCrossfade( $standardDiv ) + .promise().done( function () { + deed.swapNodes( deed.authorInput.$element[ 0 ], deed.fakeAuthorInput.$element[ 0 ] ); + } ); + + this.licenseInputField.$element + .slideUp() + .animate( { opacity: 0 }, { queue: false, easing: 'linear' } ); + + $toggler.msg( 'mwe-upwiz-license-show-all' ); + }; + + uw.deed.OwnWork.prototype.customLicense = function () { + var deed = this, + $crossfader = this.$selector.find( '.mwe-upwiz-crossfader' ), + $customDiv = this.$selector.find( '.mwe-upwiz-custom' ), + $toggler = this.$selector.find( '.mwe-more-options a' ); + + $crossfader.morphCrossfade( $customDiv ) + .promise().done( function () { + deed.swapNodes( deed.authorInput.$element[ 0 ], deed.fakeAuthorInput.$element[ 0 ] ); + } ); + + this.licenseInputField.$element + .slideDown() + .css( { opacity: 0 } ).animate( { opacity: 1 }, { queue: false, easing: 'linear' } ); + + $toggler.msg( 'mwe-upwiz-license-show-recommended' ); + }; +}( mediaWiki, mediaWiki.uploadWizard, jQuery, OO ) ); diff --git a/resources/deed/uw.deed.ThirdParty.js b/resources/deed/uw.deed.ThirdParty.js new file mode 100644 index 0000000..8f3a26f --- /dev/null +++ b/resources/deed/uw.deed.ThirdParty.js @@ -0,0 +1,247 @@ +/* + * 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, uw, $, OO ) { + /** + * Set up the form and deed object for the deed option that says these uploads are the work of a third party. + * + * @class uw.deed.ThirdParty + * @constructor + * @param {mw.UploadWizardUpload[]} uploads Array of uploads that this deed refers to + * @param {mw.Api} api API object - useful for doing previews + * @param {Object} config The UW config + */ + uw.deed.ThirdParty = function UWDeedThirdParty( uploads, api, config ) { + var deed = this; + + uw.deed.Abstract.call( this, 'thirdparty' ); + + this.config = config; + this.uploadCount = uploads.length; + + this.sourceInput = new OO.ui.MultilineTextInputWidget( { + autosize: true, + classes: [ 'mwe-source' ], + name: 'source' + } ); + this.sourceInput.$input.attr( 'id', 'mwe-source-' + this.getInstanceCount() ); + // See uw.DetailsWidget + this.sourceInput.getErrors = function () { + var + errors = [], + minLength = deed.config.minSourceLength, + maxLength = deed.config.maxSourceLength, + text = this.getValue().trim(); + + if ( text === '' ) { + errors.push( mw.message( 'mwe-upwiz-error-blank' ) ); + } else if ( text.length < minLength ) { + errors.push( mw.message( 'mwe-upwiz-error-too-short', minLength ) ); + } else if ( text.length > maxLength ) { + errors.push( mw.message( 'mwe-upwiz-error-too-long', maxLength ) ); + } + + return $.Deferred().resolve( errors ).promise(); + }; + // See uw.DetailsWidget + this.sourceInput.getWarnings = function () { + return $.Deferred().resolve( [] ).promise(); + }; + this.sourceInputField = new uw.FieldLayout( this.sourceInput, { + label: mw.message( 'mwe-upwiz-source' ).text(), + help: mw.message( 'mwe-upwiz-tooltip-source' ).text(), + required: true + } ); + + this.authorInput = new OO.ui.MultilineTextInputWidget( { + autosize: true, + classes: [ 'mwe-author' ], + name: 'author' + } ); + this.authorInput.$input.attr( 'id', 'mwe-author-' + this.getInstanceCount() ); + // See uw.DetailsWidget + this.authorInput.getErrors = function () { + var + errors = [], + minLength = deed.config.minAuthorLength, + maxLength = deed.config.maxAuthorLength, + text = this.getValue().trim(); + + if ( text === '' ) { + errors.push( mw.message( 'mwe-upwiz-error-blank' ) ); + } else if ( text.length < minLength ) { + errors.push( mw.message( 'mwe-upwiz-error-too-short', minLength ) ); + } else if ( text.length > maxLength ) { + errors.push( mw.message( 'mwe-upwiz-error-too-long', maxLength ) ); + } + + return $.Deferred().resolve( errors ).promise(); + }; + // See uw.DetailsWidget + this.authorInput.getWarnings = function () { + return $.Deferred().resolve( [] ).promise(); + }; + this.authorInputField = new uw.FieldLayout( this.authorInput, { + label: mw.message( 'mwe-upwiz-author' ).text(), + help: mw.message( 'mwe-upwiz-tooltip-author' ).text(), + required: true + } ); + + this.licenseInput = new mw.UploadWizardLicenseInput( + undefined, + this.config.licensing.thirdParty, + this.uploadCount, + api + ); + this.licenseInput.$element.addClass( 'mwe-upwiz-deed-license-groups' ); + this.licenseInput.setDefaultValues(); + this.licenseInputField = new uw.FieldLayout( this.licenseInput, { + label: mw.message( 'mwe-upwiz-source-thirdparty-cases', this.uploadCount ).text() + } ); + }; + + OO.inheritClass( uw.deed.ThirdParty, uw.deed.Abstract ); + + /** + * @return {uw.FieldLayout[]} Fields that need validation + */ + uw.deed.ThirdParty.prototype.getFields = function () { + return [ this.authorInputField, this.sourceInputField, this.licenseInputField ]; + }; + + uw.deed.ThirdParty.prototype.setFormFields = function ( $selector ) { + var $defaultLicense, defaultLicense, defaultLicenseNum, defaultType, collapsible, + $formFields = $( '<div class="mwe-upwiz-deed-form-internal" />' ); + + this.$form = $( '<form>' ); + + defaultType = this.config.licensing.defaultType; + + if ( this.uploadCount > 1 ) { + $formFields.append( $( '<div>' ).msg( 'mwe-upwiz-source-thirdparty-custom-multiple-intro' ) ); + } + + $formFields.append( + $( '<div class="mwe-upwiz-source-thirdparty-custom-multiple-intro" />' ), + $( '<div class="mwe-upwiz-thirdparty-fields" />' ) + .append( this.sourceInputField.$element ), + $( '<div class="mwe-upwiz-thirdparty-fields" />' ) + .append( this.authorInputField.$element ), + $( '<div class="mwe-upwiz-thirdparty-license" />' ) + .append( this.licenseInputField.$element ) + ); + + this.$form.append( $formFields ); + + $selector.append( this.$form ); + + if ( defaultType === 'thirdparty' ) { + defaultLicense = this.config.licensing.thirdParty.defaults; + + defaultLicenseNum = this.findLicenseRecursively( config, defaultLicense ); + + if ( defaultLicenseNum ) { + $defaultLicense = $( '#license' + defaultLicenseNum ); + collapsible = $defaultLicense + .closest( '.mwe-upwiz-deed-license-group' ) + .data( 'mw-collapsible' ); + if ( collapsible ) { + collapsible.expand(); + } + $defaultLicense.prop( 'checked', true ); + } + } + }; + + /** + * @return {string} + */ + uw.deed.Abstract.prototype.getSourceWikiText = function () { + return this.sourceInput.getValue(); + }; + + /** + * @return {string} + */ + uw.deed.Abstract.prototype.getAuthorWikiText = function () { + return this.authorInput.getValue(); + }; + + /** + * Get wikitext representing the licenses selected in the license object + * + * @return {string} wikitext of all applicable license templates. + */ + uw.deed.Abstract.prototype.getLicenseWikiText = function () { + return this.licenseInput.getWikiText(); + }; + + /** + * @return {Object} + */ + uw.deed.ThirdParty.prototype.getSerialized = function () { + return $.extend( uw.deed.Abstract.prototype.getSerialized.call( this ), { + source: this.sourceInput.getValue(), + author: this.authorInput.getValue(), + license: this.licenseInput.getSerialized() + } ); + }; + + /** + * @param {Object} serialized + */ + uw.deed.ThirdParty.prototype.setSerialized = function ( serialized ) { + uw.deed.Abstract.prototype.setSerialized.call( this, serialized ); + + if ( serialized.source ) { + this.sourceInput.setValue( serialized.source ); + } + if ( serialized.author ) { + this.authorInput.setValue( serialized.author ); + } + if ( serialized.license ) { + this.licenseInput.setSerialized( serialized.license ); + } + }; + + /** + * Runs through the third-party license groups and finds the + * relevant ID for that license. Probably really hacky. + * TODO do this properly once we build the license links properly + */ + uw.deed.ThirdParty.prototype.findLicenseRecursively = function ( config, license ) { + var val, + count = 0; + + $.each( this.config.licensing.thirdParty.licenseGroups, function ( i, licenseGroup ) { + $.each( licenseGroup.licenses, function ( j, licenseCandidate ) { + if ( licenseCandidate === license ) { + val = '2_' + count; + return false; + } + + count++; + } ); + + if ( val !== undefined ) { + return false; + } + } ); + + return val; + }; +}( mediaWiki, mediaWiki.uploadWizard, jQuery, OO ) ); diff --git a/resources/deed/uw.deed.base.js b/resources/deed/uw.deed.base.js new file mode 100644 index 0000000..92d3f6f --- /dev/null +++ b/resources/deed/uw.deed.base.js @@ -0,0 +1,21 @@ +/* + * 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 ( uw ) { + // create UploadWizard deed namespace + uw.deed = {}; +}( mediaWiki.uploadWizard ) ); diff --git a/resources/details/uw.DeedChooserDetailsWidget.js b/resources/details/uw.DeedChooserDetailsWidget.js index a0f87e6..4e22839 100644 --- a/resources/details/uw.DeedChooserDetailsWidget.js +++ b/resources/details/uw.DeedChooserDetailsWidget.js @@ -19,11 +19,11 @@ * @param {mw.UploadWizardUpload} upload */ uw.DeedChooserDetailsWidget.prototype.useCustomDeedChooser = function ( upload ) { - var config, deed, deedDiv; + var deedDiv; // Defining own deedChooser for uploads coming from external service if ( upload.file.fromURL ) { - // XXX can be made a seperate class as mw.UploadFromUrlDeedChooser + // XXX can be made a separate class as mw.UploadFromUrlDeedChooser this.deedChooser = upload.deedChooser = { deed: {}, valid: function () { @@ -42,33 +42,14 @@ if ( upload.file.license ) { // XXX need to add code in the remaining functions this.$element.append( upload.file.licenseMessage ); - this.deedChooser.deed = this.getDeed( upload ); + this.deedChooser.deed = new uw.deed.Custom( upload ); } else { - config = { type: 'or', licenses: [ 'custom' ], special: 'custom' }; - deed = {}; - - deed.licenseInput = new mw.UploadWizardLicenseInput( - undefined, - config, - 1, - upload.api + this.deedChooser.deed = new uw.deed.External( + upload, + { type: 'or', licenses: [ 'custom' ], special: 'custom' } ); - deed.licenseInput.$element.addClass( 'mwe-upwiz-custom-deed' ); - deed.licenseInputField = new uw.FieldLayout( deed.licenseInput ); this.$element.append( deed.licenseInputField.$element ); - deed.licenseInput.setDefaultValues(); - this.$element.append( upload.file.licenseMessage ); - this.deedChooser.deed = this.getDeed( upload, deed, { - getFields: function () { return [ this.licenseInputField ]; }, - getLicenseWikiText: function () { - if ( upload.file.licenseValue ) { - return upload.file.licenseValue + this.licenseInput.getWikiText(); - } else { - return this.licenseInput.getWikiText(); - } - } - } ); } } else { deedDiv = $( '<div class="mwe-upwiz-custom-deed" />' ); @@ -76,38 +57,10 @@ this.deedChooser = upload.deedChooser = new mw.UploadWizardDeedChooser( mw.UploadWizard.config, deedDiv, - mw.UploadWizard.getLicensingDeeds( 1, mw.UploadWizard.config ), + mw.UploadWizard.getLicensingDeeds( [ upload ], mw.UploadWizard.config ), [ upload ] ); this.deedChooser.onLayoutReady(); } - }; - - uw.DeedChooserDetailsWidget.prototype.getDeed = function ( upload, deed, overrides ) { - deed = deed || {}; - overrides = overrides || {}; - - // XXX need to add code in the remaining functions - return $.extend( deed, { - getFields: function () { return []; }, - getSourceWikiText: function () { - if ( typeof upload.file.sourceURL !== 'undefined' ) { - return upload.file.sourceURL; - } else { - return upload.file.url; - } - }, - getAuthorWikiText: function () { - return upload.file.author; - }, - getLicenseWikiText: function () { - return upload.file.licenseValue; - }, - getSerialized: function () { - return {}; - }, - setSerialized: function () { - } - }, overrides ); }; /** diff --git a/resources/mw.UploadWizard.js b/resources/mw.UploadWizard.js index 9aede7a..6b88240 100644 --- a/resources/mw.UploadWizard.js +++ b/resources/mw.UploadWizard.js @@ -149,11 +149,11 @@ * * @static * @since 1.2 - * @param {number} uploadsLength + * @param {mw.UploadWizardUpload[]} uploads * @param {Object} config The UW config object. - * @return {mw.UploadWizardDeed[]} + * @return {mw.deed.Abstract[]} */ - mw.UploadWizard.getLicensingDeeds = function ( uploadsLength, config ) { + mw.UploadWizard.getLicensingDeeds = function ( uploads, config ) { var deed, api, deeds = {}, doOwnWork = false, @@ -170,11 +170,11 @@ } if ( doOwnWork ) { - deed = new mw.UploadWizardDeedOwnWork( uploadsLength, api, config ); + deed = new uw.deed.OwnWork( uploads, api, config ); deeds[ deed.name ] = deed; } if ( doThirdParty ) { - deed = new mw.UploadWizardDeedThirdParty( uploadsLength, api, config ); + deed = new uw.deed.ThirdParty( uploads, api, config ); deeds[ deed.name ] = deed; } diff --git a/resources/mw.UploadWizardDeed.js b/resources/mw.UploadWizardDeed.js deleted file mode 100644 index 5c3ffa1..0000000 --- a/resources/mw.UploadWizardDeed.js +++ /dev/null @@ -1,65 +0,0 @@ -( function ( mw ) { - - /** - * Sort of an abstract class for deeds - * - * @class mw.UploadWizardDeed - * @abstract - */ - mw.UploadWizardDeed = function () { - mw.UploadWizardDeed.prototype.instanceCount++; - }; - - mw.UploadWizardDeed.prototype = { - instanceCount: 0, - - /** - * @return {uw.FieldLayout[]} Fields that need validation - */ - getFields: function () { - return []; - }, - - getInstanceCount: function () { - return this.instanceCount; - }, - - setFormFields: function () { }, - - getSourceWikiText: function () { - return this.sourceInput.getValue(); - }, - - getAuthorWikiText: function () { - return this.authorInput.getValue(); - }, - - /** - * Get wikitext representing the licenses selected in the license object - * - * @return {string} wikitext of all applicable license templates. - */ - getLicenseWikiText: function () { - return this.licenseInput.getWikiText(); - }, - - /** - * @return {Object} - */ - getSerialized: function () { - return { - name: this.name - }; - }, - - /** - * @param {Object} serialized - */ - setSerialized: function ( serialized ) { - if ( serialized.name ) { - this.name = serialized.name; - } - } - }; - -}( mediaWiki ) ); diff --git a/resources/mw.UploadWizardDeedOwnWork.js b/resources/mw.UploadWizardDeedOwnWork.js deleted file mode 100644 index 1dccf2f..0000000 --- a/resources/mw.UploadWizardDeedOwnWork.js +++ /dev/null @@ -1,299 +0,0 @@ -( function ( mw, uw, $ ) { - - function swapNodes( a, b ) { - var - parentA = a.parentNode, - parentB = b.parentNode, - nextA = a.nextSibling, - nextB = b.nextSibling; - - // This is not correct if a and b are siblings, or if one is a child of the - // other, or if they're detached, or maybe in other cases, but we don't care - parentA[ nextA ? 'insertBefore' : 'appendChild' ]( b, nextA ); - parentB[ nextB ? 'insertBefore' : 'appendChild' ]( a, nextB ); - } - - /** - * Set up the form and deed object for the deed option that says these uploads are all the user's own work. - * - * @class mw.UploadWizardDeedOwnWork - * @constructor - * @param {number} uploadCount Integer count of uploads that this deed refers to (useful for message pluralization) - * @param {mw.Api} api API object - useful for doing previews - * @param {Object} config The UW config - */ - mw.UploadWizardDeedOwnWork = function ( uploadCount, api, config ) { - var deed = new mw.UploadWizardDeed(), - ownWork = config.licensing.ownWork; - - uploadCount = uploadCount || 1; - - deed.authorInput = new OO.ui.TextInputWidget( { - name: 'author', - title: mw.message( 'mwe-upwiz-tooltip-sign' ).text(), - value: mw.config.get( 'wgUserName' ), - classes: [ 'mwe-upwiz-sign' ] - } ); - deed.fakeAuthorInput = new OO.ui.TextInputWidget( { - readOnly: true, - value: mw.config.get( 'wgUserName' ), - classes: [ 'mwe-upwiz-sign' ] - } ); - deed.authorInput.on( 'change', function () { - deed.fakeAuthorInput.setValue( deed.authorInput.getValue() ); - } ); - - deed.showCustomDiv = ownWork.licenses.length > 1; - - if ( deed.showCustomDiv ) { - deed.licenseInput = new mw.UploadWizardLicenseInput( - undefined, - config.licensing.ownWork, - deed.uploadCount, - api - ); - deed.licenseInput.$element.addClass( 'mwe-upwiz-deed-license' ); - deed.licenseInputField = new uw.FieldLayout( deed.licenseInput ); - } - - return $.extend( deed, { - - name: 'ownwork', - - /** - * @return {uw.FieldLayout[]} Fields that need validation - */ - getFields: function () { - var fields = [ this.authorInputField ]; - if ( this.showCustomDiv ) { - fields.push( this.licenseInputField ); - } - return fields; - }, - - getDefaultLicense: function () { - if ( config.licensing.defaultType === 'ownwork' ) { - return config.licensing.ownWork.defaults; - } else { - return config.licensing.ownWork.licenses[ 0 ]; - } - }, - - getLicenseWikiText: function () { - if ( this.showCustomDiv && this.licenseInput.getWikiText() !== '' ) { - return this.licenseInput.getWikiText(); - } else { - return '{{' + - config.licensing.ownWork.template + - '|' + - this.getDefaultLicense() + - '}}'; - } - }, - - getSourceWikiText: function () { - return '{{own}}'; - }, - - // XXX do we need to escape authorInput, or is wikitext a feature here? - // what about scripts? - getAuthorWikiText: function () { - var author = this.authorInput.getValue(); - - if ( author.indexOf( '[' ) >= 0 || author.indexOf( '{' ) >= 0 ) { - return author; - } - - return '[[User:' + mw.config.get( 'wgUserName' ) + '|' + author + ']]'; - }, - - setFormFields: function ( $selector ) { - var $customDiv, $formFields, $toggler, crossfaderWidget, defaultLicense, - defaultLicenseURL, defaultLicenseMsg, defaultLicenseExplainMsg, - defaultLicenseLink, $standardDiv, $crossfader, thisDeed, languageCode; - - this.$selector = $selector; - thisDeed = this; - languageCode = mw.config.get( 'wgUserLanguage' ); - - defaultLicense = this.getDefaultLicense(); - - defaultLicenseURL = config.licenses[ defaultLicense ].url === undefined ? - '#missing license URL' : - config.licenses[ defaultLicense ].url + 'deed.' + languageCode; - defaultLicenseMsg = 'mwe-upwiz-source-ownwork-assert-' + defaultLicense; - defaultLicenseExplainMsg = 'mwe-upwiz-source-ownwork-' + defaultLicense + '-explain'; - defaultLicenseLink = $( '<a>' ).attr( { target: '_blank', href: defaultLicenseURL } ); - - this.$form = $( '<form>' ); - - $standardDiv = $( '<div class="mwe-upwiz-standard" />' ).append( - $( '<p>' ).msg( - defaultLicenseMsg, - uploadCount, - this.authorInput.$element, - defaultLicenseLink, - mw.user - ), - $( '<p class="mwe-small-print"></p>' ).msg( - defaultLicenseExplainMsg, - uploadCount - ) - ); - $crossfader = $( '<div class="mwe-upwiz-crossfader" />' ).append( $standardDiv ); - - if ( this.showCustomDiv ) { - $customDiv = $( '<div class="mwe-upwiz-custom" />' ).append( - $( '<p>' ).msg( 'mwe-upwiz-source-ownwork-assert-custom', - uploadCount, - this.fakeAuthorInput.$element ) - ); - - $crossfader.append( $customDiv ); - } - - crossfaderWidget = new OO.ui.Widget(); - crossfaderWidget.$element.append( $crossfader ); - // See uw.DetailsWidget - crossfaderWidget.getErrors = function () { - var - errors = [], - minLength = config.minAuthorLength, - maxLength = config.maxAuthorLength, - text = thisDeed.authorInput.getValue().trim(); - - if ( text === '' ) { - errors.push( mw.message( 'mwe-upwiz-error-signature-blank' ) ); - } else if ( text.length < minLength ) { - errors.push( mw.message( 'mwe-upwiz-error-signature-too-short', minLength ) ); - } else if ( text.length > maxLength ) { - errors.push( mw.message( 'mwe-upwiz-error-signature-too-long', maxLength ) ); - } - - return $.Deferred().resolve( errors ).promise(); - }; - // See uw.DetailsWidget - crossfaderWidget.getWarnings = function () { - return $.Deferred().resolve( [] ).promise(); - }; - this.authorInputField = new uw.FieldLayout( crossfaderWidget ); - // Aggregate 'change' event - this.authorInput.on( 'change', OO.ui.debounce( function () { - crossfaderWidget.emit( 'change' ); - }, 500 ) ); - - $formFields = $( '<div class="mwe-upwiz-deed-form-internal" />' ) - .append( - this.authorInputField.$element, - this.showCustomDiv ? this.licenseInputField.$element.hide() : '' - ); - - $toggler = $( '<p class="mwe-more-options" style="text-align: right"></p>' ) - .append( $( '<a />' ) - .msg( 'mwe-upwiz-license-show-all' ) - .click( function () { - if ( $crossfader.data( 'crossfadeDisplay' ).get( 0 ) === $customDiv.get( 0 ) ) { - thisDeed.standardLicense(); - } else { - thisDeed.customLicense(); - } - } ) ); - - if ( this.showCustomDiv ) { - $formFields.append( $toggler ); - } - - this.$form.append( $formFields ).appendTo( $selector ); - - // done after added to the DOM, so there are true heights - $crossfader.morphCrossfader(); - - if ( this.showCustomDiv ) { - // choose default licenses - this.licenseInput.setDefaultValues(); - } - - $.each( config.licensing.ownWork.licenses, function ( i, license ) { - if ( license === defaultLicense ) { - $( '#license1_' + i ).prop( 'checked', true ); - return false; - } - } ); - }, - - standardLicense: function () { - var deed = this, - $crossfader = this.$selector.find( '.mwe-upwiz-crossfader' ), - $standardDiv = this.$selector.find( '.mwe-upwiz-standard' ), - $toggler = this.$selector.find( '.mwe-more-options a' ); - - this.licenseInput.setDefaultValues(); - - $crossfader.morphCrossfade( $standardDiv ) - .promise().done( function () { - swapNodes( deed.authorInput.$element[ 0 ], deed.fakeAuthorInput.$element[ 0 ] ); - } ); - - this.licenseInputField.$element - .slideUp() - .animate( { opacity: 0 }, { queue: false, easing: 'linear' } ); - - $toggler.msg( 'mwe-upwiz-license-show-all' ); - }, - - customLicense: function () { - var deed = this, - $crossfader = this.$selector.find( '.mwe-upwiz-crossfader' ), - $customDiv = this.$selector.find( '.mwe-upwiz-custom' ), - $toggler = this.$selector.find( '.mwe-more-options a' ); - - $crossfader.morphCrossfade( $customDiv ) - .promise().done( function () { - swapNodes( deed.authorInput.$element[ 0 ], deed.fakeAuthorInput.$element[ 0 ] ); - } ); - - this.licenseInputField.$element - .slideDown() - .css( { opacity: 0 } ).animate( { opacity: 1 }, { queue: false, easing: 'linear' } ); - - $toggler.msg( 'mwe-upwiz-license-show-recommended' ); - }, - - /** - * @return {Object} - */ - getSerialized: function () { - var serialized = $.extend( mw.UploadWizardDeed.prototype.getSerialized.call( this ), { - author: this.authorInput.getValue() - } ); - - if ( this.showCustomDiv ) { - serialized.license = this.licenseInput.getSerialized(); - } - - return serialized; - }, - - /** - * @param {Object} serialized - */ - setSerialized: function ( serialized ) { - mw.UploadWizardDeed.prototype.setSerialized.call( this, serialized ); - - if ( serialized.author ) { - this.authorInput.setValue( serialized.author ); - } - - if ( this.showCustomDiv && serialized.license ) { - // only need to set license if it's not the default license - if ( !( this.getDefaultLicense() in serialized.license ) ) { - // expand licenses container - this.customLicense(); - this.licenseInput.setSerialized( serialized.license ); - } - } - } - } ); - }; - -}( mediaWiki, mediaWiki.uploadWizard, jQuery ) ); diff --git a/resources/mw.UploadWizardDeedThirdParty.js b/resources/mw.UploadWizardDeedThirdParty.js deleted file mode 100644 index 4ad98b6..0000000 --- a/resources/mw.UploadWizardDeedThirdParty.js +++ /dev/null @@ -1,206 +0,0 @@ -( function ( mw, uw, $ ) { - - // Runs through the third-party license groups and finds the - // relevant ID for that license. Probably really hacky. - // TODO do this properly once we build the license links properly - function findLicenseRecursively( config, license ) { - var val, - count = 0; - - $.each( config.licensing.thirdParty.licenseGroups, function ( i, licenseGroup ) { - $.each( licenseGroup.licenses, function ( j, licenseCandidate ) { - if ( licenseCandidate === license ) { - val = '2_' + count; - return false; - } - - count++; - } ); - - if ( val !== undefined ) { - return false; - } - } ); - - return val; - } - - /** - * Set up the form and deed object for the deed option that says these uploads are the work of a third party. - * - * @class mw.UploadWizardDeedThirdParty - * @constructor - * @param {number} uploadCount Integer count of uploads that this deed refers to (useful for message pluralization) - * @param {mw.Api} api API object - useful for doing previews - * @param {Object} config The UW config - */ - mw.UploadWizardDeedThirdParty = function ( uploadCount, api, config ) { - var deed = new mw.UploadWizardDeed(); - - deed.uploadCount = uploadCount || 1; - - deed.sourceInput = new OO.ui.MultilineTextInputWidget( { - autosize: true, - classes: [ 'mwe-source' ], - name: 'source' - } ); - deed.sourceInput.$input.attr( 'id', 'mwe-source-' + deed.getInstanceCount() ); - // See uw.DetailsWidget - deed.sourceInput.getErrors = function () { - var - errors = [], - minLength = config.minSourceLength, - maxLength = config.maxSourceLength, - text = this.getValue().trim(); - - if ( text === '' ) { - errors.push( mw.message( 'mwe-upwiz-error-blank' ) ); - } else if ( text.length < minLength ) { - errors.push( mw.message( 'mwe-upwiz-error-too-short', minLength ) ); - } else if ( text.length > maxLength ) { - errors.push( mw.message( 'mwe-upwiz-error-too-long', maxLength ) ); - } - - return $.Deferred().resolve( errors ).promise(); - }; - // See uw.DetailsWidget - deed.sourceInput.getWarnings = function () { - return $.Deferred().resolve( [] ).promise(); - }; - deed.sourceInputField = new uw.FieldLayout( deed.sourceInput, { - label: mw.message( 'mwe-upwiz-source' ).text(), - help: mw.message( 'mwe-upwiz-tooltip-source' ).text(), - required: true - } ); - - deed.authorInput = new OO.ui.MultilineTextInputWidget( { - autosize: true, - classes: [ 'mwe-author' ], - name: 'author' - } ); - deed.authorInput.$input.attr( 'id', 'mwe-author-' + deed.getInstanceCount() ); - // See uw.DetailsWidget - deed.authorInput.getErrors = function () { - var - errors = [], - minLength = config.minAuthorLength, - maxLength = config.maxAuthorLength, - text = this.getValue().trim(); - - if ( text === '' ) { - errors.push( mw.message( 'mwe-upwiz-error-blank' ) ); - } else if ( text.length < minLength ) { - errors.push( mw.message( 'mwe-upwiz-error-too-short', minLength ) ); - } else if ( text.length > maxLength ) { - errors.push( mw.message( 'mwe-upwiz-error-too-long', maxLength ) ); - } - - return $.Deferred().resolve( errors ).promise(); - }; - // See uw.DetailsWidget - deed.authorInput.getWarnings = function () { - return $.Deferred().resolve( [] ).promise(); - }; - deed.authorInputField = new uw.FieldLayout( deed.authorInput, { - label: mw.message( 'mwe-upwiz-author' ).text(), - help: mw.message( 'mwe-upwiz-tooltip-author' ).text(), - required: true - } ); - - deed.licenseInput = new mw.UploadWizardLicenseInput( - undefined, - config.licensing.thirdParty, - deed.uploadCount, - api - ); - deed.licenseInput.$element.addClass( 'mwe-upwiz-deed-license-groups' ); - deed.licenseInput.setDefaultValues(); - deed.licenseInputField = new uw.FieldLayout( deed.licenseInput, { - label: mw.message( 'mwe-upwiz-source-thirdparty-cases', deed.uploadCount ).text() - } ); - - return $.extend( deed, mw.UploadWizardDeed.prototype, { - name: 'thirdparty', - - setFormFields: function ( $selector ) { - var $defaultLicense, defaultLicense, defaultLicenseNum, defaultType, collapsible, - $formFields = $( '<div class="mwe-upwiz-deed-form-internal" />' ); - - this.$form = $( '<form>' ); - - defaultType = config.licensing.defaultType; - - if ( this.uploadCount > 1 ) { - $formFields.append( $( '<div>' ).msg( 'mwe-upwiz-source-thirdparty-custom-multiple-intro' ) ); - } - - $formFields.append( - $( '<div class="mwe-upwiz-source-thirdparty-custom-multiple-intro" />' ), - $( '<div class="mwe-upwiz-thirdparty-fields" />' ) - .append( this.sourceInputField.$element ), - $( '<div class="mwe-upwiz-thirdparty-fields" />' ) - .append( this.authorInputField.$element ), - $( '<div class="mwe-upwiz-thirdparty-license" />' ) - .append( this.licenseInputField.$element ) - ); - - this.$form.append( $formFields ); - - $selector.append( this.$form ); - - if ( defaultType === 'thirdparty' ) { - defaultLicense = config.licensing.thirdParty.defaults; - - defaultLicenseNum = findLicenseRecursively( config, defaultLicense ); - - if ( defaultLicenseNum ) { - $defaultLicense = $( '#license' + defaultLicenseNum ); - collapsible = $defaultLicense - .closest( '.mwe-upwiz-deed-license-group' ) - .data( 'mw-collapsible' ); - if ( collapsible ) { - collapsible.expand(); - } - $defaultLicense.prop( 'checked', true ); - } - } - }, - - /** - * @return {uw.FieldLayout[]} Fields that need validation - */ - getFields: function () { - return [ this.authorInputField, this.sourceInputField, this.licenseInputField ]; - }, - - /** - * @return {Object} - */ - getSerialized: function () { - return $.extend( mw.UploadWizardDeed.prototype.getSerialized.call( this ), { - source: this.sourceInput.getValue(), - author: this.authorInput.getValue(), - license: this.licenseInput.getSerialized() - } ); - }, - - /** - * @param {Object} serialized - */ - setSerialized: function ( serialized ) { - mw.UploadWizardDeed.prototype.setSerialized.call( this, serialized ); - - if ( serialized.source ) { - this.sourceInput.setValue( serialized.source ); - } - if ( serialized.author ) { - this.authorInput.setValue( serialized.author ); - } - if ( serialized.license ) { - this.licenseInput.setSerialized( serialized.license ); - } - } - } ); - }; - -}( mediaWiki, mediaWiki.uploadWizard, jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/385997 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I444a61586b03f3f5391394750267476a2e1830b1 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/UploadWizard Gerrit-Branch: master Gerrit-Owner: Matthias Mullie <mmul...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits