Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/layouts/ve.ui.MWTwoPaneTransclusionDialogLayout.js

raw symbols SCIP
1 /**
2 * Specialized layout forked from and similar to {@see OO.ui.BookletLayout}, but to synchronize the
3 * sidebar and content pane of the transclusion dialog.
4 *
5 * Also owns the outline controls.
6 *
7 * This class has domain knowledge about its contents, for example different
8 * behaviors for template vs template parameter elements.
9 *
10 * @class
11 * @extends OO.ui.MenuLayout
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 * @property {Object.<string,OO.ui.PageLayout>} pages
16 * @property {string} currentPageName Name of the currently selected transclusion item (top-level
17 * part or template parameter). Typically represented as a blue bar in the sidebar. Special cases
18 * you should be aware of:
19 * - An unchecked parameter exists as an item in the sidebar, but not as a page in the content
20 * pane.
21 * - A parameter placeholder (to add an undocumented parameter) exists as a page in the content
22 * pane, but has no corresponding item in the sidebar.
23 */
24 ve.ui.MWTwoPaneTransclusionDialogLayout = function VeUiMWTwoPaneTransclusionDialogLayout( config ) {
25 // Parent constructor
26 ve.ui.MWTwoPaneTransclusionDialogLayout.super.call( this, config );
27
28 // Properties
29 this.pages = {};
30 this.currentPageName = null;
31 this.stackLayout = new ve.ui.MWVerticalLayout();
32 this.setContentPanel( this.stackLayout );
33 this.sidebar = new ve.ui.MWTransclusionOutlineWidget();
34 this.outlinePanel = new OO.ui.PanelLayout( { expanded: this.expanded, scrollable: true } );
35 this.setMenuPanel( this.outlinePanel );
36 this.outlineControlsWidget = new ve.ui.MWTransclusionOutlineControlsWidget();
37
38 // Events
39 this.sidebar.connect( this, {
40 filterPagesByName: 'onFilterPagesByName',
41 sidebarItemSelected: 'onSidebarItemSelected'
42 } );
43 // Event 'focus' does not bubble, but 'focusin' does
44 this.stackLayout.$element.on( 'focusin', this.onStackLayoutFocus.bind( this ) );
45
46 // Initialization
47 this.$element.addClass( 've-ui-mwTwoPaneTransclusionDialogLayout' );
48 this.stackLayout.$element.addClass( 've-ui-mwTwoPaneTransclusionDialogLayout-stackLayout' );
49 this.outlinePanel.$element
50 .addClass( 've-ui-mwTwoPaneTransclusionDialogLayout-outlinePanel' )
51 .append(
52 $( '<div>' ).addClass( 've-ui-mwTwoPaneTransclusionDialogLayout-sidebar-container' )
53 .append( this.sidebar.$element ),
54 this.outlineControlsWidget.$element
55 );
56 };
57
58 /* Setup */
59
60 OO.inheritClass( ve.ui.MWTwoPaneTransclusionDialogLayout, OO.ui.MenuLayout );
61
62 /* Methods */
63
64 /**
65 * @private
66 * @param {Object.<string,boolean>} visibility
67 */
68 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.onFilterPagesByName = function ( visibility ) {
69 this.currentPageName = null;
70 for ( const pageName in visibility ) {
71 const page = this.getPage( pageName );
72 if ( page ) {
73 page.toggle( visibility[ pageName ] );
74 }
75 }
76 };
77
78 /**
79 * @param {ve.dm.MWTransclusionPartModel|null} removed Removed part
80 * @param {ve.dm.MWTransclusionPartModel|null} added Added part
81 * @param {number} [newPosition]
82 */
83 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.onReplacePart = function ( removed, added, newPosition ) {
84 this.sidebar.onReplacePart( removed, added, newPosition );
85
86 const keys = Object.keys( this.pages ),
87 isMultiPart = keys.length > 1,
88 isLastPlaceholder = keys.length === 1 &&
89 this.pages[ keys[ 0 ] ] instanceof ve.ui.MWTemplatePlaceholderPage;
90
91 // TODO: In other cases this is disabled rather than hidden. See T311303
92 this.outlineControlsWidget.removeButton.toggle( !isLastPlaceholder );
93
94 if ( isMultiPart ) {
95 // Warning, this is intentionally never turned off again
96 this.outlineControlsWidget.toggle( true );
97 }
98 };
99
100 /**
101 * @private
102 * @param {jQuery.Event} e Focusin event
103 */
104 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.onStackLayoutFocus = function ( e ) {
105 // Find the page that an element was focused within
106 const $target = $( e.target ).closest( '.oo-ui-pageLayout' );
107 for ( const name in this.pages ) {
108 if ( this.pages[ name ].$element[ 0 ] === $target[ 0 ] ) {
109 this.setPage( name );
110 break;
111 }
112 }
113 };
114
115 /**
116 * Focus the input field for the current page.
117 *
118 * If the focus is already in an element on the current page, nothing will happen.
119 */
120 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.focus = function () {
121 const page = this.pages[ this.currentPageName ];
122 if ( !page ) {
123 return;
124 }
125
126 // Only change the focus if it's visible and is not already the current page
127 if ( page.$element[ 0 ].offsetParent !== null &&
128 !OO.ui.contains( page.$element[ 0 ], this.getElementDocument().activeElement, true )
129 ) {
130 page.focus();
131 }
132 };
133
134 /**
135 * @param {string} pageName
136 */
137 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.focusPart = function ( pageName ) {
138 this.setPage( pageName );
139 this.focus();
140 };
141
142 /**
143 * Parts and parameters can be soft-selected, or selected and focused.
144 *
145 * @param {string|null} pageName Full, unique name of part or parameter, or null to deselect
146 * @param {boolean} [soft] If true, suppress content pane focus.
147 */
148 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.onSidebarItemSelected = function ( pageName, soft ) {
149 this.setPage( pageName );
150
151 const page = this.pages[ pageName ];
152 if ( page ) {
153 // Warning, scrolling must be done before focussing. The browser will trigger a conflicting
154 // scroll when the focussed element is out of view.
155 page.scrollElementIntoView( { alignToTop: true, padding: { top: 20 } } );
156 }
157
158 // We assume "mobile" means "touch device with on-screen keyboard". That should only open when
159 // tapping the input field, not when navigating in the sidebar.
160 if ( !soft && !OO.ui.isMobile() ) {
161 this.focus();
162 }
163 };
164
165 /**
166 * @param {boolean} show If the sidebar should be shown or not.
167 */
168 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.toggleOutline = function ( show ) {
169 this.toggleMenu( show );
170 if ( show ) {
171 // HACK: Kill dumb scrollbars when the sidebar stops animating, see T161798.
172 // Only necessary when outline controls are present, delay matches transition on
173 // `.oo-ui-menuLayout-menu`.
174 setTimeout( () => {
175 OO.ui.Element.static.reconsiderScrollbars( this.outlinePanel.$element[ 0 ] );
176 }, OO.ui.theme.getDialogTransitionDuration() );
177 }
178 };
179
180 /**
181 * @return {ve.ui.MWTransclusionOutlineControlsWidget}
182 */
183 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.getOutlineControls = function () {
184 return this.outlineControlsWidget;
185 };
186
187 /**
188 * Get the list of pages on the stack ordered by appearance.
189 *
190 * @return {OO.ui.PageLayout[]}
191 */
192 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.getPagesOrdered = function () {
193 return this.stackLayout.getItems();
194 };
195
196 /**
197 * @param {string} name Symbolic name of page
198 * @return {OO.ui.PageLayout|undefined} Page, if found
199 */
200 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.getPage = function ( name ) {
201 return this.pages[ name ];
202 };
203
204 /**
205 * @return {OO.ui.PageLayout|undefined} Current page, if found
206 */
207 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.getCurrentPage = function () {
208 return this.pages[ this.currentPageName ];
209 };
210
211 /**
212 * @return {string|null} A top-level part id like "part_0" if that part is selected. When a
213 * parameter is selected null is returned.
214 */
215 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.getSelectedTopLevelPartId = function () {
216 const page = this.getCurrentPage(),
217 isParameter = page instanceof ve.ui.MWParameterPage || page instanceof ve.ui.MWAddParameterPage;
218 return page && !isParameter ? page.getName() : null;
219 };
220
221 /**
222 * @return {string|null} A top-level part id like "part_0" that corresponds to the current
223 * selection, whatever is selected. When a parameter is selected the id of the template the
224 * parameter belongs to is returned.
225 */
226 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.getTopLevelPartIdForSelection = function () {
227 return this.currentPageName ? this.currentPageName.split( '/', 1 )[ 0 ] : null;
228 };
229
230 /**
231 * When pages are added with the same names as existing pages, the existing pages will be
232 * automatically removed before the new pages are added.
233 *
234 * @param {OO.ui.PageLayout[]} pages Pages to add
235 * @param {number} index Index of the insertion point
236 */
237 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.addPages = function ( pages, index ) {
238 const stackLayoutPages = this.stackLayout.getItems();
239
240 // Remove pages with same names
241 const remove = [];
242 for ( let i = 0; i < pages.length; i++ ) {
243 const page = pages[ i ];
244 const name = page.getName();
245
246 if ( Object.prototype.hasOwnProperty.call( this.pages, name ) ) {
247 // Correct the insertion index
248 const currentIndex = stackLayoutPages.indexOf( this.pages[ name ] );
249 if ( currentIndex !== -1 && currentIndex + 1 < index ) {
250 index--;
251 }
252 remove.push( name );
253 }
254 }
255 if ( remove.length ) {
256 this.removePages( remove );
257 }
258
259 // Add new pages
260 for ( let i = 0; i < pages.length; i++ ) {
261 const page = pages[ i ];
262 const name = page.getName();
263 this.pages[ name ] = page;
264 }
265
266 this.stackLayout.addItems( pages, index );
267 };
268
269 /**
270 * @param {string[]} pagesNamesToRemove
271 */
272 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.removePages = function ( pagesNamesToRemove ) {
273 const pagesToRemove = [],
274 isCurrentParameter = this.pages[ this.currentPageName ] instanceof ve.ui.MWParameterPage;
275 let isCurrentPageRemoved = false,
276 prevSelectionCandidate, nextSelectionCandidate;
277
278 this.stackLayout.getItems().forEach( ( page ) => {
279 const pageName = page.getName();
280
281 if ( pagesNamesToRemove.indexOf( pageName ) !== -1 ) {
282 pagesToRemove.push( page );
283 delete this.pages[ pageName ];
284 if ( this.currentPageName === pageName ) {
285 this.currentPageName = null;
286 isCurrentPageRemoved = true;
287 }
288 return;
289 }
290
291 // Move the selection from a removed top-level part to another, but not to a parameter
292 if ( pageName.indexOf( '/' ) === -1 ) {
293 if ( !isCurrentPageRemoved ) {
294 // The last part before the removed one
295 prevSelectionCandidate = pageName;
296 } else if ( !nextSelectionCandidate ) {
297 // The first part after the removed one
298 nextSelectionCandidate = pageName;
299 }
300 }
301 } );
302
303 this.stackLayout.removeItems( pagesToRemove );
304 if ( isCurrentPageRemoved && !isCurrentParameter ) {
305 this.setPage( nextSelectionCandidate || prevSelectionCandidate );
306 }
307 };
308
309 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.clearPages = function () {
310 this.pages = {};
311 this.currentPageName = null;
312 this.sidebar.clear();
313 this.stackLayout.clearItems();
314 };
315
316 /**
317 * Set the current page and sidebar selection, by symbolic name. Doesn't focus the input.
318 *
319 * @param {string} [name] Symbolic name of page. Omit to remove current selection.
320 */
321 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.setPage = function ( name ) {
322 const page = this.pages[ name ];
323
324 if ( page && name === this.currentPageName ) {
325 return;
326 }
327
328 const previousPage = this.currentPageName ? this.pages[ this.currentPageName ] : null;
329 this.currentPageName = name;
330
331 if ( previousPage ) {
332 // Blur anything focused if the next page doesn't have anything focusable.
333 // This is not needed if the next page has something focusable (because once it is
334 // focused this blur happens automatically).
335 if ( !OO.ui.isMobile() &&
336 ( !page || OO.ui.findFocusable( page.$element ).length !== 0 )
337 ) {
338 const $focused = previousPage.$element.find( ':focus' );
339 if ( $focused.length ) {
340 $focused[ 0 ].blur();
341 }
342 }
343 }
344
345 this.sidebar.setSelectionByPageName( name );
346 this.refreshControls();
347 };
348
349 /**
350 * @private
351 */
352 ve.ui.MWTwoPaneTransclusionDialogLayout.prototype.refreshControls = function () {
353 const partId = this.getSelectedTopLevelPartId(),
354 canBeDeleted = !!partId;
355
356 let canMoveUp, canMoveDown = false;
357 if ( partId ) {
358 const pages = this.stackLayout.getItems(),
359 page = this.getPage( partId ),
360 index = pages.indexOf( page );
361 canMoveUp = index > 0;
362 // Check if there is at least one more top-level part below the current one
363 for ( let i = index + 1; i < pages.length; i++ ) {
364 if ( !( pages[ i ] instanceof ve.ui.MWParameterPage || pages[ i ] instanceof ve.ui.MWAddParameterPage ) ) {
365 canMoveDown = true;
366 break;
367 }
368 }
369 }
370
371 this.outlineControlsWidget.setButtonsEnabled( {
372 canMoveUp: canMoveUp,
373 canMoveDown: canMoveDown,
374 canBeDeleted: canBeDeleted
375 } );
376 };