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