Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/contexts/ve.ui.LinearContext.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Linear Context class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * UserInterface context.
9 *
10 * @class
11 * @abstract
12 * @extends ve.ui.Context
13 *
14 * @constructor
15 * @param {ve.ui.Surface} surface
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.LinearContext = function VeUiLinearContext() {
19 // Parent constructor
20 ve.ui.LinearContext.super.apply( this, arguments );
21
22 // Properties
23 this.inspector = null;
24 this.inspectors = this.createInspectorWindowManager();
25 this.isOpening = false;
26 this.lastSelectedNode = null;
27 this.afterContextChangeTimeout = null;
28 this.afterContextChangeHandler = this.afterContextChange.bind( this );
29 this.updateDimensionsDebounced = ve.debounce( this.updateDimensions.bind( this ) );
30 this.persistentSources = [];
31
32 // Events
33 this.surface.getModel().connect( this, {
34 contextChange: 'onContextChange',
35 documentUpdate: 'onDocumentUpdate'
36 } );
37 this.inspectors.connect( this, { opening: 'onInspectorOpening' } );
38 };
39
40 /* Inheritance */
41
42 OO.inheritClass( ve.ui.LinearContext, ve.ui.Context );
43
44 /* Static Properties */
45
46 /**
47 * Handle context change event.
48 *
49 * While an inspector is opening or closing, all changes are ignored so as to prevent inspectors
50 * that change the selection from within their setup or teardown processes changing context state.
51 *
52 * The response to selection changes is deferred to prevent teardown processes handlers that change
53 * the selection from causing this function to recurse. These responses are also debounced for
54 * efficiency, so that if there are three selection changes in the same tick, #afterContextChange only
55 * runs once.
56 *
57 * @see #afterContextChange
58 */
59 ve.ui.LinearContext.prototype.onContextChange = function () {
60 if ( this.inspector && ( this.inspector.isOpening() || this.inspector.isClosing() ) ) {
61 // Cancel debounced change handler
62 clearTimeout( this.afterContextChangeTimeout );
63 this.afterContextChangeTimeout = null;
64 this.lastSelectedNode = this.surface.getModel().getSelectedNode();
65 } else {
66 if ( this.afterContextChangeTimeout === null ) {
67 // Ensure change is handled on next cycle
68 this.afterContextChangeTimeout = setTimeout( this.afterContextChangeHandler );
69 }
70 }
71 // Purge related items cache
72 this.relatedSources = null;
73 };
74
75 /**
76 * Handle document update event.
77 */
78 ve.ui.LinearContext.prototype.onDocumentUpdate = function () {
79 // Only mind this event if the menu is visible
80 if ( this.isVisible() && !this.isEmpty() ) {
81 // Reuse the debounced context change handler
82 this.onContextChange();
83 }
84 };
85
86 /**
87 * Handle debounced context change events.
88 */
89 ve.ui.LinearContext.prototype.afterContextChange = function () {
90 const selectedNode = this.surface.getModel().getSelectedNode();
91
92 // Reset debouncing state
93 this.afterContextChangeTimeout = null;
94
95 if ( this.isVisible() ) {
96 if ( !this.isEmpty() ) {
97 if ( this.isInspectable() ) {
98 // Change state: menu -> menu
99 // Make a copy of items so setupMenuItems can compare it
100 const previousItems = this.items.slice();
101 this.teardownMenuItems();
102 this.setupMenuItems( previousItems );
103 this.updateDimensionsDebounced();
104 } else {
105 // Change state: menu -> closed
106 this.toggleMenu( false );
107 this.toggle( false );
108 }
109 } else if (
110 this.inspector &&
111 ( !selectedNode || ( selectedNode !== this.lastSelectedNode ) )
112 ) {
113 // Change state: inspector -> (closed|menu)
114 // Unless there is a selectedNode that hasn't changed (e.g. your inspector is editing a node)
115 this.inspector.close();
116 }
117 } else {
118 if ( this.isInspectable() ) {
119 // Change state: closed -> menu
120 this.toggleMenu( true );
121 this.toggle( true );
122 }
123 }
124
125 this.lastSelectedNode = selectedNode;
126 };
127
128 /**
129 * Handle an inspector opening event.
130 *
131 * @param {OO.ui.Window} win Window that's being opened
132 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
133 * resolved the first argument will be a promise which will be resolved when the window begins
134 * closing, the second argument will be the opening data
135 * @param {Object} data Window opening data
136 */
137 ve.ui.LinearContext.prototype.onInspectorOpening = function ( win, opening ) {
138 const observer = this.surface.getView().surfaceObserver;
139
140 this.isOpening = true;
141 this.inspector = win;
142
143 // Shut down the SurfaceObserver as soon as possible, so it doesn't get confused
144 // by the selection moving around in IE. Will be reenabled when inspector closes.
145 // FIXME this should be done in a nicer way, managed by the Surface classes
146 observer.pollOnce();
147 observer.stopTimerLoop();
148
149 opening
150 .progress( ( data ) => {
151 this.isOpening = false;
152 if ( data.state === 'setup' ) {
153 if ( !this.isVisible() ) {
154 // Change state: closed -> inspector
155 this.toggle( true );
156 }
157 if ( !this.isEmpty() ) {
158 // Change state: menu -> inspector
159 this.toggleMenu( false );
160 }
161 }
162 this.updateDimensionsDebounced();
163 } )
164 .then( ( opened ) => {
165 opened.then( ( closed ) => {
166 closed.always( () => {
167 // Don't try to close the inspector if a second
168 // opening has already been triggered
169 if ( this.isOpening ) {
170 return;
171 }
172
173 this.inspector = null;
174
175 // Reenable observer
176 observer.startTimerLoop();
177
178 if ( this.isInspectable() ) {
179 // Change state: inspector -> menu
180 this.toggleMenu( true );
181 this.updateDimensionsDebounced();
182 } else {
183 // Change state: inspector -> closed
184 this.toggle( false );
185 }
186 } );
187 } );
188 } );
189 };
190
191 /**
192 * Check if context is visible.
193 *
194 * @return {boolean} Context is visible
195 */
196 ve.ui.LinearContext.prototype.isVisible = function () {
197 return this.visible;
198 };
199
200 /**
201 * Check if current content is inspectable.
202 *
203 * @return {boolean} Content is inspectable
204 */
205 ve.ui.LinearContext.prototype.isInspectable = function () {
206 return !!this.getRelatedSources().length;
207 };
208
209 /**
210 * Add a persistent source that will stay visible until manually removed.
211 *
212 * @param {Object} source Object containing `name`, `model` and `config` properties.
213 * See #getRelatedSources.
214 */
215 ve.ui.LinearContext.prototype.addPersistentSource = function ( source ) {
216 this.persistentSources.push(
217 ve.extendObject( { type: 'persistent' }, source )
218 );
219
220 this.onContextChange();
221 };
222
223 /**
224 * Remove a persistent source by name
225 *
226 * @param {string} name Source name
227 */
228 ve.ui.LinearContext.prototype.removePersistentSource = function ( name ) {
229 this.persistentSources = this.persistentSources.filter( ( source ) => source.name !== name );
230
231 this.onContextChange();
232 };
233
234 /**
235 * @inheritdoc Also adds the `embeddable` property to each object.
236 */
237 ve.ui.LinearContext.prototype.getRelatedSources = function () {
238 const surfaceModel = this.surface.getModel(),
239 selection = surfaceModel.getSelection();
240 let selectedModels = [];
241
242 if ( !this.relatedSources ) {
243 this.relatedSources = [];
244 if ( selection instanceof ve.dm.LinearSelection ) {
245 selectedModels = this.surface.getView().getSelectedModels();
246 } else if ( selection instanceof ve.dm.TableSelection ) {
247 selectedModels = [ surfaceModel.getSelectedNode() ];
248 }
249 if ( selectedModels.length ) {
250 this.relatedSources = this.getRelatedSourcesFromModels( selectedModels );
251 }
252 this.relatedSources.push( ...this.persistentSources );
253 }
254
255 return this.relatedSources;
256 };
257
258 /**
259 * Get related for selected models
260 *
261 * @param {ve.dm.Model[]} selectedModels Models
262 * @return {Object[]} See #getRelatedSources
263 */
264 ve.ui.LinearContext.prototype.getRelatedSourcesFromModels = function ( selectedModels ) {
265 const models = [],
266 relatedSources = [],
267 items = ve.ui.contextItemFactory.getRelatedItems( selectedModels );
268
269 let i, len;
270 for ( i = 0, len = items.length; i < len; i++ ) {
271 if ( !items[ i ].model.isInspectable() ) {
272 continue;
273 }
274 if ( ve.ui.contextItemFactory.isExclusive( items[ i ].name ) ) {
275 models.push( items[ i ].model );
276 }
277 relatedSources.push( {
278 type: 'item',
279 embeddable: ve.ui.contextItemFactory.isEmbeddable( items[ i ].name ),
280 name: items[ i ].name,
281 model: items[ i ].model
282 } );
283 }
284 const tools = ve.ui.toolFactory.getRelatedItems( selectedModels );
285 for ( i = 0, len = tools.length; i < len; i++ ) {
286 if ( !tools[ i ].model.isInspectable() ) {
287 continue;
288 }
289 if ( models.indexOf( tools[ i ].model ) === -1 ) {
290 const toolClass = ve.ui.toolFactory.lookup( tools[ i ].name );
291 relatedSources.push( {
292 type: 'tool',
293 embeddable: !toolClass || toolClass.static.makesEmbeddableContextItem,
294 name: tools[ i ].name,
295 model: tools[ i ].model
296 } );
297 }
298 }
299 return relatedSources;
300 };
301
302 /**
303 * Get inspector window set.
304 *
305 * @return {ve.ui.WindowManager}
306 */
307 ve.ui.LinearContext.prototype.getInspectors = function () {
308 return this.inspectors;
309 };
310
311 /**
312 * Create a inspector window manager.
313 *
314 * @abstract
315 * @return {ve.ui.WindowManager} Inspector window manager
316 */
317 ve.ui.LinearContext.prototype.createInspectorWindowManager = null;
318
319 /**
320 * @inheritdoc
321 */
322 ve.ui.LinearContext.prototype.destroy = function () {
323 // Disconnect events
324 this.surface.getModel().disconnect( this );
325 this.inspectors.disconnect( this );
326
327 // Destroy inspectors WindowManager
328 this.inspectors.destroy();
329
330 // Stop timers
331 clearTimeout( this.afterContextChangeTimeout );
332
333 // Parent method
334 return ve.ui.LinearContext.super.prototype.destroy.call( this );
335 };