@@ -18,13 +18,13 @@ import StageUpdateAfterParamsInterface from "../../stage-update-after-params.d";
18
18
import ContentTypeAfterRenderEventParamsInterface from "../content-type-after-render-event-params.d" ;
19
19
import ContentTypeDroppedCreateEventParamsInterface from "../content-type-dropped-create-event-params" ;
20
20
import PreviewCollection from "../preview-collection" ;
21
+ import BeforeSetFocusedEventInterface from "../tabs/before-set-focused-event" ;
21
22
22
23
/**
23
24
* @api
24
25
*/
25
26
export default class Preview extends PreviewCollection {
26
27
public isLiveEditing : KnockoutObservable < boolean > = ko . observable ( false ) ;
27
- public currentMaxWidth : KnockoutObservable < number > = ko . observable ( 0 ) ;
28
28
/**
29
29
* Keeps track of number of button item to disable sortable if there is only 1.
30
30
*/
@@ -63,6 +63,10 @@ export default class Preview extends PreviewCollection {
63
63
this . resizeChildButtons ( ) ;
64
64
} , 500 ) . call ( this ) ;
65
65
} ) ;
66
+
67
+ events . on ( "tabs:beforeSetFocused" , ( eventData : BeforeSetFocusedEventInterface ) => {
68
+ this . resizeChildButtons ( ) ;
69
+ } ) ;
66
70
}
67
71
68
72
/**
@@ -235,40 +239,27 @@ export default class Preview extends PreviewCollection {
235
239
private resizeChildButtons ( ) {
236
240
if ( this . wrapperElement ) {
237
241
const buttonItems : JQuery = $ ( this . wrapperElement ) . find ( ".pagebuilder-button-item > a" ) ;
238
- let buttonResizeValue : string | number = "" ;
242
+ let buttonResizeValue : string | number = "0 " ;
239
243
if ( this . parent . dataStore . get ( "is_same_width" ) === "true" ) {
240
244
if ( buttonItems . length > 0 ) {
241
- buttonItems . css ( "min-width" , "" ) ;
242
- const currentLargestButton = this . findLargestButton ( buttonItems ) ;
243
- const currentLargestButtonWidth = currentLargestButton . outerWidth ( ) ;
244
- if ( currentLargestButtonWidth !== 0 ) {
245
- buttonResizeValue = currentLargestButtonWidth ;
246
- this . currentMaxWidth ( currentLargestButtonWidth ) ;
247
- } else {
248
- buttonResizeValue = this . currentMaxWidth ( ) ;
249
- }
245
+ const currentLargestButtonWidth = this . findLargestButtonWidth ( buttonItems ) ;
246
+ const parentWrapperWidth = $ ( this . wrapperElement ) . width ( ) ;
247
+ buttonResizeValue = Math . min ( currentLargestButtonWidth , parentWrapperWidth ) ;
250
248
}
251
249
}
250
+
252
251
buttonItems . css ( "min-width" , buttonResizeValue ) ;
253
252
}
254
253
}
255
254
256
255
/**
257
- * Find the largest button which will determine the button width we use for re-sizing .
256
+ * Find the largest button width for calculating same size value .
258
257
*
259
258
* @param {JQuery } buttonItems
260
- * @returns {JQuery }
259
+ * @returns {number }
261
260
*/
262
- private findLargestButton ( buttonItems : JQuery ) : JQuery {
263
- let largestButton : JQuery | null = null ;
264
- buttonItems . each ( ( index , element ) => {
265
- const buttonElement = $ ( element ) ;
266
- if ( largestButton === null
267
- || this . calculateButtonWidth ( buttonElement ) > this . calculateButtonWidth ( largestButton ) ) {
268
- largestButton = buttonElement ;
269
- }
270
- } ) ;
271
- return largestButton ;
261
+ private findLargestButtonWidth ( buttonItems : JQuery ) : number {
262
+ return _ . max ( _ . map ( buttonItems , ( buttonItem ) => this . calculateButtonWidth ( $ ( buttonItem ) ) ) ) ;
272
263
}
273
264
274
265
/**
@@ -279,9 +270,8 @@ export default class Preview extends PreviewCollection {
279
270
*/
280
271
private calculateButtonWidth ( buttonItem : JQuery ) : number {
281
272
const widthProperties = [ "paddingLeft" , "paddingRight" , "borderLeftWidth" , "borderRightWidth" ] ;
282
- const calculatedButtonWidth : number = widthProperties . reduce ( ( accumulatedWidth , widthProperty ) : number => {
273
+ return widthProperties . reduce ( ( accumulatedWidth , widthProperty ) : number => {
283
274
return accumulatedWidth + ( parseInt ( buttonItem . css ( widthProperty ) , 10 ) || 0 ) ;
284
275
} , buttonItem . find ( "[data-element='link_text']" ) . width ( ) ) ;
285
- return calculatedButtonWidth ;
286
276
}
287
277
}
0 commit comments