Merge "Use different varname for upgraded hash from original hash"
[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 // eslint-disable-next-line new-cap
126 return new this.bookletClass( $.extend( {
127 $overlay: this.$overlay
128 }, this.bookletConfig ) );
129 };
130
131 /**
132 * @inheritdoc
133 */
134 mw.Upload.Dialog.prototype.getBodyHeight = function () {
135 return 600;
136 };
137
138 /**
139 * Handle panelNameSet events from the upload booklet
140 *
141 * @protected
142 * @param {OO.ui.PageLayout} page Current page
143 */
144 mw.Upload.Dialog.prototype.onUploadBookletSet = function ( page ) {
145 this.actions.setMode( page.getName() );
146 this.actions.setAbilities( { upload: false, save: false } );
147 };
148
149 /**
150 * Handle uploadValid events
151 *
152 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
153 * for the dialog accordingly.
154 *
155 * @protected
156 * @param {boolean} isValid The panel is complete and valid
157 */
158 mw.Upload.Dialog.prototype.onUploadValid = function ( isValid ) {
159 this.actions.setAbilities( { upload: isValid } );
160 };
161
162 /**
163 * Handle infoValid events
164 *
165 * {@link OO.ui.ActionSet#setAbilities Sets abilities}
166 * for the dialog accordingly.
167 *
168 * @protected
169 * @param {boolean} isValid The panel is complete and valid
170 */
171 mw.Upload.Dialog.prototype.onInfoValid = function ( isValid ) {
172 this.actions.setAbilities( { save: isValid } );
173 };
174
175 /**
176 * @inheritdoc
177 */
178 mw.Upload.Dialog.prototype.getSetupProcess = function ( data ) {
179 return mw.Upload.Dialog.parent.prototype.getSetupProcess.call( this, data )
180 .next( function () {
181 return this.uploadBooklet.initialize();
182 }, this );
183 };
184
185 /**
186 * @inheritdoc
187 */
188 mw.Upload.Dialog.prototype.getActionProcess = function ( action ) {
189 var dialog = this;
190
191 if ( action === 'upload' ) {
192 return new OO.ui.Process( this.uploadBooklet.uploadFile() );
193 }
194 if ( action === 'save' ) {
195 return new OO.ui.Process( this.uploadBooklet.saveFile() );
196 }
197 if ( action === 'insert' ) {
198 return new OO.ui.Process( function () {
199 dialog.close( dialog.upload );
200 } );
201 }
202 if ( action === 'cancel' ) {
203 return new OO.ui.Process( this.close() );
204 }
205 if ( action === 'cancelupload' ) {
206 return new OO.ui.Process( this.uploadBooklet.initialize() );
207 }
208
209 return mw.Upload.Dialog.parent.prototype.getActionProcess.call( this, action );
210 };
211
212 /**
213 * @inheritdoc
214 */
215 mw.Upload.Dialog.prototype.getTeardownProcess = function ( data ) {
216 return mw.Upload.Dialog.parent.prototype.getTeardownProcess.call( this, data )
217 .next( function () {
218 this.uploadBooklet.clear();
219 }, this );
220 };
221 }( jQuery, mediaWiki ) );