import { print as printWindow , createElement , detach } from '@syncfusion/ej2-base' ; import { IGrid , PrintEventArgs } from '../base/interface' ; import { removeElement , getActualProperties , getActualPropFromColl } from '../base/util' ; import { Scroll } from '../actions/scroll' ; import { Grid } from '../base/grid' ; import * as events from '../base/constant' ; /**
 *
 * The `Print` module is used to handle print action.
 */ export class Print { private parent : IGrid ; private printWind : Window ; private scrollModule : Scroll ; private isAsyncPrint : boolean = false ; private printing : string = 'isPrinting' ; private static printGridProp : string [ ] = [ 'aggregates' , 'allowGrouping' , 'allowFiltering' , 'allowMultiSorting' , 'allowReordering' , 'allowSorting' , 'allowTextWrap' , 'childGrid' , 'columns' , 'currentViewData' , 'dataSource' , 'detailTemplate' , 'enableAltRow' , 'enableColumnVirtualization' , 'filterSettings' , 'gridLines' , 'groupSettings' , 'height' , 'locale' , 'pageSettings' , 'printMode' , 'query' , 'queryString' , 'rowHeight' , 'rowTemplate' , 'sortSettings' , 'textWrapSettings' , 'allowPaging' , events . beforePrint , events . printComplete ] ; hidden constructor ( parent ? , scrollModule ? ) { this . parent = parent ; if ( this . parent . isDestroyed ) { return ; } this . parent . on ( events . contentReady , this . contentReady . bind ( this ) ) ; this . parent . addEventListener ( events . actionBegin , this . actionBegin . bind ( this ) ) ; this . parent . on ( events . onEmpty , this . onEmpty . bind ( this ) ) ; this . scrollModule = scrollModule ; } return { void } public print ( ) { this . renderPrintGrid ( ) ; this . printWind = window . open ( '' , 'print' , 'height=' + window . outerHeight + ',width=' + window . outerWidth + ',tabbar=no' ) ; this . printWind . moveTo ( 0 , 0 ) ; this . printWind . resizeTo ( screen . availWidth , screen . availHeight ) ; } private onEmpty ( ) { if ( this . isPrintGrid ( ) ) { this . contentReady ( ) ; } } private actionBegin ( ) { if ( this . isPrintGrid ( ) ) { this . isAsyncPrint = true ; } } private renderPrintGrid ( ) { let gObj = this . parent ; let elem = 'element' ; let printGridModel = { } ; let element = createElement ( 'div' , { id : this . parent . element . id + '_print' , className : gObj . element . className + ' e-print-grid' } ) ; document . body . appendChild ( element ) ; for ( let key of Print . printGridProp ) { if ( key === 'columns' ) { printGridModel [ key ] = getActualPropFromColl ( gObj [ key ] ) ; } else if ( key === 'allowPaging' ) { printGridModel [ key ] = this . parent . printMode === 'CurrentPage' ; } else { printGridModel [ key ] = getActualProperties ( gObj [ key ] ) ; } } let printGrid = new Grid ( printGridModel ) ; printGrid . appendTo ( element as HTMLElement ) ; printGrid . registeredTemplate = this . parent . registeredTemplate ; printGrid [ this . printing ] = true ; printGrid . trigger = gObj . trigger ; } private contentReady ( ) { if ( this . isPrintGrid ( ) ) { let gObj = this . parent ; if ( this . isAsyncPrint ) { this . printGrid ( ) ; return ; } let args = { requestType : 'print' , element : gObj . element , selectedRows : gObj . getContentTable ( ) . querySelectorAll ( 'tr[aria-selected="true"]' ) , cancel : false } ; if ( ! this . isAsyncPrint ) { gObj . trigger ( events . beforePrint , args ) ; } if ( args . cancel ) { detach ( gObj . element ) ; return ; } else if ( ! this . isAsyncPrint ) { this . printGrid ( ) ; } } } private printGrid ( ) { let gObj = this . parent ; if ( gObj . allowPaging && gObj . printMode === 'CurrentPage' ) { ( gObj . element . querySelector ( '.e-gridpager' ) as HTMLElement ) . style . display = 'none' ; } if ( gObj . height !== 'auto' ) { let cssProps = this . scrollModule . getCssProperties ( ) ; let contentDiv = ( gObj . element . querySelector ( '.e-content' ) as HTMLElement ) ; let headerDiv = ( < HTMLElement > gObj . element . querySelector ( '.e-gridheader' ) ) ; contentDiv . style . height = 'auto' ; contentDiv . style . overflowY = 'auto' ; headerDiv . style [ cssProps . padding ] = '' ; ( headerDiv . firstElementChild as HTMLElement ) . style [ cssProps . border ] = '' ; } if ( gObj . allowGrouping ) { if ( ! gObj . groupSettings . columns . length ) { ( gObj . element . querySelector ( '.e-groupdroparea' ) as HTMLElement ) . style . display = 'none' ; } else { this . removeColGroup ( gObj . groupSettings . columns . length , gObj . element ) ; removeElement ( gObj . element , '.e-grouptopleftcell' ) ; removeElement ( gObj . element , '.e-recordpluscollapse' ) ; removeElement ( gObj . element , '.e-indentcell' ) ; removeElement ( gObj . element , '.e-recordplusexpand' ) ; } } ( gObj . element . querySelector ( '.e-content' ) as HTMLElement ) . style . overflowX = 'hidden' ; if ( gObj . allowFiltering && gObj . filterSettings . type === 'FilterBar' ) { ( gObj . element . querySelector ( '.e-filterbar' ) as HTMLElement ) . style . display = 'none' ; } let waitingPop = gObj . element . querySelectorAll ( '.e-spin-show' ) ; if ( waitingPop . length > 0 ) { waitingPop [ 0 ] . classList . add ( 'e-spin-hide' ) ; waitingPop [ 0 ] . classList . remove ( 'e-spin-show' ) ; } if ( gObj [ this . printing ] ) { detach ( gObj . element ) ; } gObj . element . classList . remove ( 'e-print-grid' ) ; this . printWind = printWindow ( gObj . element , this . printWind ) ; gObj [ this . printing ] = false ; let args = { element : gObj . element } ; gObj . trigger ( events . printComplete , args ) ; } private removeColGroup ( depth , element ) { let groupCaption = element . querySelectorAll ( '.e-groupcaption' ) ; let colSpan = ( < HTMLElement > groupCaption [ depth - 1 ] ) . getAttribute ( 'colspan' ) ; for ( let i = 0 ; i < groupCaption . length ; i ++ ) { ( < HTMLElement > groupCaption [ i ] ) . setAttribute ( 'colspan' , colSpan ) ; } let colGroups = element . querySelectorAll ( 'colgroup' ) ; for ( let i = 0 ; i < colGroups . length ; i ++ ) { for ( let j = 0 ; j < depth ; j ++ ) { ( < HTMLElement > colGroups [ i ] . childNodes [ j ] ) . style . display = 'none' ; } } } private isPrintGrid ( ) { return this . parent . element . id . indexOf ( '_print' ) > 0 && this . parent [ this . printing ] ; } return { void } hidden public destroy ( ) { if ( this . parent . isDestroyed ) { return ; } this . parent . off ( events . contentReady , this . contentReady . bind ( this ) ) ; this . parent . removeEventListener ( events . actionBegin , this . actionBegin . bind ( this ) ) ; this . parent . off ( events . onEmpty , this . onEmpty . bind ( this ) ) ; } private protected getModuleName ( ) { return 'print' ; } }