Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface TargetWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Creates an ve.ui.TargetWidget object.
9 *
10 * User must call #initialize after the widget has been attached
11 * to the DOM, and also after the document is changed with #setDocument.
12 *
13 * @class
14 * @abstract
15 * @extends OO.ui.Widget
16 * @mixes OO.ui.mixin.PendingElement
17 *
18 * @constructor
19 * @param {Object} [config] Configuration options
20 * @param {ve.dm.Document} [config.doc] Initial document model
21 * @param {Object} [config.modes] Available editing modes.
22 * @param {Object} [config.defaultMode] Default mode for new surfaces.
23 * @param {Object} [config.toolbarGroups] Target's toolbar groups config.
24 * @param {string[]|null} [config.includeCommands] List of commands to include, null for all registered commands
25 * @param {string[]} [config.excludeCommands] List of commands to exclude
26 * @param {Object} [config.importRules] Import rules
27 * @param {boolean} [config.multiline=true] Multi-line surface
28 * @param {string} [config.placeholder] Placeholder text to display when the surface is empty
29 * @param {boolean} [config.readOnly] Surface is read-only
30 * @param {string} [config.inDialog] The name of the dialog this surface widget is in
31 */
32 ve.ui.TargetWidget = function VeUiTargetWidget( config ) {
33 // Config initialization
34 config = config || {};
35
36 // Parent constructor
37 ve.ui.TargetWidget.super.call( this, config );
38
39 // Mixin constructor
40 OO.ui.mixin.PendingElement.call( this, config );
41
42 // Properties
43 this.toolbarGroups = config.toolbarGroups;
44 // TODO: Override document/targetTriggerListener
45 this.includeCommands = config.includeCommands;
46 this.excludeCommands = config.excludeCommands;
47 this.multiline = config.multiline !== false;
48 this.placeholder = config.placeholder;
49 this.readOnly = config.readOnly;
50 this.importRules = config.importRules;
51 this.inDialog = config.inDialog;
52 this.modes = config.modes;
53 this.defaultMode = config.defaultMode;
54
55 this.target = this.createTarget();
56
57 if ( config.doc ) {
58 this.setDocument( config.doc );
59 }
60
61 // Initialization
62 this.$element.addClass( 've-ui-targetWidget' )
63 .append( this.target.$element );
64 };
65
66 /* Inheritance */
67
68 OO.inheritClass( ve.ui.TargetWidget, OO.ui.Widget );
69 OO.mixinClass( ve.ui.TargetWidget, OO.ui.mixin.PendingElement );
70
71 /* Methods */
72
73 /**
74 * The target's surface has been changed.
75 *
76 * @event ve.ui.TargetWidget#change
77 */
78
79 /**
80 * The target's surface has been submitted, e.g. Ctrl+Enter
81 *
82 * @event ve.ui.TargetWidget#submit
83 */
84
85 /**
86 * The target's surface has been cancelled, e.g. Escape
87 *
88 * @event ve.ui.TargetWidget#cancel
89 */
90
91 /**
92 * A document has been attached to the target, and a toolbar and surface created.
93 *
94 * @event ve.ui.TargetWidget#setup
95 */
96
97 /**
98 * Create the target for this widget to use
99 *
100 * @return {ve.init.Target}
101 */
102 ve.ui.TargetWidget.prototype.createTarget = function () {
103 return new ve.init.Target( {
104 register: false,
105 toolbarGroups: this.toolbarGroups,
106 modes: this.modes,
107 defaultMode: this.defaultMode
108 } );
109 };
110
111 /**
112 * Set the document to edit
113 *
114 * This replaces the entire surface in the target.
115 *
116 * @param {ve.dm.Document} doc
117 * @fires ve.ui.TargetWidget#change
118 * @fires ve.ui.TargetWidget#setup
119 * @fires ve.ce.Surface#position
120 */
121 ve.ui.TargetWidget.prototype.setDocument = function ( doc ) {
122 // Destroy the previous surface
123 this.clear();
124 const surface = this.target.addSurface( doc, {
125 inTargetWidget: true,
126 includeCommands: this.includeCommands,
127 excludeCommands: this.excludeCommands,
128 importRules: this.importRules,
129 multiline: this.multiline,
130 placeholder: this.placeholder,
131 readOnly: this.readOnly,
132 // Reduce from default 10 so inspector callouts are positioned correctly
133 overlayPadding: 5,
134 inDialog: this.inDialog
135 } );
136 this.target.setSurface( surface );
137
138 // Events
139 surface.getView().connect( this, {
140 activation: 'onFocusChange',
141 focus: 'onFocusChange',
142 blur: 'onFocusChange'
143 } );
144 // Rethrow as target events so users don't have to re-bind when the surface is changed
145 surface.getModel().connect( this, { history: [ 'emit', 'change' ] } );
146 surface.connect( this, {
147 submit: 'onSurfaceSubmit',
148 cancel: 'onSurfaceCancel'
149 } );
150 // Emit 'position' on first focus, as target widgets are often setup before being made visible. (T303795)
151 surface.getView().once( 'focus', () => {
152 surface.getView().emit( 'position' );
153 } );
154
155 this.emit( 'setup' );
156 };
157
158 /**
159 * Handle surface submit events
160 *
161 * @fires ve.ui.TargetWidget#submit
162 */
163 ve.ui.TargetWidget.prototype.onSurfaceSubmit = function () {
164 const handled = this.emit( 'submit' );
165 if ( !handled && this.inDialog ) {
166 // If we are in a dialog, re-throw a fake keydown event for OO.ui.Dialog#onDialogKeyDown
167 this.$element.parent().trigger( $.Event( 'keydown', {
168 which: OO.ui.Keys.ENTER,
169 ctrlKey: true
170 } ) );
171 }
172 };
173
174 /**
175 * Handle surface cancel events
176 *
177 * @fires ve.ui.TargetWidget#cancel
178 */
179 ve.ui.TargetWidget.prototype.onSurfaceCancel = function () {
180 const handled = this.emit( 'cancel' );
181 if ( !handled && this.inDialog ) {
182 // If we are in a dialog, re-throw a fake keydown event for OO.ui.Dialog#onDialogKeyDown
183 this.$element.parent().trigger( $.Event( 'keydown', {
184 which: OO.ui.Keys.ESCAPE
185 } ) );
186 }
187 };
188
189 /**
190 * Check if the surface has been modified.
191 *
192 * @return {boolean} The surface has been modified
193 */
194 ve.ui.TargetWidget.prototype.hasBeenModified = function () {
195 return !!this.getSurface() && this.getSurface().getModel().hasBeenModified();
196 };
197
198 /**
199 * Set the read-only state of the widget
200 *
201 * @param {boolean} readOnly Make widget read-only
202 */
203 ve.ui.TargetWidget.prototype.setReadOnly = function ( readOnly ) {
204 this.readOnly = !!readOnly;
205 if ( this.getSurface() ) {
206 this.getSurface().setReadOnly( this.readOnly );
207 }
208 this.$element.toggleClass( 've-ui-targetWidget-readOnly', this.readOnly );
209 };
210
211 /**
212 * Check if the widget is read-only
213 *
214 * @return {boolean}
215 */
216 ve.ui.TargetWidget.prototype.isReadOnly = function () {
217 return this.readOnly;
218 };
219
220 /**
221 * Get surface.
222 *
223 * @return {ve.ui.Surface|null}
224 */
225 ve.ui.TargetWidget.prototype.getSurface = function () {
226 return this.target.getSurface();
227 };
228
229 /**
230 * Get toolbar.
231 *
232 * @return {OO.ui.Toolbar}
233 */
234 ve.ui.TargetWidget.prototype.getToolbar = function () {
235 return this.target.getToolbar();
236 };
237
238 /**
239 * Get content data.
240 *
241 * @return {Array} Content data
242 */
243 ve.ui.TargetWidget.prototype.getContent = function () {
244 return this.getSurface().getModel().getDocument().getData();
245 };
246
247 /**
248 * Initialize surface and toolbar.
249 *
250 * Widget must be attached to DOM before initializing.
251 *
252 * @deprecated
253 */
254 ve.ui.TargetWidget.prototype.initialize = function () {
255 OO.ui.warnDeprecation( 've.ui.TargetWidget#initialize is deprecated and no longer needed.' );
256 };
257
258 /**
259 * Destroy surface and toolbar.
260 */
261 ve.ui.TargetWidget.prototype.clear = function () {
262 this.target.clearSurfaces();
263 // Clear toolbar?
264 };
265
266 /**
267 * Handle focus and blur events
268 */
269 ve.ui.TargetWidget.prototype.onFocusChange = function () {
270 // This may be null if the target is in the process of being destroyed
271 const surface = this.getSurface();
272 // Replacement for the :focus pseudo selector one would be able to
273 // use on a regular input widget
274 this.$element.toggleClass(
275 've-ui-targetWidget-focused',
276 surface && surface.getView().isFocused() && !surface.getView().isDeactivated()
277 );
278 };
279
280 /**
281 * Focus the surface.
282 */
283 ve.ui.TargetWidget.prototype.focus = function () {
284 const surface = this.getSurface();
285 if ( surface ) {
286 if ( !surface.getView().attachedRoot.isLive() ) {
287 surface.once( 'ready', () => {
288 surface.getView().focus();
289 } );
290 } else {
291 surface.getView().focus();
292 }
293 }
294 };