Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/inspectors/ve.ui.LinkAnnotationInspector.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface LinkAnnotationInspector class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Inspector for linked content.
9 *
10 * @class
11 * @extends ve.ui.AnnotationInspector
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.LinkAnnotationInspector = function VeUiLinkAnnotationInspector() {
17 // Parent constructor
18 ve.ui.LinkAnnotationInspector.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.LinkAnnotationInspector, ve.ui.AnnotationInspector );
24
25 /* Static properties */
26
27 ve.ui.LinkAnnotationInspector.static.name = 'link';
28
29 ve.ui.LinkAnnotationInspector.static.modelClasses = [ ve.dm.LinkAnnotation ];
30
31 /* Methods */
32
33 /**
34 * Handle annotation input change events
35 */
36 ve.ui.LinkAnnotationInspector.prototype.onAnnotationInputChange = function () {
37 this.labelInput.$input.attr( 'placeholder', this.getInsertionText() );
38 this.updateActions();
39 };
40
41 /**
42 * Update the actions based on the annotation state
43 */
44 ve.ui.LinkAnnotationInspector.prototype.updateActions = function () {
45 const annotation = this.annotationInput.getAnnotation();
46
47 let isValid = false;
48 this.annotationInput.getTextInputWidget().getValidity()
49 .then( () => {
50 isValid = true;
51 } )
52 .always( () => {
53 isValid = isValid && !!annotation;
54 this.actions.forEach( { actions: [ 'done', 'insert' ] }, ( action ) => {
55 action.setDisabled( !isValid );
56 } );
57 } );
58 };
59
60 /**
61 * @inheritdoc
62 */
63 ve.ui.LinkAnnotationInspector.prototype.shouldRemoveAnnotation = function () {
64 return !this.annotationInput.getAnnotation();
65 };
66
67 /**
68 * @inheritdoc
69 */
70 ve.ui.LinkAnnotationInspector.prototype.getInsertionText = function () {
71 return this.labelInput.getValue().trim() || this.annotationInput.getHref();
72 };
73
74 /**
75 * @inheritdoc
76 */
77 ve.ui.LinkAnnotationInspector.prototype.getAnnotation = function () {
78 return this.annotationInput.getAnnotation();
79 };
80
81 /**
82 * @inheritdoc
83 */
84 ve.ui.LinkAnnotationInspector.prototype.getAnnotationFromFragment = function ( fragment ) {
85 const text = fragment.getText();
86
87 return text ? new ve.dm.LinkAnnotation( {
88 type: 'link',
89 attributes: { href: text }
90 } ) : null;
91 };
92
93 /**
94 * @inheritdoc
95 */
96 ve.ui.LinkAnnotationInspector.prototype.initialize = function () {
97 // Parent method
98 ve.ui.LinkAnnotationInspector.super.prototype.initialize.call( this );
99
100 // Properties
101 this.labelInput = this.createLabelInput();
102 this.annotationInput = this.createAnnotationInput();
103
104 this.labelField = new OO.ui.FieldLayout(
105 this.labelInput,
106 {
107 align: 'top',
108 label: ve.msg( 'visualeditor-linkcontext-label-label' )
109 }
110 );
111 this.annotationField = new OO.ui.FieldLayout(
112 this.annotationInput,
113 {
114 align: 'top',
115 label: ve.msg( 'visualeditor-linkinspector-title' )
116 }
117 );
118
119 // Events
120 this.annotationInput.connect( this, { change: 'onAnnotationInputChange' } );
121 this.annotationInput.getTextInputWidget().connect( this, { enter: 'onFormSubmit' } );
122 this.labelInput.connect( this, { enter: 'onFormSubmit' } );
123 this.labelInput.connect( this, { change: 'onAnnotationInputChange' } );
124
125 // Initialization
126 this.form.$element.append(
127 this.labelField.$element,
128 this.annotationField.$element
129 );
130
131 if ( !OO.ui.isMobile() ) {
132 this.annotationField.setLabel( null );
133 this.labelField.$element.detach();
134 }
135 };
136
137 /**
138 * Create a link label widget
139 *
140 * @return {OO.ui.TextInputWidget} Link label widget
141 */
142 ve.ui.LinkAnnotationInspector.prototype.createLabelInput = function () {
143 return new OO.ui.TextInputWidget();
144 };
145
146 /**
147 * Create a link annotation widget
148 *
149 * @return {ve.ui.LinkAnnotationWidget} Link annotation widget
150 */
151 ve.ui.LinkAnnotationInspector.prototype.createAnnotationInput = function () {
152 return new ve.ui.LinkAnnotationWidget();
153 };
154
155 /**
156 * @inheritdoc
157 */
158 ve.ui.LinkAnnotationInspector.prototype.shouldInsertText = function () {
159 if ( ve.ui.LinkAnnotationInspector.super.prototype.shouldInsertText.call( this ) ) {
160 // Adding a new link
161 return true;
162 }
163 if ( OO.ui.isMobile() ) {
164 return !this.labelInput.isDisabled() &&
165 // Don't touch it if the plaintext value hasn't changed, to preserve internal annotations if possible
166 this.labelInput.getValue().trim() !== this.initialLabel.trim();
167 }
168 return false;
169 };
170
171 /**
172 * @inheritdoc
173 */
174 ve.ui.LinkAnnotationInspector.prototype.getSetupProcess = function ( data ) {
175 return ve.ui.LinkAnnotationInspector.super.prototype.getSetupProcess.call( this, data )
176 .next( () => {
177 const title = ve.msg(
178 this.isReadOnly() ?
179 'visualeditor-linkinspector-title' : (
180 this.isNew ?
181 'visualeditor-linkinspector-title-add' :
182 'visualeditor-linkinspector-title-edit'
183 )
184 ),
185 fragment = this.getFragment();
186 this.title.setLabel( title ).setTitle( title );
187 this.initialLabel = fragment.getText();
188 this.labelInput.setDisabled( !fragment.containsOnlyText() );
189 this.labelInput.setValue( this.initialLabel );
190 this.annotationInput.setAnnotation( this.initialAnnotation );
191 this.annotationInput.setReadOnly( this.isReadOnly() );
192
193 this.updateActions();
194 } );
195 };
196
197 /**
198 * @inheritdoc
199 */
200 ve.ui.LinkAnnotationInspector.prototype.getReadyProcess = function ( data ) {
201 return ve.ui.LinkAnnotationInspector.super.prototype.getReadyProcess.call( this, data )
202 .next( () => {
203 if ( !OO.ui.isMobile() ) {
204 this.annotationInput.getTextInputWidget().focus().select();
205 }
206
207 // Clear validation state, so that we don't get "invalid" state immediately on focus
208 this.annotationInput.getTextInputWidget().setValidityFlag( true );
209 } );
210 };
211
212 /**
213 * @inheritdoc
214 */
215 ve.ui.LinkAnnotationInspector.prototype.getHoldProcess = function ( data ) {
216 return ve.ui.LinkAnnotationInspector.super.prototype.getHoldProcess.call( this, data )
217 .next( () => {
218 this.annotationInput.getTextInputWidget().blur();
219 } );
220 };
221
222 /**
223 * @inheritdoc
224 */
225 ve.ui.LinkAnnotationInspector.prototype.getTeardownProcess = function ( data ) {
226 return ve.ui.LinkAnnotationInspector.super.prototype.getTeardownProcess.call( this, data )
227 .next( () => {
228 this.annotationInput.setAnnotation( null );
229 this.labelInput.$input.attr( 'placeholder', '' );
230 this.labelInput.setValue( '' );
231 } );
232 };
233
234 /* Registration */
235
236 ve.ui.windowFactory.register( ve.ui.LinkAnnotationInspector );