All files / src/ce ve.ce.KeyDownHandlerFactory.js

100% Statements 33/33
91.66% Branches 11/12
100% Functions 4/4
100% Lines 29/29

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                          1x   1x     1x         1x                                         1x   7x   7x 7x     7x 24x 24x 24x                       1x 429x 429x     429x 309x 309x 309x 168x       429x                       1x 429x 429x     429x 168x 157x       429x         1x  
/*!
 * VisualEditor KeyDownHandlerFactory class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Key down handler factory.
 *
 * @class
 * @extends OO.Factory
 * @constructor
 */
ve.ce.KeyDownHandlerFactory = function VeCeKeyDownHandlerFactory() {
	// Parent constructor
	ve.ce.KeyDownHandlerFactory.super.apply( this, arguments );
 
	// Handlers which match all kinds and a specific type
	this.handlerNamesByKeys = {};
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.KeyDownHandlerFactory, 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.ce.KeyDownHandlerFactory.prototype.register = function ( constructor ) {
	// Parent method
	ve.ce.KeyDownHandlerFactory.super.prototype.register.call( this, constructor );
 
	var keys = constructor.static.keys;
	var name = constructor.static.name;
 
	// TODO: Clean up handlerNamesByKeys in unregister
	for ( var i = 0, ilen = keys.length; i < ilen; i++ ) {
		this.handlerNamesByKeys[ keys[ i ] ] = this.handlerNamesByKeys[ keys[ i ] ] || [];
		Eif ( this.handlerNamesByKeys[ keys[ i ] ].indexOf( name ) === -1 ) {
			this.handlerNamesByKeys[ keys[ i ] ].push( name );
		}
	}
};
 
/**
 * Get the handler for a specific key
 *
 * @param {number} key Key code
 * @param {string} selectionName Selection type name
 * @return {Function[]} Matched handlers
 */
ve.ce.KeyDownHandlerFactory.prototype.lookupHandlersForKey = function ( key, selectionName ) {
	var constructors = [],
		names = this.handlerNamesByKeys[ key ] || [];
 
	// Length is likely to be 1 or 0 so don't cache
	for ( var i = 0; i < names.length; i++ ) {
		var constructor = this.registry[ names[ i ] ];
		var supportedSelections = constructor.static.supportedSelections;
		if ( !supportedSelections || supportedSelections.indexOf( selectionName ) !== -1 ) {
			constructors.push( constructor );
		}
	}
 
	return constructors;
};
 
/**
 * Execute the handlers for a specific key
 *
 * @param {number} key Key code
 * @param {string} selectionName Selection type name
 * @param {ve.ce.Surface} surface
 * @param {jQuery.Event} e Key down event
 * @return {boolean} Some handlers acted
 */
ve.ce.KeyDownHandlerFactory.prototype.executeHandlersForKey = function ( key, selectionName, surface, e ) {
	var acted = false,
		handlers = this.lookupHandlersForKey( key, selectionName );
 
	// Length is likely to be 1 or 0 so don't cache
	for ( var i = 0; i < handlers.length; i++ ) {
		if ( handlers[ i ].static.execute( surface, e ) ) {
			acted = true;
		}
	}
 
	return acted;
};
 
/* Initialization */
 
ve.ce.keyDownHandlerFactory = new ve.ce.KeyDownHandlerFactory();