import watch can now also add filters instead of clearing existing

This commit is contained in:
BroodjeAap 2023-01-20 14:25:09 +00:00
parent baa442104a
commit be7ca26b19
5 changed files with 43 additions and 2 deletions

28
main.go
View file

@ -3,6 +3,7 @@ package main
import ( import (
"embed" "embed"
"encoding/json" "encoding/json"
"errors"
"flag" "flag"
"fmt" "fmt"
"html/template" "html/template"
@ -690,6 +691,22 @@ func (web *Web) importWatch(c *gin.Context) {
c.AbortWithError(http.StatusBadRequest, err) c.AbortWithError(http.StatusBadRequest, err)
return return
} }
importType := c.PostForm("type")
if !(importType == "clear" || importType == "add") {
c.AbortWithError(http.StatusBadRequest, errors.New("Unknown Import Type"))
return
}
clearFilters := importType == "clear"
offsetX := 0
offsetY := 0
if !clearFilters {
offsetX, _ = strconv.Atoi(c.PostForm("offset_x"))
offsetY, _ = strconv.Atoi(c.PostForm("offset_y"))
offsetX *= -1
offsetY *= -1
}
file, err := c.FormFile("json") file, err := c.FormFile("json")
@ -714,7 +731,9 @@ func (web *Web) importWatch(c *gin.Context) {
// stop/delete cronjobs running for this watch // stop/delete cronjobs running for this watch
var cronFilters []Filter var cronFilters []Filter
if !clearFilters {
web.db.Model(&Filter{}).Where("watch_id = ? AND type = 'cron'", watchID).Find(&cronFilters) web.db.Model(&Filter{}).Where("watch_id = ? AND type = 'cron'", watchID).Find(&cronFilters)
}
for _, filter := range cronFilters { for _, filter := range cronFilters {
entryID, exist := web.cronWatch[filter.ID] entryID, exist := web.cronWatch[filter.ID]
if exist { if exist {
@ -728,9 +747,14 @@ func (web *Web) importWatch(c *gin.Context) {
filter := &export.Filters[i] filter := &export.Filters[i]
filterMap[filter.ID] = filter filterMap[filter.ID] = filter
filter.ID = 0 filter.ID = 0
filter.X += offsetX
filter.Y += offsetY
filter.WatchID = uint(watchID) filter.WatchID = uint(watchID)
} }
if clearFilters {
web.db.Delete(&Filter{}, "watch_id = ?", watchID) web.db.Delete(&Filter{}, "watch_id = ?", watchID)
}
if len(export.Filters) > 0 { if len(export.Filters) > 0 {
tx := web.db.Create(&export.Filters) tx := web.db.Create(&export.Filters)
@ -740,7 +764,7 @@ func (web *Web) importWatch(c *gin.Context) {
} }
for i := range export.Filters { for i := range export.Filters {
filter := &export.Filters[i] filter := &export.Filters[i]
if filter.Type == "cron" { if filter.Type == "cron" && filter.Var2 != nil && *filter.Var2 == "yes" {
entryID, err := web.cron.AddFunc(filter.Var1, func() { triggerSchedule(filter.WatchID, web, &filter.ID) }) entryID, err := web.cron.AddFunc(filter.Var1, func() { triggerSchedule(filter.WatchID, web, &filter.ID) })
if err != nil { if err != nil {
log.Println("Could not start job for Watch: ", filter.WatchID) log.Println("Could not start job for Watch: ", filter.WatchID)
@ -752,7 +776,9 @@ func (web *Web) importWatch(c *gin.Context) {
} }
} }
if clearFilters {
web.db.Delete(&FilterConnection{}, "watch_id = ?", watchID) web.db.Delete(&FilterConnection{}, "watch_id = ?", watchID)
}
for i := range export.Connections { for i := range export.Connections {
connection := &export.Connections[i] connection := &export.Connections[i]
connection.ID = 0 connection.ID = 0

View file

@ -568,6 +568,10 @@ var Diagrams = /** @class */ (function () {
if (this.mouseState.panning) { if (this.mouseState.panning) {
this.mouseState.offset.x += this.mouseState.delta.x; this.mouseState.offset.x += this.mouseState.delta.x;
this.mouseState.offset.y += this.mouseState.delta.y; this.mouseState.offset.y += this.mouseState.delta.y;
var importOffsetInputX = document.getElementById("offset_x");
importOffsetInputX.value = this.mouseState.offset.x.toString();
var importOffsetInputY = document.getElementById("offset_y");
importOffsetInputY.value = this.mouseState.offset.y.toString();
} }
this.mouseState.world.x = this.mouseState.canvas.x - this.mouseState.offset.x; this.mouseState.world.x = this.mouseState.canvas.x - this.mouseState.offset.x;
this.mouseState.world.y = this.mouseState.canvas.y - this.mouseState.offset.y; this.mouseState.world.y = this.mouseState.canvas.y - this.mouseState.offset.y;

View file

@ -611,6 +611,12 @@ class Diagrams {
if (this.mouseState.panning){ if (this.mouseState.panning){
this.mouseState.offset.x += this.mouseState.delta.x; this.mouseState.offset.x += this.mouseState.delta.x;
this.mouseState.offset.y += this.mouseState.delta.y; this.mouseState.offset.y += this.mouseState.delta.y;
let importOffsetInputX = document.getElementById("offset_x") as HTMLInputElement;
importOffsetInputX.value = this.mouseState.offset.x.toString();
let importOffsetInputY = document.getElementById("offset_y") as HTMLInputElement;
importOffsetInputY.value = this.mouseState.offset.y.toString();
} }
this.mouseState.world.x = this.mouseState.canvas.x - this.mouseState.offset.x; this.mouseState.world.x = this.mouseState.canvas.x - this.mouseState.offset.x;

View file

@ -169,6 +169,12 @@ GoWatch Edit {{ .Watch.Name }}
<div class="mb-3"> <div class="mb-3">
<label for="json" class="form-label">Upload JSON to import</label> <label for="json" class="form-label">Upload JSON to import</label>
<input class="form-control" type="file" id="json" name="json"> <input class="form-control" type="file" id="json" name="json">
<select class="form-select form-select-lg mb-3 mt-2" name="type">
<option value="clear" selected>Clear filters</option>
<option value="add">Add to filters</option>
</select>
<input type="hidden" id="offset_x" name="offset_x" value="0">
<input type="hidden" id="offset_y" name="offset_y" value="0">
<div class="col-auto mt-3"> <div class="col-auto mt-3">
<button type="submit" class="btn btn-primary mb-3">Import</button> <button type="submit" class="btn btn-primary mb-3">Import</button>
</div> </div>

View file

@ -1,5 +1,4 @@
# Todo # Todo
- import options add/clear
- comments - comments
- safe escape {{ }} for pages - safe escape {{ }} for pages
- 'jitter' for cronjobs after first start ? - 'jitter' for cronjobs after first start ?