Skip to content

Commit

Permalink
fix feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Sep 16, 2024
1 parent 64071a8 commit 297a0da
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 94 deletions.
48 changes: 38 additions & 10 deletions assets/js/editor-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import { progress } from "./uploader.mjs";
default:
// note, the #runapp hash is used by js-dos
}

// New file download form event listener.
document.body.addEventListener("htmx:afterRequest", function (event) {
afterFormRequest(
Expand Down Expand Up @@ -110,27 +111,49 @@ import { progress } from "./uploader.mjs";
"artifact-editor-link-delete",
"artifact-editor-link-feedback"
);
afterLinkRequest(
event,
"artifact-editor-comp-previewcopy",
"artifact-editor-comp-feedback"
);
afterLinkRequest(
event,
"artifact-editor-comp-previewtext",
"artifact-editor-comp-feedback"
);
afterLinkRequest(
event,
"artifact-editor-comp-textcopy",
"artifact-editor-comp-feedback"
);
});

function afterDeleteRequest(event, inputId, feedbackId) {
/**
* After link request event listener.
* @param {Event} event - The htmx event.
* @param {string} inputId - The inputId is the id of the input element that triggered the request,
* or the name of the input element that triggered the request.
* @param {string} feedbackId - The feedback name.
* @returns {void}
**/
function afterLinkRequest(event, inputId, feedbackId) {
if (event.detail.elt === null) return;
if (event.detail.elt.id !== `${inputId}`) return;
if (
event.detail.elt.id !== `${inputId}` &&
event.detail.elt.name !== inputId
)
return;
const feedback = document.getElementById(feedbackId);
if (feedback === null) {
throw new Error(
`The htmx successful feedback element ${feedbackId} is null`
);
}
const errClass = "text-danger";
const okClass = "text-success";
const xhr = event.detail.xhr;
if (event.detail.successful) {
feedback.innerText = `The delete request was successful, about to refresh the page.`;
feedback.innerText = `${xhr.responseText}`;
feedback.classList.remove(errClass);
feedback.classList.add(okClass);
setTimeout(() => {
location.reload();
}, 500);
return;
}
if (event.detail.failed && event.detail.xhr) {
Expand All @@ -146,7 +169,7 @@ import { progress } from "./uploader.mjs";
" please try again or refresh the page.";
}

function afterLinkRequest(event, inputId, feedbackId) {
function afterDeleteRequest(event, inputId, feedbackId) {
if (event.detail.elt === null) return;
if (event.detail.elt.id !== `${inputId}`) return;
const feedback = document.getElementById(feedbackId);
Expand All @@ -156,10 +179,15 @@ import { progress } from "./uploader.mjs";
);
}
const errClass = "text-danger";
const okClass = "text-success";
const xhr = event.detail.xhr;
if (event.detail.successful) {
feedback.innerText = `${xhr.responseText}`;
feedback.innerText = `The delete request was successful, about to refresh the page.`;
feedback.classList.remove(errClass);
feedback.classList.add(okClass);
setTimeout(() => {
location.reload();
}, 500);
return;
}
if (event.detail.failed && event.detail.xhr) {
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/Defacto2/server
go 1.22.7

require (
github.com/Defacto2/archive v1.0.1
github.com/Defacto2/archive v1.0.2
github.com/Defacto2/helper v1.1.1
github.com/Defacto2/magicnumber v1.0.1
github.com/Defacto2/releaser v1.0.4
Expand Down Expand Up @@ -33,6 +33,11 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

replace (
github.com/Defacto2/archive => /home/ben/github/archive
github.com/Defacto2/helper => /home/ben/github/helper
)

require (
cloud.google.com/go/auth v0.9.4 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1MRDJM=
github.com/DATA-DOG/go-sqlmock v1.4.1/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/Defacto2/archive v1.0.1 h1:f6qli42DWJWwNLbcB6SS0MSmVmaUzCJGXzTRsk8PskE=
github.com/Defacto2/archive v1.0.1/go.mod h1:0/aa1BwDOBp4d87R/N33Lcr3+WzELv041wunK8R5A5c=
github.com/Defacto2/helper v1.1.1 h1:wu8ynKF4ZXruTZNrDumzPVyXxFwhCvtWlsdimPNSXXc=
github.com/Defacto2/helper v1.1.1/go.mod h1:vtHe6R1Pl3YsxsqXe/UZldvNx1n1Y6dM1JUL9Uczr6g=
github.com/Defacto2/magicnumber v1.0.1 h1:UmiBUrYwTk3NPR1lGaaQ3Js+3YllBRf9XxSKInE3Da4=
Expand Down
88 changes: 51 additions & 37 deletions handler/app/internal/filerecord/filerecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,65 +58,64 @@ type ListEntry struct {
UniqueID string
Executable magicnumber.Windows
Images, Programs, Texts bool
name string
platform string
section string
bytes int64
}

// HTML returns the HTML for an file item in the "Download content" section of the File editor.
func (m ListEntry) HTML(bytes int64, platform, section string) string {
name := url.QueryEscape(m.RelativeName)
titlename := m.RelativeName
func (m *ListEntry) HTML(bytes int64, platform, section string) string {
m.name = url.QueryEscape(m.RelativeName)
m.bytes = bytes
m.platform = platform
m.section = section
displayname := m.RelativeName
if strings.EqualFold(platform, tags.DOS.String()) {
if msdos.Rename(displayname) != displayname {
displayname = `<span class="text-danger">` + displayname + `</span>`
}
}
htm := fmt.Sprintf(`<div class="col d-inline-block text-truncate" data-bs-toggle="tooltip" `+
`data-bs-title="%s">%s</div>`, titlename, displayname)
return m.Column1(bytes, htm, name, platform, section)
}

// osTool returns true if the file extension matches the known operating system tools.
// This includes batch scripts, executables, commands and ini configurations files.
func osTool(ext string) bool {
switch ext {
case bat, exe, com, ini:
return true
default:
return false
}
htm := fmt.Sprintf(`<div class="col d-inline-block text-truncate">%s</div>`,
displayname)
return m.Column1(htm)
}

func (m ListEntry) Column1(bytes int64, htm, name, platform, section string) string {
func (m ListEntry) Column1(htm string) string {
const blank = `<div class="col col-1"></div>`
ext := strings.ToLower(filepath.Ext(name))
ext := strings.ToLower(filepath.Ext(m.name))
switch {
case osTool(ext):
htm += blank
case m.Images:
htm += previewcopy(m.UniqueID, name)
htm += previewcopy(m.UniqueID, m.name)
case m.Texts:
htm += readmepreview(m.UniqueID, name)
htm += readmepreview(m.UniqueID, m.name)
default:
htm += blank
}
return m.readme(bytes, htm, platform, section)
return m.Column2(htm)
}

func (m ListEntry) readme(bytes int64, htm, platform, section string) string {
func (m ListEntry) Column2(htm string) string {
soloText := func() bool {
if !strings.EqualFold(platform, tags.Text.String()) && !strings.EqualFold(platform, textamiga) {
if !strings.EqualFold(m.platform, tags.Text.String()) &&
!strings.EqualFold(m.platform, textamiga) {
return false
}
return strings.EqualFold(section, tags.Nfo.String())
return strings.EqualFold(m.section, tags.Nfo.String())
}
const blank = `<div class="col col-1"></div>`
name := url.QueryEscape(m.RelativeName)
ext := strings.ToLower(filepath.Ext(name))
switch {
case
m.Programs && ext == exe,
m.Programs,
ext == exe,
ext == com:
htm += `<div class="col col-1 text-end"><svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
htm += `<div class="col col-1 text-end" ` +
`data-bs-toggle="tooltip" data-bs-title="Known program or executable">` +
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#terminal-plus"></use></svg></div>`
case osTool(ext):
htm += blank
Expand All @@ -125,18 +124,19 @@ func (m ListEntry) readme(bytes int64, htm, platform, section string) string {
default:
htm += blank
}
htm += fmt.Sprintf(`<div><small data-bs-toggle="tooltip" data-bs-title="%d bytes">%s</small>`, bytes, m.Filesize)
return m.binaries(bytes, ext, htm)
htm += fmt.Sprintf(`<div><small data-bs-toggle="tooltip" data-bs-title="%d bytes">%s</small>`,
m.bytes, m.Filesize)
return m.Column1and2(ext, htm)
}

func (m ListEntry) binaries(bytes int64, ext, htm string) string {
func (m ListEntry) Column1and2(ext, htm string) string {
switch {
case m.Texts && (ext == bat || ext == cmd):
htm += fmt.Sprintf(` <small class="">%s</small></div>`, "command script")
case m.Texts && (ext == ini):
htm += fmt.Sprintf(` <small class="">%s</small></div>`, "configuration textfile")
case m.Programs || ext == com:
htm = progr(m.Executable, ext, htm, bytes)
htm = progr(m.Executable, ext, htm, m.bytes)
case m.MusicConfig != "":
htm += fmt.Sprintf(` <small class="">%s</small></div>`, m.MusicConfig)
case m.Images:
Expand All @@ -149,26 +149,29 @@ func (m ListEntry) binaries(bytes int64, ext, htm string) string {
}

func previewcopy(uniqueID, name string) string {
return `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" id="" `+
return `<div class="col col-1 text-end" ` +
`data-bs-toggle="tooltip" data-bs-title="Use image for preview">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" name="artifact-editor-comp-previewcopy" `+
`hx-target="#artifact-editor-comp-feedback" `+
`hx-patch="/editor/preview/copy/%s/%s">`, uniqueID, name) +
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#images"></use></svg></a></div>`
}

func readmepreview(uniqueID, name string) string {
return `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" `+
return `<div class="col col-1 text-end" ` +
`data-bs-toggle="tooltip" data-bs-title="Use file for preview">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" name="artifact-editor-comp-previewtext" `+
`hx-target="#artifact-editor-comp-feedback" `+
`hx-patch="/editor/readme/preview/%s/%s">`, uniqueID, name) +
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#images"></use></svg></a></div>`
}

func readmecopy(uniqueID, name string) string {
return `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" `+
return `<div class="col col-1 text-end" ` +
`data-bs-toggle="tooltip" data-bs-title="Use file as readme">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" name="artifact-editor-comp-textcopy" `+
`hx-target="#artifact-editor-comp-feedback" `+
`hx-patch="/editor/readme/copy/%s/%s">`, uniqueID, name) +
`<svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true">` +
Expand Down Expand Up @@ -210,6 +213,17 @@ func progrDos(x8086 int, bytes int64) string {
return "Dos executable"
}

// osTool returns true if the file extension matches the known operating system tools.
// This includes batch scripts, executables, commands and ini configurations files.
func osTool(ext string) bool {
switch ext {
case bat, exe, com, ini:
return true
default:
return false
}
}

// AlertURL returns the VirusTotal URL for the security alert for the file record.
// This will normally return an empty string unless the file has a security alert.
func AlertURL(art *models.File) string {
Expand Down
14 changes: 7 additions & 7 deletions handler/htmx/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func RecordThumb(c echo.Context, thumb command.Thumb, dirs command.Dirs) error {
}
c = pageRefresh(c)
return c.String(http.StatusOK,
`<span class="text-success">Thumb created, the browser will refresh.</span>`)
`Thumb created, the browser will refresh.`)
}

func RecordThumbAlignment(c echo.Context, align command.Align, dirs command.Dirs) error {
Expand All @@ -58,7 +58,7 @@ func RecordThumbAlignment(c echo.Context, align command.Align, dirs command.Dirs
}
c = pageRefresh(c)
return c.String(http.StatusOK,
`<span class="text-success">Thumb realigned, the browser will refresh.</span>`)
`Thumb realigned, the browser will refresh.`)
}

func RecordImageCropper(c echo.Context, crop command.Crop, dirs command.Dirs) error {
Expand All @@ -69,7 +69,7 @@ func RecordImageCropper(c echo.Context, crop command.Crop, dirs command.Dirs) er
}
c = pageRefresh(c)
return c.String(http.StatusOK,
`<span class="text-success">Images cropped, the browser will refresh.</span>`)
`Images cropped, the browser will refresh.`)
}

func RecordImageCopier(c echo.Context, debug *zap.SugaredLogger, dirs command.Dirs) error {
Expand Down Expand Up @@ -99,7 +99,7 @@ func RecordImageCopier(c echo.Context, debug *zap.SugaredLogger, dirs command.Di
}
c = pageRefresh(c)
return c.String(http.StatusOK,
`<span class="text-success">Images copied, the browser will refresh.</span>`)
`Images copied, the browser will refresh.`)
}

func RecordReadmeImager(c echo.Context, logger *zap.SugaredLogger, dirs command.Dirs) error {
Expand All @@ -126,7 +126,7 @@ func RecordReadmeImager(c echo.Context, logger *zap.SugaredLogger, dirs command.
}
c = pageRefresh(c)
return c.String(http.StatusOK,
`<span class="text-success">Text filed imaged, the browser will refresh.</span>`)
`Text filed imaged, the browser will refresh.`)
}

func RecordReadmeCopier(c echo.Context, extraDir string) error {
Expand Down Expand Up @@ -154,7 +154,7 @@ func RecordReadmeCopier(c echo.Context, extraDir string) error {
}
c = pageRefresh(c)
return c.String(http.StatusOK,
`<span class="text-success">Images copied, the browser will refresh.</span>`)
`Images copied, the browser will refresh.`)
}

func RecordImagePixelator(c echo.Context, dirs ...string) error {
Expand All @@ -164,7 +164,7 @@ func RecordImagePixelator(c echo.Context, dirs ...string) error {
}
c = pageRefresh(c)
return c.String(http.StatusOK,
`<span class="text-success">Images pixelated, the browser will refresh.</span>`)
`Images pixelated, the browser will refresh.`)
}

func RecordImagesDeleter(c echo.Context, dirs ...string) error {
Expand Down
Loading

0 comments on commit 297a0da

Please sign in to comment.