Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface CommentAnnotationInspector 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.CommentAnnotationInspector = function VeUiCommentAnnotationInspector() {
17 // Parent constructor
18 ve.ui.CommentAnnotationInspector.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.CommentAnnotationInspector, ve.ui.AnnotationInspector );
24
25 /* Static properties */
26
27 ve.ui.CommentAnnotationInspector.static.name = 'commentAnnotation';
28
29 ve.ui.CommentAnnotationInspector.static.title =
30 OO.ui.deferMsg( 'visualeditor-commentinspector-title' );
31
32 ve.ui.CommentAnnotationInspector.static.modelClasses = [ ve.dm.CommentAnnotation ];
33
34 /* Methods */
35
36 /**
37 * Handle annotation input change events
38 *
39 * @param {ve.dm.CommentAnnotation} annotation New comment annotation value
40 */
41 ve.ui.CommentAnnotationInspector.prototype.onTextInputChange = function () {
42 this.updateActions();
43 };
44
45 /**
46 * Handle annotation input resize events
47 */
48 ve.ui.CommentAnnotationInspector.prototype.onTextInputResize = function () {
49 this.updateSize();
50 };
51
52 /**
53 * Update the actions based on the annotation state
54 */
55 ve.ui.CommentAnnotationInspector.prototype.updateActions = function () {
56 let isValid = false;
57
58 this.textInput.getValidity()
59 .then( () => {
60 isValid = true;
61 } )
62 .always( () => {
63 this.actions.forEach( { actions: [ 'done', 'insert' ] }, ( action ) => {
64 action.setDisabled( !isValid );
65 } );
66 } );
67 };
68
69 /**
70 * @inheritdoc
71 */
72 ve.ui.CommentAnnotationInspector.prototype.shouldRemoveAnnotation = function () {
73 return !this.textInput.getValue();
74 };
75
76 /**
77 * @inheritdoc
78 */
79 ve.ui.CommentAnnotationInspector.prototype.getInsertionText = function () {
80 return '…';
81 };
82
83 /**
84 * @inheritdoc
85 */
86 ve.ui.CommentAnnotationInspector.prototype.getAnnotation = function () {
87 const comments = ( this.initialAnnotation && this.initialAnnotation.getAttribute( 'comments' ).slice() ) || [];
88 comments.push( {
89 author: this.getFragment().getSurface().synchronizer.getAuthorData().name,
90 text: this.textInput.getValue().trim()
91 } );
92 return new ve.dm.CommentAnnotation( {
93 type: 'commentAnnotation',
94 attributes: {
95 comments: comments
96 }
97 } );
98 };
99
100 /**
101 * @inheritdoc
102 */
103 ve.ui.CommentAnnotationInspector.prototype.getAnnotationFromFragment = function () {
104 return new ve.dm.CommentAnnotation( {
105 type: 'commentAnnotation',
106 attributes: {
107 comments: []
108 }
109 } );
110 };
111
112 /**
113 * @inheritdoc
114 */
115 ve.ui.CommentAnnotationInspector.prototype.initialize = function () {
116 // Parent method
117 ve.ui.CommentAnnotationInspector.super.prototype.initialize.call( this );
118
119 // Properties
120 this.textInput = new OO.ui.MultilineTextInputWidget( { autosize: true } );
121 this.$thread = $( '<div>' );
122 this.$user = $( '<strong>' );
123
124 // Events
125 this.textInput.connect( this, {
126 change: 'onTextInputChange',
127 resize: 'onTextInputResize'
128 } );
129
130 // Initialization
131 this.form.$element.append(
132 this.$thread,
133 $( '<div>' ).addClass( 've-ui-commentAnnotationContextItem-comment' ).append(
134 this.$user,
135 this.textInput.$element
136 )
137 );
138 };
139
140 /**
141 * @inheritdoc
142 */
143 ve.ui.CommentAnnotationInspector.prototype.getSetupProcess = function ( data ) {
144 return ve.ui.CommentAnnotationInspector.super.prototype.getSetupProcess.call( this, data )
145 .next( () => {
146 if ( this.initialAnnotation ) {
147 const $thread = ve.ui.CommentAnnotationContextItem.static.renderThread( this.initialAnnotation );
148 this.$thread.empty().append( $thread );
149 }
150 this.$user.text( this.getFragment().getSurface().synchronizer.getAuthorData().name );
151 this.textInput.setValue( '' );
152 this.actions.forEach( { actions: [ 'done' ] }, ( action ) => {
153 action.setLabel( ve.msg( 'visualeditor-commentannotationcontextitem-comment' ) );
154 } );
155 this.updateActions();
156 } );
157 };
158
159 /**
160 * @inheritdoc
161 */
162 ve.ui.CommentAnnotationInspector.prototype.getReadyProcess = function ( data ) {
163 return ve.ui.CommentAnnotationInspector.super.prototype.getReadyProcess.call( this, data )
164 .next( () => {
165 this.textInput.focus().select();
166
167 // Clear validation state, so that we don't get "invalid" state immediately on focus
168 this.textInput.setValidityFlag( true );
169 } );
170 };
171
172 /**
173 * @inheritdoc
174 */
175 ve.ui.CommentAnnotationInspector.prototype.getHoldProcess = function ( data ) {
176 return ve.ui.CommentAnnotationInspector.super.prototype.getHoldProcess.call( this, data )
177 .next( () => {
178 this.textInput.blur();
179 } );
180 };
181
182 /**
183 * @inheritdoc
184 */
185 ve.ui.CommentAnnotationInspector.prototype.getTeardownProcess = function ( data ) {
186 return ve.ui.CommentAnnotationInspector.super.prototype.getTeardownProcess.call( this, data )
187 .next( () => {
188 this.textInput.setValue( '' );
189 } );
190 };
191
192 /* Registration */
193
194 ve.ui.windowFactory.register( ve.ui.CommentAnnotationInspector );