all files / src/ui/dialogs/ ve.ui.CommandHelpDialog.js

16.46% Statements 13/79
0% Branches 0/30
0% Functions 0/5
16.46% Lines 13/79
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/*!
 * VisualEditor UserInterface CommandHelpDialog class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Dialog for listing all command keyboard shortcuts.
 *
 * @class
 * @extends OO.ui.ProcessDialog
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.CommandHelpDialog = function VeUiCommandHelpDialog( config ) {
	// Parent constructor
	ve.ui.CommandHelpDialog.super.call( this, config );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.CommandHelpDialog, OO.ui.ProcessDialog );
 
/* Static Properties */
 
ve.ui.CommandHelpDialog.static.name = 'commandHelp';
 
ve.ui.CommandHelpDialog.static.size = 'larger';
 
ve.ui.CommandHelpDialog.static.title =
	OO.ui.deferMsg( 'visualeditor-dialog-command-help-title' );
 
ve.ui.CommandHelpDialog.static.actions = [
	{
		label: OO.ui.deferMsg( 'visualeditor-dialog-action-done' ),
		flags: 'safe'
	}
];
 
ve.ui.CommandHelpDialog.static.commandGroups = {
	textStyle: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-text-style' ),
		promote: [ 'bold', 'italic', 'link' ],
		demote: [ 'clear' ]
	},
	clipboard: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-clipboard' )
	},
	formatting: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-formatting' ),
		promote: [ 'paragraph', 'pre', 'blockquote' ]
	},
	history: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-history' ),
		promote: [ 'undo', 'redo' ]
	},
	dialog: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-dialog' )
	},
	other: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-other' ),
		promote: [ 'findAndReplace', 'findNext', 'findPrevious' ],
		demote: [ 'commandHelp' ]
	},
	insert: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-insert' )
	}
};
 
