Rollback: Change @since 1.27 to @since 1.28
[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', 'info' ]
73 },
74 {
75 flags: [ 'primary', 'progressive' ],
76 label: mw.msg( 'upload-dialog-button-done' ),
77 action: 'insert',
78 modes: 'insert'
79 },
80 {
81 flags: [ 'primary', 'constructive' ],
82 label: mw.msg( 'upload-dialog-button-save' ),
83 action: 'save',
84 modes: 'info'
85 },
86 {
87 flags: [ 'primary', 'progressive' ],
88 label: mw.msg( 'upload-dialog-button-upload' ),
89 action: 'upload',
90 modes: 'upload'
91 }
92 ];
93
94 /*jshint +W024*/
95
96 /* Methods */
97
98 /**
99 * @inheritdoc
100 */
101 mw.Upload.Dialog.prototype.initialize = function () {
102 // Parent method
103 mw.Upload.Dialog.parent.prototype.initialize.call( this );
104
105 this.uploadBooklet = this.createUploadBooklet();
106 this.uploadBooklet.connect( this, {
107 set: 'onUploadBookletSet',
108 uploadValid: 'onUploadValid',
109 infoValid: 'onInfoValid'
110 } );
111
112 this.$body.append( this.uploadBooklet.$element );
113 };
114
115 /**
116 * Create an upload booklet
117 *
118 * @protected
119 * @return {mw.Upload.BookletLayout} An upload booklet
120 */
121 mw.Upload.Dialog.prototype.createUploadBooklet = function () {
122 return new this.bookletClass( $.extend( {
123 $overlay: this.$overlay
124 }, this.bookletConfig ) );
125 };
126
127 /**
128 * @inheritdoc
129 */
130 mw.Upload.Dialog.prototype.getBodyHeight = function () {
131 return 600;
132 };
133
134 /**
135 * Handle panelNameSet events from the upload booklet
136 *
137 * @protected
138 * @param {OO.ui.PageLayout} page Current page
139 */
140 mw.Upload.Dialog.prototype.onUploadBookletSet = function ( page ) {
141 this.actions.setMode( page.getName() );
142 this.actions.setAbilities( { upload: false, save: false } );
143 };
144
145 /**
146 * Handle uploadValid events
147 *
148 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
149 * for the dialog accordingly.
150 *
151 * @protected
152 * @param {boolean} isValid The panel is complete and valid
153 */
154 mw.Upload.Dialog.prototype.onUploadValid = function ( isValid ) {
155 this.actions.setAbilities( { upload: isValid } );
156 };
157
158 /**
159 * Handle infoValid events
160 *
161 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
162 * for the dialog accordingly.
163 *
164 * @protected
165 * @param {boolean} isValid The panel is complete and valid
166 */
167 mw.Upload.Dialog.prototype.onInfoValid = function ( isValid ) {
168 this.actions.setAbilities( { save: isValid } );
169 };
170
171 /**
172 * @inheritdoc
173 */
174 mw.Upload.Dialog.prototype.getSetupProcess = function ( data ) {
175 return mw.Upload.Dialog.parent.prototype.getSetupProcess.call( this, data )
176 .next( function () {
177 return this.uploadBooklet.initialize();
178 }, this );
179 };
180
181 /**
182 * @inheritdoc
183 */
184 mw.Upload.Dialog.prototype.getActionProcess = function ( action ) {
185 var dialog = this;
186
187 if ( action === 'upload' ) {
188 return new OO.ui.Process( this.uploadBooklet.uploadFile() );
189 }
190 if ( action === 'save' ) {
191 return new OO.ui.Process( this.uploadBooklet.saveFile() );
192 }
193 if ( action === 'insert' ) {
194 return new OO.ui.Process( function () {
195 dialog.close( dialog.upload );
196 } );
197 }
198 if ( action === 'cancel' ) {
199 return new OO.ui.Process( this.close() );
200 }
201
202 return mw.Upload.Dialog.parent.prototype.getActionProcess.call( this, action );
203 };
204
205 /**
206 * @inheritdoc
207 */
208 mw.Upload.Dialog.prototype.getTeardownProcess = function ( data ) {
209 return mw.Upload.Dialog.parent.prototype.getTeardownProcess.call( this, data )
210 .next( function () {
211 this.uploadBooklet.clear();
212 }, this );
213 };
214 }( jQuery, mediaWiki ) );