Debian Codegraph @

skins/Vector/resources/skins.vector.legacy.js/collapsibleTabs.js

raw symbols SCIP
1 /* eslint-disable no-jquery/no-jquery-constructor, no-jquery/no-other-methods, no-jquery/no-class,
2 no-jquery/no-extend, no-jquery/no-data, no-jquery/no-css, no-jquery/no-visibility,
3 no-jquery/no-trigger, no-jquery/no-is-empty-object, no-jquery/no-find-collection,
4 no-jquery/no-attr, no-jquery/no-parent, no-jquery/no-each-collection */
5 /**
6 * This adds behaviour to Vector's tabs in the bottom right so that at smaller
7 * displays they collapse under the more menu.
8 */
9
10 /** @interface CollapsibleTabsOptions */
11 function init() {
12 /** @type {boolean|undefined} */ let boundEvent;
13 const isRTL = document.documentElement.dir === 'rtl';
14 const rAF = window.requestAnimationFrame || setTimeout;
15
16 // Mark the tabs which can be collapsed under the more menu
17 // eslint-disable-next-line no-jquery/no-global-selector
18 $( '#p-views li' )
19 .not( '#ca-watch, #ca-unwatch' ).addClass( 'collapsible' );
20
21 $.fn.collapsibleTabs = function ( options ) {
22 // Merge options into the defaults
23 const settings = $.extend( {}, $.collapsibleTabs.defaults, options );
24
25 // return if the function is called on an empty jquery object
26 if ( !this.length ) {
27 return this;
28 }
29
30 this.each( function () {
31 const $el = $( this );
32 // add the element to our array of collapsible managers
33 $.collapsibleTabs.instances.push( $el );
34 // attach the settings to the elements
35 $el.data( 'collapsibleTabsSettings', settings );
36 // attach data to our collapsible elements
37 $el.children( settings.collapsible ).each( function () {
38 $.collapsibleTabs.addData( $( this ) );
39 } );
40 } );
41
42 // if we haven't already bound our resize handler, bind it now
43 if ( !boundEvent ) {
44 boundEvent = true;
45 $( window ).on( 'resize', mw.util.debounce( () => {
46 rAF( $.collapsibleTabs.handleResize );
47 }, 10 ) );
48 }
49
50 // call our resize handler to setup the page
51 rAF( $.collapsibleTabs.handleResize );
52 // When adding new links, a resize should be triggered (T139830).
53 mw.hook( 'util.addPortletLink' ).add( $.collapsibleTabs.handleResize );
54 return this;
55 };
56 $.collapsibleTabs = {
57 instances: [],
58 defaults: {
59 expandedContainer: '#p-views ul',
60 collapsedContainer: '#p-cactions ul',
61 collapsible: 'li.collapsible',
62 shifting: false,
63 expandedWidth: 0,
64 expandCondition: function ( eleWidth ) {
65 // If there are at least eleWidth + 1 pixels of free space, expand.
66 // We add 1 because .width() will truncate fractional values but .offset() will not.
67 return $.collapsibleTabs.calculateTabDistance() >= eleWidth + 1;
68 },
69 collapseCondition: function () {
70 // If there's an overlap, collapse.
71 return $.collapsibleTabs.calculateTabDistance() < 0;
72 }
73 },
74 addData: function ( $collapsible ) {
75 const settings = $collapsible.parent().data( 'collapsibleTabsSettings' );
76 if ( settings ) {
77 $collapsible.data( 'collapsibleTabsSettings', {
78 expandedContainer: settings.expandedContainer,
79 collapsedContainer: settings.collapsedContainer,
80 expandedWidth: $collapsible.outerWidth( true )
81 } );
82 }
83 },
84 getSettings: function ( $collapsible ) {
85 let settings = $collapsible.data( 'collapsibleTabsSettings' );
86 if ( !settings ) {
87 $.collapsibleTabs.addData( $collapsible );
88 settings = $collapsible.data( 'collapsibleTabsSettings' );
89 }
90 // it's possible for getSettings to return undefined
91 // if no data attributes have been set
92 // see T177108#6310908.
93 // In particular, a gadget may have added a collapsible link to the list:
94 // e.g.
95 // $('<li class="collapsible">my link</a>').appendTo( $('#p-cactions ul') )
96 return settings || {};
97 },
98 handleResize: function () {
99 $.collapsibleTabs.instances.forEach( ( $el ) => {
100 const data = $.collapsibleTabs.getSettings( $el );
101
102 if ( $.isEmptyObject( data ) || data.shifting ) {
103 return;
104 }
105
106 // if the two navigations are colliding
107 if ( $el.children( data.collapsible ).length && data.collapseCondition() ) {
108 /**
109 * Fired before tabs are moved to "collapsedContainer".
110 *
111 * @event beforeTabCollapse
112 * @memberof jQuery.plugin.collapsibleTabs
113 */
114 $el.trigger( 'beforeTabCollapse' );
115 // Move the element to the dropdown menu.
116 $.collapsibleTabs.moveToCollapsed( $el.children( data.collapsible ).last() );
117 }
118
119 const $tab = $( data.collapsedContainer ).children( data.collapsible ).first();
120 // if there are still moveable items in the dropdown menu,
121 // and there is sufficient space to place them in the tab container
122 if (
123 $( data.collapsedContainer + ' ' + data.collapsible ).length &&
124 data.expandCondition(
125 $.collapsibleTabs.getSettings( $tab ).expandedWidth
126 )
127 ) {
128 /**
129 * Fired before tabs are moved to "expandedContainer".
130 *
131 * @event beforeTabExpand
132 * @memberof jQuery.plugin.collapsibleTabs
133 */
134 $el.trigger( 'beforeTabExpand' );
135 $.collapsibleTabs.moveToExpanded( $tab );
136 }
137 } );
138 },
139 moveToCollapsed: function ( $moving ) {
140 const outerData = $.collapsibleTabs.getSettings( $moving );
141 if ( !outerData ) {
142 return;
143 }
144 const collapsedContainerSettings = $.collapsibleTabs.getSettings(
145 $( outerData.expandedContainer )
146 );
147 if ( !collapsedContainerSettings ) {
148 return;
149 }
150 collapsedContainerSettings.shifting = true;
151
152 // Remove the element from where it's at and put it in the dropdown menu
153 const target = outerData.collapsedContainer;
154 // eslint-disable-next-line no-jquery/no-animate
155 $moving.css( 'position', 'relative' )
156 .css( ( isRTL ? 'left' : 'right' ), 0 )
157 .animate( { width: '1px' }, 'normal', function () {
158 $( this ).hide();
159 // add the placeholder
160 $( '<span>' ).addClass( 'placeholder' ).css( 'display', 'none' ).insertAfter( this );
161 $( this ).detach().prependTo( target ).data( 'collapsibleTabsSettings', outerData );
162 $( this ).attr( 'style', 'display: list-item;' );
163 collapsedContainerSettings.shifting = false;
164 rAF( $.collapsibleTabs.handleResize );
165 } );
166 },
167 moveToExpanded: function ( $moving ) {
168 const data = $.collapsibleTabs.getSettings( $moving );
169 if ( !data ) {
170 return;
171 }
172 const expandedContainerSettings =
173 $.collapsibleTabs.getSettings( $( data.expandedContainer ) );
174 if ( !expandedContainerSettings ) {
175 return;
176 }
177 expandedContainerSettings.shifting = true;
178
179 // grab the next appearing placeholder so we can use it for replacing
180 const $target = $( data.expandedContainer ).find( 'span.placeholder' ).first();
181 const expandedWidth = data.expandedWidth;
182 $moving.css( 'position', 'relative' ).css( ( isRTL ? 'right' : 'left' ), 0 ).css( 'width', '1px' );
183 $target.replaceWith(
184 // eslint-disable-next-line no-jquery/no-animate
185 $moving
186 .detach()
187 .css( 'width', '1px' )
188 .data( 'collapsibleTabsSettings', data )
189 .animate( { width: expandedWidth + 'px' }, 'normal', function () {
190 $( this ).attr( 'style', 'display: block;' );
191 rAF( () => {
192 // Update the 'expandedWidth' in case someone was brazen enough to
193 // change the tab's contents after the page load *gasp* (T71729). This
194 // doesn't prevent a tab from collapsing back and forth once, but at
195 // least it won't continue to do that forever.
196 data.expandedWidth = $moving.outerWidth( true ) || 0;
197 $moving.data( 'collapsibleTabsSettings', data );
198 expandedContainerSettings.shifting = false;
199 $.collapsibleTabs.handleResize();
200 } );
201 } )
202 );
203 },
204 /**
205 * Get the amount of horizontal distance between the two tabs groups in pixels.
206 *
207 * Uses `#left-navigation` and `#right-navigation`. If negative, this
208 * means that the tabs overlap, and the value is the width of overlapping
209 * parts.
210 *
211 * Used in default `expandCondition` and `collapseCondition` options.
212 *
213 * @return {number} distance/overlap in pixels
214 */
215 calculateTabDistance: function () {
216 let leftTab, rightTab, leftEnd, rightStart;
217
218 // In RTL, #right-navigation is actually on the left and vice versa.
219 // Hooray for descriptive naming.
220 if ( !isRTL ) {
221 leftTab = document.getElementById( 'left-navigation' );
222 rightTab = document.getElementById( 'right-navigation' );
223 } else {
224 leftTab = document.getElementById( 'right-navigation' );
225 rightTab = document.getElementById( 'left-navigation' );
226 }
227 if ( leftTab && rightTab ) {
228 leftEnd = leftTab.getBoundingClientRect().right;
229 rightStart = rightTab.getBoundingClientRect().left;
230 return rightStart - leftEnd;
231 }
232 return 0;
233 }
234 };
235 }
236
237 module.exports = Object.freeze( { init: init } );