Merge "Fix Selenium tests"
[mediawiki.git] / resources / src / mediawiki / mediawiki.Upload.Dialog.js
blob9d9c0a61bf975f33d944a9537bacf9b2689c2d46
1 ( function ( $, mw ) {
3 /**
4 * mw.Upload.Dialog controls a {@link mw.Upload.BookletLayout BookletLayout}.
6 * ## Usage
8 * To use, setup a {@link OO.ui.WindowManager window manager} like for normal
9 * dialogs:
11 * var uploadDialog = new mw.Upload.Dialog();
12 * var windowManager = new OO.ui.WindowManager();
13 * $( 'body' ).append( windowManager.$element );
14 * windowManager.addWindows( [ uploadDialog ] );
15 * windowManager.openWindow( uploadDialog );
17 * The dialog's closing promise can be used to get details of the upload.
19 * If you want to use a different OO.ui.BookletLayout, for example the
20 * mw.ForeignStructuredUpload.BookletLayout, like in the case of of the upload
21 * interface in VisualEditor, you can pass it in the {@link #cfg-bookletClass}:
23 * var uploadDialog = new mw.Upload.Dialog( {
24 * bookletClass: mw.ForeignStructuredUpload.BookletLayout
25 * } );
28 * @class mw.Upload.Dialog
29 * @uses mw.Upload
30 * @uses mw.Upload.BookletLayout
31 * @extends OO.ui.ProcessDialog
33 * @constructor
34 * @param {Object} [config] Configuration options
35 * @cfg {Function} [bookletClass=mw.Upload.BookletLayout] Booklet class to be
36 * used for the steps
37 * @cfg {Object} [booklet] Booklet constructor configuration
39 mw.Upload.Dialog = function ( config ) {
40 // Config initialization
41 config = $.extend( {
42 bookletClass: mw.Upload.BookletLayout
43 }, config );
45 // Parent constructor
46 mw.Upload.Dialog.parent.call( this, config );
48 // Initialize
49 this.bookletClass = config.bookletClass;
50 this.bookletConfig = config.booklet;
53 /* Setup */
55 OO.inheritClass( mw.Upload.Dialog, OO.ui.ProcessDialog );
57 /* Static Properties */
59 /**
60 * @inheritdoc
61 * @property title
63 mw.Upload.Dialog.static.title = mw.msg( 'upload-dialog-title' );
65 /**
66 * @inheritdoc
67 * @property actions
69 mw.Upload.Dialog.static.actions = [
71 flags: 'safe',
72 action: 'cancel',
73 label: mw.msg( 'upload-dialog-button-cancel' ),
74 modes: [ 'upload', 'insert' ]
77 flags: 'safe',
78 action: 'cancelupload',
79 label: mw.msg( 'upload-dialog-button-back' ),
80 modes: [ 'info' ]
83 flags: [ 'primary', 'progressive' ],
84 label: mw.msg( 'upload-dialog-button-done' ),
85 action: 'insert',
86 modes: 'insert'
89 flags: [ 'primary', 'progressive' ],
90 label: mw.msg( 'upload-dialog-button-save' ),
91 action: 'save',
92 modes: 'info'
95 flags: [ 'primary', 'progressive' ],
96 label: mw.msg( 'upload-dialog-button-upload' ),
97 action: 'upload',
98 modes: 'upload'
102 /* Methods */
105 * @inheritdoc
107 mw.Upload.Dialog.prototype.initialize = function () {
108 // Parent method
109 mw.Upload.Dialog.parent.prototype.initialize.call( this );
111 this.uploadBooklet = this.createUploadBooklet();
112 this.uploadBooklet.connect( this, {
113 set: 'onUploadBookletSet',
114 uploadValid: 'onUploadValid',
115 infoValid: 'onInfoValid'
116 } );
118 this.$body.append( this.uploadBooklet.$element );
122 * Create an upload booklet
124 * @protected
125 * @return {mw.Upload.BookletLayout} An upload booklet
127 mw.Upload.Dialog.prototype.createUploadBooklet = function () {
128 // eslint-disable-next-line new-cap
129 return new this.bookletClass( $.extend( {
130 $overlay: this.$overlay
131 }, this.bookletConfig ) );
135 * @inheritdoc
137 mw.Upload.Dialog.prototype.getBodyHeight = function () {
138 return 600;
142 * Handle panelNameSet events from the upload booklet
144 * @protected
145 * @param {OO.ui.PageLayout} page Current page
147 mw.Upload.Dialog.prototype.onUploadBookletSet = function ( page ) {
148 this.actions.setMode( page.getName() );
149 this.actions.setAbilities( { upload: false, save: false } );
153 * Handle uploadValid events
155 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
156 * for the dialog accordingly.
158 * @protected
159 * @param {boolean} isValid The panel is complete and valid
161 mw.Upload.Dialog.prototype.onUploadValid = function ( isValid ) {
162 this.actions.setAbilities( { upload: isValid } );
166 * Handle infoValid events
168 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
169 * for the dialog accordingly.
171 * @protected
172 * @param {boolean} isValid The panel is complete and valid
174 mw.Upload.Dialog.prototype.onInfoValid = function ( isValid ) {
175 this.actions.setAbilities( { save: isValid } );
179 * @inheritdoc
181 mw.Upload.Dialog.prototype.getSetupProcess = function ( data ) {
182 return mw.Upload.Dialog.parent.prototype.getSetupProcess.call( this, data )
183 .next( function () {
184 return this.uploadBooklet.initialize();
185 }, this );
189 * @inheritdoc
191 mw.Upload.Dialog.prototype.getActionProcess = function ( action ) {
192 var dialog = this;
194 if ( action === 'upload' ) {
195 return new OO.ui.Process( this.uploadBooklet.uploadFile() );
197 if ( action === 'save' ) {
198 return new OO.ui.Process( this.uploadBooklet.saveFile() );
200 if ( action === 'insert' ) {
201 return new OO.ui.Process( function () {
202 dialog.close( dialog.upload );
203 } );
205 if ( action === 'cancel' ) {
206 return new OO.ui.Process( this.close() );
208 if ( action === 'cancelupload' ) {
209 return new OO.ui.Process( this.uploadBooklet.initialize() );
212 return mw.Upload.Dialog.parent.prototype.getActionProcess.call( this, action );
216 * @inheritdoc
218 mw.Upload.Dialog.prototype.getTeardownProcess = function ( data ) {
219 return mw.Upload.Dialog.parent.prototype.getTeardownProcess.call( this, data )
220 .next( function () {
221 this.uploadBooklet.clear();
222 }, this );
224 }( jQuery, mediaWiki ) );