nicer let syntax for fb

This commit is contained in:
peter
2019-08-14 01:13:49 +02:00
parent b706e36b49
commit fed2b455ce
4 changed files with 81 additions and 106 deletions
@@ -3,11 +3,7 @@ import { FrontblockApiConf } from 'frontblock';
import { SubscriptionResponse, parseResponse, SuccessResponse } from 'frontblock-generic/Types.js'; import { SubscriptionResponse, parseResponse, SuccessResponse } from 'frontblock-generic/Types.js';
import { environment } from "../../environments/environment" import { environment } from "../../environments/environment"
let fb const fb = environment.production?window["fb"]:{
if(environment.production){
fb = window["fb"]
}else{
fb = {
ApiClient : { ApiClient : {
setConfig: async () => { setConfig: async () => {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
@@ -32,8 +28,6 @@ if(environment.production){
}, },
unsubscribe: (uid) => new SuccessResponse(), unsubscribe: (uid) => new SuccessResponse(),
quit: (uid) => new SuccessResponse() quit: (uid) => new SuccessResponse()
}
} }
} }
@Component({ @Component({
@@ -1,23 +1,19 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { fbind } from 'q';
import { FrontblockApiConf } from 'frontblock'; import { FrontblockApiConf } from 'frontblock';
import { environment } from "../../environments/environment" import { environment } from "../../environments/environment"
let fb const fb = environment.production ? window["fb"] : {
if(environment.production){ ApiClient: {
fb = window["fb"]
}else{
fb = {
ApiClient : {
setConfig: async () => { setConfig: async () => {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve('Promise A win!'); resolve('Promise A win!');
}, 500) }, 500)
})} })
} }
} }
} }
@Component({ @Component({
selector: 'settings', selector: 'settings',
template: ` template: `
@@ -77,10 +73,10 @@ if(environment.production){
` `
}) })
export class ApiclientFormComponent implements OnInit { export class ApiclientFormComponent implements OnInit {
testnet:boolean = true testnet: boolean = true
saving:boolean = false saving: boolean = false
advanced:boolean = false advanced: boolean = false
data:FrontblockApiConf = { data: FrontblockApiConf = {
apiHost: "api.testnet.frontblock.me", apiHost: "api.testnet.frontblock.me",
apiPort: 10001, apiPort: 10001,
tls: false, tls: false,
@@ -91,9 +87,9 @@ export class ApiclientFormComponent implements OnInit {
ngOnInit() { } ngOnInit() { }
setAdvanced(){ this.advanced = true } setAdvanced() { this.advanced = true }
checkData():boolean{ checkData(): boolean {
return ( return (
typeof this.data.apiPort === "number" && typeof this.data.apiPort === "number" &&
typeof this.data.apiHost === "string" && typeof this.data.apiHost === "string" &&
@@ -102,17 +98,17 @@ export class ApiclientFormComponent implements OnInit {
) )
} }
updateTestnet(){ updateTestnet() {
if(this.testnet){ if (this.testnet) {
this.data.apiHost = "api.testnet.frontblock.me" this.data.apiHost = "api.testnet.frontblock.me"
this.data.apiPort = 10001 this.data.apiPort = 10001
}else{ } else {
this.data.apiHost = "api.frontblock.me" this.data.apiHost = "api.frontblock.me"
this.data.apiPort = 10000 this.data.apiPort = 10000
} }
} }
updatePort(obj){ updatePort(obj) {
switch (this.data.apiHost) { switch (this.data.apiHost) {
case "api.testnet.frontblock.me": case "api.testnet.frontblock.me":
this.data.apiPort = 10001 this.data.apiPort = 10001
@@ -122,21 +118,21 @@ export class ApiclientFormComponent implements OnInit {
break; break;
default: default:
console.warn("A non-standard api host was chosen: "+this.data.apiPort) console.warn("A non-standard api host was chosen: " + this.data.apiPort)
break; break;
} }
console.log(this.data) console.log(this.data)
} }
async save(){ async save() {
this.saving = true; this.saving = true;
if(typeof this.data.apiPort === "string") if (typeof this.data.apiPort === "string")
this.data.apiPort = parseInt(this.data.apiPort) this.data.apiPort = parseInt(this.data.apiPort)
if(this.checkData()){ if (this.checkData()) {
const conf = await fb.ApiClient.setConfig(this.data) const conf = await fb.ApiClient.setConfig(this.data)
console.log(conf) console.log(conf)
}else{ } else {
//show error in gui //show error in gui
console.error("bad data :(") console.error("bad data :(")
} }
@@ -1,15 +1,10 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { fbind } from 'q';
import { FrontblockApiConf } from 'frontblock'; import { FrontblockApiConf } from 'frontblock';
import { SubscriptionResponse, SuccessResponse, ErrorResponse, parseResponse } from 'frontblock-generic/Types.js'; import { SubscriptionResponse, SuccessResponse, parseResponse } from 'frontblock-generic/Types.js';
import { environment } from "../../environments/environment" import { environment } from "../../environments/environment"
let fb const fb = environment.production ? window["fb"] : {
if(environment.production){ ApiClient: {
fb = window["fb"]
}else{
fb = {
ApiClient : {
setConfig: async () => { setConfig: async () => {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
@@ -33,8 +28,6 @@ if(environment.production){
}, },
unsubscribe: (uid) => new SuccessResponse(), unsubscribe: (uid) => new SuccessResponse(),
quit: (uid) => new SuccessResponse() quit: (uid) => new SuccessResponse()
}
} }
} }
@@ -87,9 +80,9 @@ if(environment.production){
` `
}) })
export class ApiclientSubscriptionComponent implements OnInit { export class ApiclientSubscriptionComponent implements OnInit {
loading:boolean = false loading: boolean = false
actionName:string = "load" actionName: string = "load"
entries:SubscriptionResponse[] = [] entries: SubscriptionResponse[] = []
alerts: { alerts: {
severity: "danger" | "warning" | "success" severity: "danger" | "warning" | "success"
message: string message: string
@@ -99,24 +92,24 @@ export class ApiclientSubscriptionComponent implements OnInit {
ngOnInit() { } ngOnInit() { }
async load(){ async load() {
this.loading = true this.loading = true
this.entries = await fb.ApiClient.getSubscriptions() this.entries = await fb.ApiClient.getSubscriptions()
if(this.entries.length === 0){ if (this.entries.length === 0) {
this.alerts.push({ severity: "warning", message: "0 results were returned"}) this.alerts.push({ severity: "warning", message: "0 results were returned" })
} }
this.loading = false this.loading = false
this.actionName = "refresh" this.actionName = "refresh"
} }
async quit(uid:string){ async quit(uid: string) {
const r = await fb.ApiClient.unsubscribe(uid) const r = await fb.ApiClient.unsubscribe(uid)
const res = parseResponse(r) const res = parseResponse(r)
if(res instanceof SuccessResponse){ if (res instanceof SuccessResponse) {
this.alerts.push({ severity: "success", message: "Stopped subscription "+uid}) this.alerts.push({ severity: "success", message: "Stopped subscription " + uid })
this.load() this.load()
}else{ } else {
this.alerts.push({ severity: "danger", message: "Error "+res.message}) this.alerts.push({ severity: "danger", message: "Error " + res.message })
} }
} }
@@ -36,76 +36,68 @@ SystemJS.config({ meta: { '*': { authorization: true } } });
/** --------- */ /** --------- */
import { AfterViewInit, Component, Input, ViewChild } from '@angular/core'; import { AfterViewInit, Component } from '@angular/core';
import { SidebarComponent } from '../sidebar/sidebar.component'; import { SidebarComponent } from '../sidebar/sidebar.component';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { FrontendPlugin, SidebarEntries, SidebarEntry } from "frontblock-generic/Plugin" import { FrontendPlugin, SidebarEntries, SidebarEntry } from "frontblock-generic/Plugin"
import { SidebarEntryService } from '../sidebar-entry-service.service'; import { SidebarEntryService } from '../sidebar-entry-service.service';
import { environment } from "../../environments/environment" import { environment } from "../../environments/environment"
const fb = environment.production ? window["fb"] : {
let fb PluginManager: {
if(environment.production){
fb = window["fb"]
}else{
fb = {
PluginManager : {
getLoadedPluginNames: () => ["ApiClient", "PluginManager"] getLoadedPluginNames: () => ["ApiClient", "PluginManager"]
} }
}
} }
@Component({ @Component({
selector: 'dynamic-loader', selector: 'dynamic-loader',
templateUrl: './dynamic-loader.component.html', templateUrl: './dynamic-loader.component.html',
styleUrls: ['./dynamic-loader.component.scss'] styleUrls: ['./dynamic-loader.component.scss']
}) })
export class DynamicLoaderComponent implements AfterViewInit { export class DynamicLoaderComponent implements AfterViewInit {
sidebar:SidebarComponent sidebar: SidebarComponent
constructor( constructor(
private router: Router, private router: Router,
private sidebarService:SidebarEntryService private sidebarService: SidebarEntryService
) { ) {
} }
ngAfterViewInit() { ngAfterViewInit() {
const _this = this; const _this = this;
(function awaitSidebar(){ (function awaitSidebar() {
const sb = _this.sidebarService.getSidebar() const sb = _this.sidebarService.getSidebar()
if(sb != null){ if (sb != null) {
_this.sidebar = sb _this.sidebar = sb
_this.loadWidgets() _this.loadWidgets()
return return
} }
setTimeout(awaitSidebar,25) setTimeout(awaitSidebar, 25)
})() })()
} }
private async loadWidgets(){ private async loadWidgets() {
while(!fb.PluginManager){ while (!fb.PluginManager) {
await new Promise((resolve) => setTimeout(resolve, 50)) await new Promise((resolve) => setTimeout(resolve, 50))
} }
const pluginNames = await fb.PluginManager.getLoadedPluginNames() const pluginNames = await fb.PluginManager.getLoadedPluginNames()
pluginNames.forEach(element => this.installWidget(element)); pluginNames.forEach(element => this.installWidget(element));
} }
private async installWidget(pluginName:string){ private async installWidget(pluginName: string) {
let module: FrontendPlugin let module: FrontendPlugin
if(environment.production) if (environment.production)
module = await SystemJS.import("plugins/"+pluginName+".js"); module = await SystemJS.import("plugins/" + pluginName + ".js");
else else
module = await import("../"+pluginName+"/module") module = await import("../" + pluginName + "/module")
const plugin = new module['PluginModule']() const plugin = new module['PluginModule']()
const entry:SidebarEntries | SidebarEntry = plugin.getSidebarEntry() const entry: SidebarEntries | SidebarEntry = plugin.getSidebarEntry()
const rc = this.router.config const rc = this.router.config
switch (typeof (<any>entry).links) { switch (typeof (<any>entry).links) {
case "undefined": case "undefined":
const e:SidebarEntry = <SidebarEntry>entry const e: SidebarEntry = <SidebarEntry>entry
e.route = "plugin/" + e.route e.route = "plugin/" + e.route
this.sidebar.entries.push(e) this.sidebar.entries.push(e)
@@ -116,10 +108,10 @@ export class DynamicLoaderComponent implements AfterViewInit {
break; break;
case "object": case "object":
const m:SidebarEntries = <SidebarEntries>entry const m: SidebarEntries = <SidebarEntries>entry
m.links = m.links.map(link => { m.links = m.links.map(link => {
return {text: link.text, route: m.parentRoute + "/" + link.route } return { text: link.text, route: m.parentRoute + "/" + link.route }
}) })
this.sidebar.multientires.push(m) this.sidebar.multientires.push(m)