import { Injectable } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import * as rdfstore from 'rdfstore' ; import * as _ from 'lodash' ; import * as N3 from 'n3' ; import { HttpClient } from '@angular/common/http' ; import { DomSanitizer , DOCUMENT } from '@angular/platform-browser' ; import { Observable } from 'rxjs/Observable' ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/mergeMap' ; import * as existsFile from 'exists-file' ; import { ProjectSettingsService } from './project-settings.service' ; export interface TabsData { title : string ; description : string ; triples : string ; query : string ; reasoning ? : boolean ; textOnly ? : boolean ; } export interface ProjectData { title : string ; creator ? : string ; } export interface Data { project : ProjectData ; tabs : TabsData [ ] ; } @ Injectable ( ) export class DataService { private filePath = './assets/data.json' ; private prefixPath = './assets/prefixes.json' ; constructor ( public http , private route , private pss , private sanitizer ) { } getPath ( ) { return this . route . queryParams . map ( x => { var path = './assets/data.json' ; if ( x . file ) { path = x . file . replace ( 'www.dropbox' , 'dl.dropboxusercontent' ) ; } return path ; } ) ; } getProjectData ( ) { return this . getPath ( ) . mergeMap ( path => { return this . http . get < any > ( path ) . map ( x => { if ( this . isOldJSONFormat ( x ) ) { return { title : "visualization" } ; } else { var dataNew = x ; return dataNew . project ; } } ) ; } ) ; } getProjectSettings ( ) { return this . getPath ( ) . mergeMap ( path => { return this . http . get < any > ( path ) . map ( x => { return x . settings ? x . settings : false ; } ) . map ( settings => { if ( ! settings ) settings = this . pss . getDataSettings ( ) ; return settings ; } ) ; } ) ; } getTabTitles ( ) { return this . getPath ( ) . mergeMap ( path => { return this . http . get < any > ( path ) . map ( x => { if ( this . isOldJSONFormat ( x ) ) { var dataOld = x ; return _ . map ( dataOld , d => d . title ) ; } else { var dataNew = x ; return _ . map ( dataNew . tabs , d => d . title ) ; } } ) ; } ) ; } getSingle ( index ) { return this . getPath ( ) . mergeMap ( path => { return this . http . get < any > ( path ) . map ( x => { if ( this . isOldJSONFormat ( x ) ) { return x [ index ] ; } else { return x . tabs [ index ] ; } } ) . map ( x => { x . query = Array . isArray ( x . query ) ? x . query . join ( '\n' ) : x . query ; x . description = Array . isArray ( x . description ) ? x . description . join ( '\n' ) : x . description ; x . triples = Array . isArray ( x . triples ) ? x . triples . join ( '\n' ) : x . triples ; return x ; } ) ; } ) ; } getPrefixes ( ) { return this . http . get < any > ( this . prefixPath ) ; } private loadingSource = new BehaviorSubject < boolean > ( false ) ; public loadingStatus = this . loadingSource . asObservable ( ) ; setLoaderStatus ( status ) { this . loadingSource . next ( status ) ; } private loadingMsgSource = new BehaviorSubject < string > ( "loading..." ) ; public loadingMessage = this . loadingMsgSource . asObservable ( ) ; setLoadingMessage ( msg ) { this . loadingMsgSource . next ( msg ) ; } private isOldJSONFormat ( data ) { if ( Array . isArray ( data ) ) { return true ; } else { return false ; } } }