Buttons are refreshed every 3s

This commit is contained in:
jamesread 2021-06-20 00:12:51 +01:00
parent 95dcbcbeed
commit 5d01183198
7 changed files with 51 additions and 13 deletions

View File

@ -5,8 +5,9 @@ option go_package = "gen/grpc";
import "google/api/annotations.proto"; import "google/api/annotations.proto";
message ActionButton { message ActionButton {
string title = 1; string id = 1;
string icon = 2; string title = 2;
string icon = 3;
} }
message GetButtonsResponse { message GetButtonsResponse {

View File

@ -4,6 +4,7 @@ import ()
// ActionButton represents a button that is shown in the webui. // ActionButton represents a button that is shown in the webui.
type ActionButton struct { type ActionButton struct {
Id string
Title string Title string
Icon string Icon string
Shell string Shell string

View File

@ -6,6 +6,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"google.golang.org/grpc" "google.golang.org/grpc"
"net" "net"
"crypto/md5"
"fmt"
config "github.com/jamesread/OliveTin/internal/config" config "github.com/jamesread/OliveTin/internal/config"
executor "github.com/jamesread/OliveTin/internal/executor" executor "github.com/jamesread/OliveTin/internal/executor"
@ -28,8 +30,9 @@ func (api *oliveTinAPI) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (
for _, action := range cfg.ActionButtons { for _, action := range cfg.ActionButtons {
btn := pb.ActionButton{ btn := pb.ActionButton{
Title: action.Title, Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))),
Icon: lookupHTMLIcon(action.Icon), Title: action.Title,
Icon: lookupHTMLIcon(action.Icon),
} }
res.Actions = append(res.Actions, &btn) res.Actions = append(res.Actions, &btn)

View File

@ -18,6 +18,12 @@ class ActionButton extends window.HTMLButtonElement {
this.constructTemplate() this.constructTemplate()
this.updateHtml() this.updateHtml()
this.setAttribute("id", "actionButton_" + json.id)
}
updateFromJson (json) {
console.log("updating button")
} }
startAction () { startAction () {
@ -67,6 +73,7 @@ class ActionButton extends window.HTMLButtonElement {
this.domTitle = this.querySelector('.title') this.domTitle = this.querySelector('.title')
this.domIcon = this.querySelector('.icon') this.domIcon = this.querySelector('.icon')
} }
updateHtml () { updateHtml () {

14
webui/js/logger.js Normal file
View File

@ -0,0 +1,14 @@
window.logs = []
export function addToLog (evt) {
window.logs.append(evt)
showLog(evt)
}
function showLog (evt) {
let msg = document.createElement('pre')
msg.innerText = evt;
document.body.appendChild(msg)
}

View File

@ -2,9 +2,14 @@ import './ActionButton.js' // To define action-button
export function marshalActionButtonsJsonToHtml (json) { export function marshalActionButtonsJsonToHtml (json) {
for (const jsonButton of json.actions) { for (const jsonButton of json.actions) {
const a = document.createElement('button', { is: 'action-button' }) var htmlButton = document.querySelector('#actionButton_' + jsonButton.id)
a.constructFromJson(jsonButton)
document.getElementById('rootGroup').appendChild(a) if (htmlButton == null) {
htmlButton = document.createElement('button', { is: 'action-button' })
htmlButton.constructFromJson(jsonButton)
document.getElementById('rootGroup').appendChild(htmlButton)
} else {
htmlButton.updateFromJson(jsonButton)
}
} }
} }

View File

@ -3,19 +3,19 @@
import { marshalActionButtonsJsonToHtml } from './js/marshaller.js' import { marshalActionButtonsJsonToHtml } from './js/marshaller.js'
function showBigError (type, friendlyType, message) { function showBigError (type, friendlyType, message) {
clearInterval(window.buttonInterval)
console.error('Error ' + type + ': ', message) console.error('Error ' + type + ': ', message)
const domErr = document.createElement('div') const domErr = document.createElement('div')
domErr.classList.add('error') domErr.classList.add('error')
domErr.innerHTML = '<h1>Error ' + friendlyType + '</h1><p>' + message + "</p><p><a href = 'http://github.com/jamesread/OliveTin' target = 'blank'/>OliveTin Documentation</a></p>" domErr.innerHTML = '<h1>Error ' + friendlyType + '</h1><p>' + message + "</p><p><a href = 'http://olivetin.app/_errors_troubleshooting.html' target = 'blank'/>OliveTin Documentation</a></p>"
document.getElementById('rootGroup').appendChild(domErr) document.getElementById('rootGroup').appendChild(domErr)
} }
function onInitialLoad (res) { function fetchGetButtons() {
window.restBaseUrl = res.Rest window.fetch(window.restBaseUrl + 'GetButtons', {
window.fetch(window.restBaseUrl + 'GetButtons', {
cors: 'cors' cors: 'cors'
// No fetch options // No fetch options
}).then(res => { }).then(res => {
@ -23,10 +23,17 @@ function onInitialLoad (res) {
}).then(res => { }).then(res => {
marshalActionButtonsJsonToHtml(res) marshalActionButtonsJsonToHtml(res)
}).catch(err => { }).catch(err => {
showBigError('fetch-initial-buttons', 'getting initial buttons', err, 'blat') showBigError('fetch-buttons', 'getting buttons', err, 'blat')
}) })
} }
function onInitialLoad (res) {
window.restBaseUrl = res.Rest
window.buttonInterval = setInterval(fetchGetButtons, 3000);
fetchGetButtons()
}
window.fetch('webUiSettings.json').then(res => { window.fetch('webUiSettings.json').then(res => {
return res.json() return res.json()
}).then(res => { }).then(res => {