all files / src/ ve.TriggerListener.js

89.47% Statements 17/19
100% Branches 2/2
50% Functions 2/4
89.47% Lines 17/19
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                              286×     286× 286× 286×   286× 17612× 17612× 17612× 7956× 9663×   7956×                                               12×                      
/*!
 * VisualEditor UserInterface TriggerListener class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Trigger listener
 *
 * @class
 *
 * @constructor
 * @param {string[]} commands Commands to listen to triggers for
 * @param {ve.ui.CommandRegistry} commandRegistry Command registry to get commands from
 */
ve.TriggerListener = function VeTriggerListener( commands, commandRegistry ) {
	var i, j, command, triggers;
 
	// Properties
	this.commands = commands;
	this.commandsByTrigger = {};
	this.triggers = {};
 
	for ( i = this.commands.length - 1; i >= 0; i-- ) {
		command = this.commands[ i ];
		triggers = ve.ui.triggerRegistry.lookup( command );
		if ( triggers ) {
			for ( j = triggers.length - 1; j >= 0; j-- ) {
				this.commandsByTrigger[ triggers[ j ].toString() ] = commandRegistry.lookup( command );
			}
			this.triggers[ command ] = triggers;
		}
	}
};
 
/* Inheritance */
 
OO.initClass( ve.TriggerListener );
 
/* Methods */
 
/**
 * Get list of commands.
 *
 * @return {string[]} Commands
 */
ve.TriggerListener.prototype.getCommands = function () {
	return this.commands;
};
 
/**
 * Get command associated with trigger string.
 *
 * @param {string} trigger Trigger string
 * @return {ve.ui.Command|undefined} Command
 */
ve.TriggerListener.prototype.getCommandByTrigger = function ( trigger ) {
	return this.commandsByTrigger[ trigger ];
};
 
/**
 * Get triggers for a specified name.
 *
 * @param {string} name Trigger name
 * @return {ve.ui.Trigger[]|undefined} Triggers
 */
ve.TriggerListener.prototype.getTriggers = function ( name ) {
	return this.triggers[ name ];
};