Browse Source

Optimized BalloonPanelView#keepAttachedTo by extracting some logic from DOM "scroll" listener.

Aleksander Nowodzinski 8 years ago
parent
commit
6921e60044
1 changed files with 9 additions and 5 deletions
  1. 9 5
      packages/ckeditor5-ui/src/panel/balloon/balloonpanelview.js

+ 9 - 5
packages/ckeditor5-ui/src/panel/balloon/balloonpanelview.js

@@ -180,16 +180,20 @@ export default class BalloonPanelView extends View {
 		// First we need to attach the balloon panel to the target element.
 		this.attachTo( options );
 
-		const target = options.target;
 		const limiter = options.limiter || defaultLimiterElement;
+		let target = null;
+
+		// We need to take HTMLElement related to the target if it is possible.
+		if ( isElement( options.target ) ) {
+			target = target;
+		} else if ( isRange( options.target ) ) {
+			target = options.target.commonAncestorContainer;
+		}
 
 		// Then we need to listen on scroll event of eny element in the document.
 		this.listenTo( global.document, 'scroll', ( evt, domEvt ) => {
-			// We need to take HTMLElement related to the target if it is possible.
-			const targetNode = isElement( target ) ? target : isRange( target ) ? target.commonAncestorContainer : null;
-
 			// We need to update position if scrolled element contains related to the balloon elements.
-			if ( ( targetNode && domEvt.target.contains( targetNode ) ) || domEvt.target.contains( limiter ) ) {
+			if ( ( target && domEvt.target.contains( target ) ) || domEvt.target.contains( limiter ) ) {
 				this.attachTo( options );
 			}
 		}, { useCapture: true } );