all files / src/ui/contextitems/ ve.ui.AlignableContextItem.js

48.15% Statements 13/27
50% Branches 2/4
16.67% Functions 1/6
48.15% Lines 13/27
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                                                                                                  23×                                                  
/*!
 * VisualEditor Alignable class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Context item for an alignable node.
 *
 * @class
 * @extends ve.ui.LinearContextItem
 *
 * @param {ve.ui.Context} context Context item is in
 * @param {ve.dm.Model} model Model item is related to
 * @param {Object} config Configuration options
 */
ve.ui.AlignableContextItem = function VeUiAlignableContextItem( context, model, config ) {
	var align;
 
	// Parent constructor
	ve.ui.AlignableContextItem.super.call( this, context, model, config );
 
	align = model.getAttribute( 'align' );
 
	this.align = new ve.ui.AlignWidget( {
		dir: this.context.getSurface().getDir()
	} );
	this.align.selectItemByData( align );
	this.align.connect( this, { choose: 'onAlignChoose' } );
 
	if ( OO.ui.isMobile() ) {
		this.align.items.forEach( function ( item ) {
			item.setLabel( null );
		} );
	}
 
	// Initialization
	this.$element.addClass( 've-ui-alignableContextItem' );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.AlignableContextItem, ve.ui.LinearContextItem );
 
/* Static Properties */
 
ve.ui.AlignableContextItem.static.name = 'alignable';
 
ve.ui.AlignableContextItem.static.icon = 'alignLeft';
 
ve.ui.AlignableContextItem.static.label = OO.ui.deferMsg( 'visualeditor-alignablecontextitem-title' );
 
ve.ui.AlignableContextItem.static.editable = false;
 
ve.ui.AlignableContextItem.static.exclusive = false;
 
ve.ui.AlignableContextItem.static.isCompatibleWith = function ( model ) {
	return model instanceof ve.dm.Node && model.isAlignable();
};
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.AlignableContextItem.prototype.setup = function () {
	this.align.setDisabled( this.context.getSurface().isReadOnly() );
 
	// Parent method
	ve.ui.AlignableContextItem.super.prototype.setup.apply( this, arguments );
};
 
/**
 * @inheritdoc
 */
ve.ui.AlignableContextItem.prototype.renderBody = function () {
	this.$body.empty().append( this.align.$element );
};
 
ve.ui.AlignableContextItem.prototype.onAlignChoose = function ( item ) {
	this.getFragment().changeAttributes( { align: item.getData() } );
};
 
/* Registration */
 
ve.ui.contextItemFactory.register( ve.ui.AlignableContextItem );