Skip to content

Commit

Permalink
[0.12.1] add function of creating one-to-many relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
dog-ears committed Feb 23, 2018
1 parent c7b772c commit c6b7fa5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/app/class/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ export class Data {
public getModelByElementH2Id(model_element_h2_id:string): Model{
return this.models.filter( (v,i) => v.getElementH2Id()===model_element_h2_id )[0];
}

public getModelByName(model_name:string): Model{
return this.models.filter( (v,i) => v.name===model_name )[0];
}
}
13 changes: 12 additions & 1 deletion src/app/schema/schema.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';

// service
import { DataService } from '../service/data.service';
import { JsPlumbService } from '../service/jsPlumb.service';

// component
import { ModalSchemaComponent } from '../modal-schema/modal-schema.component';
Expand All @@ -28,7 +29,17 @@ export class SchemaComponent {

private bsModalRef: BsModalRef;

constructor( private bsModalService: BsModalService, private dataService: DataService ) {}
constructor( private bsModalService: BsModalService, private dataService: DataService, private jsPlumbService: JsPlumbService ) {}

ngAfterViewInit(){
console.log( 'SchemaComponent(' + this.mySchema.parent_id + ' / ' + this.mySchema.id + ').ngAfterViewInit() is called!' );
this.jsPlumbService.initSchema(this.mySchema);
}

ngOnDestroy(){
console.log( 'SchemaComponent(' + this.mySchema.parent_id + ' / ' + this.mySchema.id + ').ngOnDestroy() is called!' );
this.jsPlumbService.destroySchema(this.mySchema);
}

private editSchema():void{
console.log('SchemaComponent(' + this.mySchema.parent_id + ' / ' + this.mySchema.id +').editSchema() is called!');
Expand Down
18 changes: 18 additions & 0 deletions src/app/service/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ export class DataService {

public addOneToManyRelation( source_model:Model, target_model:Model, source_model_display_schema:string, target_model_display_schema:string ):void{
console.log('DataService.addOneToManyRelation() is called!');

// add schema ( [source_model.name]_id ) to target_model
var schema = new Schema();
schema.id = target_model.getNewSchemaId();
schema.name = source_model.name + "_id";
schema.display_name = source_model.name + " - NAME";
schema.type = "integer";
schema.input_type = "select";
schema.varidate = "";
schema.faker_type = "numberBetween(1,30)";
schema.nullable = true;
schema.show_in_list = true;
schema.show_in_detail = true;
schema.belongsto = source_model.name;
schema.belongsto_column = source_model_display_schema;
schema.parent_id = target_model.id;

target_model.schemas.push(schema);
}

public addManyToManyRelation( source_model:Model, target_model:Model, source_model_display_schema:string, target_model_display_schema:string ):void{
Expand Down
36 changes: 36 additions & 0 deletions src/app/service/jsPlumb.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ModalRelationComponent } from '../modal-relation/modal-relation.compone

// class
import { Model } from '../class/model';
import { Schema } from '../class/schema';

declare var jsPlumb:any;

Expand Down Expand Up @@ -68,6 +69,41 @@ export class JsPlumbService {
}).delete();
}

public initSchema(schema:Schema){

console.log('JsPlumbService.initSchema() is called!');

if(schema.belongsto){

var source_id = this.dataService.data.getModelByName(schema.belongsto).getElementH2Id();
var target_id = schema.getElementId();
var option = {
source: source_id,
target: target_id
};
if( this._instance.getConnections(option).length === 0 ){
this._instance.connect(option);
console.log('schema connected!(' + source_id + '--->' + target_id + ')');
}
}
this.dataService.flg_repaint = true;
}

public destroySchema(schema:Schema){

console.log('JsPlumbService.destroySchema() is called!');

if(schema.belongsto){
var option = {
target: schema.getElementId()
};
var connections_to_delete = this._instance.getConnections(option);
for( let i=0 ; i < connections_to_delete.length; i++){
this._instance.deleteConnection(connections_to_delete[i]);
}
}
}

public toggleDraggable(model:Model): void{

console.log('JsPlumbService.toggleDraggable() is called!');
Expand Down

0 comments on commit c6b7fa5

Please sign in to comment.