Implement client side enable, disable, edit
parent
4467094230
commit
0971908da7
|
|
@ -10,19 +10,15 @@ tbody tr:nth-child(2n+1) {
|
||||||
background-color: #161f27;
|
background-color: #161f27;
|
||||||
}
|
}
|
||||||
|
|
||||||
#upserts {
|
#upsert {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
#upserts > textarea {
|
#upsert > textarea {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
resize: vertical;
|
resize: vertical;
|
||||||
}
|
}
|
||||||
|
|
||||||
#upserts > input[name="id"] {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
#jobs > tbody > tr > td {
|
#jobs > tbody > tr > td {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,9 @@
|
||||||
</h1>
|
</h1>
|
||||||
<details>
|
<details>
|
||||||
<summary>Upsert Job</summary>
|
<summary>Upsert Job</summary>
|
||||||
<form id="upserts" action="#" method="get" onsubmit="upserts(); return false;">
|
<form id="upsert" action="#" method="get" onsubmit="upsert(); return false;">
|
||||||
<input type="text" name="id" placeholder="id"/>
|
<input type="text" name="id" placeholder="id"/>
|
||||||
|
<label><input type="checkbox" name="disabled"/> Disabled</label>
|
||||||
<select name="language" required>
|
<select name="language" required>
|
||||||
<option value="bash" selected>bash</option>
|
<option value="bash" selected>bash</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,11 @@ function http(method, remote, callback, body) {
|
||||||
xmlhttp.send(body);
|
xmlhttp.send(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
function upserts() {
|
function upsert() {
|
||||||
function cb(body, status) {
|
function cb(body, status) {
|
||||||
console.log(status, body)
|
console.log(status, body)
|
||||||
}
|
}
|
||||||
http("POST", "/upserts", cb, jsonifyForm("upserts"))
|
http("POST", "/api/job/upsert", cb, jsonifyForm("upsert"))
|
||||||
}
|
}
|
||||||
|
|
||||||
function jsonifyForm(id) {
|
function jsonifyForm(id) {
|
||||||
|
|
@ -33,8 +33,6 @@ function init() {
|
||||||
jobs.forEach(function(job) {
|
jobs.forEach(function(job) {
|
||||||
var s = format(job)
|
var s = format(job)
|
||||||
inject(s)
|
inject(s)
|
||||||
console.log("job=", job)
|
|
||||||
console.log("s=", s)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
function format(job) {
|
function format(job) {
|
||||||
|
|
@ -55,12 +53,12 @@ function init() {
|
||||||
btns.forEach(function(e) {
|
btns.forEach(function(e) {
|
||||||
buttons += `
|
buttons += `
|
||||||
<span>
|
<span>
|
||||||
<input type="button" onclick="${e.name}(this);" value="&#${e.icon};" alt="${e.name}" title="${e.name}"/>
|
<input type="button" onclick="job${e.name}(this);" value="&#${e.icon};" alt="${e.name}" title="${e.name}" job="${btoa(JSON.stringify(job))}"/>
|
||||||
</span>
|
</span>
|
||||||
`
|
`
|
||||||
})
|
})
|
||||||
return `<tr><td><details>
|
return `<tr><td><details>
|
||||||
<summary name="${job.id}"}">
|
<summary name="${job.id}">
|
||||||
<span>${job.title}</span>
|
<span>${job.title}</span>
|
||||||
<span>${passing}</span>
|
<span>${passing}</span>
|
||||||
${buttons}
|
${buttons}
|
||||||
|
|
@ -87,7 +85,50 @@ function init() {
|
||||||
var table = document.getElementById("jobs").getElementsByTagName("tbody")[0]
|
var table = document.getElementById("jobs").getElementsByTagName("tbody")[0]
|
||||||
table.innerHTML += s
|
table.innerHTML += s
|
||||||
}
|
}
|
||||||
http("GET", "/list", cb, null)
|
http("GET", "/api/job/list", cb, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
init()
|
init()
|
||||||
|
|
||||||
|
function jobdisable(input) {
|
||||||
|
jobmodify(input)
|
||||||
|
getField("disabled").checked = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function jobenable(input) {
|
||||||
|
jobmodify(input)
|
||||||
|
getField("disabled").checked = false
|
||||||
|
}
|
||||||
|
|
||||||
|
function jobmodify(input) {
|
||||||
|
var form = getForm()
|
||||||
|
var job = jobFromInput(input)
|
||||||
|
var fields = ["id", "language", "cron", "script", "disabled"]
|
||||||
|
fields.forEach(function(field) {
|
||||||
|
var e = getField(field)
|
||||||
|
e.checked = job[field]
|
||||||
|
e.value = job[field]
|
||||||
|
})
|
||||||
|
var details = form.parentElement
|
||||||
|
details.setAttribute("open", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
function jobdelete(input) {
|
||||||
|
http("DELETE", "/api/job/delete", cb, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
function jobFromInput(input) {
|
||||||
|
var b64 = input.getAttribute("job")
|
||||||
|
var json = atob(b64)
|
||||||
|
return JSON.parse(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getForm() {
|
||||||
|
return document.getElementById("upsert")
|
||||||
|
}
|
||||||
|
|
||||||
|
function getField(name) {
|
||||||
|
var form = getForm()
|
||||||
|
var matches = form.elements[name]
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ type Job struct {
|
||||||
LastOutput string
|
LastOutput string
|
||||||
LastRuntime time.Duration
|
LastRuntime time.Duration
|
||||||
LastRun time.Time
|
LastRun time.Time
|
||||||
|
Disabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJob(runner Runner, schedule, raw string) (*Job, error) {
|
func NewJob(runner Runner, schedule, raw string) (*Job, error) {
|
||||||
|
|
@ -100,6 +101,7 @@ func (j *Job) Decode(b []byte) error {
|
||||||
k.LastOutput = j.LastOutput
|
k.LastOutput = j.LastOutput
|
||||||
k.LastRuntime = j.LastRuntime
|
k.LastRuntime = j.LastRuntime
|
||||||
k.LastRun = j.LastRun
|
k.LastRun = j.LastRun
|
||||||
|
k.Disabled = j.Disabled
|
||||||
*j = *k
|
*j = *k
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,9 @@ func TestJobEncodeDecode(t *testing.T) {
|
||||||
if k.Name != j.Name {
|
if k.Name != j.Name {
|
||||||
t.Error(k.Name, "vs", j.Name)
|
t.Error(k.Name, "vs", j.Name)
|
||||||
}
|
}
|
||||||
|
if k.Disabled != j.Disabled {
|
||||||
|
t.Error(k.Disabled, "vs", j.Disabled)
|
||||||
|
}
|
||||||
if k.Title != j.Title {
|
if k.Title != j.Title {
|
||||||
t.Error(k.Title, "vs", j.Title)
|
t.Error(k.Title, "vs", j.Title)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
out[i]["disabled"] = j.Disabled
|
||||||
out[i]["id"] = j.Name
|
out[i]["id"] = j.Name
|
||||||
out[i]["title"] = j.Title
|
out[i]["title"] = j.Title
|
||||||
out[i]["cron"] = j.Schedule
|
out[i]["cron"] = j.Schedule
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,11 @@ func (s *Server) Routes() error {
|
||||||
handler http.HandlerFunc
|
handler http.HandlerFunc
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
path: fmt.Sprintf("/upserts"),
|
path: fmt.Sprintf("/api/job/upsert"),
|
||||||
handler: s.gzip(s.authenticate(s.upserts)),
|
handler: s.gzip(s.authenticate(s.upsert)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: fmt.Sprintf("/list"),
|
path: fmt.Sprintf("/api/job/list"),
|
||||||
handler: s.gzip(s.authenticate(s.list)),
|
handler: s.gzip(s.authenticate(s.list)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ type upsertRequest struct {
|
||||||
Language string `json:"language"`
|
Language string `json:"language"`
|
||||||
Cron string `json:"cron"`
|
Cron string `json:"cron"`
|
||||||
Script string `json:"script"`
|
Script string `json:"script"`
|
||||||
|
Disabled bool `json:"disabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func newUpsertRequest(r io.Reader) (upsertRequest, error) {
|
func newUpsertRequest(r io.Reader) (upsertRequest, error) {
|
||||||
|
|
@ -51,7 +52,7 @@ func (u *upsertRequest) validate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) upserts(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) upsert(w http.ResponseWriter, r *http.Request) {
|
||||||
upsert, err := newUpsertRequest(r.Body)
|
upsert, err := newUpsertRequest(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
Loading…
Reference in New Issue