-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
364 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
Frame, | ||
PlacementOptions, | ||
PopoverPlacement, | ||
PopoverPlacements, | ||
} from './type'; | ||
|
||
const PLACEMENT_FAMILIES: string[] = [ | ||
PopoverPlacements.BOTTOM.rawValue, | ||
PopoverPlacements.TOP.rawValue, | ||
PopoverPlacements.LEFT.rawValue, | ||
PopoverPlacements.RIGHT.rawValue, | ||
]; | ||
|
||
export class PopoverPlacementService { | ||
|
||
public find(preferredValue: PopoverPlacement, options: PlacementOptions): PopoverPlacement { | ||
const placement: PopoverPlacement = this.findRecursive(preferredValue, PLACEMENT_FAMILIES, options); | ||
|
||
return placement || preferredValue; | ||
} | ||
|
||
private findRecursive(placement: PopoverPlacement, families: string[], options: PlacementOptions): PopoverPlacement { | ||
const oneOfCurrentFamily: PopoverPlacement = this.findFromFamily(placement, options); | ||
|
||
if (oneOfCurrentFamily) { | ||
return oneOfCurrentFamily; | ||
} | ||
|
||
const oneOfReversedFamily: PopoverPlacement = this.findFromFamily(placement.reverse(), options); | ||
|
||
if (oneOfReversedFamily) { | ||
return oneOfReversedFamily; | ||
} | ||
|
||
delete families[families.indexOf(placement.parent().rawValue)]; | ||
delete families[families.indexOf(placement.reverse().parent().rawValue)]; | ||
|
||
const firstTruthy: string = families.filter(Boolean)[0]; | ||
|
||
if (firstTruthy) { | ||
const nextPlacement: PopoverPlacement = PopoverPlacements.parse(firstTruthy); | ||
|
||
return this.findRecursive(nextPlacement, families, options); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private findFromFamily(placement: PopoverPlacement, options: PlacementOptions): PopoverPlacement { | ||
const preferredFrame: Frame = placement.frame(options); | ||
|
||
if (placement.fits(preferredFrame, options.bounds)) { | ||
return placement; | ||
} | ||
|
||
return placement.family().find((familyValue: PopoverPlacement): boolean => { | ||
const familyFrame = familyValue.frame(options); | ||
|
||
return familyValue.fits(familyFrame, options.bounds); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.