Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions SBOLCanvasFrontend/src/app/embedded.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { GraphService } from './graph.service'
import { ActivatedRoute } from '@angular/router'
import { Observable } from 'rxjs'

type data = {
sbol: string,
panelType: string
}

@Injectable({
providedIn: 'root'
Expand All @@ -11,21 +15,25 @@ import { Observable } from 'rxjs'
export class EmbeddedService {

private parent: any
public sbol: Observable<string>
public sbol: Observable<data>

constructor(private route: ActivatedRoute) {

// create observable that watches for messages
this.sbol = new Observable<string>(observer => {
this.sbol = new Observable<data>(observer => {
window.addEventListener('message', ({data, source}) => {
// check that message is coming from external window
if(source != window) {
this.parent = source

// Check if no sbol, but plasmids panel was open
if(data && data.panelType && !data.sbol){
observer.next({sbol: null, panelType: data.panelType})
}
// check if message includes SBOL
if(data && data.sbol) {
console.debug('[Embedded] Received SBOL from up above:', data.sbol.substring(0, 20) + '...')
observer.next(data.sbol)
observer.next({sbol: data.sbol, panelType: data.panelType });
}
}
})
Expand Down
43 changes: 27 additions & 16 deletions SBOLCanvasFrontend/src/app/graph.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,50 @@ export class GraphService extends GraphHelpers {
constructor(dialog: MatDialog, metadataService: MetadataService, glyphService: GlyphService, embeddedService: EmbeddedService, fileService: FilesService, private sanitizer : DomSanitizer) {
super(dialog, metadataService, glyphService);



// handle selection changes
this.graph.getSelectionModel().addListener(mx.mxEvent.CHANGE, mx.mxUtils.bind(this, this.handleSelectionChange))

// handle double click on glyph to enter it
this.graph.addListener(mx.mxEvent.DOUBLE_CLICK, mx.mxUtils.bind(this, this.enterGlyph))


// --- For when SBOLCanvas is embedded in another app ---

// Plasmid files from SynBioSuite should only open in Component Mode
const plasmidObjectId: string = "synbio.object-type.plasmid"

// send changes in mxgraph model to parent
// doing this via an Observable so we can debounce
new Observable<any>(observer => {
this.graph.getModel().addListener(mx.mxEvent.CHANGE, mx.mxUtils.bind(this, () => {
observer.next(this.getGraphXML())
}))
})
.pipe(debounceTime(100))
.subscribe(graphXml => {
if (embeddedService.isAppEmbedded()) {
console.debug('[GraphService] Model changed. Sending to parent.')
fileService.exportDesignToString({}, 'SBOL2', graphXml).subscribe(sbol => {
embeddedService.postMessage({ sbol })
})
}
})

.pipe(debounceTime(100))
.subscribe(graphXml => {
if (embeddedService.isAppEmbedded()) {
console.debug('[GraphService] Model changed. Sending to parent.')
fileService.exportDesignToString({}, 'SBOL2', graphXml).subscribe(sbol => {
embeddedService.postMessage({ sbol })
})
}
})
// observe changes in parent SBOL
embeddedService.sbol.subscribe(sbolContent => {
console.debug('[GraphService] Loading SBOL from external message...')
fileService.convertToMxGraph(sbolContent).subscribe(result => {
this.setGraphToXML(result)
if (!sbolContent.sbol && sbolContent.panelType === plasmidObjectId){ // Account for empty sbol, but plasmids opened
this.setComponentDefinitionMode(true);
}
fileService.convertToMxGraph(sbolContent.sbol).subscribe(result => {
this.setGraphToXML(result) // Will automatically set to Module mode, so need to set to component again if plasmid file opened
if (sbolContent.panelType === plasmidObjectId){
this.setComponentDefinitionMode(true);
}
console.debug('[GraphService] Done.')

// post message back letting parent know it loaded
embeddedService.postMessage('graphServiceLoadedSBOL')
})
Expand Down