All files / src/ui/dialogs ve.ui.ProgressDialog.js

21.15% Statements 11/52
0% Branches 0/12
0% Functions 0/7
22.91% Lines 11/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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