5 * mw.ForeignStructuredUpload.BookletLayout encapsulates the process
6 * of uploading a file to MediaWiki using the mw.ForeignStructuredUpload model.
8 * var uploadDialog = new mw.Upload.Dialog( {
9 * bookletClass: mw.ForeignStructuredUpload.BookletLayout,
14 * var windowManager = new OO.ui.WindowManager();
15 * $( 'body' ).append( windowManager.$element );
16 * windowManager.addWindows( [ uploadDialog ] );
18 * @class mw.ForeignStructuredUpload.BookletLayout
19 * @uses mw.ForeignStructuredUpload
20 * @extends mw.Upload.BookletLayout
21 * @cfg {string} [target] Used to choose the target repository.
22 * If nothing is passed, the {@link mw.ForeignUpload#property-target default} is used.
24 mw
.ForeignStructuredUpload
.BookletLayout = function ( config
) {
25 config
= config
|| {};
27 mw
.ForeignStructuredUpload
.BookletLayout
.parent
.call( this, config
);
29 this.target
= config
.target
;
34 OO
.inheritClass( mw
.ForeignStructuredUpload
.BookletLayout
, mw
.Upload
.BookletLayout
);
41 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.initialize = function () {
42 mw
.ForeignStructuredUpload
.BookletLayout
.parent
.prototype.initialize
.call( this );
43 // Point the CategorySelector to the right wiki as soon as we know what the right wiki is
44 this.upload
.apiPromise
.done( function ( api
) {
45 // If this is a ForeignApi, it will have a apiUrl, otherwise we don't need to do anything
47 // Can't reuse the same object, CategorySelector calls #abort on its mw.Api instance
48 this.categoriesWidget
.api
= new mw
.ForeignApi( api
.apiUrl
);
54 * Returns a {@link mw.ForeignStructuredUpload mw.ForeignStructuredUpload}
55 * with the {@link #cfg-target target} specified in config.
60 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.createUpload = function () {
61 return new mw
.ForeignStructuredUpload( this.target
);
69 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.renderUploadForm = function () {
70 var fieldset
, $ownWorkMessage
, $notOwnWorkMessage
,
71 ownWorkMessage
, notOwnWorkMessage
, notOwnWorkLocal
,
72 validTargets
= mw
.config
.get( 'wgForeignUploadTargets' ),
73 target
= this.target
|| validTargets
[ 0 ] || 'local',
76 // foreign-structured-upload-form-label-own-work-message-local
77 // foreign-structured-upload-form-label-own-work-message-shared
78 ownWorkMessage
= mw
.message( 'foreign-structured-upload-form-label-own-work-message-' + target
);
79 // foreign-structured-upload-form-label-not-own-work-message-local
80 // foreign-structured-upload-form-label-not-own-work-message-shared
81 notOwnWorkMessage
= mw
.message( 'foreign-structured-upload-form-label-not-own-work-message-' + target
);
82 // foreign-structured-upload-form-label-not-own-work-local-local
83 // foreign-structured-upload-form-label-not-own-work-local-shared
84 notOwnWorkLocal
= mw
.message( 'foreign-structured-upload-form-label-not-own-work-local-' + target
);
86 if ( !ownWorkMessage
.exists() ) {
87 ownWorkMessage
= mw
.message( 'foreign-structured-upload-form-label-own-work-message-default' );
89 if ( !notOwnWorkMessage
.exists() ) {
90 notOwnWorkMessage
= mw
.message( 'foreign-structured-upload-form-label-not-own-work-message-default' );
92 if ( !notOwnWorkLocal
.exists() ) {
93 notOwnWorkLocal
= mw
.message( 'foreign-structured-upload-form-label-not-own-work-local-default' );
96 $ownWorkMessage
= $( '<p>' ).html( ownWorkMessage
.parse() )
97 .addClass( 'mw-foreignStructuredUpload-bookletLayout-license' );
98 $notOwnWorkMessage
= $( '<div>' ).append(
99 $( '<p>' ).html( notOwnWorkMessage
.parse() ),
100 $( '<p>' ).html( notOwnWorkLocal
.parse() )
102 $ownWorkMessage
.add( $notOwnWorkMessage
).find( 'a' ).attr( 'target', '_blank' );
104 this.selectFileWidget
= new OO
.ui
.SelectFileWidget();
105 this.messageLabel
= new OO
.ui
.LabelWidget( {
106 label
: $notOwnWorkMessage
108 this.ownWorkCheckbox
= new OO
.ui
.CheckboxInputWidget().on( 'change', function ( on
) {
109 layout
.messageLabel
.toggle( !on
);
112 fieldset
= new OO
.ui
.FieldsetLayout();
114 new OO
.ui
.FieldLayout( this.selectFileWidget
, {
116 label
: mw
.msg( 'upload-form-label-select-file' )
118 new OO
.ui
.FieldLayout( this.ownWorkCheckbox
, {
120 label
: $( '<div>' ).append(
121 $( '<p>' ).text( mw
.msg( 'foreign-structured-upload-form-label-own-work' ) ),
125 new OO
.ui
.FieldLayout( this.messageLabel
, {
129 this.uploadForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
132 this.selectFileWidget
.on( 'change', this.onUploadFormChange
.bind( this ) );
133 this.ownWorkCheckbox
.on( 'change', this.onUploadFormChange
.bind( this ) );
135 return this.uploadForm
;
141 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.onUploadFormChange = function () {
142 var file
= this.selectFileWidget
.getValue(),
143 ownWork
= this.ownWorkCheckbox
.isSelected(),
144 valid
= !!file
&& ownWork
;
145 this.emit( 'uploadValid', valid
);
151 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.renderInfoForm = function () {
154 this.filenameWidget
= new OO
.ui
.TextInputWidget( {
158 this.descriptionWidget
= new OO
.ui
.TextInputWidget( {
164 this.dateWidget
= new mw
.widgets
.DateInputWidget( {
165 $overlay
: this.$overlay
,
167 mustBeBefore
: moment().add( 1, 'day' ).locale( 'en' ).format( 'YYYY-MM-DD' ) // Tomorrow
169 this.categoriesWidget
= new mw
.widgets
.CategorySelector( {
170 // Can't be done here because we don't know the target wiki yet... done in #initialize.
171 // api: new mw.ForeignApi( ... ),
172 $overlay
: this.$overlay
175 fieldset
= new OO
.ui
.FieldsetLayout( {
176 label
: mw
.msg( 'upload-form-label-infoform-title' )
179 new OO
.ui
.FieldLayout( this.filenameWidget
, {
180 label
: mw
.msg( 'upload-form-label-infoform-name' ),
183 new OO
.ui
.FieldLayout( this.descriptionWidget
, {
184 label
: mw
.msg( 'upload-form-label-infoform-description' ),
187 new OO
.ui
.FieldLayout( this.categoriesWidget
, {
188 label
: mw
.msg( 'foreign-structured-upload-form-label-infoform-categories' ),
191 new OO
.ui
.FieldLayout( this.dateWidget
, {
192 label
: mw
.msg( 'foreign-structured-upload-form-label-infoform-date' ),
196 this.infoForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
199 this.filenameWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
200 this.descriptionWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
201 this.dateWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
203 return this.infoForm
;
209 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.onInfoFormChange = function () {
212 this.filenameWidget
.getValidity(),
213 this.descriptionWidget
.getValidity(),
214 this.dateWidget
.getValidity()
215 ).done( function () {
216 layout
.emit( 'infoValid', true );
217 } ).fail( function () {
218 layout
.emit( 'infoValid', false );
227 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.getText = function () {
228 var language
= mw
.config
.get( 'wgContentLanguage' );
229 this.upload
.addDescription( language
, this.descriptionWidget
.getValue() );
230 this.upload
.setDate( this.dateWidget
.getValue() );
231 this.upload
.addCategories( this.categoriesWidget
.getItemsData() );
232 return this.upload
.getText();
240 mw
.ForeignStructuredUpload
.BookletLayout
.prototype.clear = function () {
241 mw
.ForeignStructuredUpload
.BookletLayout
.parent
.prototype.clear
.call( this );
243 this.ownWorkCheckbox
.setSelected( false );
244 this.categoriesWidget
.setItemsFromData( [] );
245 this.dateWidget
.setValue( '' ).setValidityFlag( true );
248 }( jQuery
, mediaWiki
) );