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