-/*global moment */
+/* global moment, Uint8Array */
( function ( $, mw ) {
/**
var booklet = this;
return mw.ForeignStructuredUpload.BookletLayout.parent.prototype.initialize.call( this ).then(
function () {
- // Point the CategorySelector to the right wiki
- return booklet.upload.getApi().then(
- function ( api ) {
+ return $.when(
+ // Point the CategorySelector to the right wiki
+ booklet.upload.getApi().then( function ( api ) {
// If this is a ForeignApi, it will have a apiUrl, otherwise we don't need to do anything
if ( api.apiUrl ) {
// Can't reuse the same object, CategorySelector calls #abort on its mw.Api instance
booklet.categoriesWidget.api = new mw.ForeignApi( api.apiUrl );
}
return $.Deferred().resolve();
- },
- function () {
- return $.Deferred().resolve();
- }
+ } ),
+ // Set up booklet fields and license messages to match configuration
+ booklet.upload.loadConfig().then( function ( config ) {
+ var
+ msgPromise,
+ isLocal = booklet.upload.target === 'local',
+ fields = config.fields,
+ msgs = config.licensemessages[ isLocal ? 'local' : 'foreign' ];
+
+ // Hide disabled fields
+ booklet.descriptionField.toggle( !!fields.description );
+ booklet.categoriesField.toggle( !!fields.categories );
+ booklet.dateField.toggle( !!fields.date );
+ // Update form validity
+ booklet.onInfoFormChange();
+
+ // Load license messages from the remote wiki if we don't have these messages locally
+ // (this means that we only load messages from the foreign wiki for custom config)
+ if ( mw.message( 'upload-form-label-own-work-message-' + msgs ).exists() ) {
+ msgPromise = $.Deferred().resolve();
+ } else {
+ msgPromise = booklet.upload.apiPromise.then( function ( api ) {
+ return api.loadMessages( [
+ 'upload-form-label-own-work-message-' + msgs,
+ 'upload-form-label-not-own-work-message-' + msgs,
+ 'upload-form-label-not-own-work-local-' + msgs
+ ] );
+ } );
+ }
+
+ // Update license messages
+ return msgPromise.then( function () {
+ var $labels;
+ booklet.$ownWorkMessage.msg( 'upload-form-label-own-work-message-' + msgs );
+ booklet.$notOwnWorkMessage.msg( 'upload-form-label-not-own-work-message-' + msgs );
+ booklet.$notOwnWorkLocal.msg( 'upload-form-label-not-own-work-local-' + msgs );
+
+ $labels = $( [
+ booklet.$ownWorkMessage[ 0 ],
+ booklet.$notOwnWorkMessage[ 0 ],
+ booklet.$notOwnWorkLocal[ 0 ]
+ ] );
+
+ // Improve the behavior of links inside these labels, which may point to important
+ // things like licensing requirements or terms of use
+ $labels.find( 'a' )
+ .attr( 'target', '_blank' )
+ .on( 'click', function ( e ) {
+ // OO.ui.FieldLayout#onLabelClick is trying to prevent default on all clicks,
+ // which causes the links to not be openable. Don't let it do that.
+ e.stopPropagation();
+ } );
+ } );
+ } )
);
- },
- function () {
- return $.Deferred().resolve();
}
+ ).then(
+ null,
+ // Always resolve, never reject
+ function () { return $.Deferred().resolve(); }
);
};
* @inheritdoc
*/
mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm = function () {
- var fieldset, $ownWorkMessage, $notOwnWorkMessage,
- ownWorkMessage, notOwnWorkMessage, notOwnWorkLocal,
- validTargets = mw.config.get( 'wgForeignUploadTargets' ),
- target = this.target || validTargets[ 0 ] || 'local',
+ var fieldset,
layout = this;
- // upload-form-label-own-work-message-local
- // upload-form-label-own-work-message-shared
- ownWorkMessage = mw.message( 'upload-form-label-own-work-message-' + target );
- // upload-form-label-not-own-work-message-local
- // upload-form-label-not-own-work-message-shared
- notOwnWorkMessage = mw.message( 'upload-form-label-not-own-work-message-' + target );
- // upload-form-label-not-own-work-local-local
- // upload-form-label-not-own-work-local-shared
- notOwnWorkLocal = mw.message( 'upload-form-label-not-own-work-local-' + target );
-
- if ( !ownWorkMessage.exists() ) {
- ownWorkMessage = mw.message( 'upload-form-label-own-work-message-default' );
- }
- if ( !notOwnWorkMessage.exists() ) {
- notOwnWorkMessage = mw.message( 'upload-form-label-not-own-work-message-default' );
- }
- if ( !notOwnWorkLocal.exists() ) {
- notOwnWorkLocal = mw.message( 'upload-form-label-not-own-work-local-default' );
- }
-
- $ownWorkMessage = $( '<p>' ).append( ownWorkMessage.parseDom() )
+ // These elements are filled with text in #initialize
+ // TODO Refactor this to be in one place
+ this.$ownWorkMessage = $( '<p>' )
.addClass( 'mw-foreignStructuredUpload-bookletLayout-license' );
- $notOwnWorkMessage = $( '<div>' ).append(
- $( '<p>' ).append( notOwnWorkMessage.parseDom() ),
- $( '<p>' ).append( notOwnWorkLocal.parseDom() )
- );
- $ownWorkMessage.add( $notOwnWorkMessage ).find( 'a' ).attr( 'target', '_blank' );
+ this.$notOwnWorkMessage = $( '<p>' );
+ this.$notOwnWorkLocal = $( '<p>' );
this.selectFileWidget = new OO.ui.SelectFileWidget( {
showDropTarget: true
} );
this.messageLabel = new OO.ui.LabelWidget( {
- label: $notOwnWorkMessage
+ label: $( '<div>' ).append(
+ this.$notOwnWorkMessage,
+ this.$notOwnWorkLocal
+ )
} );
this.ownWorkCheckbox = new OO.ui.CheckboxInputWidget().on( 'change', function ( on ) {
layout.messageLabel.toggle( !on );
align: 'inline',
label: $( '<div>' ).append(
$( '<p>' ).text( mw.msg( 'upload-form-label-own-work' ) ),
- $ownWorkMessage
+ this.$ownWorkMessage
)
} ),
new OO.ui.FieldLayout( this.messageLabel, {
mustBeBefore: moment().add( 1, 'day' ).locale( 'en' ).format( 'YYYY-MM-DD' ) // Tomorrow
} );
+ this.filenameField = new OO.ui.FieldLayout( this.filenameWidget, {
+ label: mw.msg( 'upload-form-label-infoform-name' ),
+ align: 'top',
+ classes: [ 'mw-foreignStructuredUploa-bookletLayout-small-notice' ],
+ notices: [ mw.msg( 'upload-form-label-infoform-name-tooltip' ) ]
+ } );
+ this.descriptionField = new OO.ui.FieldLayout( this.descriptionWidget, {
+ label: mw.msg( 'upload-form-label-infoform-description' ),
+ align: 'top',
+ classes: [ 'mw-foreignStructuredUploa-bookletLayout-small-notice' ],
+ notices: [ mw.msg( 'upload-form-label-infoform-description-tooltip' ) ]
+ } );
+ this.categoriesField = new OO.ui.FieldLayout( this.categoriesWidget, {
+ label: mw.msg( 'upload-form-label-infoform-categories' ),
+ align: 'top'
+ } );
+ this.dateField = new OO.ui.FieldLayout( this.dateWidget, {
+ label: mw.msg( 'upload-form-label-infoform-date' ),
+ align: 'top'
+ } );
+
fieldset = new OO.ui.FieldsetLayout( {
label: mw.msg( 'upload-form-label-infoform-title' )
} );
fieldset.addItems( [
- new OO.ui.FieldLayout( this.filenameWidget, {
- label: mw.msg( 'upload-form-label-infoform-name' ),
- align: 'top',
- classes: [ 'mw-foreignStructuredUploa-bookletLayout-small-notice' ],
- notices: [ mw.msg( 'upload-form-label-infoform-name-tooltip' ) ]
- } ),
- new OO.ui.FieldLayout( this.descriptionWidget, {
- label: mw.msg( 'upload-form-label-infoform-description' ),
- align: 'top',
- classes: [ 'mw-foreignStructuredUploa-bookletLayout-small-notice' ],
- notices: [ mw.msg( 'upload-form-label-infoform-description-tooltip' ) ]
- } ),
- new OO.ui.FieldLayout( this.categoriesWidget, {
- label: mw.msg( 'upload-form-label-infoform-categories' ),
- align: 'top'
- } ),
- new OO.ui.FieldLayout( this.dateWidget, {
- label: mw.msg( 'upload-form-label-infoform-date' ),
- align: 'top'
- } )
+ this.filenameField,
+ this.descriptionField,
+ this.categoriesField,
+ this.dateField
] );
this.infoForm = new OO.ui.FormLayout( {
classes: [ 'mw-upload-bookletLayout-infoForm' ],
* @inheritdoc
*/
mw.ForeignStructuredUpload.BookletLayout.prototype.onInfoFormChange = function () {
- var layout = this;
- $.when(
- this.filenameWidget.getValidity(),
- this.descriptionWidget.getValidity(),
- this.dateWidget.getValidity()
- ).done( function () {
+ var layout = this,
+ validityPromises = [];
+
+ validityPromises.push( this.filenameWidget.getValidity() );
+ if ( this.descriptionField.isVisible() ) {
+ validityPromises.push( this.descriptionWidget.getValidity() );
+ }
+ if ( this.dateField.isVisible() ) {
+ validityPromises.push( this.dateWidget.getValidity() );
+ }
+
+ $.when.apply( $, validityPromises ).done( function () {
layout.emit( 'infoValid', true );
} ).fail( function () {
layout.emit( 'infoValid', false );
} );
};
+ /**
+ * @param {mw.Title} filename
+ * @return {jQuery.Promise} Resolves (on success) or rejects with OO.ui.Error
+ */
+ mw.ForeignStructuredUpload.BookletLayout.prototype.validateFilename = function ( filename ) {
+ return ( new mw.Api() ).get( {
+ action: 'query',
+ prop: 'info',
+ titles: filename.getPrefixedDb(),
+ formatversion: 2
+ } ).then(
+ function ( result ) {
+ // if the file already exists, reject right away, before
+ // ever firing finishStashUpload()
+ if ( !result.query.pages[ 0 ].missing ) {
+ return $.Deferred().reject( new OO.ui.Error(
+ $( '<p>' ).msg( 'fileexists', filename.getPrefixedDb() ),
+ { recoverable: false }
+ ) );
+ }
+ },
+ function () {
+ // API call failed - this could be a connection hiccup...
+ // Let's just ignore this validation step and turn this
+ // failure into a successful resolve ;)
+ return $.Deferred().resolve();
+ }
+ );
+ };
+
+ /**
+ * @inheritdoc
+ */
+ mw.ForeignStructuredUpload.BookletLayout.prototype.saveFile = function () {
+ var title = mw.Title.newFromText(
+ this.getFilename(),
+ mw.config.get( 'wgNamespaceIds' ).file
+ );
+
+ return this.uploadPromise
+ .then( this.validateFilename.bind( this, title ) )
+ .then( mw.ForeignStructuredUpload.BookletLayout.parent.prototype.saveFile.bind( this ) );
+ };
+
/* Getters */
/**
if ( file && file.type === 'image/jpeg' ) {
fileReader = new FileReader();
fileReader.onload = function () {
+ // TODO: fileStr is never used.
var fileStr, arr, i, metadata;
if ( typeof fileReader.result === 'string' ) {
}
try {
- metadata = mw.libs.jpegmeta( this.result, file.name );
+ metadata = mw.libs.jpegmeta( fileStr, file.name );
} catch ( e ) {
metadata = null;
}