import { Builder , CSSUtils , EventData , View , ViewBase } from '@nativescript/core' ; export interface ShownBottomSheetData extends EventData { /**
     * The context (optional, may be undefined) passed to the view when shown modally.
     */ context ? : any ; /**
     * A callback to call when you want to close the modally shown view.
     * Pass in any kind of arguments and you will receive when the callback parameter
     * of View.showModal is executed.
     */ closeCallback ? : Function ; } export enum StateBottomSheet { CLOSED = - 1 , COLLAPSED = 0 , EXPANDED = 1 , DRAGGING = 2 } export type onChangeStateBottomSheet = ( stateBottomSheet : StateBottomSheet , slideOffset ? : number ) => void ; export const shownInBottomSheetEvent = 'shownInBottomSheet' ; export const showingInBottomSheetEvent = 'showingInBottomSheet' ; export interface BottomSheetOptions { view : string | ViewBase ; context ? : any ; animated ? : boolean ; dismissOnBackgroundTap ? : boolean ; dismissOnDraggingDownSheet ? : boolean ; dismissOnBackButton ? : boolean ; closeCallback ? : Function ; trackingScrollView ? : string ; transparent ? : boolean ; ignoreTopSafeArea ? : boolean ; ignoreBottomSafeArea ? : boolean ; disableDimBackground ? : boolean ; skipCollapsedState ? : boolean ; peekHeight ? : number ; ignoreKeyboardHeight ? : boolean ; onChangeState ? : onChangeStateBottomSheet ; } export abstract class ViewWithBottomSheetBase extends View { protected _closeBottomSheetCallback : Function ; public _onDismissBottomSheetCallback : Function ; public _onChangeStateBottomSheetCallback : onChangeStateBottomSheet ; _bottomSheetFragment : any ; protected abstract _hideNativeBottomSheet ( parent , whenClosedCallback ) ; protected _bottomSheetContext : any ; _raiseShownBottomSheetEvent ( ) { const args = { eventName : shownInBottomSheetEvent , object : this , context : this . _bottomSheetContext , closeCallback : this . _closeBottomSheetCallback } ; this . notify ( args ) ; } public _bottomSheetClosed ( ) { const _rootModalViews = this . _getRootModalViews ( ) ; const modalIndex = _rootModalViews . indexOf ( this ) ; _rootModalViews . splice ( modalIndex ) ; this . _tearDownUI ( ) ; } protected abstract _showNativeBottomSheet ( parent , options ) ; protected _commonShowNativeBottomSheet ( parent , options ) { this . _getRootModalViews ( ) . push ( this ) ; this . cssClasses . add ( CSSUtils . MODAL_ROOT_VIEW_CSS_CLASS ) ; const modalRootViewCssClasses = CSSUtils . getSystemCssClasses ( ) ; modalRootViewCssClasses . forEach ( ( c ) => this . cssClasses . add ( c ) ) ; options . context = options . context || { } ; this . _bottomSheetContext = options . context ; this . _onDismissBottomSheetCallback = ( ... originalArgs ) => { if ( ! this . _onDismissBottomSheetCallback ) { return ; } this . _onDismissBottomSheetCallback = null ; this . _closeBottomSheetCallback = null ; this . _bottomSheetClosed ( ) ; if ( this . _bottomSheetContext . closeCallback ) { if ( typeof options . closeCallback === 'function' ) { options . closeCallback . apply ( undefined , originalArgs ) ; } } this . _bottomSheetContext = null ; } ; this . _closeBottomSheetCallback = ( ... originalArgs ) => { if ( ! this . _closeBottomSheetCallback ) { return ; } this . _closeBottomSheetCallback = null ; if ( this . _bottomSheetContext ) { this . _bottomSheetContext . closeCallback = null ; } const whenClosedCallback = ( ) => { if ( typeof options . closeCallback === 'function' ) { options . closeCallback . apply ( undefined , originalArgs ) ; } } ; this . _hideNativeBottomSheet ( parent , whenClosedCallback ) ; } ; if ( options . onChangeState && typeof options . onChangeState === 'function' ) { this . _onChangeStateBottomSheetCallback = ( stateBottomSheet , slideOffset ) => { options . onChangeState ( stateBottomSheet , slideOffset ?? stateBottomSheet ) ; } ; } this . _bottomSheetContext . closeCallback = this . _closeBottomSheetCallback ; } protected _raiseShowingBottomSheetEvent ( ) { const args = { eventName : showingInBottomSheetEvent , object : this , context : this . _bottomSheetContext , closeCallback : this . _closeBottomSheetCallback } ; this . notify ( args ) ; } public closeBottomSheet ( ... args ) { const closeCallback = this . _closeBottomSheetCallback ; if ( closeCallback ) { closeCallback . apply ( undefined , arguments ) ; } else { const parent = this . parent as ViewWithBottomSheetBase ; if ( parent ) { parent . closeBottomSheet ( ... args ) ; } } } public showBottomSheet ( options ) { if ( arguments . length === 0 ) { throw new Error ( 'showModal without parameters is deprecated. Please call showModal on a view instance instead.' ) ; } else { const view = options . view instanceof View ? ( options . view as ViewWithBottomSheetBase ) : ( Builder . createViewFromEntry ( { moduleName : options . view as string } ) as ViewWithBottomSheetBase ) ; view . _showNativeBottomSheet ( this , options ) ; return view ; } } }