ve.ui.CommandHelpDialog.static.commandGroupsOrder = [
	'textStyle', 'clipboard',
	'formatting', 'history',
	'dialog', 'other', 'insert'
];
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.CommandHelpDialog.prototype.getBodyHeight = function () {
	return Math.round( this.contentLayout.$element[ 0 ].scrollHeight );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommandHelpDialog.prototype.initialize = function () {
	var i, iLen, j, jLen, k, kLen, triggerList, commands, shortcut,
		$list, $shortcut, groupName, commandGroup, commandGroups, commandGroupsOrder, sequence, hasCommand, hasShortcut,
		surface = ve.init.target.getSurface(),
		sequenceRegistry = surface.sequenceRegistry,
		commandRegistry = surface.commandRegistry;
 
	// Parent method
	ve.ui.CommandHelpDialog.super.prototype.initialize.call( this );
 
	commandGroups = this.constructor.static.commandGroups;
	commandGroupsOrder = this.constructor.static.commandGroupsOrder;
 
	this.contentLayout = new OO.ui.PanelLayout( {
		scrollable: true,
		padded: true,
		expanded: false
	} );
	this.$container = $( '<div>' ).addClass( 've-ui-commandHelpDialog-container' );
 
	for ( i = 0, iLen = commandGroupsOrder.length; i < iLen; i++ ) {
		hasCommand = false;
		groupName = commandGroupsOrder[ i ];
		commandGroup = commandGroups[ groupName ];
		commands = this.constructor.static.sortedCommandsFromGroup( groupName, commandGroup.promote, commandGroup.demote );
		$list = $( '<dl>' ).addClass( 've-ui-commandHelpDialog-list' );
		for ( j = 0, jLen = commands.length; j < jLen; j++ ) {
			if ( commands[ j ].trigger ) {
				if ( !commands[ j ].ignoreCommand && !commandRegistry.lookup( commands[ j ].trigger ) ) {
					// Trigger is specified by unavailable command
					continue;
				}
				triggerList = ve.ui.triggerRegistry.lookup( commands[ j ].trigger );
			} else {
				triggerList = [];
				if ( commands[ j ].shortcuts ) {
					for ( k = 0, kLen = commands[ j ].shortcuts.length; k < kLen; k++ ) {
						shortcut = commands[ j ].shortcuts[ k ];
						triggerList.push(
							new ve.ui.Trigger( shortcut, true )
						);
					}
				}
			}
 
			hasShortcut = false;
 
			$shortcut = $( '<dt>' );
			for ( k = 0, kLen = triggerList.length; k < kLen; k++ ) {
				$shortcut.append( $( '<kbd>' ).append(
					triggerList[ k ].getMessage( true ).map( this.constructor.static.buildKeyNode )
				).find( 'kbd + kbd' ).before( '+' ).end() );
				hasShortcut = true;
			}
			if ( commands[ j ].sequences ) {
				for ( k = 0, kLen = commands[ j ].sequences.length; k < kLen; k++ ) {
					sequence = sequenceRegistry.lookup( commands[ j ].sequences[ k ] );
					if ( sequence ) {
						$shortcut.append( $( '<kbd>' ).addClass( 've-ui-commandHelpDialog-sequence' )
							.attr( 'data-label', ve.msg( 'visualeditor-shortcuts-sequence-notice' ) )
							.append(
								sequence.getMessage( true ).map( this.constructor.static.buildKeyNode )
							)
						);
						hasShortcut = true;
					}
				}
			}
			if ( hasShortcut ) {
				$list.append(
					$shortcut,
					$( '<dd>' ).text( OO.ui.resolveMsg( commands[ j ].label ) )
				);
				hasCommand = true;
			}
		}
		if ( hasCommand ) {
			this.$container.append(
				$( '<div>' )
					.addClass( 've-ui-commandHelpDialog-section' )
					.append(
						$( '<h3>' ).text( OO.ui.resolveMsg( commandGroup.title ) ),
						$list
					)
			);
		}
	}
 
	this.contentLayout.$element.append( this.$container );
	this.$body.append( this.contentLayout.$element );
};
 
/* Static methods */
 
/**
 * Wrap a key (as provided by a Trigger) in a node, for display
 *
 * @static
 * @param {string} key Key to display
 * @return {jQuery} A kbd wrapping the key text
 */
ve.ui.CommandHelpDialog.static.buildKeyNode = function ( key ) {
	var $key = $( '<kbd>' );
	if ( key === ' ' ) {
		// Might need to expand this if other keys show up, but currently things like
		// the tab-character only come from Triggers and are pre-localized there into
		// "tab" anyway.
		key = ( new ve.ui.Trigger( 'space' ) ).getMessage();
		$key.addClass( 've-ui-commandHelpDialog-specialKey' );
	}
	return $key.text( key );
};
 
/**
 * Extract a properly sorted list of commands from a command-group
 *
 * @static
 * @param {string} groupName The dialog-category in which to display this
 * @param {string[]} [promote] Commands which should be displayed first
 * @param {string[]} [demote] Commands which should be displayed last
 * @return {Object[]} List of commands in order
 */
ve.ui.CommandHelpDialog.static.sortedCommandsFromGroup = function ( groupName, promote, demote ) {
	var i,
		commands = ve.ui.commandHelpRegistry.lookupByGroup( groupName ),
		keys = Object.keys( commands ),
		used = {},
		auto = [],
		promoted = [],
		demoted = [];
	keys.sort();
	if ( promote ) {
		for ( i = 0; i < promote.length; i++ ) {
			if ( !commands[ promote[ i ] ] ) {
				continue;
			}
			promoted.push( commands[ promote[ i ] ] );
			used[ promote[ i ] ] = true;
		}
	}
	if ( demote ) {
		for ( i = 0; i < demote.length; i++ ) {
			if ( used[ demote[ i ] ] || !commands[ demote[ i ] ] ) {
				continue;
			}
			demoted.push( commands[ demote[ i ] ] );
			used[ demote[ i ] ] = true;
		}
	}
	for ( i = 0; i < keys.length; i++ ) {
		if ( used[ keys[ i ] ] ) {
			continue;
		}
		auto.push( commands[ keys[ i ] ] );
	}
	return promoted.concat( auto, demoted );
};
 
/* Registration */
 
ve.ui.windowFactory.register( ve.ui.CommandHelpDialog );