import { createPlugin , findElements , flushToDom , CalendarContext , removeExact , config , } from '@fullcalendar/common' ; import premiumCommonPlugin from '@fullcalendar/premium-common' ; import '@fullcalendar/premium-common' ; import './main.css' ; config . COLLAPSIBLE_WIDTH_THRESHOLD = 1200 ; let contexts = [ ] ; let undoFuncs = [ ] ; export default createPlugin ( { deps : [ premiumCommonPlugin , ] , contextInit ( context ) { if ( ! contexts . length ) { attachGlobalHandlers ( ) ; } contexts . push ( context ) ; context . calendarApi . on ( '_unmount' , ( ) => { removeExact ( contexts , context ) ; if ( ! contexts . length ) { removeGlobalHandlers ( ) ; } } ) ; } } ) ; function attachGlobalHandlers ( ) { window . addEventListener ( 'beforeprint' , handleBeforePrint ) ; window . addEventListener ( 'afterprint' , handleAfterPrint ) ; } function removeGlobalHandlers ( ) { window . removeEventListener ( 'beforeprint' , handleBeforePrint ) ; window . removeEventListener ( 'afterprint' , handleAfterPrint ) ; } function handleBeforePrint ( ) { let scrollEls = queryScrollerEls ( ) ; let scrollCoords = queryScrollerCoords ( scrollEls ) ; for ( let context of contexts ) { context . emitter . trigger ( '_beforeprint' ) ; } flushToDom ( ) ; killHorizontalScrolling ( scrollEls , scrollCoords ) ; undoFuncs . push ( ( ) => restoreScrollerCoords ( scrollEls , scrollCoords ) ) ; undoFuncs . push ( freezeScrollgridWidths ( ) ) ; } function handleAfterPrint ( ) { for ( let context of contexts ) { context . emitter . trigger ( '_afterprint' ) ; } flushToDom ( ) ; while ( undoFuncs . length ) { undoFuncs . shift ( ) ( ) ; } } function freezeScrollgridWidths ( ) { let els = findElements ( document . body , '.fc-scrollgrid' ) ; els . forEach ( freezeScrollGridWidth ) ; return ( ) => els . forEach ( unfreezeScrollGridWidth ) ; } function freezeScrollGridWidth ( el ) { let elWidth = el . getBoundingClientRect ( ) . width ; if ( ! el . classList . contains ( 'fc-scrollgrid-collapsible' ) || elWidth < config . COLLAPSIBLE_WIDTH_THRESHOLD ) { el . style . width = elWidth + 'px' ; } } function unfreezeScrollGridWidth ( el ) { el . style . width = '' ; } function queryScrollerEls ( ) { return findElements ( document . body , '.fc-scroller-harness > .fc-scroller' ) ; } interface ScrollerCoords { scrollLeft : number ; scrollTop : number ; overflowX : string ; overflowY : string ; marginBottom : string ; } function queryScrollerCoords ( els ) { return els . map ( ( el ) => { let computedStyle = window . getComputedStyle ( el ) ; return { scrollLeft : el . scrollLeft , scrollTop : el . scrollTop , overflowX : computedStyle . overflowX , overflowY : computedStyle . overflowY , marginBottom : computedStyle . marginBottom } ; } ) ; } function killHorizontalScrolling ( els , coords ) { els . forEach ( ( el , i ) => { el . style . overflowX = 'visible' ; el . style . overflowY = 'visible' ; el . style . marginBottom = '' ; el . style . left = - coords [ i ] . scrollLeft + 'px' ; } ) ; } function restoreScrollerCoords ( els , coords ) { els . forEach ( ( el , i ) => { let c = coords [ i ] ; el . style . overflowX = c . overflowX ; el . style . overflowY = c . overflowY ; el . style . marginBottom = c . marginBottom ; el . style . left = '' ; el . scrollLeft = c . scrollLeft ; el . scrollTop = c . scrollTop ; } ) ; }