[search] Fix method call on null value
[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 * target: 'local'
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} [target] Used to choose the target repository.
22 * If nothing is passed, the {@link mw.ForeignUpload#property-target 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.target = config.target;
30 };
31
32 /* Setup */
33
34 OO.inheritClass( mw.ForeignStructuredUpload.BookletLayout, mw.Upload.BookletLayout );
35
36 /* Uploading */
37
38 /**
39 * @inheritdoc
40 */
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
46 if ( api.apiUrl ) {
47 // Can't reuse the same object, CategorySelector calls #abort on its mw.Api instance
48 this.categoriesWidget.api = new mw.ForeignApi( api.apiUrl );
49 }
50 }.bind( this ) );
51 };
52
53 /**
54 * Returns a {@link mw.ForeignStructuredUpload mw.ForeignStructuredUpload}
55 * with the {@link #cfg-target target} specified in config.
56 *
57 * @protected
58 * @return {mw.Upload}
59 */
60 mw.ForeignStructuredUpload.BookletLayout.prototype.createUpload = function () {
61 return new mw.ForeignStructuredUpload( this.target );
62 };
63
64 /* Form renderers */
65
66 /**
67 * @inheritdoc
68 */
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',
74 layout = this;
75
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 );
85
86 if ( !ownWorkMessage.exists() ) {
87 ownWorkMessage = mw.message( 'foreign-structured-upload-form-label-own-work-message-default' );
88 }
89 if ( !notOwnWorkMessage.exists() ) {
90 notOwnWorkMessage = mw.message( 'foreign-structured-upload-form-label-not-own-work-message-default' );
91 }
92 if ( !notOwnWorkLocal.exists() ) {
93 notOwnWorkLocal = mw.message( 'foreign-structured-upload-form-label-not-own-work-local-default' );
94 }
95
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() )
101 );
102 $ownWorkMessage.add( $notOwnWorkMessage ).find( 'a' ).attr( 'target', '_blank' );
103
104 this.selectFileWidget = new OO.ui.SelectFileWidget();
105 this.messageLabel = new OO.ui.LabelWidget( {
106 label: $notOwnWorkMessage
107 } );
108 this.ownWorkCheckbox = new OO.ui.CheckboxInputWidget().on( 'change', function ( on ) {
109 layout.messageLabel.toggle( !on );
110 } );
111
112 fieldset = new OO.ui.FieldsetLayout();
113 fieldset.addItems( [
114 new OO.ui.FieldLayout( this.selectFileWidget, {
115 align: 'top',
116 label: mw.msg( 'upload-form-label-select-file' )
117 } ),
118 new OO.ui.FieldLayout( this.ownWorkCheckbox, {
119 align: 'inline',
120 label: $( '<div>' ).append(
121 $( '<p>' ).text( mw.msg( 'foreign-structured-upload-form-label-own-work' ) ),
122 $ownWorkMessage
123 )
124 } ),
125 new OO.ui.FieldLayout( this.messageLabel, {
126 align: 'top'
127 } )
128 ] );
129 this.uploadForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
130
131 // Validation
132 this.selectFileWidget.on( 'change', this.onUploadFormChange.bind( this ) );
133 this.ownWorkCheckbox.on( 'change', this.onUploadFormChange.bind( this ) );
134
135 return this.uploadForm;
136 };
137
138 /**
139 * @inheritdoc
140 */
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 );
146 };
147
148 /**
149 * @inheritdoc
150 */
151 mw.ForeignStructuredUpload.BookletLayout.prototype.renderInfoForm = function () {
152 var fieldset;
153
154 this.filenameWidget = new OO.ui.TextInputWidget( {
155 required: true,
156 validate: /.+/
157 } );
158 this.descriptionWidget = new OO.ui.TextInputWidget( {
159 required: true,
160 validate: /.+/,
161 multiline: true,
162 autosize: true
163 } );
164 this.dateWidget = new mw.widgets.DateInputWidget( {
165 $overlay: this.$overlay,
166 required: true,
167 mustBeBefore: moment().add( 1, 'day' ).locale( 'en' ).format( 'YYYY-MM-DD' ) // Tomorrow
168 } );
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
173 } );
174
175 fieldset = new OO.ui.FieldsetLayout( {
176 label: mw.msg( 'upload-form-label-infoform-title' )
177 } );
178 fieldset.addItems( [
179 new OO.ui.FieldLayout( this.filenameWidget, {
180 label: mw.msg( 'upload-form-label-infoform-name' ),
181 align: 'top'
182 } ),
183 new OO.ui.FieldLayout( this.descriptionWidget, {
184 label: mw.msg( 'upload-form-label-infoform-description' ),
185 align: 'top'
186 } ),
187 new OO.ui.FieldLayout( this.categoriesWidget, {
188 label: mw.msg( 'foreign-structured-upload-form-label-infoform-categories' ),
189 align: 'top'
190 } ),
191 new OO.ui.FieldLayout( this.dateWidget, {
192 label: mw.msg( 'foreign-structured-upload-form-label-infoform-date' ),
193 align: 'top'
194 } )
195 ] );
196 this.infoForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
197
198 // Validation
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 ) );
202
203 return this.infoForm;
204 };
205
206 /**
207 * @inheritdoc
208 */
209 mw.ForeignStructuredUpload.BookletLayout.prototype.onInfoFormChange = function () {
210 var layout = this;
211 $.when(
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 );
219 } );
220 };
221
222 /* Getters */
223
224 /**
225 * @inheritdoc
226 */
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();
233 };
234
235 /* Setters */
236
237 /**
238 * @inheritdoc
239 */
240 mw.ForeignStructuredUpload.BookletLayout.prototype.clear = function () {
241 mw.ForeignStructuredUpload.BookletLayout.parent.prototype.clear.call( this );
242
243 this.ownWorkCheckbox.setSelected( false );
244 this.categoriesWidget.setItemsFromData( [] );
245 this.dateWidget.setValue( '' ).setValidityFlag( true );
246 };
247
248 }( jQuery, mediaWiki ) );