Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/actions/ve.ui.WindowAction.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface WindowAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Window action.
9 *
10 * @class
11 * @extends ve.ui.Action
12 * @constructor
13 * @param {ve.ui.Surface} surface Surface to act on
14 * @param {string} [source]
15 */
16 ve.ui.WindowAction = function VeUiWindowAction() {
17 // Parent constructor
18 ve.ui.WindowAction.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.WindowAction, ve.ui.Action );
24
25 /* Static Properties */
26
27 ve.ui.WindowAction.static.name = 'window';
28
29 ve.ui.WindowAction.static.methods = [ 'open', 'close', 'toggle' ];
30
31 /* Methods */
32
33 /**
34 * Open a window.
35 *
36 * @param {string} name Symbolic name of window to open
37 * @param {Object} [data] Window opening data
38 * @param {string} [action] Action to execute after opening, or immediately if the window is already open
39 * @return {boolean|jQuery.Promise} Action was executed; if a Promise, it'll resolve once the action is finished executing
40 */
41 ve.ui.WindowAction.prototype.open = function ( name, data, action ) {
42 data = data || {};
43 const windowType = this.getWindowType( name ),
44 windowManager = this.getWindowManager( windowType ),
45 currentWindow = windowManager.getCurrentWindow(),
46 autoClosePromises = [],
47 surface = this.surface,
48 surfaceFragment = data.fragment || surface.getModel().getFragment( undefined, true ),
49 dir = surface.getView().getSelectionDirectionality(),
50 windowClass = ve.ui.windowFactory.lookup( name ),
51 isFragmentWindow = !!windowClass.prototype.getFragment,
52 mayRequireFragment = isFragmentWindow ||
53 // HACK: Pass fragment to toolbar dialogs as well
54 windowType.name === 'toolbar',
55 // TODO: Add 'doesHandleSource' method to factory
56 sourceMode = surface.getMode() === 'source' && !windowClass.static.handlesSource,
57 openDeferred = ve.createDeferred(),
58 openPromise = openDeferred.promise();
59
60 ve.track(
61 'activity.' + name,
62 { action: 'window-open-from-' + ( this.source || 'command' ) }
63 );
64
65 if ( !windowManager ) {
66 return false;
67 }
68
69 let fragmentPromise;
70 let originalFragment;
71 if ( !mayRequireFragment ) {
72 fragmentPromise = ve.createDeferred().resolve().promise();
73 } else if ( sourceMode ) {
74 const text = surfaceFragment.getText( true );
75 originalFragment = surfaceFragment;
76
77 fragmentPromise = surfaceFragment.convertFromSource( text ).then( ( selectionDocument ) => {
78 const tempSurfaceModel = new ve.dm.Surface( selectionDocument ),
79 tempFragment = tempSurfaceModel.getLinearFragment(
80 // TODO: Select all content using content offset methods
81 new ve.Range(
82 1,
83 Math.max( 1, selectionDocument.getDocumentRange().end - 1 )
84 )
85 );
86
87 return tempFragment;
88 } );
89 } else {
90 fragmentPromise = ve.createDeferred().resolve( surfaceFragment ).promise();
91 }
92
93 data = ve.extendObject( { dir: dir }, data, { surface: surface, $returnFocusTo: null } );
94
95 if ( windowType.name === 'toolbar' || windowType.name === 'inspector' ) {
96 // Auto-close the current window if it is different to the one we are
97 // trying to open.
98 // TODO: Make auto-close a window manager setting
99 if ( currentWindow && currentWindow.constructor.static.name !== name ) {
100 autoClosePromises.push( windowManager.closeWindow( currentWindow ).closed );
101 }
102 }
103
104 // If we're opening a dialog, close all inspectors first
105 if ( windowType.name === 'dialog' ) {
106 const inspectorWindowManager = this.getWindowManager( { name: 'inspector' } );
107 const currentInspector = inspectorWindowManager.getCurrentWindow();
108 if ( currentInspector ) {
109 autoClosePromises.push( inspectorWindowManager.closeWindow( currentInspector ).closed );
110 }
111 }
112
113 fragmentPromise.then( ( fragment ) => {
114 ve.extendObject( data, { fragment: fragment } );
115
116 ve.promiseAll( autoClosePromises ).always( () => {
117 windowManager.getWindow( name ).then( ( win ) => {
118 const instance = windowManager.openWindow( win, data );
119
120 if ( sourceMode ) {
121 win.sourceMode = sourceMode;
122 }
123
124 if ( !win.constructor.static.activeSurface ) {
125 surface.getView().deactivate( false );
126 }
127
128 instance.opened.then( () => {
129 if ( sourceMode ) {
130 // HACK: initialFragment/previousSelection is assumed to be in the visible surface
131 win.initialFragment = null;
132 win.previousSelection = null;
133 }
134 } );
135 instance.opened.always( () => {
136 // This uses .always() so that the action is executed even if the window is already open
137 // (in which case opening it again fails). Hopefully we'll never have a situation where
138 // it's closed, the opening fails for some reason, and then weird things happen.
139 if ( action ) {
140 win.executeAction( action );
141 }
142 openDeferred.resolve( instance );
143 } );
144
145 if ( !win.constructor.static.activeSurface ) {
146 windowManager.once( 'closing', () => {
147 // Collapsed mobile selection: We need to re-activate the surface in case an insertion
148 // annotation was generated. We also need to do it during the same event cycle otherwise
149 // the device may not open the virtual keyboard, so use the 'closing' event. (T203517)
150 if ( OO.ui.isMobile() && surface.getModel().getSelection().isCollapsed() ) {
151 surface.getView().activate();
152 } else {
153 // Otherwise use the `closed` promise to wait until the dialog has performed its actions,
154 // such as creating new annotations or moving focus, before re-activating.
155 instance.closed.then( () => {
156 // Don't activate if mobile and expanded
157 if ( !( OO.ui.isMobile() && !surface.getModel().getSelection().isCollapsed() ) ) {
158 surface.getView().activate();
159 }
160 } );
161 }
162 } );
163 }
164
165 instance.closed.then( ( closedData ) => {
166 // Sequence-triggered window closed without action, undo
167 if ( data.strippedSequence && !( closedData && closedData.action ) ) {
168 surface.getModel().undo();
169 // Prevent redoing (which would remove the typed text)
170 surface.getModel().truncateUndoStack();
171 surface.getModel().emit( 'history' );
172 }
173 if ( sourceMode && fragment && fragment.getSurface().hasBeenModified() ) {
174 // Action may be async, so we use auto select to ensure the content is selected
175 originalFragment.setAutoSelect( true );
176 originalFragment.insertDocument( fragment.getDocument() );
177 }
178 surface.getView().emit( 'position' );
179 } );
180 } );
181 } );
182 } );
183
184 return openPromise;
185 };
186
187 /**
188 * Close a window
189 *
190 * @param {string} name Symbolic name of window to open
191 * @param {Object} [data] Window closing data
192 * @return {boolean} Action was executed
193 */
194 ve.ui.WindowAction.prototype.close = function ( name, data ) {
195 const windowType = this.getWindowType( name ),
196 windowManager = this.getWindowManager( windowType );
197
198 if ( !windowManager ) {
199 return false;
200 }
201
202 windowManager.closeWindow( name, data );
203 return true;
204 };
205
206 /**
207 * Toggle a window between open and close
208 *
209 * @param {string} name Symbolic name of window to open or close
210 * @param {Object} [data] Window opening or closing data
211 * @return {boolean} Action was executed
212 */
213 ve.ui.WindowAction.prototype.toggle = function ( name, data ) {
214 const windowType = this.getWindowType( name ),
215 windowManager = this.getWindowManager( windowType );
216
217 if ( !windowManager ) {
218 return false;
219 }
220
221 const win = windowManager.getCurrentWindow();
222 if ( !win || win.constructor.static.name !== name ) {
223 this.open( name, data );
224 } else {
225 this.close( name, data );
226 }
227 return true;
228 };
229
230 /**
231 * @typedef {Object} WindowType
232 * @memberof ve.ui.WindowAction
233 * @property {string|null} name Window name ('inspector', 'toolbar', 'dialog' or null)
234 * @property {string} [position] Window position (for toolbar dialogs)
235 */
236
237 /**
238 * Get the specified window type
239 *
240 * @param {string} name Window name
241 * @return {ve.ui.WindowAction.WindowType}
242 */
243 ve.ui.WindowAction.prototype.getWindowType = function ( name ) {
244 const windowClass = ve.ui.windowFactory.lookup( name );
245 if ( !windowClass ) {
246 throw new Error( 'No window class registered with the name "' + name + '"' );
247 }
248 if ( windowClass.prototype instanceof ve.ui.FragmentInspector ) {
249 return { name: 'inspector' };
250 } else if ( windowClass.prototype instanceof ve.ui.ToolbarDialog ) {
251 return {
252 name: 'toolbar',
253 position: windowClass.static.position
254 };
255 } else if ( windowClass.prototype instanceof OO.ui.Dialog ) {
256 return { name: 'dialog' };
257 }
258 return { name: null };
259 };
260
261 /**
262 * Get the window manager for a specified window type
263 *
264 * @param {ve.ui.WindowAction.WindowType} windowType Window type object. See #getWindowType
265 * @return {ve.ui.WindowManager|null} Window manager
266 */
267 ve.ui.WindowAction.prototype.getWindowManager = function ( windowType ) {
268 switch ( windowType.name ) {
269 case 'inspector':
270 return this.surface.getContext().getInspectors();
271 case 'toolbar':
272 return this.surface.getToolbarDialogs( windowType.position );
273 case 'dialog':
274 return this.surface.getDialogs();
275 }
276 return null;
277 };
278
279 /* Registration */
280
281 ve.ui.actionFactory.register( ve.ui.WindowAction );