All files / src/ui/inspectors ve.ui.CommentAnnotationInspector.js

33.33% Statements 18/54
0% Branches 0/5
0% Functions 0/21
33.33% Lines 18/54

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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                              1x             1x       1x   1x     1x                 1x             1x             1x                                 1x             1x             1x                                 1x                       1x                                                       1x                                     1x                         1x                   1x                 1x  
/*!
 * VisualEditor UserInterface CommentAnnotationInspector class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Inspector for linked content.
 *
 * @class
 * @extends ve.ui.AnnotationInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.CommentAnnotationInspector = function VeUiCommentAnnotationInspector() {
	// Parent constructor
	ve.ui.CommentAnnotationInspector.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.CommentAnnotationInspector, ve.ui.AnnotationInspector );
 
/* Static properties */
 
ve.ui.CommentAnnotationInspector.static.name = 'commentAnnotation';
 
ve.ui.CommentAnnotationInspector.static.title =
	OO.ui.deferMsg( 'visualeditor-commentinspector-title' );
 
ve.ui.CommentAnnotationInspector.static.modelClasses = [ ve.dm.CommentAnnotation ];
 
/* Methods */
 
/**
 * Handle annotation input change events
 *
 * @param {ve.dm.CommentAnnotation} annotation New comment annotation value
 */
ve.ui.CommentAnnotationInspector.prototype.onTextInputChange = function () {
	this.updateActions();
};
 
/**
 * Handle annotation input resize events
 */
ve.ui.CommentAnnotationInspector.prototype.onTextInputResize = function () {
	this.updateSize();
};
 
/**
 * Update the actions based on the annotation state
 */
ve.ui.CommentAnnotationInspector.prototype.updateActions = function () {
	let isValid = false;
 
	this.textInput.getValidity()
		.then( () => {
			isValid = true;
		} )
		.always( () => {
			this.actions.forEach( { actions: [ 'done', 'insert' ] }, ( action ) => {
				action.setDisabled( !isValid );
			} );
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.shouldRemoveAnnotation = function () {
	return !this.textInput.getValue();
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.getInsertionText = function () {
	return '…';
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.getAnnotation = function () {
	const comments = ( this.initialAnnotation && this.initialAnnotation.getAttribute( 'comments' ).slice() ) || [];
	comments.push( {
		author: this.getFragment().getSurface().synchronizer.getAuthorData().name,
		text: this.textInput.getValue().trim()
	} );
	return new ve.dm.CommentAnnotation( {
		type: 'commentAnnotation',
		attributes: {
			comments: comments
		}
	} );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.getAnnotationFromFragment = function () {
	return new ve.dm.CommentAnnotation( {
		type: 'commentAnnotation',
		attributes: {
			comments: []
		}
	} );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.initialize = function () {
	// Parent method
	ve.ui.CommentAnnotationInspector.super.prototype.initialize.call( this );
 
	// Properties
	this.textInput = new OO.ui.MultilineTextInputWidget( { autosize: true } );
	this.$thread = $( '<div>' );
	this.$user = $( '<strong>' );
 
	// Events
	this.textInput.connect( this, {
		change: 'onTextInputChange',
		resize: 'onTextInputResize'
	} );
 
	// Initialization
	this.form.$element.append(
		this.$thread,
		$( '<div>' ).addClass( 've-ui-commentAnnotationContextItem-comment' ).append(
			this.$user,
			this.textInput.$element
		)
	);
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.CommentAnnotationInspector.super.prototype.getSetupProcess.call( this, data )
		.next( () => {
			if ( this.initialAnnotation ) {
				const $thread = ve.ui.CommentAnnotationContextItem.static.renderThread( this.initialAnnotation );
				this.$thread.empty().append( $thread );
			}
			this.$user.text( this.getFragment().getSurface().synchronizer.getAuthorData().name );
			this.textInput.setValue( '' );
			this.actions.forEach( { actions: [ 'done' ] }, ( action ) => {
				action.setLabel( ve.msg( 'visualeditor-commentannotationcontextitem-comment' ) );
			} );
			this.updateActions();
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.getReadyProcess = function ( data ) {
	return ve.ui.CommentAnnotationInspector.super.prototype.getReadyProcess.call( this, data )
		.next( () => {
			this.textInput.focus().select();
 
			// Clear validation state, so that we don't get "invalid" state immediately on focus
			this.textInput.setValidityFlag( true );
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.getHoldProcess = function ( data ) {
	return ve.ui.CommentAnnotationInspector.super.prototype.getHoldProcess.call( this, data )
		.next( () => {
			this.textInput.blur();
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommentAnnotationInspector.prototype.getTeardownProcess = function ( data ) {
	return ve.ui.CommentAnnotationInspector.super.prototype.getTeardownProcess.call( this, data )
		.next( () => {
			this.textInput.setValue( '' );
		} );
};
 
/* Registration */
 
ve.ui.windowFactory.register( ve.ui.CommentAnnotationInspector );