import { Injectable, TemplateRef, ViewContainerRef, ReflectiveInjector } from '@angular/core'; import { MdDialog, MdDialogConfig, MdDialogRef, MdSidenav } from '@angular/material'; import { CfDialogComponent } from '../../components/dialog/dialog.component'; import { CfCalendarService } from '../../services/calendar-service/calendar.service'; const LIMIT_RANGE = 300; /** *

Component Template Service.

*/ @Injectable() export class CfComponentTemplateService { dialogRef: MdDialogRef; dialogContainer: any; dialogOptions: any; cfCalendarService: CfCalendarService; constructor(public dialog: MdDialog) {} /** *

Method to show template in sidenav.

*

Param 'options' should have mode and align properties as strings with MdDialog format.

*/ showTemplateInSidenav(sidenav: MdSidenav, options): void { sidenav.mode = options.mode || 'over'; sidenav.align = options.align || 'start'; sidenav.open(); } /** *

Method to show template in dialog container.

*/ showTemplateInContainer(templateRef: TemplateRef, containerRef: ViewContainerRef): void { let viewRef = templateRef.createEmbeddedView(null); containerRef.clear(); containerRef.insert(viewRef); } /** *

Method to show template or text in dialog window.

*

Parameter options is an object of structure: * { template: TemplateRef, text: string, dialogOptions: any }. * Note that if text is present, template will be ignored.

*/ showInDialog(options): void { this.dialogOptions = options.dialogOptions; this.openDialog(); this.insertContentToDialog(options); this.setDialogCallbacks(); } /** *

Method to show template or text in floating dialog, next to the cursor where user clicked.

*

Parameter options is an object of structure: * { template: TemplateRef, text: string, dialogOptions: any }. * Note that if text is present, template will be ignored.

*/ showInFloatingDialog(options): void { this.dialogOptions = options.dialogOptions; this.openFloatingDialog(); this.setFloatingDialogBackdropElement(); this.setFloatingDialogContainers(); this.setFloatingDialogContainerPosition(); this.insertContentToDialog(options); this.setDialogCallbacks(); } private openDialog(isFloating?: boolean) { let config = new MdDialogConfig(); config['width'] = this.dialogOptions.width; config['height'] = this.dialogOptions.height; config['disableClose'] = this.dialogOptions.disableClose; config['position'] = "absolute"; this.dialogRef = this.dialog.open(CfDialogComponent, config); this.dialogContainer = this.getDialogContainer(); //set relative position to default normal (not floating) dialog panel this.getDialogPane().style.position = 'relative'; this.setDialogContainerInComponentPosition(); this.setCalendarCallbacks(); } private openFloatingDialog() { this.dialogOptions.disableClose = false;//must be for floating window, to close it by clicking on MdDialog backdropElement this.openDialog(true); } private setFloatingDialogBackdropElement() { let backdropElement = this.getDialogBackdropElement(); backdropElement.style.opacity = '0'; // to catch click event to close dialog backdropElement.style.left = '-10000px'; backdropElement.style.top = '-10000px'; backdropElement.style.bottom = '-10000px'; backdropElement.style.right = '-10000px'; backdropElement.style.zIndex = -1; } private setFloatingDialogContainers() { let dialogPaneWrapper = this.getDialogPaneWrapper(); dialogPaneWrapper.style.display = 'block'; dialogPaneWrapper.style.position = 'static'; let dialogContainer = dialogPaneWrapper.parentElement; if (this.dialogOptions.width.indexOf('%') == -1) { dialogContainer.style.width = 'auto'; } dialogContainer.style.height = 'auto'; dialogContainer.style.position = 'absolute'; } private setFloatingDialogContainerPosition() { if (this.dialogOptions.position == 'auto') { this.setFloatingDialogContainerAutoPosition(); } else { this.setFloatingDialogContainerFixedPosition(); } } private setFloatingDialogContainerAutoPosition() { let clientWidth = document.getElementsByTagName('body')[0].clientWidth; let clientX = parseInt(this.dialogOptions.sourceEvent.clientX); let clientY = parseInt(this.dialogOptions.sourceEvent.clientY); let innerHeight = window.innerHeight; if ((innerHeight - clientY) <= LIMIT_RANGE) { if ((clientWidth - clientX) <= LIMIT_RANGE) { this.dialogOptions.position = 'leftUp'; } else { this.dialogOptions.position = 'rightUp'; } } else { if ((clientWidth - clientX) <= LIMIT_RANGE) { this.dialogOptions.position = 'leftBelow'; } else { this.dialogOptions.position = 'rightBelow'; } } this.setFloatingDialogContainerFixedPosition(); } private setFloatingDialogContainerFixedPosition() { let dialogContainer = this.getDialogContainer(); let clientWidth = document.getElementsByTagName('body')[0].clientWidth; let scrollTop = document.getElementsByTagName('body')[0].scrollTop; let clientX = parseInt(this.dialogOptions.sourceEvent.clientX); let clientY = parseInt(this.dialogOptions.sourceEvent.clientY); let innerHeight = window.innerHeight; switch (this.dialogOptions.position) { case 'leftBelow': dialogContainer.style.left = 'auto'; dialogContainer.style.right = (clientWidth - clientX) + 'px'; dialogContainer.style.top = (clientY + scrollTop) + 'px'; break; case 'rightBelow': dialogContainer.style.left = clientX + 'px'; dialogContainer.style.top = (clientY + scrollTop) + 'px'; break; case 'leftUp': dialogContainer.style.left = 'auto'; dialogContainer.style.right = (clientWidth - clientX) + 'px'; dialogContainer.style.top = 'initial'; dialogContainer.style.bottom = (innerHeight - clientY - scrollTop) + 'px'; break; case 'rightUp': dialogContainer.style.left = clientX + 'px'; dialogContainer.style.top = 'initial'; dialogContainer.style.bottom = (innerHeight - clientY - scrollTop) + 'px'; break; default:// rightBelow dialogContainer.style.left = clientX + 'px'; dialogContainer.style.top = (clientY + scrollTop) + 'px'; break; } } private setDialogContainerInComponentPosition() { if (this.dialogOptions.target) { let body = document.getElementsByTagName('body')[0]; let target = document.getElementById(this.dialogOptions.target); let boundingClientRect = target.getBoundingClientRect(); let dialogContainer = this.getDialogContainer(); dialogContainer.style.left = parseInt('' + boundingClientRect.left) + 'px'; dialogContainer.style.top = (parseInt('' + boundingClientRect.top) + body.scrollTop) + 'px'; dialogContainer.style.width = parseInt('' + boundingClientRect.width) + 'px'; dialogContainer.style.height = parseInt('' + boundingClientRect.height) + 'px'; dialogContainer.style.position = 'absolute'; } } private insertContentToDialog(content: any) { let componentInstance = this.dialogRef.componentInstance; componentInstance.properties = this.dialogOptions || {};// set CfDialog properties if (content.text) { this.insertTextToDialog(content.text); } else if (content.template) { this.insertComponentViewToDialog(content.template); } } private insertTextToDialog(text: string) { let componentInstance = this.dialogRef.componentInstance; setTimeout(() => { let cfDialogViewContainer = this.getDialogViewContainer(componentInstance.cfDialogViewContainer['_results'], this.dialogOptions.dialogType); //let viewContainerNativeElement = cfDialogViewContainer._element.nativeElement;//for Angular 2 let viewContainerNativeElement = cfDialogViewContainer._data.renderElement; cfDialogViewContainer.clear(); let textNodeWrapper = document.createElement('div'); textNodeWrapper.setAttribute('class', 'cf-dialog-text-content'); let textNode = document.createTextNode(text); textNodeWrapper.appendChild(textNode); viewContainerNativeElement.parentNode.insertBefore(textNodeWrapper, viewContainerNativeElement); // need min height for dialog where no height is set this.getDialogPane().querySelector('md-dialog-container').style.minHeight = '180px'; }, 0); } private insertComponentViewToDialog(templateRef: TemplateRef) { let componentInstance = this.dialogRef.componentInstance; setTimeout(() => { let viewRef = templateRef.createEmbeddedView(null); let cfDialogViewContainer = this.getDialogViewContainer(componentInstance.cfDialogViewContainer['_results'], this.dialogOptions.dialogType); cfDialogViewContainer.clear(); cfDialogViewContainer.insert(viewRef); }, 0); } private getDialogViewContainer(dialogViewContainersArray, dialogType) { for (let i = 0; i < dialogViewContainersArray.length; i++) { //let nextDialogType = dialogViewContainersArray[i]._element.nativeElement.parentElement.getAttribute('dialog-type');//for Angular 2 let nextDialogType = dialogViewContainersArray[i]._data.renderElement.parentElement.getAttribute('dialog-type'); if (dialogType == nextDialogType) { return dialogViewContainersArray[i]; } } return null; } private setDialogCallbacks() { this.dialogRef.afterClosed().subscribe(result => { this.cleanDialogContainerStyle(); if (result && this.dialogOptions[result.callback]) { this.dialogOptions[result.callback](result.data); } }); } private cleanDialogContainerStyle() { // Have to clean AM2 overlay container style, because once added, this overlay DIV // is never removed and used by AM2 for all next Dialogs if (this.dialogContainer) { this.dialogContainer.removeAttribute('style'); } } private getDialogBackdropElement() { return this.dialogRef['_overlayRef']['_backdropElement']; } private getDialogPane() { return this.dialogRef['_overlayRef']['_pane']; } private getDialogPaneWrapper() { return this.getDialogPane().parentElement; } private getDialogContainer() { return this.getDialogPaneWrapper().parentElement; } /** * Sets listeners to CfCalendar templates events. */ private setCalendarCallbacks() { if (this.cfCalendarService) { this.dialogRef['addCalendarEvent'] = this.cfCalendarService.parseAddCalendarEventForm; this.dialogRef['updateCalendarEvent'] = this.cfCalendarService.parseUpdateCalendarEventForm; } } setCfCalendarService(cfCalendarService: CfCalendarService): void { this.cfCalendarService = cfCalendarService; } } //TODO: //0) Charts - i18n even for hints on mouse over, examples to move legends on left // examples on click on one chart that changes another chart content - drill-down ex click on bar changes pie content, // in core comp take defult theme colors and in chart comp take it as default to apply if nothing in colorScheme passed //2) Rename showTemplateInSidenav() and showTemplateInContainer().