import { Component, Input, OnChanges, SimpleChanges, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; import { CommonService } from '../../../../../services/common.service'; // import { AdminCompanyCustomerService } from '@core/services/admin/admin-company/admin-company-customer.service'; import { MenuManagementService } from '../../../../../services/admin/company/menu-management/menu-management.service'; import { first, finalize } from 'rxjs/operators'; import { Subscription } from 'rxjs'; import { IMenuItemAdmin } from '../../../../../models/admin/company/menu-management/menu-management'; import { nullSafeIsEquivalent } from '@angular/compiler/src/output/output_ast'; @Component({ selector: 'app-form-menu', templateUrl: './form-menu.component.html', styleUrls: ['./form-menu.component.css'] }) export class FormMenuComponent implements OnChanges, OnInit, OnDestroy { private base64ImageFile: string = null; private imageFileName: string = null; private valueChangeSubscription = new Subscription(); previewItem: IMenuItemAdmin; masterPreviewContent: string = ''; ContentFile: File = null; imageName: string; format: string; imageData: string = ''; previewModal; showModal: boolean = false; deleteModal: boolean = false; tinyMCEInitOptions; @Input() menuItem: IMenuItemAdmin; @Output() readonly menuItemApplied = new EventEmitter(); @Output() readonly menuItemCancelDelete = new EventEmitter(); MenuAdminForm: FormGroup = new FormGroup({ IsActive: new FormControl(''), Name: new FormControl('', Validators.required), IsHttp: new FormControl(''), URL: new FormControl(''), Content: new FormControl(''), ImageVideo: new FormControl('') }); private setFormValues(inputMenuItem) { this.MenuAdminForm.setValue({ IsActive: inputMenuItem.IsActive, Name: inputMenuItem.Name, IsHttp: inputMenuItem.IsHttp, URL: inputMenuItem.URL, Content: inputMenuItem.Content, ImageVideo: inputMenuItem.ImageVideo, }); if (inputMenuItem.ImageVideoFileName != null) { this.imageData = inputMenuItem.ImageVideoData; this.format = 'image'; this.imageName = inputMenuItem.ImageVideoFileName; } } private clearControlValidation(control: AbstractControl) { control.clearValidators(); control.updateValueAndValidity(); } private setControlValidation(control: AbstractControl) { control.setValidators([Validators.required]); control.updateValueAndValidity(); } ngOnInit(): void { this.tinyMCEInitOptions = this.common.tinyMCEOptions(); } get formTitle(): string { if (!this.menuItem) { return ''; } const action = this.menuItem.isAdd ? 'Add' : 'Edit'; const Type = this.menuItem.IsGroup ? 'Group' : 'Static'; return `${action} ${Type} Menu Node`; } get isHttpForm(): boolean { return this.MenuAdminForm.get('IsHttp').value; } get Content(): AbstractControl { if (!this.MenuAdminForm) { return null; } return this.MenuAdminForm.get('Content'); } get contentForm(): string { return this.Content.value; } get URL(): AbstractControl { return this.MenuAdminForm.get('URL'); } get urlForm(): string { return this.URL.value; } get isActiveForm(): boolean { return this.MenuAdminForm.get('IsActive').value; } get nameForm(): string { return this.MenuAdminForm.get('Name').value; } constructor( private common: CommonService, private menuManagementService: MenuManagementService, ) { } ngOnChanges(changes: SimpleChanges): void { if (changes.menuItem && changes.menuItem.currentValue) { if (this.menuItem.isAdd || !this.menuItem.IsPartiallyLoaded) { this.setFormValues(this.menuItem); } else { var res: IMenuItemAdmin = { IsActive: true, IsEditable: true, IsInherited: true, IsMovable: true, IsPartiallyLoaded: false, AllowChildren: true, Content: "", Name: "", ImageVideo: null, ImageVideoFileName: "", isAdd: true, ChildNode: [], id: "0099", IsMobile: true, isSettings: true, Type: 0, URL: "/", IsHttp: true, IsRouterPath: false, MenuAdminId: 0, IsGroup: false, LstMenuAdmin: [], ParentId: 0, CompanyId: 1, ADMINID: 0, ImageVideoData: "" }; this.menuItem = res; this.setFormValues(this.menuItem); } } } applyChanges() { if (!this.menuItem.IsGroup) { const ImageVideo = this.base64ImageFile === null ? null : this.base64ImageFile; // only set these guys if we're a group node this.menuItem.IsHttp = this.isHttpForm; // if IsHttp redirect - clear Content, ImageVideo, and ImageVideoFileName this.menuItem.Content = this.isHttpForm ? null : this.contentForm; if (this.MenuAdminForm.get('ImageVideo').dirty || this.isHttpForm) { this.menuItem.ImageVideo = this.isHttpForm ? null : this.ContentFile; this.menuItem.ImageVideoFileName = this.isHttpForm ? null : this.imageName; this.menuItem.ImageVideoData = this.isHttpForm ? null : this.imageData; } // if not IsHttp redirect this.menuItem.URL = !this.isHttpForm ? null : this.urlForm; } // always set these guys this.menuItem.Name = this.nameForm; this.menuItem.IsActive = this.isActiveForm; this.menuItemApplied.emit(this.menuItem); } cancel() { this.menuItemCancelDelete.emit(false); } setModalToggleValue() { this.deleteModal = false; } delete() { this.deleteModal = true; } deleteItem() { this.menuManagementService.deleteNodeName = this.nameForm; this.menuManagementService.callDeleteNode(); } onFile(fileInput: any) { const reader = new FileReader(); if (fileInput.target.files.length > 0) { this.ContentFile = fileInput.target.files[0]; this.imageName = fileInput.target.files[0].name; let FileFormat = this.ContentFile.name.split('.'); let formatType = FileFormat.slice(-1)[0]; if (formatType == 'jpg' || formatType == 'mp4' || formatType == 'png' || formatType == 'jpeg') { if (formatType == 'mp4') { this.format = 'video'; } else { this.format = 'image'; } reader.readAsDataURL(this.ContentFile); reader.onload = () => { this.base64ImageFile = ''; this.imageData = reader.result as string; this.base64ImageFile = this.imageData; }; } else { this.common.showError("Please Select Valid File.", "Wrong file format") this.imageName = ''; this.imageData = null; } } } preview() { this.showModal = true; this.masterPreviewContent = this.MenuAdminForm.get("Content").value; this.previewModal = { content: this.masterPreviewContent, file: this.imageData, fileFormat: this.format } } removeimage() { this.imageName = ""; this.imageData = null; this.menuItem.ImageVideoFileName = ""; this.menuItem.ImageVideoData = null; this.menuItem.ImageVideo = null; } getModalToggleValue(data) { this.showModal = data.toggle; } submitform() { } ngOnDestroy(): void { this.valueChangeSubscription.unsubscribe(); } displayFieldCss(field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.MenuAdminForm.get(field).valid, 'is-invalid': (this.MenuAdminForm.get(field).invalid && (this.MenuAdminForm.get(field).dirty || this.MenuAdminForm.get(field).touched)) }; } else { return { 'plain-valid': this.MenuAdminForm.get(field).valid, 'plain-invalid': (this.MenuAdminForm.get(field).invalid && (this.MenuAdminForm.get(field).dirty || this.MenuAdminForm.get(field).touched)) }; } } }