All files / src/ui/datatransferhandlers ve.ui.DSVFileTransferHandler.js

96.87% Statements 31/32
90.9% Branches 10/11
100% Functions 2/2
96.66% Lines 29/30

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                                1x   1x         1x       1x   1x   1x             1x 1x 1x   1x       1x 1x 1x   1x   1x         1x 3x     3x 1x     2x 2x 4x       4x 4x         2x     1x         1x       1x         1x  
/*!
 * VisualEditor UserInterface delimiter-separated values file transfer handler class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Delimiter-separated values file transfer handler.
 *
 * @class
 * @extends ve.ui.FileTransferHandler
 *
 * @constructor
 * @param {ve.ui.Surface} surface
 * @param {ve.ui.DataTransferItem} item
 */
ve.ui.DSVFileTransferHandler = function VeUiDSVFileTransferHandler() {
	// Parent constructor
	ve.ui.DSVFileTransferHandler.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.DSVFileTransferHandler, ve.ui.FileTransferHandler );
 
/* Static properties */
 
ve.ui.DSVFileTransferHandler.static.name = 'dsv';
 
ve.ui.DSVFileTransferHandler.static.types = [ 'text/csv', 'text/tab-separated-values' ];
 
ve.ui.DSVFileTransferHandler.static.extensions = [ 'csv', 'tsv' ];
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.DSVFileTransferHandler.prototype.onFileLoad = function () {
	const data = [];
	const input = Papa.parse( this.reader.result );
 
	Iif ( input.meta.aborted || ( input.data.length <= 0 ) ) {
		this.abort();
	} else {
		// Lookup the type for table elements
		const tableNodeName = ve.dm.modelRegistry.matchElement( document.createElement( 'table' ) );
		const tableNodeClass = ve.dm.modelRegistry.lookup( tableNodeName );
		const tableElement = { type: tableNodeName };
		// Sanitize, as this can add default attributes for the table type
		tableNodeClass.static.sanitize( tableElement );
 
		data.push(
			tableElement,
			{ type: 'tableSection', attributes: { style: 'body' } }
		);
 
		for ( let i = 0; i < input.data.length; i++ ) {
			const line = input.data[ i ];
 
			// Skip 'empty' row if at the end of the file
			if ( i === input.data.length - 1 && line.length === 1 && line[ 0 ] === '' ) {
				continue;
			}
 
			data.push( { type: 'tableRow' } );
			for ( let j = 0; j < line.length; j++ ) {
				data.push(
					{ type: 'tableCell', attributes: { style: ( i === 0 ? 'header' : 'data' ) } },
					{ type: 'paragraph', internal: { generated: 'wrapper' } }
				);
				ve.batchPush( data, line[ j ].split( '' ) );
				data.push(
					{ type: '/paragraph' },
					{ type: '/tableCell' }
				);
			}
			data.push( { type: '/tableRow' } );
		}
 
		data.push(
			{ type: '/tableSection' },
			{ type: '/' + tableElement.type }
		);
 
		this.resolve( data );
	}
 
	// Parent method
	ve.ui.DSVFileTransferHandler.super.prototype.onFileLoad.apply( this, arguments );
};
 
/* Registration */
 
ve.ui.dataTransferHandlerFactory.register( ve.ui.DSVFileTransferHandler );