@@ -15,12 +15,12 @@ import { NG_VALUE_ACCESSOR } from "@angular/forms";
1515
1616// Observable import is required here so typescript can compile correctly
1717import { Observable , fromEvent , of , Subscription } from "rxjs" ;
18- import { throttleTime } from "rxjs/operators" ;
1918
2019import { AbstractDropdownView } from "./abstract-dropdown-view.class" ;
2120import { position } from "../utils/position" ;
2221import { I18n } from "./../i18n/i18n.module" ;
2322import { ListItem } from "./list-item.interface" ;
23+ import { DropdownService } from "./dropdown.service" ;
2424
2525/**
2626 * Drop-down lists enable users to select one or more items from a list.
@@ -183,21 +183,12 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
183183 */
184184 dropUp = false ;
185185
186- /**
187- * Used by the various appendToX methods to keep a reference to our wrapper div
188- */
189- dropdownWrapper : HTMLElement ;
190186 // .bind creates a new function, so we declare the methods below
191187 // but .bind them up here
192188 noop = this . _noop . bind ( this ) ;
193189 outsideClick = this . _outsideClick . bind ( this ) ;
194190 outsideKey = this . _outsideKey . bind ( this ) ;
195191 keyboardNav = this . _keyboardNav . bind ( this ) ;
196- /**
197- * Maintains an Event Observable Subscription for tracking window resizes.
198- * Window resizing is tracked if the `Dropdown` is appended to the body, otherwise it does not need to be supported.
199- */
200- resize : Subscription ;
201192 /**
202193 * Maintians an Event Observable Subscription for tracking scrolling within the open `Dropdown` list.
203194 */
@@ -208,7 +199,7 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
208199 /**
209200 * Creates an instance of Dropdown.
210201 */
211- constructor ( protected elementRef : ElementRef , protected i18n : I18n ) { }
202+ constructor ( protected elementRef : ElementRef , protected i18n : I18n , protected dropdownService : DropdownService ) { }
212203
213204 /**
214205 * Updates the `type` property in the `@ContentChild`.
@@ -413,37 +404,19 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
413404 * Creates the `Dropdown` list appending it to the dropdown parent object instead of the body.
414405 */
415406 _appendToDropdown ( ) {
416- if ( document . body . contains ( this . dropdownWrapper ) ) {
417- this . dropdownMenu . nativeElement . style . display = "none" ;
418- this . elementRef . nativeElement . appendChild ( this . dropdownMenu . nativeElement ) ;
419- document . body . removeChild ( this . dropdownWrapper ) ;
420- this . resize . unsubscribe ( ) ;
421- this . dropdownWrapper . removeEventListener ( "keydown" , this . keyboardNav , true ) ;
422- }
407+ this . dropdownService . appendToDropdown ( this . elementRef . nativeElement ) ;
408+ this . dropdownMenu . nativeElement . removeEventListener ( "keydown" , this . keyboardNav , true ) ;
423409 }
424410
425411 /**
426412 * Creates the `Dropdown` list as an element that is appended to the DOM body.
427413 */
428414 _appendToBody ( ) {
429- const positionDropdown = ( ) => {
430- let pos = position . findAbsolute ( this . dropdownButton . nativeElement , this . dropdownWrapper , "bottom" ) ;
431- // add -40 to the top position to account for carbon styles
432- pos = position . addOffset ( pos , - 40 , 0 ) ;
433- position . setElement ( this . dropdownWrapper , pos ) ;
434- } ;
435- this . dropdownMenu . nativeElement . style . display = "block" ;
436- this . dropdownWrapper = document . createElement ( "div" ) ;
437- this . dropdownWrapper . className = `dropdown ${ this . elementRef . nativeElement . className } ` ;
438- this . dropdownWrapper . style . width = this . dropdownButton . nativeElement . offsetWidth + "px" ;
439- this . dropdownWrapper . style . position = "absolute" ;
440- this . dropdownWrapper . appendChild ( this . dropdownMenu . nativeElement ) ;
441- document . body . appendChild ( this . dropdownWrapper ) ;
442- positionDropdown ( ) ;
443- this . dropdownWrapper . addEventListener ( "keydown" , this . keyboardNav , true ) ;
444- this . resize = fromEvent ( window , "resize" )
445- . pipe ( throttleTime ( 100 ) )
446- . subscribe ( ( ) => positionDropdown ( ) ) ;
415+ this . dropdownService . appendToBody (
416+ this . dropdownButton . nativeElement ,
417+ this . dropdownMenu . nativeElement ,
418+ this . elementRef . nativeElement . className ) ;
419+ this . dropdownMenu . nativeElement . addEventListener ( "keydown" , this . keyboardNav , true ) ;
447420 }
448421
449422 /**
@@ -529,12 +502,7 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
529502 this . scroll = fromEvent ( container , "scroll" )
530503 . subscribe ( ( ) => {
531504 if ( this . isVisibleInContainer ( this . elementRef . nativeElement , container ) ) {
532- position . setElement (
533- this . dropdownWrapper ,
534- position . addOffset (
535- position . findAbsolute ( this . elementRef . nativeElement , this . dropdownWrapper , "bottom" )
536- )
537- ) ;
505+ this . dropdownService . updatePosition ( this . dropdownButton . nativeElement ) ;
538506 } else {
539507 this . closeMenu ( ) ;
540508 }
0 commit comments