All files / src/ui ve.ui.FileTransferHandler.js

77.77% Statements 21/27
50% Branches 2/4
57.14% Functions 4/7
77.77% Lines 21/27

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                                  1x   1x     1x   1x     1x 1x 1x         1x       1x                     1x             1x 1x 1x               1x                         1x 1x               1x             1x                         1x   1x     1x    
/*!
 * VisualEditor UserInterface data transfer handler class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Data transfer handler.
 *
 * @class
 * @extends ve.ui.DataTransferHandler
 * @abstract
 *
 * @constructor
 * @param {ve.ui.Surface} surface
 * @param {ve.ui.DataTransferItem} item
 */
ve.ui.FileTransferHandler = function VeUiFileTransferHandler() {
	// Parent constructor
	ve.ui.FileTransferHandler.super.apply( this, arguments );
 
	// Properties
	this.file = this.item.getAsFile();
 
	this.reader = new FileReader();
 
	// Events
	this.reader.addEventListener( 'progress', this.onFileProgress.bind( this ) );
	this.reader.addEventListener( 'load', this.onFileLoad.bind( this ) );
	this.reader.addEventListener( 'error', this.onFileError.bind( this ) );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.FileTransferHandler, ve.ui.DataTransferHandler );
 
/* Static properties */
 
ve.ui.FileTransferHandler.static.kinds = [ 'file' ];
 
/**
 * List of file extensions supported by this handler
 *
 * This is used as a fallback if no types were matched.
 *
 * @static
 * @property {string[]}
 * @inheritable
 */
ve.ui.FileTransferHandler.static.extensions = [];
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.FileTransferHandler.prototype.process = function () {
	this.createProgress( this.insertableDataDeferred.promise() );
	this.reader.readAsText( this.file );
};
 
/**
 * Handle progress events from the file reader
 *
 * @param {Event} e Progress event
 */
ve.ui.FileTransferHandler.prototype.onFileProgress = function ( e ) {
	if ( e.lengthComputable ) {
		this.setProgress( 100 * e.loaded / e.total );
	} else {
		this.setProgress( false );
	}
};
 
/**
 * Handle load events from the file reader
 *
 * @param {Event} e Load event
 */
ve.ui.FileTransferHandler.prototype.onFileLoad = function () {
	this.setProgress( 100 );
};
 
/**
 * Handle error events from the file reader
 *
 * @param {Event} e Error event
 */
ve.ui.FileTransferHandler.prototype.onFileError = function () {
	this.abort();
};
 
/**
 * @inheritdoc
 */
ve.ui.FileTransferHandler.prototype.abort = function () {
	// Parent method
	ve.ui.FileTransferHandler.super.prototype.abort.apply( this, arguments );
 
	this.reader.abort();
};
 
/**
 * Overrides the parent to make the default label the filename
 *
 * @param {jQuery.Promise} progressCompletePromise
 * @param {jQuery|string|Function} [label] Progress bar label, defaults to file name
 */
ve.ui.FileTransferHandler.prototype.createProgress = function ( progressCompletePromise, label ) {
	// Make the default label the filename
	label = label || this.file.name;
 
	// Parent method
	ve.ui.FileTransferHandler.super.prototype.createProgress.call( this, progressCompletePromise, label );
};