All files / src/ce/nodes ve.ce.ContentEditableNode.js

84% Statements 21/25
66.66% Branches 4/6
71.42% Functions 5/7
84% Lines 21/25

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                            1x 920x 920x 920x   920x               1x               1x   904x     904x   904x   904x           1x   773x     773x 773x               1x                 1x 1824x               1x 1476x               1x      
/*!
 * VisualEditor ContentEditable ContentEditableNode class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * A ContentEditableNode maintains its own contentEditable property
 *
 * @class
 * @abstract
 *
 * @constructor
 */
ve.ce.ContentEditableNode = function VeCeContentEditableNode() {
	this.ceSurface = null;
	this.setContentEditable( true );
	this.setReadOnly( false );
 
	this.connect( this, {
		setup: 'onContentEditableSetup',
		teardown: 'onContentEditableTeardown'
	} );
};
 
/* Inheritance */
 
OO.initClass( ve.ce.ContentEditableNode );
// Assumes ve.ce.Node as a base class
 
/* Methods */
 
/**
 * Handle setup events on the node
 */
ve.ce.ContentEditableNode.prototype.onContentEditableSetup = function () {
	// Exit if already setup or not attached
	Iif ( this.ceSurface || !this.root ) {
		return;
	}
	this.ceSurface = this.root.getSurface().getSurface();
 
	this.ceSurface.connect( this, { readOnly: 'onSurfaceReadOnly' } );
	// Set initial state
	this.setReadOnly( this.ceSurface.isReadOnly() );
};
 
/**
 * Handle teardown events on the node
 */
ve.ce.ContentEditableNode.prototype.onContentEditableTeardown = function () {
	// Exit if not setup
	Iif ( !this.ceSurface ) {
		return;
	}
	this.ceSurface.disconnect( this, { readOnly: 'onSurfaceReadOnly' } );
	this.ceSurface = null;
};
 
/**
 * Handle readOnly events from the surface
 *
 * @param {boolean} readOnly Surface is read-only
 */
ve.ce.ContentEditableNode.prototype.onSurfaceReadOnly = function ( readOnly ) {
	this.setReadOnly( readOnly );
};
 
/**
 * Called when the surface read-only state changes
 *
 * @param {boolean} readOnly Surface is read-only
 */
ve.ce.ContentEditableNode.prototype.setReadOnly = function ( readOnly ) {
	this.$element.prop( 'spellcheck', !readOnly );
};
 
/**
 * Enable or disable editing on this node
 *
 * @param {boolean} enabled Whether to enable editing
 */
ve.ce.ContentEditableNode.prototype.setContentEditable = function ( enabled ) {
	this.$element.prop( 'contentEditable', ( !!enabled ).toString() );
};
 
/**
 * Check if the node is currently editable
 *
 * @return {boolean} Node is currently editable
 */
ve.ce.ContentEditableNode.prototype.isContentEditable = function () {
	return this.$element.prop( 'contentEditable' ) === 'true';
};