Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/widgets/ve.ui.LinkAnnotationWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface LinkAnnotationWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Creates an ve.ui.LinkAnnotationWidget object.
9 *
10 * @class
11 * @extends OO.ui.Widget
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.LinkAnnotationWidget = function VeUiLinkAnnotationWidget( config ) {
17 // Properties
18 this.annotation = null;
19 this.input = this.createInputWidget( config );
20
21 // Parent constructor
22 // Must be called after this.input is set as parent constructor calls this.setDisabled
23 ve.ui.LinkAnnotationWidget.super.apply( this, arguments );
24
25 // Initialization
26 this.$element
27 .append( this.input.$element )
28 .addClass( 've-ui-linkAnnotationWidget' );
29
30 // Events
31 this.getTextInputWidget().connect( this, { change: 'onTextChange' } );
32 };
33
34 /* Inheritance */
35
36 OO.inheritClass( ve.ui.LinkAnnotationWidget, OO.ui.Widget );
37
38 /* Events */
39
40 /**
41 * A change event is emitted when the annotation value of the input changes.
42 *
43 * @event ve.ui.LinkAnnotationWidget#change
44 * @param {ve.dm.LinkAnnotation|null} annotation
45 */
46
47 /* Static Methods */
48
49 /**
50 * Get an annotation from the current text value
51 *
52 * @static
53 * @param {string} value Text value
54 * @return {ve.dm.LinkAnnotation|null} Link annotation
55 */
56 ve.ui.LinkAnnotationWidget.static.getAnnotationFromText = function ( value ) {
57 const href = value.trim();
58
59 // Keep annotation in sync with value
60 if ( href === '' ) {
61 return null;
62 } else {
63 return new ve.dm.LinkAnnotation( {
64 type: 'link',
65 attributes: {
66 href: href
67 }
68 } );
69 }
70 };
71
72 /**
73 * Get a text value for the current annotation
74 *
75 * @static
76 * @param {ve.dm.LinkAnnotation|null} annotation Link annotation
77 * @return {string} Text value for the annotation
78 */
79 ve.ui.LinkAnnotationWidget.static.getTextFromAnnotation = function ( annotation ) {
80 return annotation ? annotation.getHref() : '';
81 };
82
83 /* Methods */
84
85 /**
86 * Create a widget to be used by the annotation widget
87 *
88 * @param {Object} [config] Configuration options
89 * @return {OO.ui.Widget} Text input widget
90 */
91 ve.ui.LinkAnnotationWidget.prototype.createInputWidget = function ( config ) {
92 return new OO.ui.TextInputWidget( ve.extendObject( { validate: 'non-empty' }, config ) );
93 };
94
95 /**
96 * Get the text input widget used by the annotation widget
97 *
98 * @return {OO.ui.TextInputWidget} Text input widget
99 */
100 ve.ui.LinkAnnotationWidget.prototype.getTextInputWidget = function () {
101 return this.input;
102 };
103
104 /**
105 * @inheritdoc
106 */
107 ve.ui.LinkAnnotationWidget.prototype.setDisabled = function () {
108 // Parent method
109 ve.ui.LinkAnnotationWidget.super.prototype.setDisabled.apply( this, arguments );
110
111 this.getTextInputWidget().setDisabled( this.isDisabled() );
112 return this;
113 };
114
115 /**
116 * Handle value-changing events from the text input
117 *
118 * @param {string} value New input value
119 */
120 ve.ui.LinkAnnotationWidget.prototype.onTextChange = function ( value ) {
121 // RTL/LTR check
122 // TODO: Make this work properly
123 if ( document.body.classList.contains( 'rtl' ) ) {
124 const isExt = ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value.trim() );
125 // If URL is external, flip to LTR. Otherwise, set back to RTL
126 this.getTextInputWidget().setDir( isExt ? 'ltr' : 'rtl' );
127 }
128
129 this.getTextInputWidget().getValidity()
130 .done( () => {
131 this.setAnnotation( this.constructor.static.getAnnotationFromText( value ), true );
132 } )
133 .fail( () => {
134 this.setAnnotation( null, true );
135 } );
136 };
137
138 /**
139 * Sets the annotation value.
140 *
141 * The input value will automatically be updated.
142 *
143 * @param {ve.dm.LinkAnnotation|null} annotation Link annotation
144 * @param {boolean} [fromText] Annotation was generated from text input
145 * @return {ve.ui.LinkAnnotationWidget}
146 * @chainable
147 * @fires ve.ui.LinkAnnotationWidget#change
148 */
149 ve.ui.LinkAnnotationWidget.prototype.setAnnotation = function ( annotation, fromText ) {
150 if ( ve.compare(
151 annotation ? annotation.getComparableObject() : {},
152 this.annotation ? this.annotation.getComparableObject() : {}
153 ) ) {
154 // No change
155 return this;
156 }
157
158 this.annotation = annotation;
159
160 // If this method was triggered by a change to the text input, leave it alone.
161 if ( !fromText ) {
162 this.getTextInputWidget().setValue( this.constructor.static.getTextFromAnnotation( annotation ) );
163 }
164
165 this.emit( 'change', this.annotation );
166
167 return this;
168 };
169
170 /**
171 * Gets the annotation value.
172 *
173 * @return {ve.dm.LinkAnnotation} Link annotation
174 */
175 ve.ui.LinkAnnotationWidget.prototype.getAnnotation = function () {
176 return this.annotation;
177 };
178
179 /**
180 * Get the hyperlink location.
181 *
182 * @return {string} Hyperlink location
183 */
184 ve.ui.LinkAnnotationWidget.prototype.getHref = function () {
185 return this.constructor.static.getTextFromAnnotation( this.annotation );
186 };
187
188 /**
189 * Set the read-only state of the widget
190 *
191 * @param {boolean} readOnly Make widget read-only
192 * @return {ve.ui.LinkAnnotationWidget}
193 * @chainable
194 */
195 ve.ui.LinkAnnotationWidget.prototype.setReadOnly = function ( readOnly ) {
196 this.input.setReadOnly( readOnly );
197 return this;
198 };