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

96.66% Statements 58/60
90.62% Branches 29/32
100% Functions 9/9
96.55% Lines 56/58

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                          1x   2x     2x   2x   2x         1x                                         1x   23x   23x                     1x   11x   11x                 1x   48x 23x   48x       30x 3x   30x         22x 22x         34x 34x 34x   34x 6x 10x 10x           28x 28x 52x 30x         22x         34x 5x 8x 8x                               1x           447x 455x       321x   134x   126x     149x                 149x 166x 166x   166x 6x     160x 106x     54x 41x     13x       136x         1x  
/*!
 * VisualEditor DataTransferHandlerFactory class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Data transfer handler factory.
 *
 * @class
 * @extends OO.Factory
 * @constructor
 */
ve.ui.DataTransferHandlerFactory = function VeUiDataTransferHandlerFactory() {
	// Parent constructor
	ve.ui.DataTransferHandlerFactory.super.apply( this, arguments );
 
	// Handlers which match all kinds and a specific type
	this.handlerNamesByType = {};
	// Handlers which match a specific kind and type
	this.handlerNamesByKindAndType = {};
	// Handlers which match a specific file extension as a fallback
	this.handlerNamesByExtension = {};
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.DataTransferHandlerFactory, OO.Factory );
 
/* Methods */
 
/**
 * Register a constructor with the factory.
 *
 *     function MyClass() {};
 *     OO.initClass( MyClass );
 *     MyClass.static.name = 'hello';
 *     // Register class with the factory, available via the symbolic name "hello"
 *     factory.register( MyClass );
 *
 * See https://doc.wikimedia.org/oojs/master/OO.Factory.html
 *
 * @param {Function} constructor Constructor to use when creating object
 * @param {string} [name] Symbolic name to use for #create().
 *  This parameter may be omitted in favour of letting the constructor decide
 *  its own name, through `constructor.static.name`.
 * @throws {Error} If a parameter is invalid
 */
ve.ui.DataTransferHandlerFactory.prototype.register = function ( constructor ) {
	// Parent method
	ve.ui.DataTransferHandlerFactory.super.prototype.register.apply( this, arguments );
 
	this.updateIndexes( constructor, true );
};
 
/**
 * Unregister a constructor from the factory.
 *
 * See https://doc.wikimedia.org/oojs/master/OO.Factory.html
 *
 * @param {string|Function} constructor Constructor function or symbolic name to unregister
 * @throws {Error} If a parameter is invalid
 */
ve.ui.DataTransferHandlerFactory.prototype.unregister = function ( constructor ) {
	// Parent method
	ve.ui.DataTransferHandlerFactory.super.prototype.unregister.apply( this, arguments );
 
	this.updateIndexes( constructor, false );
};
 
/**
 * Update indexes used for handler loopup
 *
 * @param {Function} constructor Handler's constructor to insert/remove
 * @param {boolean} insert Insert the handler into the indexes, remove otherwise
 */
ve.ui.DataTransferHandlerFactory.prototype.updateIndexes = function ( constructor, insert ) {
	function ensureArray( obj, prop ) {
		if ( obj[ prop ] === undefined ) {
			obj[ prop ] = [];
		}
		return obj[ prop ];
	}
 
	function ensureMap( obj, prop ) {
		if ( obj[ prop ] === undefined ) {
			obj[ prop ] = {};
		}
		return obj[ prop ];
	}
 
	function remove( arr, item ) {
		var index;
		Eif ( ( index = arr.indexOf( item ) ) !== -1 ) {
			arr.splice( index, 1 );
		}
	}
 
	var i, j, ilen, jlen,
		kinds = constructor.static.kinds,
		types = constructor.static.types,
		extensions = constructor.static.extensions;
 
	if ( !kinds ) {
		for ( j = 0, jlen = types.length; j < jlen; j++ ) {
			if ( insert ) {
				ensureArray( this.handlerNamesByType, types[ j ] ).unshift( constructor.static.name );
			} else E{
				remove( this.handlerNamesByType[ types[ j ] ], constructor.static.name );
			}
		}
	} else {
		for ( i = 0, ilen = kinds.length; i < ilen; i++ ) {
			for ( j = 0, jlen = types.length; j < jlen; j++ ) {
				if ( insert ) {
					ensureArray(
						ensureMap( this.handlerNamesByKindAndType, kinds[ i ] ),
						types[ j ]
					).unshift( constructor.static.name );
				} else {
					remove( this.handlerNamesByKindAndType[ kinds[ i ] ][ types[ j ] ], constructor.static.name );
				}
			}
		}
	}
	if ( constructor.prototype instanceof ve.ui.FileTransferHandler ) {
		for ( i = 0, ilen = extensions.length; i < ilen; i++ ) {
			if ( insert ) {
				ensureArray( this.handlerNamesByExtension, extensions[ i ] ).unshift( constructor.static.name );
			} else E{
				remove( this.handlerNamesByExtension[ extensions[ i ] ], constructor.static.name );
			}
		}
	}
};
 
/**
 * Get a handler name for a specific data transfer item
 *
 * @param {ve.ui.DataTransferItem} item Data transfer item
 * @param {boolean} isPaste Handler being used for paste
 * @param {boolean} isPasteSpecial Handler being used for "paste special"
 * @return {string|undefined} Handler name, or undefined if not found
 */
ve.ui.DataTransferHandlerFactory.prototype.getHandlerNameForItem = function ( item, isPaste, isPasteSpecial ) {
	// Fetch a given nested property, returning a zero-length array if
	// any component of the path is not present.
	// This is similar to ve.getProp, except with a `hasOwnProperty`
	// test to ensure we aren't fooled by __proto__ and friends.
	function fetch( obj /* , argsā€¦ */ ) {
		for ( var j = 1; j < arguments.length; j++ ) {
			if (
				typeof arguments[ j ] !== 'string' ||
				!Object.prototype.hasOwnProperty.call( obj, arguments[ j ] )
			) {
				return [];
			}
			obj = obj[ arguments[ j ] ];
		}
		return obj;
	}
 
	var names = [].concat(
		// 1. Match by kind + type (e.g. 'file' + 'text/html')
		fetch( this.handlerNamesByKindAndType, item.kind, item.type ),
		// 2. Match by just type (e.g. 'image/jpeg')
		fetch( this.handlerNamesByType, item.type ),
		// 3. Match by file extension (e.g. 'csv')
		fetch( this.handlerNamesByExtension, item.getExtension() )
	);
 
	for ( var i = 0; i < names.length; i++ ) {
		var name = names[ i ];
		var constructor = this.registry[ name ];
 
		if ( isPasteSpecial && !constructor.static.handlesPasteSpecial ) {
			continue;
		}
 
		if ( isPaste && !constructor.static.handlesPaste ) {
			continue;
		}
 
		if ( constructor.static.matchFunction && !constructor.static.matchFunction( item ) ) {
			continue;
		}
 
		return name;
	}
 
	// No matching handler
	return;
};
 
/* Initialization */
 
ve.ui.dataTransferHandlerFactory = new ve.ui.DataTransferHandlerFactory();