dadou-json-editor
Version:
Angular 8 with Electron (Typescript + SASS + Hot Reload)
248 lines (232 loc) • 6.65 kB
text/typescript
import { Component, OnInit } from '@angular/core';
import { TypeService, Arbre , ConfigArbre, Objet, Propriete, ValeurString, ValeurBoolean, Tableau } from 'dadou-tree';
import { ConfigTypes } from './config.types';
import { Type } from 'generator-666';
import * as g from 'generator-666' ;
import { HttpClient } from '@angular/common/http';
export class AppComponent implements OnInit {
ressource: string;
types: Type[] = [
{
nom: 'Types',
abstrait: false ,
champs: [
{ nom: 'types' , type: '*TypeDef' }
]
},
{
nom: 'Type',
abstrait: true
},
{
nom: 'TypeRef',
super: 'Type',
abstrait: false ,
champs: [
{nom: 'nom' , type: 'string'}
]
},
{
nom: 'TypeList' ,
super: 'Type' ,
abstrait: false ,
champs: [
{ nom: 'type' , type: 'Type'}
]
},
{
nom: 'TypeChaine',
super: 'Type' ,
abstrait: false
},
{
nom: 'TypeNombre',
super: 'Type' ,
abstrait: false
},
{
nom: 'TypeBoolean',
super: 'Type' ,
abstrait: false
},
{
nom: 'Champ' ,
abstrait: false ,
champs: [
{ nom: 'nom' , type: 'string'},
{ nom: 'type' , type: 'Type'}
]
},
{
nom: 'TypeDef' ,
abstrait: false ,
champs: [
{ nom: 'nom' , type: 'string'},
{ nom: 'abstrait' , type: 'boolean'},
{ nom: 'champs' , type: '*Champ'},
{ nom: 'sousTypes' , type: '*TypeDef'}
],
}
];
typeService: TypeService ;
configArbre: ConfigArbre;
objet: Objet;
message = ' en attente ...';
constructor( private http: HttpClient ) {
this.typeService = new TypeService();
this.typeService.init(this.types);
this.configArbre = new ConfigTypes();
// this.objet = this.typeService.creerObjet('Types');
}
ngOnInit() {
}
creerType( obj: any) {
if (obj.type === 'TypeChaine') {
return 'string';
}
if (obj.type === 'TypeNombre') {
return 'number';
}
if (obj.type === 'TypeBoolean') {
return 'boolean'
}
if (obj.type === 'TypeList') {
return '*'+this.creerType(obj.valeur.type)
}
if (obj.type === 'TypeRef') {
return obj.valeur.nom
}
}
creerTypeUI( nom: string) : Objet{
const objet: Objet = new Objet()
objet.typeBase ='Type' ;
if (nom === 'number') {
objet.type = 'TypeNombre' ;
return objet;
}
if (nom === 'boolean') {
objet.type = 'TypeBoolean'
return objet;
}
if (nom === 'string') {
objet.type = 'TypeChaine'
return objet;
}
if (nom.startsWith('*')) {
objet.type = 'TypeList';
objet.ajouter('type',this.creerTypeUI(nom.substr(1)));
return objet;
}
objet.type = 'TypeRef';
objet.ajouter('nom' , new ValeurString(nom));
return objet;
}
creerTypeDefinition(parent:any , obj: any , ls: any []) {
const type:any = {};
type.nom = obj.nom;
if (parent) {
type.super = parent.nom;
}
type.abstrait = obj.abstrait;
if (obj.champs) {
type.champs = obj.champs.map( ( champ)=> {
return { nom:champ.nom , type:this.creerType(champ.type)}
});
}
if (obj.sousTypes) {
obj.sousTypes.forEach( element => {
this.creerTypeDefinition(obj , element , ls);
});
}
ls.push(type);
}
creerTypeDefinitionUI( parent: Tableau , obj: any , ls: any []): Objet {
const result = new Objet();
result.tableauParent = parent;
result.ajouter('abstrait', new ValeurBoolean(obj.abstrait));
result.ajouter('nom' , new ValeurString(obj.nom) );
const tableau = new Tableau();
tableau.typeBase = 'Champ';
obj.champs.forEach( (obj)=> {
const champ = new Objet();
champ.ajouter('nom' , new ValeurString(obj.nom));
champ.ajouter('type' , this.creerTypeUI(obj.type));
champ.tableauParent = tableau;
tableau.valeurs.push(champ);
})
result.ajouter('champs',tableau);
result.ajouter('sousTypes',this.sousTypes( obj.nom , ls));
return result;
}
sousTypes( nom: string , ls: any []) : Tableau {
const result= new Tableau();
result.typeBase = 'TypeDef';
ls.forEach( (obj) => {
if ( obj.super === nom) {
const objet: Objet = this.creerTypeDefinitionUI( result ,obj , ls) ;
objet.tableauParent = result ;
result.valeurs.push( objet);
}
});
return result;
}
charger( ressource: string ) {
this.ressource = ressource;
this.http.post('http://localhost:8080/lire' , ressource ).subscribe(
( data: any) => {
const ls: any [] = []
if (!data.data) {
return;
}
const obj = JSON.parse(data.data);
const tableau = new Tableau();
tableau.typeBase ='TypeDef';
obj.forEach(element => {
if (!element.super) {
tableau.valeurs.push(this.creerTypeDefinitionUI(tableau,element,obj))
}
});
const types = new Objet();
types.typeBase ='Types';
types.typeBase = 'Types';
types.ajouter('types', tableau);
this.objet = types;
}
)
}
enregistrer() {
const obj = this.typeService.convertirArbre(this.objet);
const ls = [];
obj.types.forEach(element => {
this.creerTypeDefinition(undefined , element , ls);
});
const idx = this.ressource.lastIndexOf('/');
const chemin = this.ressource.substring(0,idx);
const nom = this.ressource.substring(idx+1,this.ressource.lastIndexOf('.'));
const typeService: g.TypeService = new g.TypeService();
const types : Type [] = ls;
typeService.init(types);
let src = typeService.creerSourceAvecReferences();
const sourceVisiteurSimple = typeService.creerSourceFonctionVisiteurSimplePourTousLesType(nom);
this.http.post('http://localhost:8080/ecrire' ,JSON.stringify({
ressource: this.ressource ,
type: 'file' ,
contenu: JSON.stringify(ls)
})).subscribe((data: any) => {});
this.http.post('http://localhost:8080/ecrire' ,JSON.stringify({
ressource: `${chemin}/${nom}.ts` ,
type: 'file' ,
contenu: src
})).subscribe((data: any) => {});
this.http.post('http://localhost:8080/ecrire' ,JSON.stringify({
ressource: `${chemin}/${nom}.visitor.ts` ,
type: 'file' ,
contenu: sourceVisiteurSimple
})).subscribe((data: any) => {});
}
}