All files / src/ui/widgets ve.ui.LinkAnnotationWidget.js

93.18% Statements 41/44
75% Branches 12/16
100% Functions 13/13
93.18% Lines 41/44

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                              1x   1x 1x       1x     1x         1x         1x                                       1x 12x     12x     12x                               1x 32x                     1x 1x               1x 131x           1x   1x   1x 1x               1x     23x           23x   12x     11x                             1x 49x         26x     23x     23x 19x     23x   23x               1x 80x               1x 13x                   1x 13x 13x    
/*!
 * VisualEditor UserInterface LinkAnnotationWidget class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Creates an ve.ui.LinkAnnotationWidget object.
 *
 * @class
 * @extends OO.ui.Widget
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.LinkAnnotationWidget = function VeUiLinkAnnotationWidget( config ) {
	// Properties
	this.annotation = null;
	this.input = this.createInputWidget( config );
 
	// Parent constructor
	// Must be called after this.input is set as parent constructor calls this.setDisabled
	ve.ui.LinkAnnotationWidget.super.apply( this, arguments );
 
	// Initialization
	this.$element
		.append( this.input.$element )
		.addClass( 've-ui-linkAnnotationWidget' );
 
	// Events
	this.getTextInputWidget().connect( this, { change: 'onTextChange' } );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.LinkAnnotationWidget, OO.ui.Widget );
 
/* Events */
 
/**
 * A change event is emitted when the annotation value of the input changes.
 *
 * @event ve.ui.LinkAnnotationWidget#change
 * @param {ve.dm.LinkAnnotation|null} annotation
 */
 
/* Static Methods */
 
/**
 * Get an annotation from the current text value
 *
 * @static
 * @param {string} value Text value
 * @return {ve.dm.LinkAnnotation|null} Link annotation
 */
ve.ui.LinkAnnotationWidget.static.getAnnotationFromText = function ( value ) {
	const href = value.trim();
 
	// Keep annotation in sync with value
	Iif ( href === '' ) {
		return null;
	} else {
		return new ve.dm.LinkAnnotation( {
			type: 'link',
			attributes: {
				href: href
			}
		} );
	}
};
 
/**
 * Get a text value for the current annotation
 *
 * @static
 * @param {ve.dm.LinkAnnotation|null} annotation Link annotation
 * @return {string} Text value for the annotation
 */
ve.ui.LinkAnnotationWidget.static.getTextFromAnnotation = function ( annotation ) {
	return annotation ? annotation.getHref() : '';
};
 
/* Methods */
 
/**
 * Create a widget to be used by the annotation widget
 *
 * @param {Object} [config] Configuration options
 * @return {OO.ui.Widget} Text input widget
 */
ve.ui.LinkAnnotationWidget.prototype.createInputWidget = function ( config ) {
	return new OO.ui.TextInputWidget( ve.extendObject( { validate: 'non-empty' }, config ) );
};
 
/**
 * Get the text input widget used by the annotation widget
 *
 * @return {OO.ui.TextInputWidget} Text input widget
 */
ve.ui.LinkAnnotationWidget.prototype.getTextInputWidget = function () {
	return this.input;
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkAnnotationWidget.prototype.setDisabled = function () {
	// Parent method
	ve.ui.LinkAnnotationWidget.super.prototype.setDisabled.apply( this, arguments );
 
	this.getTextInputWidget().setDisabled( this.isDisabled() );
	return this;
};
 
/**
 * Handle value-changing events from the text input
 *
 * @param {string} value New input value
 */
ve.ui.LinkAnnotationWidget.prototype.onTextChange = function ( value ) {
	// RTL/LTR check
	// TODO: Make this work properly
	Iif ( document.body.classList.contains( 'rtl' ) ) {
		const isExt = ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value.trim() );
		// If URL is external, flip to LTR. Otherwise, set back to RTL
		this.getTextInputWidget().setDir( isExt ? 'ltr' : 'rtl' );
	}
 
	this.getTextInputWidget().getValidity()
		.done( () => {
			this.setAnnotation( this.constructor.static.getAnnotationFromText( value ), true );
		} )
		.fail( () => {
			this.setAnnotation( null, true );
		} );
};
 
/**
 * Sets the annotation value.
 *
 * The input value will automatically be updated.
 *
 * @param {ve.dm.LinkAnnotation|null} annotation Link annotation
 * @param {boolean} [fromText] Annotation was generated from text input
 * @return {ve.ui.LinkAnnotationWidget}
 * @chainable
 * @fires ve.ui.LinkAnnotationWidget#change
 */
ve.ui.LinkAnnotationWidget.prototype.setAnnotation = function ( annotation, fromText ) {
	if ( ve.compare(
		annotation ? annotation.getComparableObject() : {},
		this.annotation ? this.annotation.getComparableObject() : {}
	) ) {
		// No change
		return this;
	}
 
	this.annotation = annotation;
 
	// If this method was triggered by a change to the text input, leave it alone.
	if ( !fromText ) {
		this.getTextInputWidget().setValue( this.constructor.static.getTextFromAnnotation( annotation ) );
	}
 
	this.emit( 'change', this.annotation );
 
	return this;
};
 
/**
 * Gets the annotation value.
 *
 * @return {ve.dm.LinkAnnotation} Link annotation
 */
ve.ui.LinkAnnotationWidget.prototype.getAnnotation = function () {
	return this.annotation;
};
 
/**
 * Get the hyperlink location.
 *
 * @return {string} Hyperlink location
 */
ve.ui.LinkAnnotationWidget.prototype.getHref = function () {
	return this.constructor.static.getTextFromAnnotation( this.annotation );
};
 
/**
 * Set the read-only state of the widget
 *
 * @param {boolean} readOnly Make widget read-only
 * @return {ve.ui.LinkAnnotationWidget}
 * @chainable
 */
ve.ui.LinkAnnotationWidget.prototype.setReadOnly = function ( readOnly ) {
	this.input.setReadOnly( readOnly );
	return this;
};