'use strict' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import * as vscode from 'vscode' ; import { ExtensionUtil } from '../util/ExtensionUtil' ; export abstract class View { protected static openPanels : Array < vscode . WebviewPanel > = [ ] ; protected panelTitle : string ; protected panelID : string ; protected context : vscode . ExtensionContext ; constructor ( context , panelID , panelTitle ) { this . panelID = panelID ; this . panelTitle = panelTitle ; this . context = context ; } public async openView ( keepContext , viewColumn = vscode . ViewColumn . One ) { View . openPanels = View . openPanels . filter ( ( _panel ) => { return _panel [ '_isDisposed' ] === false || _panel [ '_store' ] && _panel [ '_store' ] [ '_isDisposed' ] === false ; } ) ; let panel = View . openPanels . find ( ( tempPanel ) => { return tempPanel . title === this . panelTitle ; } ) ; if ( panel ) { panel . reveal ( undefined ) ; } else { panel = vscode . window . createWebviewPanel ( this . panelID , this . panelTitle , viewColumn , { enableScripts : true , retainContextWhenHidden : keepContext , enableCommandUris : true , localResourceRoots : [ vscode . Uri . file ( path . join ( this . context . extensionPath , 'resources' ) ) , vscode . Uri . file ( path . join ( this . context . extensionPath , 'build' ) ) ] } ) ; View . openPanels . push ( panel ) ; panel . onDidDispose ( ( ) => { View . openPanels = View . openPanels . filter ( ( tempPanel ) => { return tempPanel . title !== this . panelTitle ; } ) ; } , null , this . context . subscriptions ) ; panel . webview . html = await this . getHTMLString ( panel . webview ) ; this . loadComponent ( panel ) ; if ( ! keepContext ) { panel . onDidChangeViewState ( async ( ) => { panel . webview . html = await this . getHTMLString ( panel . webview ) ; this . loadComponent ( panel ) ; } ) ; } await this . openPanelInner ( panel ) ; } } public async getRepositories ( ) { const extensionPath = ExtensionUtil . getExtensionPath ( ) ; const repositoriesPath = path . join ( extensionPath , 'repositories.json' ) ; const json = await fs . readJson ( repositoriesPath ) ; const repositories = json . repositories ; return repositories ; } public async getRepository ( name ) { const repositories = await this . getRepositories ( ) ; const repository = repositories . find ( ( repo ) => { return repo . name === name ; } ) ; return repository ; } getTutorialsDirectory ( ) { const extensionPath = ExtensionUtil . getExtensionPath ( ) ; return path . join ( extensionPath , 'resources' , 'tutorials' ) ; } async getTutorialInfo ( ) { const tutorialsPath = path . join ( this . getTutorialsDirectory ( ) , 'tutorials.json' ) ; const json = await fs . readJson ( tutorialsPath ) ; return json ; } async getSeries ( name ) { const tutorials = await this . getTutorialInfo ( ) ; const series = tutorials . find ( ( _series ) => { return _series . name === name ; } ) ; return series ; } public async getTutorial ( series , tutorialName ) { const tutorials = series . tutorials ; const tutorial = tutorials . find ( ( tut ) => { return tut . title === tutorialName ; } ) ; return tutorial ; } public getSample ( repository , sampleName ) { const samples = repository . samples ; const sample = samples . find ( ( samp ) => { return samp . name === sampleName ; } ) ; return sample ; } protected async abstract openPanelInner ( panel ) ; protected async abstract getHTMLString ( webview ) ; protected abstract loadComponent ( panel ) ; }