dadou-json-editor
Version:
Angular 8 with Electron (Typescript + SASS + Hot Reload)
151 lines (129 loc) • 3.91 kB
text/typescript
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { HttpClient } from '@angular/common/http';
interface Element {
name: string;
type: string;
}
interface ElementNouveau {
name: string;
type: string;
valider: boolean;
}
interface Parent {
chemin: string;
parent?: Parent;
}
export class ExplorateurComponent implements OnInit {
chemin: string = '';
parent: Parent;
nouveau: ElementNouveau;
elements: Element[] = [];
erreurRecupererElement = false;
loadingChemin = '';
ouvrir: EventEmitter<string> = new EventEmitter();
constructor(private http: HttpClient) {
}
ngOnInit() {
this.http.get('http://localhost:8080/travail').subscribe((dataDirTravail: any) => {
this.chemin = dataDirTravail.dir;
});
}
explorer(elt: Element) {
const nouveauChemin = this.chemin + '/' + elt.name;
this.parent = { chemin: this.chemin, parent: this.parent }
this.chemin = nouveauChemin;
this.http.post('http://localhost:8080/lire', nouveauChemin).subscribe((data: any) => {
this.elements = [];
if (data.type === 'directory') {
this.elements = data.data;
}
});
}
explorerParent() {
this.chemin = this.parent.chemin;
this.parent = this.parent.parent;
this.http.post('http://localhost:8080/lire', this.chemin).subscribe((data: any) => {
this.elements = [];
if (data.type === 'directory') {
this.elements = data.data;
}
});
}
nouveauRepertoire() {
this.nouveau = { name: '', type: 'directory', valider: false }
}
nouveauFichier() {
this.nouveau = { name: '', type: 'file', valider: false }
}
afficherValider() {
this.nouveau.valider = false;
if (!this.nouveau.name.endsWith('.type.json')) {
return;
}
this.http.post('http://localhost:8080/type', this.chemin + '/' + this.nouveau.name).subscribe((data: any) => {
if (!data.type) {
this.nouveau.valider = true
}
});
}
valider() {
this.http.post('http://localhost:8080/ecrire', JSON.stringify({
ressource: this.chemin + '/' + this.nouveau.name,
type: this.nouveau.type,
contenu: '[]'
})
).subscribe((data: any) => {
this.loadingChemin = '';
//this.recharger();
});
}
ouvrirFichier(elt: Element) {
this.ouvrir.emit(this.chemin + '/' + elt.name);
}
recupererElements(): Element [] {
if (this.chemin !== this.loadingChemin) {
this.loadingChemin = this.chemin;
this.http.post('http://localhost:8080/lire', this.chemin).subscribe((data: any) => {
this.elements = [];
this.erreurRecupererElement = true;
if (data.type === 'directory') {
this.elements = data.data;
this.elements = this.elements.filter( (e:Element)=> {
if (e.type == 'directory') {
return true;
}
return e.name.endsWith('.type.json');
});
this.nouveau = undefined;
this.erreurRecupererElement = false;
}
});
}
return this.elements;
}
initialiserRepertoireTravail() {
this.http.post('http://localhost:8080/travail', this.chemin).subscribe((data: any) => {
});
}
recharger() {
this.http.post('http://localhost:8080/lire', this.chemin).subscribe((data: any) => {
this.elements = [];
if (data.type === 'directory') {
this.elements = data.data;
this.nouveau = undefined;
}
});
}
supprimer(elt: Element) {
this.elements = this.elements.filter((e) => { return e.name !== elt.name; });
this.http.post('http://localhost:8080/supprimer', this.chemin + '/' + elt.name
).subscribe((data: any) => {
});
}
}