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

80% Statements 28/35
60% Branches 6/10
50% Functions 5/10
80% Lines 28/35

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                                  1x   24x     24x 24x 24x     24x         24x                   24x         1x                                 1x   1x                     1x                     1x                     1x                       1x 408x               1x 24x               1x                 1x 28x 24x 24x       28x               1x 50x                 1x                   1x      
/*!
 * VisualEditor UserInterface ContextItem class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Item in a context.
 *
 * @class
 * @extends OO.ui.Widget
 *
 * @constructor
 * @param {ve.ui.LinearContext} context Context the item is in
 * @param {ve.dm.Model} [model] Model the item is related to
 * @param {Object} [config] Configuration options
 */
ve.ui.ContextItem = function VeUiContextItem( context, model, config ) {
	// Parent constructor
	ve.ui.ContextItem.super.call( this, config );
 
	// Properties
	this.context = context;
	this.model = model;
	this.fragment = null;
 
	// Events
	this.$element.on( 'mousedown', function () {
		// Deactivate so context is not automatically closed
		// by null selection
		context.getSurface().getView().deactivate();
	} );
	this.$element.on( 'keydown', function ( e ) {
		// Pressing escape while focus is in the context should
		// return focus to the surface
		if ( e.keyCode === OO.ui.Keys.ESCAPE && context.getSurface().getView().isDeactivated() ) {
			context.getSurface().getView().activate();
			return false;
		}
	} );
 
	// Initialization
	this.$element.addClass( 've-ui-contextItem' );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.ContextItem, OO.ui.Widget );
 
/* Events */
 
/**
 * @event command
 */
 
/* Static Properties */
 
/**
 * Whether this item exclusively handles any model class
 *
 * @static
 * @property {boolean}
 * @inheritable
 */
ve.ui.ContextItem.static.exclusive = true;
 
ve.ui.ContextItem.static.commandName = null;
 
/**
 * Sort order of the context item within the context
 *
 * Items are sorted top to bottom in ascending order. Negative values are allowed.
 *
 * @static
 * @property {number}
 * @inheritable
 */
ve.ui.ContextItem.static.sortOrder = 0;
 
/**
 * Annotation or node models this item is related to.
 *
 * Used by #isCompatibleWith.
 *
 * @static
 * @property {Function[]}
 * @inheritable
 */
ve.ui.ContextItem.static.modelClasses = [];
 
/**
 * Context items (by name) which this context item suppresses.
 *
 * See ve.ui.ModeledFactory.
 *
 * @static
 * @property {string[]}
 * @inheritable
 */
ve.ui.ContextItem.static.suppresses = [];
 
/* Methods */
 
/**
 * Check if this item is compatible with a given model.
 *
 * @static
 * @inheritable
 * @param {ve.dm.Model} model Model to check
 * @return {boolean} Item can be used with model
 */
ve.ui.ContextItem.static.isCompatibleWith = function ( model ) {
	return ve.isInstanceOfAny( model, this.modelClasses );
};
 
/**
 * Check if model is a node
 *
 * @return {boolean} Model is a node
 */
ve.ui.ContextItem.prototype.isNode = function () {
	return this.model && this.model instanceof ve.dm.Node;
};
 
/**
 * Get the command for this item.
 *
 * @return {ve.ui.Command}
 */
ve.ui.ContextItem.prototype.getCommand = function () {
	return this.context.getSurface().commandRegistry.lookup( this.constructor.static.commandName );
};
 
/**
 * Get a surface fragment covering the related model node, or the current selection otherwise
 *
 * @return {ve.dm.SurfaceFragment} Surface fragment
 */
ve.ui.ContextItem.prototype.getFragment = function () {
	if ( !this.fragment ) {
		var surfaceModel = this.context.getSurface().getModel();
		this.fragment = this.isNode() ?
			surfaceModel.getLinearFragment( this.model.getOuterRange() ) :
			surfaceModel.getFragment();
	}
	return this.fragment;
};
 
/**
 * Check if the context's surface is readOnly
 *
 * @return {boolean} Context's surface is readOnly
 */
ve.ui.ContextItem.prototype.isReadOnly = function () {
	return this.context.getSurface().isReadOnly();
};
 
/**
 * Setup the item.
 *
 * @return {ve.ui.ContextItem}
 * @chainable
 */
ve.ui.ContextItem.prototype.setup = function () {
	return this;
};
 
/**
 * Teardown the item.
 *
 * @return {ve.ui.ContextItem}
 * @chainable
 */
ve.ui.ContextItem.prototype.teardown = function () {
	return this;
};