Merge "mediawiki.page.watch.ajax: Add dependency on mediawiki.page.startup"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignStructuredUpload.BookletLayout.js
1 /*global moment */
2 ( function ( $, mw ) {
3
4 /**
5 * mw.ForeignStructuredUpload.BookletLayout encapsulates the process
6 * of uploading a file to MediaWiki using the mw.ForeignStructuredUpload model.
7 *
8 * var uploadDialog = new mw.Upload.Dialog( {
9 * bookletClass: mw.ForeignStructuredUpload.BookletLayout,
10 * booklet: {
11 * targetHost: 'localhost:8080'
12 * }
13 * } );
14 * var windowManager = new OO.ui.WindowManager();
15 * $( 'body' ).append( windowManager.$element );
16 * windowManager.addWindows( [ uploadDialog ] );
17 *
18 * @class mw.ForeignStructuredUpload.BookletLayout
19 * @uses mw.ForeignStructuredUpload
20 * @extends mw.Upload.BookletLayout
21 * @cfg {string} [targetHost] Used to set up the target wiki.
22 * If nothing is passed, the {@link mw.ForeignUpload#property-targetHost default} is used.
23 */
24 mw.ForeignStructuredUpload.BookletLayout = function ( config ) {
25 config = config || {};
26 // Parent constructor
27 mw.ForeignStructuredUpload.BookletLayout.parent.call( this, config );
28
29 this.targetHost = config.targetHost;
30 };
31
32 /* Setup */
33
34 OO.inheritClass( mw.ForeignStructuredUpload.BookletLayout, mw.Upload.BookletLayout );
35
36 /* Uploading */
37
38 /**
39 * Returns a {@link mw.ForeignStructuredUpload mw.ForeignStructuredUpload}
40 * with the {@link #cfg-targetHost targetHost} specified in config.
41 *
42 * @protected
43 * @return {mw.Upload}
44 */
45 mw.ForeignStructuredUpload.BookletLayout.prototype.createUpload = function () {
46 return new mw.ForeignStructuredUpload( this.targetHost );
47 };
48
49 /* Form renderers */
50
51 /**
52 * @inheritdoc
53 */
54 mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm = function () {
55 var fieldset,
56 target = mw.config.get( 'wgRemoteUploadTarget' ),
57 $ownWorkMessage = $( '<p>' ).html(
58 mw.message( 'foreign-structured-upload-form-label-own-work-message-' + target ).parse()
59 ),
60 $notOwnWorkMessage = $( '<div>' ).append(
61 $( '<p>' ).html(
62 mw.message( 'foreign-structured-upload-form-label-not-own-work-message-' + target ).parse()
63 ),
64 $( '<p>' ).html(
65 mw.message( 'foreign-structured-upload-form-label-not-own-work-local-' + target ).parse()
66 )
67 ),
68 layout = this;
69
70 this.selectFileWidget = new OO.ui.SelectFileWidget();
71 this.messageLabel = new OO.ui.LabelWidget( {
72 label: $notOwnWorkMessage
73 } );
74 this.ownWorkCheckbox = new OO.ui.CheckboxInputWidget().on( 'change', function ( on ) {
75 if ( on ) {
76 layout.messageLabel.setLabel( $ownWorkMessage );
77 } else {
78 layout.messageLabel.setLabel( $notOwnWorkMessage );
79 }
80 } );
81
82 fieldset = new OO.ui.FieldsetLayout();
83 fieldset.addItems( [
84 new OO.ui.FieldLayout( this.selectFileWidget, {
85 align: 'top',
86 label: mw.msg( 'upload-form-label-select-file' )
87 } ),
88 new OO.ui.FieldLayout( this.ownWorkCheckbox, {
89 align: 'inline',
90 label: mw.msg( 'foreign-structured-upload-form-label-own-work' )
91 } ),
92 this.messageLabel
93 ] );
94 this.uploadForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
95
96 // Validation
97 this.selectFileWidget.on( 'change', this.onUploadFormChange.bind( this ) );
98 this.ownWorkCheckbox.on( 'change', this.onUploadFormChange.bind( this ) );
99
100 return this.uploadForm;
101 };
102
103 /**
104 * @inheritdoc
105 */
106 mw.ForeignStructuredUpload.BookletLayout.prototype.onUploadFormChange = function () {
107 var file = this.selectFileWidget.getValue(),
108 ownWork = this.ownWorkCheckbox.isSelected(),
109 valid = !!file && ownWork;
110 this.emit( 'uploadValid', valid );
111 };
112
113 /**
114 * @inheritdoc
115 */
116 mw.ForeignStructuredUpload.BookletLayout.prototype.renderInfoForm = function () {
117 var fieldset;
118
119 this.filenameWidget = new OO.ui.TextInputWidget( {
120 required: true,
121 validate: /.+/
122 } );
123 this.descriptionWidget = new OO.ui.TextInputWidget( {
124 required: true,
125 validate: /.+/,
126 multiline: true,
127 autosize: true
128 } );
129 this.dateWidget = new mw.widgets.DateInputWidget( {
130 $overlay: this.$overlay,
131 required: true,
132 mustBeBefore: moment().add( 1, 'day' ).locale( 'en' ).format( 'YYYY-MM-DD' ) // Tomorrow
133 } );
134 this.categoriesWidget = new mw.widgets.CategorySelector( {
135 $overlay: this.$overlay
136 } );
137
138 fieldset = new OO.ui.FieldsetLayout( {
139 label: mw.msg( 'upload-form-label-infoform-title' )
140 } );
141 fieldset.addItems( [
142 new OO.ui.FieldLayout( this.filenameWidget, {
143 label: mw.msg( 'upload-form-label-infoform-name' ),
144 align: 'top'
145 } ),
146 new OO.ui.FieldLayout( this.categoriesWidget, {
147 label: mw.msg( 'foreign-structured-upload-form-label-infoform-categories' ),
148 align: 'top'
149 } ),
150 new OO.ui.FieldLayout( this.descriptionWidget, {
151 label: mw.msg( 'upload-form-label-infoform-description' ),
152 align: 'top'
153 } ),
154 new OO.ui.FieldLayout( this.dateWidget, {
155 label: mw.msg( 'foreign-structured-upload-form-label-infoform-date' ),
156 align: 'top'
157 } )
158 ] );
159 this.infoForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
160
161 // Validation
162 this.filenameWidget.on( 'change', this.onInfoFormChange.bind( this ) );
163 this.descriptionWidget.on( 'change', this.onInfoFormChange.bind( this ) );
164 this.dateWidget.on( 'change', this.onInfoFormChange.bind( this ) );
165
166 return this.infoForm;
167 };
168
169 /**
170 * @inheritdoc
171 */
172 mw.ForeignStructuredUpload.BookletLayout.prototype.onInfoFormChange = function () {
173 var layout = this;
174 $.when(
175 this.filenameWidget.getValidity(),
176 this.descriptionWidget.getValidity(),
177 this.dateWidget.getValidity()
178 ).done( function () {
179 layout.emit( 'infoValid', true );
180 } ).fail( function () {
181 layout.emit( 'infoValid', false );
182 } );
183 };
184
185 /* Getters */
186
187 /**
188 * @inheritdoc
189 */
190 mw.ForeignStructuredUpload.BookletLayout.prototype.getText = function () {
191 this.upload.addDescription( 'en', this.descriptionWidget.getValue() );
192 this.upload.setDate( this.dateWidget.getValue() );
193 this.upload.addCategories( this.categoriesWidget.getItemsData() );
194 return this.upload.getText();
195 };
196 }( jQuery, mediaWiki ) );