Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / resources / src / mediawiki / mediawiki.Upload.Dialog.js
blob9d9c0a61bf975f33d944a9537bacf9b2689c2d46
1 ( function ( $, mw ) {
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          *
33          * @constructor
34          * @param {Object} [config] Configuration options
35          * @cfg {Function} [bookletClass=mw.Upload.BookletLayout] Booklet class to be
36          *     used for the steps
37          * @cfg {Object} [booklet] Booklet constructor configuration
38          */
39         mw.Upload.Dialog = function ( config ) {
40                 // Config initialization
41                 config = $.extend( {
42                         bookletClass: mw.Upload.BookletLayout
43                 }, config );
45                 // Parent constructor
46                 mw.Upload.Dialog.parent.call( this, config );
48                 // Initialize
49                 this.bookletClass = config.bookletClass;
50                 this.bookletConfig = config.booklet;
51         };
53         /* Setup */
55         OO.inheritClass( mw.Upload.Dialog, OO.ui.ProcessDialog );
57         /* Static Properties */
59         /**
60          * @inheritdoc
61          * @property title
62          */
63         mw.Upload.Dialog.static.title = mw.msg( 'upload-dialog-title' );
65         /**
66          * @inheritdoc
67          * @property actions
68          */
69         mw.Upload.Dialog.static.actions = [
70                 {
71                         flags: 'safe',
72                         action: 'cancel',
73                         label: mw.msg( 'upload-dialog-button-cancel' ),
74                         modes: [ 'upload', 'insert' ]
75                 },
76                 {
77                         flags: 'safe',
78                         action: 'cancelupload',
79                         label: mw.msg( 'upload-dialog-button-back' ),
80                         modes: [ 'info' ]
81                 },
82                 {
83                         flags: [ 'primary', 'progressive' ],
84                         label: mw.msg( 'upload-dialog-button-done' ),
85                         action: 'insert',
86                         modes: 'insert'
87                 },
88                 {
89                         flags: [ 'primary', 'progressive' ],
90                         label: mw.msg( 'upload-dialog-button-save' ),
91                         action: 'save',
92                         modes: 'info'
93                 },
94                 {
95                         flags: [ 'primary', 'progressive' ],
96                         label: mw.msg( 'upload-dialog-button-upload' ),
97                         action: 'upload',
98                         modes: 'upload'
99                 }
100         ];
102         /* Methods */
104         /**
105          * @inheritdoc
106          */
107         mw.Upload.Dialog.prototype.initialize = function () {
108                 // Parent method
109                 mw.Upload.Dialog.parent.prototype.initialize.call( this );
111                 this.uploadBooklet = this.createUploadBooklet();
112                 this.uploadBooklet.connect( this, {
113                         set: 'onUploadBookletSet',
114                         uploadValid: 'onUploadValid',
115                         infoValid: 'onInfoValid'
116                 } );
118                 this.$body.append( this.uploadBooklet.$element );
119         };
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                 // eslint-disable-next-line new-cap
129                 return new this.bookletClass( $.extend( {
130                         $overlay: this.$overlay
131                 }, this.bookletConfig ) );
132         };
134         /**
135          * @inheritdoc
136          */
137         mw.Upload.Dialog.prototype.getBodyHeight = function () {
138                 return 600;
139         };
141         /**
142          * Handle panelNameSet events from the upload booklet
143          *
144          * @protected
145          * @param {OO.ui.PageLayout} page Current page
146          */
147         mw.Upload.Dialog.prototype.onUploadBookletSet = function ( page ) {
148                 this.actions.setMode( page.getName() );
149                 this.actions.setAbilities( { upload: false, save: false } );
150         };
152         /**
153          * Handle uploadValid events
154          *
155          * {@link OO.ui.ActionSet#setAbilities Sets abilities}
156          * for the dialog accordingly.
157          *
158          * @protected
159          * @param {boolean} isValid The panel is complete and valid
160          */
161         mw.Upload.Dialog.prototype.onUploadValid = function ( isValid ) {
162                 this.actions.setAbilities( { upload: isValid } );
163         };
165         /**
166          * Handle infoValid events
167          *
168          * {@link OO.ui.ActionSet#setAbilities Sets abilities}
169          * for the dialog accordingly.
170          *
171          * @protected
172          * @param {boolean} isValid The panel is complete and valid
173          */
174         mw.Upload.Dialog.prototype.onInfoValid = function ( isValid ) {
175                 this.actions.setAbilities( { save: isValid } );
176         };
178         /**
179          * @inheritdoc
180          */
181         mw.Upload.Dialog.prototype.getSetupProcess = function ( data ) {
182                 return mw.Upload.Dialog.parent.prototype.getSetupProcess.call( this, data )
183                         .next( function () {
184                                 return this.uploadBooklet.initialize();
185                         }, this );
186         };
188         /**
189          * @inheritdoc
190          */
191         mw.Upload.Dialog.prototype.getActionProcess = function ( action ) {
192                 var dialog = this;
194                 if ( action === 'upload' ) {
195                         return new OO.ui.Process( this.uploadBooklet.uploadFile() );
196                 }
197                 if ( action === 'save' ) {
198                         return new OO.ui.Process( this.uploadBooklet.saveFile() );
199                 }
200                 if ( action === 'insert' ) {
201                         return new OO.ui.Process( function () {
202                                 dialog.close( dialog.upload );
203                         } );
204                 }
205                 if ( action === 'cancel' ) {
206                         return new OO.ui.Process( this.close() );
207                 }
208                 if ( action === 'cancelupload' ) {
209                         return new OO.ui.Process( this.uploadBooklet.initialize() );
210                 }
212                 return mw.Upload.Dialog.parent.prototype.getActionProcess.call( this, action );
213         };
215         /**
216          * @inheritdoc
217          */
218         mw.Upload.Dialog.prototype.getTeardownProcess = function ( data ) {
219                 return mw.Upload.Dialog.parent.prototype.getTeardownProcess.call( this, data )
220                         .next( function () {
221                                 this.uploadBooklet.clear();
222                         }, this );
223         };
224 }( jQuery, mediaWiki ) );