Skip to content

Commit

Permalink
Added output general prepared for general usage. Also added some logg…
Browse files Browse the repository at this point in the history
…ing for the basicui command. (#156)

Signed-off-by: Jerome Luckenbach <github@luckenba.ch>
  • Loading branch information
Confectrician authored Oct 26, 2019
1 parent 26855ba commit 00e4af3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
18 changes: 18 additions & 0 deletions client/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {PreviewPanel} from './WebView/PreviewPanel'

import * as _ from 'lodash'
import * as request from 'request-promise-native'
import { OutputChannel } from 'vscode'

//Create output channel
let extensionOutput: OutputChannel = null

/**
* humanize function adapter from the previously included underscore.string library
Expand Down Expand Up @@ -165,6 +169,7 @@ export function openBrowser(url) {

export function openUI(extensionPath: string, query: string = "/basicui/app", title?: string) {
let srcPath: string = getHost().concat(query);
appendToOutput(`URL that will be opened is: ${srcPath}`)

PreviewPanel.createOrShow(
extensionPath,
Expand All @@ -190,3 +195,16 @@ export async function handleRequestError(err) {
break
}
}

/**
* This will send a message frmo the extension to its output channel.
* If the channel isn't existing already, it will be created during method run.
*
* @param message The message to append to the extensions output Channel
*/
export function appendToOutput(message: string){

if(!extensionOutput) { extensionOutput = window.createOutputChannel("openHAB Extension") }

extensionOutput.appendLine(message)
}
6 changes: 5 additions & 1 deletion client/src/WebView/PreviewPanel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import * as vscode from 'vscode';

import { appendToOutput } from "../Utils";
/**
* Manages the extension WebView panel
*
Expand Down Expand Up @@ -30,10 +30,12 @@ export class PreviewPanel {

// If we already have a panel, show it.
if (PreviewPanel.currentPanel) {
appendToOutput(`There is already a preview panel existing. Revealing it.`)
PreviewPanel.currentPanel._panel.reveal(vscode.ViewColumn.Two);

// Update panel too, if an url was passed
if(url !== undefined && url !== PreviewPanel._lastUrl){
appendToOutput(`Updating existing preview panel now...`)
PreviewPanel.currentPanel._update(title, url);
PreviewPanel._lastUrl = url;
}
Expand All @@ -42,6 +44,7 @@ export class PreviewPanel {
}

// Otherwise, create a new panel.
appendToOutput(`Creating new preview panel.`)
const panel = vscode.window.createWebviewPanel(PreviewPanel.viewType, title, vscode.ViewColumn.Two, {
// Enable javascript in the webview
enableScripts: true
Expand All @@ -51,6 +54,7 @@ export class PreviewPanel {

// Update panel too, if an url was passed
if(url !== undefined){
appendToOutput(`Updating new preview panel now...`)
PreviewPanel.currentPanel._update(title, url);
PreviewPanel._lastUrl = url;
}
Expand Down
45 changes: 23 additions & 22 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import {
workspace,
} from 'vscode'

import {
openBrowser,
getSimpleModeState,
getSitemaps,
openUI,
getRestHover,
} from './Utils'
import * as utils from './Utils'

import { ItemsExplorer } from './ItemsExplorer/ItemsExplorer'
import { ThingsExplorer } from './ThingsExplorer/ThingsExplorer'
Expand Down Expand Up @@ -44,14 +38,15 @@ async function init(disposables: Disposable[], config, context): Promise<void> {
return
}

let absolutePath = editor.document.fileName
let fileName = path.basename(absolutePath)
let fileName = path.basename(editor.document.fileName)
let ui = config.sitemapPreviewUI

// Open specific sitemap if a sitemap file is active
if (fileName.endsWith('sitemap')) {
let sitemap = fileName.split('.')[0]
return openUI(

utils.appendToOutput(`Attempting to open Sitemap "${sitemap}" in BasicUI.`)
return utils.openUI(
_extensionPath,
`/${ui}/app?sitemap=${sitemap}`,
sitemap
Expand All @@ -60,20 +55,20 @@ async function init(disposables: Disposable[], config, context): Promise<void> {

// Open classic ui, if choosen in config
if (ui === 'classicui') {
return openUI(
return utils.openUI(
_extensionPath,
`/${ui}/app?sitemap=_default`,
'Classic UI'
)
}

// If there is only one user created sitemap open it directly, open sitemap list otherwise
getSitemaps().then(sitemaps => {
utils.getSitemaps().then(sitemaps => {
const defaultName = sitemap => sitemap.name === '_default'
const defaultSitemap = sitemaps.find(defaultName)

if (sitemaps.length === 1) {
return openUI(
return utils.openUI(
_extensionPath,
`/${ui}/app?sitemap=${sitemaps[0].name}`,
sitemaps[0].name
Expand All @@ -82,21 +77,21 @@ async function init(disposables: Disposable[], config, context): Promise<void> {

if (sitemaps.length === 2 && typeof defaultSitemap !== 'undefined') {
const index = sitemaps.indexOf(defaultName) === 0 ? 1 : 0
return openUI(
return utils.openUI(
_extensionPath,
`/${ui}/app?sitemap=${sitemaps[index].name}`,
sitemaps[index].name
)
}

return openUI(_extensionPath)
return utils.openUI(_extensionPath)
});

}))

disposables.push(commands.registerCommand('openhab.searchCommunity', (phrase?) => {
let query: string = phrase || '%s'
openBrowser(`https://community.openhab.org/search?q=${query}`)
utils.openBrowser(`https://community.openhab.org/search?q=${query}`)
}))

disposables.push(commands.registerCommand('openhab.openKaraf', () => {
Expand All @@ -114,20 +109,20 @@ async function init(disposables: Disposable[], config, context): Promise<void> {
route += (query.UID) ? `things/view/${query.UID}` : `item/edit/${param}` ;

// Check if simple mode is enabled
getSimpleModeState().then(simpleModeActive => {
utils.getSimpleModeState().then(simpleModeActive => {

if(!query.UID && simpleModeActive){
window.showWarningMessage(`Your openHAB environment is running in simple mode. Paper UI can't edit items when this mode is activated!`);
return;
}

return openBrowser(route.replace(/%23/g, '#'))
return utils.openBrowser(route.replace(/%23/g, '#'))
});

}))

disposables.push(commands.registerCommand('openhab.command.things.docs', (query: Thing) =>
openBrowser(`https://www.openhab.org/addons/bindings/${query.binding}/`)))
utils.openBrowser(`https://www.openhab.org/addons/bindings/${query.binding}/`)))

if (config.useRestApi) {
const itemsExplorer = new ItemsExplorer()
Expand Down Expand Up @@ -177,7 +172,7 @@ async function init(disposables: Disposable[], config, context): Promise<void> {
return null
}

return getRestHover(hoveredText)
return utils.getRestHover(hoveredText)
}
})

Expand All @@ -204,11 +199,17 @@ export function activate(context: ExtensionContext) {
init(disposables, config, context)
.catch(err => console.error(err));

console.log(`openHAB vscode extension has been activated`);
var message = `openHAB vscode extension has been activated`;

console.log(message);
utils.appendToOutput(message);

}

// this method is called when your extension is deactivated
export function deactivate() {
console.log(`openHAB vscode extension has been shut down`);
var message = `openHAB vscode extension has been shut down`;

console.log(message);
utils.appendToOutput(message);
}

0 comments on commit 00e4af3

Please sign in to comment.