2020-10-09 19:35:46 +02:00
import ViewController from './ViewController' ;
2020-11-05 18:58:23 +02:00
import shim from '../../shim' ;
2020-11-13 20:48:42 +02:00
import { ButtonSpec , DialogResult } from './api/types' ;
2020-11-05 18:58:23 +02:00
const { toSystemSlashes } = require ( '../../path-utils' ) ;
2020-10-09 19:35:46 +02:00
export enum ContainerType {
Panel = 'panel' ,
Dialog = 'dialog' ,
}
export interface Options {
2020-11-12 21:29:22 +02:00
containerType : ContainerType ;
2020-10-09 19:35:46 +02:00
}
interface CloseResponse {
resolve : Function ;
reject : Function ;
}
export default class WebviewController extends ViewController {
2020-11-12 21:13:28 +02:00
private baseDir_ : string ;
private messageListener_ : Function = null ;
private closeResponse_ : CloseResponse = null ;
2020-10-09 19:35:46 +02:00
2020-11-13 19:09:28 +02:00
constructor ( id : string , pluginId : string , store : any , baseDir : string , containerType : ContainerType ) {
2020-10-09 19:35:46 +02:00
super ( id , pluginId , store ) ;
this . baseDir_ = toSystemSlashes ( baseDir , 'linux' ) ;
this . store . dispatch ( {
type : 'PLUGIN_VIEW_ADD' ,
pluginId : pluginId ,
view : {
id : this.handle ,
type : this . type ,
2020-11-13 19:09:28 +02:00
containerType : containerType ,
2020-10-09 19:35:46 +02:00
html : '' ,
scripts : [ ] ,
opened : false ,
buttons : null ,
} ,
} ) ;
}
2020-11-12 21:13:28 +02:00
public get type ( ) : string {
2020-10-09 19:35:46 +02:00
return 'webview' ;
}
2020-11-12 21:13:28 +02:00
private setStoreProp ( name : string , value : any ) {
2020-10-09 19:35:46 +02:00
this . store . dispatch ( {
type : 'PLUGIN_VIEW_PROP_SET' ,
pluginId : this.pluginId ,
id : this.handle ,
name : name ,
value : value ,
} ) ;
}
2020-11-12 21:13:28 +02:00
public get html ( ) : string {
2020-10-09 19:35:46 +02:00
return this . storeView . html ;
}
2020-11-12 21:13:28 +02:00
public set html ( html : string ) {
2020-10-09 19:35:46 +02:00
this . setStoreProp ( 'html' , html ) ;
}
2020-11-12 21:13:28 +02:00
public get containerType ( ) : ContainerType {
2020-10-09 19:35:46 +02:00
return this . storeView . containerType ;
}
2020-11-12 21:13:28 +02:00
public async addScript ( path : string ) {
2020-10-09 19:35:46 +02:00
const fullPath = toSystemSlashes ( shim . fsDriver ( ) . resolve ( ` ${ this . baseDir_ } / ${ path } ` ) , 'linux' ) ;
if ( fullPath . indexOf ( this . baseDir_ ) !== 0 ) throw new Error ( ` Script appears to be outside of plugin base directory: ${ fullPath } (Base dir: ${ this . baseDir_ } ) ` ) ;
this . store . dispatch ( {
type : 'PLUGIN_VIEW_PROP_PUSH' ,
pluginId : this.pluginId ,
id : this.handle ,
name : 'scripts' ,
value : fullPath ,
} ) ;
}
2020-11-12 21:13:28 +02:00
public emitMessage ( event : any ) {
2020-10-09 19:35:46 +02:00
if ( ! this . messageListener_ ) return ;
this . messageListener_ ( event . message ) ;
}
2020-11-12 21:13:28 +02:00
public onMessage ( callback : any ) {
2020-10-09 19:35:46 +02:00
this . messageListener_ = callback ;
}
// ---------------------------------------------
// Specific to dialogs
// ---------------------------------------------
2020-11-13 20:48:42 +02:00
public async open ( ) : Promise < DialogResult > {
2020-10-09 19:35:46 +02:00
this . setStoreProp ( 'opened' , true ) ;
2020-11-12 21:13:28 +02:00
return new Promise ( ( resolve : Function , reject : Function ) = > {
2020-10-09 19:35:46 +02:00
this . closeResponse_ = { resolve , reject } ;
} ) ;
}
public async close() {
this . setStoreProp ( 'opened' , false ) ;
}
2020-11-13 20:48:42 +02:00
public closeWithResponse ( result : DialogResult ) {
2020-10-09 19:35:46 +02:00
this . close ( ) ;
this . closeResponse_ . resolve ( result ) ;
}
2020-11-12 21:13:28 +02:00
public get buttons ( ) : ButtonSpec [ ] {
2020-10-09 19:35:46 +02:00
return this . storeView . buttons ;
}
2020-11-12 21:13:28 +02:00
public set buttons ( buttons : ButtonSpec [ ] ) {
2020-10-09 19:35:46 +02:00
this . setStoreProp ( 'buttons' , buttons ) ;
}
}