/
step_AsModal.go
124 lines (91 loc) · 3.75 KB
/
step_AsModal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package builder
import (
"bytes"
"io"
"strings"
"github.com/EmissarySocial/emissary/model/step"
"github.com/benpate/derp"
)
// StepAsModal represents an action-step that can update the data.DataMap custom data stored in a Stream
type StepAsModal struct {
SubSteps []step.Step
Options []string
Background string
}
// Get displays a form where users can update stream data
func (step StepAsModal) Get(builder Builder, buffer io.Writer) PipelineBehavior {
const location = "build.StepAsModal.Get"
// Partial pages only build the modal window. This happens MOST of the time.
if builder.IsPartialRequest() {
modalContent, status := step.getModalContent(builder)
if status.Halt {
return UseResult(status)
}
if _, err := io.WriteString(buffer, modalContent); err != nil {
return Halt().WithError(derp.Wrap(err, location, "Error writing from builder to buffer"))
}
result := UseResult(status).
WithHeader("HX-Retarget", "aside").
WithHeader("HX-Reswap", "innerHTML").
AsFullPage()
if step.Background == "" {
result = result.WithHeader("HX-Push-Url", "false")
} else {
result = result.WithHeader("HX-Push-Url", "true")
}
return result
}
// Otherwise, we can build the modal on a page background... IF we have a background view defined.
if step.Background == "" {
return Halt().WithError(derp.NewBadRequestError(location, "Cannot open this route directly."))
}
// Full pages build the entire page, including the modal window
fullPageBuilder, err := builder.clone(step.Background)
if err != nil {
return Halt().WithError(derp.Wrap(err, location, "Error creating fullPageBuilder"))
}
// Execute the action pipeline
var partialPage bytes.Buffer
factory := fullPageBuilder.factory()
pipeline := Pipeline(fullPageBuilder.Action().Steps)
status := pipeline.Execute(factory, fullPageBuilder, &partialPage, ActionMethodGet)
if status.Error != nil {
return Halt().WithError(derp.Wrap(status.Error, location, "Error executing action pipeline on fullPageBuilder"))
}
// Copy status values into the Response...
status.Apply(fullPageBuilder.response())
// Full Page requests require the theme service to wrap the builded content
var fullPage bytes.Buffer
htmlTemplate := factory.Domain().Theme().HTMLTemplate
fullPageBuilder.SetContent(partialPage.String())
if err := htmlTemplate.ExecuteTemplate(&fullPage, "page", fullPageBuilder); err != nil {
return Halt().WithError(derp.Wrap(err, location, "Error executing template"))
}
// Insert the modal into the page
asideBegin := "<aside>"
asideEnd := "</aside>"
modalString, result := step.getModalContent(builder)
fullPageString := strings.Replace(fullPage.String(), asideBegin+asideEnd, asideBegin+modalString+asideEnd, 1)
if _, err := io.WriteString(buffer, fullPageString); err != nil {
return Halt().WithError(derp.Wrap(err, location, "Error writing from builder to buffer"))
}
return UseResult(result).AsFullPage()
}
// Post updates the stream with approved data from the request body.
func (step StepAsModal) Post(builder Builder, buffer io.Writer) PipelineBehavior {
// Write inner items
result := Pipeline(step.SubSteps).Post(builder.factory(), builder, buffer)
result.Error = derp.Wrap(result.Error, "build.StepAsModal.Post", "Error executing subSteps")
return UseResult(result).WithEvent("closeModal", "true")
}
func (step StepAsModal) getModalContent(builder Builder) (string, PipelineResult) {
const location = "build.StepAsModal.getModalContent"
// Write inner items
var buffer bytes.Buffer
result := Pipeline(step.SubSteps).Get(builder.factory(), builder, &buffer)
result.Error = derp.Wrap(result.Error, location, "Error executing subSteps")
if result.Halt {
return "", result
}
return WrapModal(builder.response(), buffer.String(), step.Options...), result
}