import { createState , removeState } from '../store' ; import { State } from '../types/store' ; import { createPicker } from './picker/create-picker' ; import { destroyHook , getDate } from './util/method' ; import { BetterPicker , BetterPickerInstance } from '../types/core' ; import { getEventListener } from '../utils/event' ; import { listenToScrollParents } from '../utils/listenToParents' ; import clickOutside from '../utils/clickoutside' ; import { Off } from '../types/event' ; import { Bind } from '../utils/bind' ; import { canIUseAnimation } from '../utils/env' ; export default function Picker ( ) { let state ; let onRef , offRef , onBody , offBody ; function openPopover ( ) { if ( state ) state . visible = true ; } function popoverVisibleControl ( ) { if ( ! state . reference ) return ; [ onRef , offRef ] = getEventListener ( state . reference ) ; [ onBody , offBody ] = getEventListener ( document . body ) ; onRef ( openPopover ) ; onBody ( Bind ( clickOutside , state ) ) ; if ( state ) listenToScrollParents ( state ) ; } function getCurrentDate ( ) { return getDate ( state ) ; } function update ( options ) { state = Object . assign ( state , options ) ; } function destroyed ( ) { destroyHook ( state ) ; offBody ( ) ; offRef ( ) ; removeState ( state . id ) ; } function listenToAnimation ( pop ) { if ( ! pop ) return ; pop . style . display = 'none' ; if ( canIUseAnimation ( ) ) { pop . addEventListener ( 'animationend' , ( e ) => { pop . style . display = e . animationName === 'hidden' ? 'none' : '' ; } ) ; } } function create ( options ) { state = Object . assign ( createState ( options ) , { update , destroyed , } ) ; changeWeekFormat ( ) ; popoverVisibleControl ( ) ; createPicker ( state ) ; listenToAnimation ( state . popover ) ; } function clear ( ) { if ( state . reference ) { state . reference . value = '' ; state . range . start = state . range . end = state . start . date = state . end . date = null ; } } function open ( ) { state . visible = true ; } function close ( ) { state . visible = false ; } function changeWeekFormat ( ) { if ( state . type === 'week' ) { state . format = state . locale . weekFormat ; } } return function ( options ) : BetterPickerInstance { create ( options ) ; return { id : state . id , state , getCurrentDate , update , destroyed , clear , open , close , } ; } ; }