Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #158 from ckeditor/t/157
Browse files Browse the repository at this point in the history
Other: `getOptimalPosition` utility should accept the target option defined as a function. Closes #157.
  • Loading branch information
oskarwrobel authored May 17, 2017
2 parents aa3e35d + 01b8684 commit d63abae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/dom/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ import getPositionedAncestor from './getpositionedancestor';
* @returns {module:utils/dom/position~Position}
*/
export function getOptimalPosition( { element, target, positions, limiter, fitInViewport } ) {
// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
// https://github.com/ckeditor/ckeditor5-utils/issues/157
if ( typeof target == 'function' ) {
target = target();
}

const positionedElementAncestor = getPositionedAncestor( element.parentElement );
const elementRect = new Rect( element );
const targetRect = new Rect( target );
Expand Down Expand Up @@ -255,7 +261,7 @@ function getAbsoluteRectCoordinates( { left, top } ) {
/**
* Target with respect to which the `element` is to be positioned.
*
* @member {HTMLElement|Range|ClientRect} #target
* @member {HTMLElement|Range|ClientRect|Function} #target
*/

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/dom/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ describe( 'getOptimalPosition()', () => {
} );
} );

it( 'should work when the target is a Function', () => {
setElementTargetPlayground();

assertPosition( {
element,
target: () => target,
positions: [ attachLeft ]
}, {
top: 100,
left: 80,
name: 'left'
} );
} );

describe( 'for single position', () => {
beforeEach( setElementTargetPlayground );

Expand Down

0 comments on commit d63abae

Please sign in to comment.