Site/src/app/edit-home/edit-home.component.ts

173 lines
4.2 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { environment, FuncoesGlobais } from 'src/environments/environment';
import { GeralService } from '../services/geral.service';
@Component({
selector: 'app-edit-home',
templateUrl: './edit-home.component.html',
styleUrls: ['./edit-home.component.scss', './../app.component.scss']
})
export class EditHomeComponent implements OnInit {
LinkProjeto: string = environment.LinkProjeto;
Logo: File = null;
Favicon: File = null;
Background: File = null;
CaminhoLogo: string = environment.LinkProjeto + "/assets/logo_escrito.png";
CaminhoFavicon: string = "/favicon.ico";
CaminhoBackground: string = "/assets/background.jpg";
TituloAnterior: string = "";
Titulo: string = "";
APIUrl: string = "";
APIUrlAnterior: string = "";
APIUrlCheck: boolean = false;
ProjetoUrl: string = "";
ProjetoUrlAnterior: string = "";
ProjetoUrlCheck: boolean = false;
StringConfig: string = "";
constructor(
private router: Router,
private geralService: GeralService,
private funcoesGlobais: FuncoesGlobais,
) { }
ngOnInit() {
this.funcoesGlobais.CheckAdminRoute();
this.geralService.readEnderecos().then(resGeral => {
this.StringConfig = resGeral || "";
const partes = this.StringConfig.split(';');
this.ProjetoUrlAnterior = partes[0].split('=')[1] || "";
this.ProjetoUrl = this.ProjetoUrlAnterior;
this.APIUrlAnterior = partes[1].split('=')[1] || "";
this.APIUrl = this.APIUrlAnterior;
this.TituloAnterior = partes[2].split('=')[1] || "";
this.Titulo = this.TituloAnterior;
});
}
Visualizar(Janela: string) {
this.router.navigate(['/dashboard/admin/' + Janela + '/visualizar']);
}
Cadastrar(Janela: string) {
this.router.navigate(['/dashboard/admin/' + Janela]);
}
onFileChangedLogo(event) {
this.CarregarPreviewArquivo(event, 'logo');
}
onFileChangedFavicon(event) {
this.CarregarPreviewArquivo(event, 'favicon');
}
onFileChangedBackground(event) {
this.CarregarPreviewArquivo(event, 'background');
}
CarregarPreviewArquivo(event, tipo: string) {
if (event.target.files && event.target.files[0]) {
const arquivo = event.target.files[0];
if (tipo === 'logo') {
this.Logo = arquivo;
}
if (tipo === 'favicon') {
this.Favicon = arquivo;
}
if (tipo === 'background') {
this.Background = arquivo;
}
const reader = new FileReader();
reader.readAsDataURL(arquivo);
reader.onload = (event) => {
const result = event.target['result'].toString();
if (tipo === 'logo') {
this.CaminhoLogo = result;
}
if (tipo === 'favicon') {
this.CaminhoFavicon = result;
}
if (tipo === 'background') {
this.CaminhoBackground = result;
}
};
}
}
CopiarArquivos(Nome: string, Arquivo: File, Caminho: string) {
if (!Arquivo) {
alert("Selecione um arquivo antes de salvar.");
return;
}
const req = new XMLHttpRequest();
const formData = new FormData();
formData.append("nome", Nome);
formData.append("arquivo", Arquivo);
formData.append("caminho", Caminho);
req.open("POST", environment.ApiUrl + "/arquivos/copy");
req.send(formData);
req.onreadystatechange = () => {
if (req.readyState === 4) {
alert(Nome + " alterado com sucesso!");
if (Nome === 'logo_escrito.png') {
this.Logo = null;
}
if (Nome === 'favicon.ico') {
this.Favicon = null;
}
if (Nome === 'background.jpg') {
this.Background = null;
}
}
};
}
Editar(Item: string) {
this.StringConfig = `Dominio=${this.ProjetoUrl};API=${this.APIUrl};Título=${this.Titulo}`;
const form = {
content: this.StringConfig
};
this.geralService.writeEnderecos(form).then(resEdit => {
if (resEdit == true) {
alert(Item + " alterado com sucesso!");
this.ProjetoUrlAnterior = this.ProjetoUrl;
this.APIUrlAnterior = this.APIUrl;
this.TituloAnterior = this.Titulo;
this.ProjetoUrlCheck = false;
this.APIUrlCheck = false;
}
});
}
}