From fed2b455ce47bec12fca7f72e362d5b1109a2ee0 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 14 Aug 2019 01:13:49 +0200 Subject: [PATCH] nicer let syntax for fb --- .../apiclient-consumptions.component.ts | 8 +-- .../apiclient-settings-form.component.ts | 68 +++++++++---------- .../apiclient-subscriptions.component.ts | 55 +++++++-------- .../dynamic-loader.component.ts | 56 +++++++-------- 4 files changed, 81 insertions(+), 106 deletions(-) diff --git a/src/frontend/dashboard/src/app/ApiClient/apiclient-consumptions.component.ts b/src/frontend/dashboard/src/app/ApiClient/apiclient-consumptions.component.ts index d37b31e..cd43cc1 100644 --- a/src/frontend/dashboard/src/app/ApiClient/apiclient-consumptions.component.ts +++ b/src/frontend/dashboard/src/app/ApiClient/apiclient-consumptions.component.ts @@ -3,11 +3,7 @@ import { FrontblockApiConf } from 'frontblock'; import { SubscriptionResponse, parseResponse, SuccessResponse } from 'frontblock-generic/Types.js'; import { environment } from "../../environments/environment" -let fb -if(environment.production){ - fb = window["fb"] -}else{ - fb = { +const fb = environment.production?window["fb"]:{ ApiClient : { setConfig: async () => { await new Promise((resolve, reject) => { @@ -32,9 +28,7 @@ if(environment.production){ }, unsubscribe: (uid) => new SuccessResponse(), quit: (uid) => new SuccessResponse() - } - } } @Component({ selector: 'consumptions', diff --git a/src/frontend/dashboard/src/app/ApiClient/apiclient-settings-form.component.ts b/src/frontend/dashboard/src/app/ApiClient/apiclient-settings-form.component.ts index c9105e5..cb3e5c8 100644 --- a/src/frontend/dashboard/src/app/ApiClient/apiclient-settings-form.component.ts +++ b/src/frontend/dashboard/src/app/ApiClient/apiclient-settings-form.component.ts @@ -1,26 +1,22 @@ import { Component, OnInit } from '@angular/core'; -import { fbind } from 'q'; import { FrontblockApiConf } from 'frontblock'; import { environment } from "../../environments/environment" -let fb -if(environment.production){ - fb = window["fb"] -}else{ - fb = { - ApiClient : { - setConfig: async () => { - await new Promise((resolve, reject) => { - setTimeout(() => { - resolve('Promise A win!'); - }, 500) - })} +const fb = environment.production ? window["fb"] : { + ApiClient: { + setConfig: async () => { + await new Promise((resolve, reject) => { + setTimeout(() => { + resolve('Promise A win!'); + }, 500) + }) + } } - } } + @Component({ - selector: 'settings', - template: ` + selector: 'settings', + template: `
Settings @@ -77,10 +73,10 @@ if(environment.production){ ` }) export class ApiclientFormComponent implements OnInit { - testnet:boolean = true - saving:boolean = false - advanced:boolean = false - data:FrontblockApiConf = { + testnet: boolean = true + saving: boolean = false + advanced: boolean = false + data: FrontblockApiConf = { apiHost: "api.testnet.frontblock.me", apiPort: 10001, tls: false, @@ -91,28 +87,28 @@ export class ApiclientFormComponent implements OnInit { ngOnInit() { } - setAdvanced(){ this.advanced = true } + setAdvanced() { this.advanced = true } - checkData():boolean{ + checkData(): boolean { return ( - typeof this.data.apiPort === "number" && + typeof this.data.apiPort === "number" && typeof this.data.apiHost === "string" && typeof this.data.tls === "boolean" && - typeof this.data.apiKey === "string" - ) + typeof this.data.apiKey === "string" + ) } - updateTestnet(){ - if(this.testnet){ + updateTestnet() { + if (this.testnet) { this.data.apiHost = "api.testnet.frontblock.me" this.data.apiPort = 10001 - }else{ + } else { this.data.apiHost = "api.frontblock.me" this.data.apiPort = 10000 } } - updatePort(obj){ + updatePort(obj) { switch (this.data.apiHost) { case "api.testnet.frontblock.me": this.data.apiPort = 10001 @@ -120,23 +116,23 @@ export class ApiclientFormComponent implements OnInit { case "api.frontblock.me": this.data.apiPort = 10000 break; - + 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; } console.log(this.data) } - async save(){ + async save() { this.saving = true; - if(typeof this.data.apiPort === "string") + if (typeof this.data.apiPort === "string") this.data.apiPort = parseInt(this.data.apiPort) - - if(this.checkData()){ + + if (this.checkData()) { const conf = await fb.ApiClient.setConfig(this.data) console.log(conf) - }else{ + } else { //show error in gui console.error("bad data :(") } diff --git a/src/frontend/dashboard/src/app/ApiClient/apiclient-subscriptions.component.ts b/src/frontend/dashboard/src/app/ApiClient/apiclient-subscriptions.component.ts index 3d6fd25..0b2b32f 100644 --- a/src/frontend/dashboard/src/app/ApiClient/apiclient-subscriptions.component.ts +++ b/src/frontend/dashboard/src/app/ApiClient/apiclient-subscriptions.component.ts @@ -1,46 +1,39 @@ import { Component, OnInit } from '@angular/core'; -import { fbind } from 'q'; 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" -let fb -if(environment.production){ - fb = window["fb"] -}else{ - fb = { - ApiClient : { - setConfig: async () => { +const fb = environment.production ? window["fb"] : { + ApiClient: { + setConfig: async () => { await new Promise((resolve, reject) => { - setTimeout(() => { - resolve('Promise A win!'); - }, 500) - }) + setTimeout(() => { + resolve('Promise A win!'); + }, 500) + }) }, getConsumptions: async () => { return await new Promise((resolve, reject) => { setTimeout(() => { - resolve([new SubscriptionResponse("654", "321"), new SubscriptionResponse("456", "123")]); + resolve([new SubscriptionResponse("654", "321"), new SubscriptionResponse("456", "123")]); }, 500) }) }, getSubscriptions: async () => { return await new Promise((resolve, reject) => { setTimeout(() => { - resolve([new SubscriptionResponse("321"), new SubscriptionResponse("123")]); + resolve([new SubscriptionResponse("321"), new SubscriptionResponse("123")]); }, 500) }) }, unsubscribe: (uid) => new SuccessResponse(), quit: (uid) => new SuccessResponse() - } - } } @Component({ - selector: 'subscriptions', - template: ` + selector: 'subscriptions', + template: `
Subscriptions @@ -87,9 +80,9 @@ if(environment.production){ ` }) export class ApiclientSubscriptionComponent implements OnInit { - loading:boolean = false - actionName:string = "load" - entries:SubscriptionResponse[] = [] + loading: boolean = false + actionName: string = "load" + entries: SubscriptionResponse[] = [] alerts: { severity: "danger" | "warning" | "success" message: string @@ -99,24 +92,24 @@ export class ApiclientSubscriptionComponent implements OnInit { ngOnInit() { } - async load(){ + async load() { this.loading = true this.entries = await fb.ApiClient.getSubscriptions() - if(this.entries.length === 0){ - this.alerts.push({ severity: "warning", message: "0 results were returned"}) + if (this.entries.length === 0) { + this.alerts.push({ severity: "warning", message: "0 results were returned" }) } this.loading = false this.actionName = "refresh" } - async quit(uid:string){ + async quit(uid: string) { const r = await fb.ApiClient.unsubscribe(uid) - const res = parseResponse(r) - if(res instanceof SuccessResponse){ - this.alerts.push({ severity: "success", message: "Stopped subscription "+uid}) + const res = parseResponse(r) + if (res instanceof SuccessResponse) { + this.alerts.push({ severity: "success", message: "Stopped subscription " + uid }) this.load() - }else{ - this.alerts.push({ severity: "danger", message: "Error "+res.message}) + } else { + this.alerts.push({ severity: "danger", message: "Error " + res.message }) } } diff --git a/src/frontend/dashboard/src/app/dynamic-loader/dynamic-loader.component.ts b/src/frontend/dashboard/src/app/dynamic-loader/dynamic-loader.component.ts index 92f438b..91f5fe2 100644 --- a/src/frontend/dashboard/src/app/dynamic-loader/dynamic-loader.component.ts +++ b/src/frontend/dashboard/src/app/dynamic-loader/dynamic-loader.component.ts @@ -36,90 +36,82 @@ 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 { Router } from '@angular/router'; import { FrontendPlugin, SidebarEntries, SidebarEntry } from "frontblock-generic/Plugin" import { SidebarEntryService } from '../sidebar-entry-service.service'; import { environment } from "../../environments/environment" - -let fb -if(environment.production){ - fb = window["fb"] -}else{ - - fb = { - PluginManager : { - getLoadedPluginNames: () => ["ApiClient", "PluginManager"] - } +const fb = environment.production ? window["fb"] : { + PluginManager: { + getLoadedPluginNames: () => ["ApiClient", "PluginManager"] } } - @Component({ selector: 'dynamic-loader', templateUrl: './dynamic-loader.component.html', styleUrls: ['./dynamic-loader.component.scss'] }) export class DynamicLoaderComponent implements AfterViewInit { - sidebar:SidebarComponent + sidebar: SidebarComponent constructor( private router: Router, - private sidebarService:SidebarEntryService + private sidebarService: SidebarEntryService ) { } ngAfterViewInit() { const _this = this; - (function awaitSidebar(){ + (function awaitSidebar() { const sb = _this.sidebarService.getSidebar() - if(sb != null){ + if (sb != null) { _this.sidebar = sb _this.loadWidgets() return } - setTimeout(awaitSidebar,25) + setTimeout(awaitSidebar, 25) })() } - private async loadWidgets(){ - while(!fb.PluginManager){ + private async loadWidgets() { + while (!fb.PluginManager) { await new Promise((resolve) => setTimeout(resolve, 50)) } const pluginNames = await fb.PluginManager.getLoadedPluginNames() pluginNames.forEach(element => this.installWidget(element)); } - private async installWidget(pluginName:string){ - let module: FrontendPlugin - if(environment.production) - module = await SystemJS.import("plugins/"+pluginName+".js"); + private async installWidget(pluginName: string) { + let module: FrontendPlugin + if (environment.production) + module = await SystemJS.import("plugins/" + pluginName + ".js"); else - module = await import("../"+pluginName+"/module") + module = await import("../" + pluginName + "/module") const plugin = new module['PluginModule']() - const entry:SidebarEntries | SidebarEntry = plugin.getSidebarEntry() + const entry: SidebarEntries | SidebarEntry = plugin.getSidebarEntry() const rc = this.router.config - + switch (typeof (entry).links) { case "undefined": - const e:SidebarEntry = entry - e.route = "plugin/" + e.route + const e: SidebarEntry = entry + e.route = "plugin/" + e.route this.sidebar.entries.push(e) rc.unshift({ path: "plugin", loadChildren: async () => module['PluginModule'] }) - + break; case "object": - const m:SidebarEntries = entry + const m: SidebarEntries = entry 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) @@ -127,7 +119,7 @@ export class DynamicLoaderComponent implements AfterViewInit { path: m.parentRoute, loadChildren: async () => module['PluginModule'] }) - break; + break; } this.router.resetConfig(rc) }