@@ -18,7 +18,7 @@ import {
1818} from "rxjs" ;
1919import { throttleTime } from "rxjs/operators" ;
2020// the AbsolutePosition is required to import the declaration correctly
21- import position , { AbsolutePosition } from "./../ utils/ position" ;
21+ import { position , AbsolutePosition } from "@carbon/ utils- position" ;
2222import { cycleTabs , getFocusElementList } from "./../common/tab.service" ;
2323import { DialogConfig } from "./dialog-config.interface" ;
2424
@@ -40,63 +40,46 @@ import { DialogConfig } from "./dialog-config.interface";
4040export class Dialog implements OnInit , AfterViewInit , OnDestroy {
4141 /**
4242 * One static event observable to handle window resizing.
43- * @protected
44- * @static
45- * @type {Observable<any> }
46- * @memberof Dialog
4743 */
4844 protected static resizeObservable : Observable < any > = fromEvent ( window , "resize" ) . pipe ( throttleTime ( 100 ) ) ;
4945 /**
5046 * Emits event that handles the closing of a `Dialog` object.
51- * @type {EventEmitter<any> }
52- * @memberof Dialog
5347 */
5448 @Output ( ) close : EventEmitter < any > = new EventEmitter ( ) ;
5549 /**
5650 * Receives `DialogConfig` interface object with properties of `Dialog`
57- * explictly defined.
58- * @type {DialogConfig }
59- * @memberof Dialog
51+ * explicitly defined.
6052 */
6153 @Input ( ) dialogConfig : DialogConfig ;
6254 /**
6355 * Maintains a reference to the view DOM element of the `Dialog`.
64- * @type {ElementRef }
65- * @memberof Dialog
6656 */
6757 @ViewChild ( "dialog" ) dialog : ElementRef ;
6858
6959 /**
7060 * Stores the data received from `dialogConfig`.
71- * @memberof Dialog
7261 */
7362 public data = { } ;
7463
7564 /**
76- * The placement of the `Dialog` is recieved from the `Position` service.
77- * @type {Placement }
78- * @memberof Dialog
65+ * The placement of the `Dialog` is received from the `Position` service.
7966 */
8067 public placement : string ;
8168
8269 /**
8370 * `Subscription` used to update placement in the event of a window resize.
84- * @protected
85- * @type {Subscription }
86- * @memberof Dialog
8771 */
8872 protected resizeSubscription : Subscription ;
8973 /**
9074 * Subscription to all the scrollable parents `scroll` event
9175 */
92- // add a new subscription temprarily so that contexts (such as tests)
76+ // add a new subscription temporarily so that contexts (such as tests)
9377 // that don't run ngAfterViewInit have something to unsubscribe in ngOnDestroy
9478 protected scrollSubscription : Subscription = new Subscription ( ) ;
9579 /**
9680 * Handles offsetting the `Dialog` item based on the defined position
9781 * to not obscure the content beneath.
9882 * @protected
99- * @memberof Dialog
10083 */
10184 protected addGap = {
10285 "left" : pos => position . addOffset ( pos , 0 , - this . dialogConfig . gap ) ,
@@ -110,13 +93,11 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
11093 /**
11194 * Creates an instance of `Dialog`.
11295 * @param {ElementRef } elementRef
113- * @memberof Dialog
11496 */
115- constructor ( protected elementRef : ElementRef ) { }
97+ constructor ( protected elementRef : ElementRef ) { }
11698
11799 /**
118- * Initilize the `Dialog`, set the placement and gap, and add a `Subscription` to resize events.
119- * @memberof Dialog
100+ * Initialize the `Dialog`, set the placement and gap, and add a `Subscription` to resize events.
120101 */
121102 ngOnInit ( ) {
122103 this . placement = this . dialogConfig . placement . split ( "," ) [ 0 ] ;
@@ -126,14 +107,13 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
126107 this . placeDialog ( ) ;
127108 } ) ;
128109
129- // run any additional initlization code that consuming classes may have
110+ // run any additional initialization code that consuming classes may have
130111 this . onDialogInit ( ) ;
131112 }
132113
133114 /**
134115 * After the DOM is ready, focus is set and dialog is placed
135116 * in respect to the parent element.
136- * @memberof Dialog
137117 */
138118 ngAfterViewInit ( ) {
139119 const dialogElement = this . dialog . nativeElement ;
@@ -236,30 +216,9 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
236216 // split always returns an array, so we can just use the auto position logic
237217 // for single positions too
238218 const placements = this . dialogConfig . placement . split ( "," ) ;
239- const weightedPlacements = placements . map ( placement => {
240- const pos = findPosition ( parentEl , el , placement ) ;
241- let box = position . getPlacementBox ( el , pos ) ;
242- let hiddenHeight = box . bottom - window . innerHeight - window . scrollY ;
243- let hiddenWidth = box . right - window . innerWidth - window . scrollX ;
244- // if the hiddenHeight or hiddenWidth is negative, reset to offsetHeight or offsetWidth
245- hiddenHeight = hiddenHeight < 0 ? el . offsetHeight : hiddenHeight ;
246- hiddenWidth = hiddenWidth < 0 ? el . offsetWidth : hiddenWidth ;
247- const area = el . offsetHeight * el . offsetWidth ;
248- const hiddenArea = hiddenHeight * hiddenWidth ;
249- let visibleArea = area - hiddenArea ;
250- // if the visibleArea is 0 set it back to area (to calculate the percentage in a useful way)
251- visibleArea = visibleArea === 0 ? area : visibleArea ;
252- const visiblePercent = visibleArea / area ;
253- return {
254- placement,
255- weight : visiblePercent
256- } ;
257- } ) ;
258219
259- // sort the placements from best to worst
260- weightedPlacements . sort ( ( a , b ) => b . weight - a . weight ) ;
261- // pick the best!
262- dialogPlacement = weightedPlacements [ 0 ] . placement ;
220+ // find the best placement
221+ dialogPlacement = position . findBestPlacement ( parentEl , el , placements ) ;
263222
264223 // calculate the final position
265224 const pos = findPosition ( parentEl , el , dialogPlacement ) ;
@@ -293,7 +252,6 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
293252 * Sets up a event Listener to close `Dialog` if click event occurs outside
294253 * `Dialog` object.
295254 * @param {any } event
296- * @memberof Dialog
297255 */
298256 @HostListener ( "document:click" , [ "$event" ] )
299257 clickClose ( event ) {
@@ -305,15 +263,13 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
305263
306264 /**
307265 * Closes `Dialog` object by emitting the close event upwards to parents.
308- * @memberof Dialog
309266 */
310267 public doClose ( ) {
311268 this . close . emit ( ) ;
312269 }
313270
314271 /**
315272 * At destruction of component, `Dialog` unsubscribes from handling window resizing changes.
316- * @memberof Dialog
317273 */
318274 ngOnDestroy ( ) {
319275 this . resizeSubscription . unsubscribe ( ) ;
0 commit comments