Replace deprecated `constructive` with `progressive`
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.Upload.Dialog.js
1 ( function ( $, mw ) {
2
3 /**
4 * mw.Upload.Dialog controls a {@link mw.Upload.BookletLayout BookletLayout}.
5 *
6 * ## Usage
7 *
8 * To use, setup a {@link OO.ui.WindowManager window manager} like for normal
9 * dialogs:
10 *
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 );
16 *
17 * The dialog's closing promise can be used to get details of the upload.
18 *
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}:
22 *
23 * var uploadDialog = new mw.Upload.Dialog( {
24 * bookletClass: mw.ForeignStructuredUpload.BookletLayout
25 * } );
26 *
27 *
28 * @class mw.Upload.Dialog
29 * @uses mw.Upload
30 * @uses mw.Upload.BookletLayout
31 * @extends OO.ui.ProcessDialog
32 * @cfg {Function} [bookletClass=mw.Upload.BookletLayout] Booklet class to be
33 * used for the steps
34 * @cfg {Object} [booklet] Booklet constructor configuration
35 */
36 mw.Upload.Dialog = function ( config ) {
37 // Config initialization
38 config = $.extend( {
39 bookletClass: mw.Upload.BookletLayout
40 }, config );
41
42 // Parent constructor
43 mw.Upload.Dialog.parent.call( this, config );
44
45 // Initialize
46 this.bookletClass = config.bookletClass;
47 this.bookletConfig = config.booklet;
48 };
49
50 /* Setup */
51
52 OO.inheritClass( mw.Upload.Dialog, OO.ui.ProcessDialog );
53
54 /* Static Properties */
55
56 /**
57 * @inheritdoc
58 * @property title
59 */
60 /*jshint -W024*/
61 mw.Upload.Dialog.static.title = mw.msg( 'upload-dialog-title' );
62
63 /**
64 * @inheritdoc
65 * @property actions
66 */
67 mw.Upload.Dialog.static.actions = [
68 {
69 flags: 'safe',
70 action: 'cancel',
71 label: mw.msg( 'upload-dialog-button-cancel' ),
72 modes: [ 'upload', 'insert' ]
73 },
74 {
75 flags: 'safe',
76 action: 'cancelupload',
77 label: mw.msg( 'upload-dialog-button-back' ),
78 modes: [ 'info' ]
79 },
80 {
81 flags: [ 'primary', 'progressive' ],
82 label: mw.msg( 'upload-dialog-button-done' ),
83 action: 'insert',
84 modes: 'insert'
85 },
86 {
87 flags: [ 'primary', 'progressive' ],
88 label: mw.msg( 'upload-dialog-button-save' ),
89 action: 'save',
90 modes: 'info'
91 },
92 {
93 flags: [ 'primary', 'progressive' ],
94 label: mw.msg( 'upload-dialog-button-upload' ),
95 action: 'upload',
96 modes: 'upload'
97 }
98 ];
99
100 /*jshint +W024*/
101
102 /* Methods */
103
104 /**
105 * @inheritdoc
106 */
107 mw.Upload.Dialog.prototype.initialize = function () {
108 // Parent method
109 mw.Upload.Dialog.parent.prototype.initialize.call( this );
110
111 this.uploadBooklet = this.createUploadBooklet();
112 this.uploadBooklet.connect( this, {
113 set: 'onUploadBookletSet',
114 uploadValid: 'onUploadValid',
115 infoValid: 'onInfoValid'
116 } );
117
118 this.$body.append( this.uploadBooklet.$element );
119 };
120
121 /**
122 * Create an upload booklet
123 *
124 * @protected
125 * @return {mw.Upload.BookletLayout} An upload booklet
126 */
127 mw.Upload.Dialog.prototype.createUploadBooklet = function () {
128 return new this.bookletClass( $.extend( {
129 $overlay: this.$overlay
130 }, this.bookletConfig ) );
131 };
132
133 /**
134 * @inheritdoc
135 */
136 mw.Upload.Dialog.prototype.getBodyHeight = function () {
137 return 600;
138 };
139
140 /**
141 * Handle panelNameSet events from the upload booklet
142 *
143 * @protected
144 * @param {OO.ui.PageLayout} page Current page
145 */
146 mw.Upload.Dialog.prototype.onUploadBookletSet = function ( page ) {
147 this.actions.setMode( page.getName() );
148 this.actions.setAbilities( { upload: false, save: false } );
149 };
150
151 /**
152 * Handle uploadValid events
153 *
154 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
155 * for the dialog accordingly.
156 *
157 * @protected
158 * @param {boolean} isValid The panel is complete and valid
159 */
160 mw.Upload.Dialog.prototype.onUploadValid = function ( isValid ) {
161 this.actions.setAbilities( { upload: isValid } );
162 };
163
164 /**
165 * Handle infoValid events
166 *
167 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
168 * for the dialog accordingly.
169 *
170 * @protected
171 * @param {boolean} isValid The panel is complete and valid
172 */
173 mw.Upload.Dialog.prototype.onInfoValid = function ( isValid ) {
174 this.actions.setAbilities( { save: isValid } );
175 };
176
177 /**
178 * @inheritdoc
179 */
180 mw.Upload.Dialog.prototype.getSetupProcess = function ( data ) {
181 return mw.Upload.Dialog.parent.prototype.getSetupProcess.call( this, data )
182 .next( function () {
183 return this.uploadBooklet.initialize();
184 }, this );
185 };
186
187 /**
188 * @inheritdoc
189 */
190 mw.Upload.Dialog.prototype.getActionProcess = function ( action ) {
191 var dialog = this;
192
193 if ( action === 'upload' ) {
194 return new OO.ui.Process( this.uploadBooklet.uploadFile() );
195 }
196 if ( action === 'save' ) {
197 return new OO.ui.Process( this.uploadBooklet.saveFile() );
198 }
199 if ( action === 'insert' ) {
200 return new OO.ui.Process( function () {
201 dialog.close( dialog.upload );
202 } );
203 }
204 if ( action === 'cancel' ) {
205 return new OO.ui.Process( this.close() );
206 }
207 if ( action === 'cancelupload' ) {
208 return new OO.ui.Process( this.uploadBooklet.initialize() );
209 }
210
211 return mw.Upload.Dialog.parent.prototype.getActionProcess.call( this, action );
212 };
213
214 /**
215 * @inheritdoc
216 */
217 mw.Upload.Dialog.prototype.getTeardownProcess = function ( data ) {
218 return mw.Upload.Dialog.parent.prototype.getTeardownProcess.call( this, data )
219 .next( function () {
220 this.uploadBooklet.clear();
221 }, this );
222 };
223 }( jQuery, mediaWiki ) );