Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/dialogs/ve.ui.ProgressDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface ProgressDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Dialog for showing operations in progress.
9 *
10 * @class
11 * @extends OO.ui.MessageDialog
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.ProgressDialog = function VeUiProgressDialog( config ) {
17 // Parent constructor
18 ve.ui.ProgressDialog.super.call( this, config );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.ProgressDialog, OO.ui.MessageDialog );
24
25 /* Static Properties */
26
27 ve.ui.ProgressDialog.static.name = 'progress';
28
29 ve.ui.ProgressDialog.static.size = 'medium';
30
31 ve.ui.ProgressDialog.static.actions = [
32 {
33 action: 'cancel',
34 label: OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' ),
35 flags: 'destructive',
36 modes: 'cancellable'
37 }
38 ];
39
40 // Individual progress items can be cancellable, but the whole
41 // dialog should not be escapable.
42 ve.ui.ProgressDialog.static.escapable = false;
43
44 /* Methods */
45
46 /**
47 * @inheritdoc
48 */
49 ve.ui.ProgressDialog.prototype.initialize = function () {
50 // Parent method
51 ve.ui.ProgressDialog.super.prototype.initialize.call( this );
52
53 // Properties
54 this.inProgress = 0;
55 this.cancelDeferreds = [];
56 };
57
58 /**
59 * @inheritdoc
60 */
61 ve.ui.ProgressDialog.prototype.getSetupProcess = function ( data ) {
62 data = data || {};
63
64 // Parent method
65 return ve.ui.ProgressDialog.super.prototype.getSetupProcess.call( this, data )
66 .next( () => {
67 const progresses = data.progresses;
68
69 let cancellable = false;
70 this.inProgress = progresses.length;
71 this.text.$element.empty();
72 this.cancelDeferreds = [];
73
74 for ( let i = 0, l = progresses.length; i < l; i++ ) {
75 const cancelDeferred = ve.createDeferred();
76 const $row = $( '<div>' ).addClass( 've-ui-progressDialog-row' );
77 const progressBar = new OO.ui.ProgressBarWidget();
78 const fieldLayout = new OO.ui.FieldLayout(
79 progressBar,
80 {
81 label: progresses[ i ].label,
82 align: 'top'
83 }
84 );
85
86 $row.append( fieldLayout.$element );
87
88 if ( progresses[ i ].cancellable ) {
89 const cancelButton = new OO.ui.ButtonWidget( {
90 framed: false,
91 icon: 'cancel',
92 title: OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' )
93 } ).on( 'click', cancelDeferred.reject.bind( cancelDeferred ) );
94 $row.append( cancelButton.$element );
95 cancellable = true;
96 }
97
98 this.text.$element.append( $row );
99 progresses[ i ].progressBarDeferred.resolve( progressBar, cancelDeferred.promise() );
100 progresses[ i ].progressCompletePromise.then(
101 this.progressComplete.bind( this, $row, false ),
102 this.progressComplete.bind( this, $row, true )
103 );
104 this.cancelDeferreds.push( cancelDeferred );
105 }
106 this.actions.setMode( cancellable ? 'cancellable' : 'default' );
107 } );
108 };
109
110 /**
111 * @inheritdoc
112 */
113 ve.ui.ProgressDialog.prototype.getActionProcess = function ( action ) {
114 return new OO.ui.Process( () => {
115 if ( action === 'cancel' ) {
116 for ( let i = 0, l = this.cancelDeferreds.length; i < l; i++ ) {
117 this.cancelDeferreds[ i ].reject();
118 }
119 }
120 this.close( { action: action } );
121 } );
122 };
123
124 /**
125 * Progress has completed for an item
126 *
127 * @param {jQuery} $row Row containing progress bar which has completed
128 * @param {boolean} failed The item failed
129 */
130 ve.ui.ProgressDialog.prototype.progressComplete = function ( $row, failed ) {
131 this.inProgress--;
132 if ( !this.inProgress ) {
133 this.close();
134 }
135 if ( failed ) {
136 $row.remove();
137 this.updateSize();
138 }
139 };
140
141 /* Static methods */
142
143 /* Registration */
144
145 ve.ui.windowFactory.register( ve.ui.ProgressDialog );