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

95.94% Statements 71/74
70% Branches 14/20
90.9% Functions 20/22
95.94% Lines 71/74

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 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                              1x   1x         1x       1x   1x             1x 43x 43x           1x 56x   56x 56x   22x     56x 56x 74x               1x 11x           1x 44x           1x 13x           1x 6x   6x                 1x   1x     1x 1x   1x             1x                 1x 1x 1x 1x     1x         1x 1x 1x                 1x 1x               1x 1x           1x 10x   1x   9x         9x           1x 13x   13x               13x 13x 13x 13x 13x 13x 13x   13x             1x 13x   13x 13x       13x             1x                   1x 13x   13x 13x 13x           1x  
/*!
 * VisualEditor UserInterface LinkAnnotationInspector class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Inspector for linked content.
 *
 * @class
 * @extends ve.ui.AnnotationInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.LinkAnnotationInspector = function VeUiLinkAnnotationInspector() {
	// Parent constructor
	ve.ui.LinkAnnotationInspector.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.LinkAnnotationInspector, ve.ui.AnnotationInspector );
 
/* Static properties */
 
ve.ui.LinkAnnotationInspector.static.name = 'link';
 
ve.ui.LinkAnnotationInspector.static.modelClasses = [ ve.dm.LinkAnnotation ];
 
/* Methods */
 
/**
 * Handle annotation input change events
 */
ve.ui.LinkAnnotationInspector.prototype.onAnnotationInputChange = function () {
	this.labelInput.$input.attr( 'placeholder', this.getInsertionText() );
	this.updateActions();
};
 
/**
 * Update the actions based on the annotation state
 */
ve.ui.LinkAnnotationInspector.prototype.updateActions = function () {
	const annotation = this.annotationInput.getAnnotation();
 
	let isValid = false;
	this.annotationInput.getTextInputWidget().getValidity()
		.then( () => {
			isValid = true;
		} )
		.always( () => {
			isValid = isValid && !!annotation;
			this.actions.forEach( { actions: [ 'done', 'insert' ] }, ( action ) => {
				action.setDisabled( !isValid );
			} );
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.shouldRemoveAnnotation = function () {
	return !this.annotationInput.getAnnotation();
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.getInsertionText = function () {
	return this.labelInput.getValue().trim() || this.annotationInput.getHref();
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.getAnnotation = function () {
	return this.annotationInput.getAnnotation();
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.getAnnotationFromFragment = function ( fragment ) {
	const text = fragment.getText();
 
	return text ? new ve.dm.LinkAnnotation( {
		type: 'link',
		attributes: { href: text }
	} ) : null;
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.initialize = function () {
	// Parent method
	ve.ui.LinkAnnotationInspector.super.prototype.initialize.call( this );
 
	// Properties
	this.labelInput = this.createLabelInput();
	this.annotationInput = this.createAnnotationInput();
 
	this.labelField = new OO.ui.FieldLayout(
		this.labelInput,
		{
			align: 'top',
			label: ve.msg( 'visualeditor-linkcontext-label-label' )
		}
	);
	this.annotationField = new OO.ui.FieldLayout(
		this.annotationInput,
		{
			align: 'top',
			label: ve.msg( 'visualeditor-linkinspector-title' )
		}
	);
 
	// Events
	this.annotationInput.connect( this, { change: 'onAnnotationInputChange' } );
	this.annotationInput.getTextInputWidget().connect( this, { enter: 'onFormSubmit' } );
	this.labelInput.connect( this, { enter: 'onFormSubmit' } );
	this.labelInput.connect( this, { change: 'onAnnotationInputChange' } );
 
	// Initialization
	this.form.$element.append(
		this.labelField.$element,
		this.annotationField.$element
	);
 
	Eif ( !OO.ui.isMobile() ) {
		this.annotationField.setLabel( null );
		this.labelField.$element.detach();
	}
};
 
/**
 * Create a link label widget
 *
 * @return {OO.ui.TextInputWidget} Link label widget
 */
ve.ui.LinkAnnotationInspector.prototype.createLabelInput = function () {
	return new OO.ui.TextInputWidget();
};
 
/**
 * Create a link annotation widget
 *
 * @return {ve.ui.LinkAnnotationWidget} Link annotation widget
 */
ve.ui.LinkAnnotationInspector.prototype.createAnnotationInput = function () {
	return new ve.ui.LinkAnnotationWidget();
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.shouldInsertText = function () {
	if ( ve.ui.LinkAnnotationInspector.super.prototype.shouldInsertText.call( this ) ) {
		// Adding a new link
		return true;
	}
	Iif ( OO.ui.isMobile() ) {
		return !this.labelInput.isDisabled() &&
			// Don't touch it if the plaintext value hasn't changed, to preserve internal annotations if possible
			this.labelInput.getValue().trim() !== this.initialLabel.trim();
	}
	return false;
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.LinkAnnotationInspector.super.prototype.getSetupProcess.call( this, data )
		.next( () => {
			const title = ve.msg(
					this.isReadOnly() ?
						'visualeditor-linkinspector-title' : (
							this.isNew ?
								'visualeditor-linkinspector-title-add' :
								'visualeditor-linkinspector-title-edit'
						)
				),
				fragment = this.getFragment();
			this.title.setLabel( title ).setTitle( title );
			this.initialLabel = fragment.getText();
			this.labelInput.setDisabled( !fragment.containsOnlyText() );
			this.labelInput.setValue( this.initialLabel );
			this.annotationInput.setAnnotation( this.initialAnnotation );
			this.annotationInput.setReadOnly( this.isReadOnly() );
 
			this.updateActions();
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.getReadyProcess = function ( data ) {
	return ve.ui.LinkAnnotationInspector.super.prototype.getReadyProcess.call( this, data )
		.next( () => {
			Eif ( !OO.ui.isMobile() ) {
				this.annotationInput.getTextInputWidget().focus().select();
			}
 
			// Clear validation state, so that we don't get "invalid" state immediately on focus
			this.annotationInput.getTextInputWidget().setValidityFlag( true );
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.getHoldProcess = function ( data ) {
	return ve.ui.LinkAnnotationInspector.super.prototype.getHoldProcess.call( this, data )
		.next( () => {
			this.annotationInput.getTextInputWidget().blur();
		} );
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationInspector.prototype.getTeardownProcess = function ( data ) {
	return ve.ui.LinkAnnotationInspector.super.prototype.getTeardownProcess.call( this, data )
		.next( () => {
			this.annotationInput.setAnnotation( null );
			this.labelInput.$input.attr( 'placeholder', '' );
			this.labelInput.setValue( '' );
		} );
};
 
/* Registration */
 
ve.ui.windowFactory.register( ve.ui.LinkAnnotationInspector );