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