-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from donseba/connector
refactor to work with connectors as mentioned in #8
- Loading branch information
Showing
26 changed files
with
1,632 additions
and
421 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package connector | ||
|
||
import "net/http" | ||
|
||
type AlpineAjax struct { | ||
base | ||
} | ||
|
||
func NewAlpineAjax(c *Config) Connector { | ||
return &AlpineAjax{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "X-Alpine-Target", | ||
selectHeader: "X-Alpine-Select", | ||
actionHeader: "X-Alpine-Action", | ||
}, | ||
} | ||
} | ||
|
||
func (a *AlpineAjax) RenderPartial(r *http.Request) bool { | ||
return r.Header.Get(a.targetHeader) != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package connector | ||
|
||
import "net/http" | ||
|
||
type Alpine struct { | ||
base | ||
} | ||
|
||
func NewAlpine(c *Config) Connector { | ||
return &Alpine{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "X-Alpine-Target", | ||
selectHeader: "X-Alpine-Select", | ||
actionHeader: "X-Alpine-Action", | ||
}, | ||
} | ||
} | ||
|
||
func (a *Alpine) RenderPartial(r *http.Request) bool { | ||
return r.Header.Get(a.targetHeader) != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package connector | ||
|
||
import "net/http" | ||
|
||
type ( | ||
Connector interface { | ||
RenderPartial(r *http.Request) bool | ||
GetTargetValue(r *http.Request) string | ||
GetSelectValue(r *http.Request) string | ||
GetActionValue(r *http.Request) string | ||
|
||
GetTargetHeader() string | ||
GetSelectHeader() string | ||
GetActionHeader() string | ||
} | ||
|
||
Config struct { | ||
UseURLQuery bool | ||
} | ||
|
||
base struct { | ||
config *Config | ||
targetHeader string | ||
selectHeader string | ||
actionHeader string | ||
} | ||
) | ||
|
||
func (x *base) RenderPartial(r *http.Request) bool { | ||
return r.Header.Get(x.targetHeader) != "" | ||
} | ||
|
||
func (x *base) GetTargetHeader() string { | ||
return x.targetHeader | ||
} | ||
|
||
func (x *base) GetSelectHeader() string { | ||
return x.selectHeader | ||
} | ||
|
||
func (x *base) GetActionHeader() string { | ||
return x.actionHeader | ||
} | ||
|
||
func (x *base) GetTargetValue(r *http.Request) string { | ||
if targetValue := r.Header.Get(x.targetHeader); targetValue != "" { | ||
return targetValue | ||
} | ||
|
||
if x.config.useURLQuery() { | ||
return r.URL.Query().Get("target") | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (x *base) GetSelectValue(r *http.Request) string { | ||
if selectValue := r.Header.Get(x.selectHeader); selectValue != "" { | ||
return selectValue | ||
} | ||
|
||
if x.config.useURLQuery() { | ||
return r.URL.Query().Get("select") | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (x *base) GetActionValue(r *http.Request) string { | ||
if actionValue := r.Header.Get(x.actionHeader); actionValue != "" { | ||
return actionValue | ||
} | ||
|
||
if x.config.useURLQuery() { | ||
return r.URL.Query().Get("action") | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (c *Config) useURLQuery() bool { | ||
if c == nil { | ||
return false | ||
} | ||
|
||
return c.UseURLQuery | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package connector | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type HTMX struct { | ||
base | ||
|
||
requestHeader string | ||
boostedHeader string | ||
historyRestoreRequestHeader string | ||
} | ||
|
||
func NewHTMX(c *Config) Connector { | ||
return &HTMX{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "HX-Target", | ||
selectHeader: "X-Select", | ||
actionHeader: "X-Action", | ||
}, | ||
requestHeader: "HX-Request", | ||
boostedHeader: "HX-Boosted", | ||
historyRestoreRequestHeader: "HX-History-Restore-Request", | ||
} | ||
} | ||
|
||
func (h *HTMX) RenderPartial(r *http.Request) bool { | ||
hxRequest := r.Header.Get(h.requestHeader) | ||
hxBoosted := r.Header.Get(h.boostedHeader) | ||
hxHistoryRestoreRequest := r.Header.Get(h.historyRestoreRequestHeader) | ||
|
||
return (hxRequest == "true" || hxBoosted == "true") && hxHistoryRestoreRequest != "true" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package connector | ||
|
||
type Partial struct { | ||
base | ||
} | ||
|
||
func NewPartial(c *Config) Connector { | ||
return &Partial{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "X-Target", | ||
selectHeader: "X-Select", | ||
actionHeader: "X-Action", | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package connector | ||
|
||
import "net/http" | ||
|
||
type Stimulus struct { | ||
base | ||
} | ||
|
||
func NewStimulus(c *Config) Connector { | ||
return &Stimulus{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "X-Stimulus-Target", | ||
selectHeader: "X-Stimulus-Select", | ||
actionHeader: "X-Stimulus-Action", | ||
}, | ||
} | ||
} | ||
|
||
func (s *Stimulus) RenderPartial(r *http.Request) bool { | ||
return r.Header.Get(s.targetHeader) != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package connector | ||
|
||
type Turbo struct { | ||
base | ||
} | ||
|
||
func NewTurbo(c *Config) Connector { | ||
return &Turbo{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "Turbo-Frame", | ||
selectHeader: "Turbo-Select", | ||
actionHeader: "Turbo-Action", | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package connector | ||
|
||
import "net/http" | ||
|
||
type Unpoly struct { | ||
base | ||
} | ||
|
||
func NewUnpoly(c *Config) Connector { | ||
return &Unpoly{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "X-Up-Target", | ||
selectHeader: "X-Up-Select", | ||
actionHeader: "X-Up-Action", | ||
}, | ||
} | ||
} | ||
|
||
func (u *Unpoly) RenderPartial(r *http.Request) bool { | ||
return r.Header.Get(u.targetHeader) != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package connector | ||
|
||
import "net/http" | ||
|
||
type Vue struct { | ||
base | ||
} | ||
|
||
func NewVue(c *Config) Connector { | ||
return &Vue{ | ||
base: base{ | ||
config: c, | ||
targetHeader: "X-Vue-Target", | ||
selectHeader: "X-Vue-Select", | ||
actionHeader: "X-Vue-Action", | ||
}, | ||
} | ||
} | ||
|
||
func (v *Vue) RenderPartial(r *http.Request) bool { | ||
return r.Header.Get(v.targetHeader) != "" | ||
} |
Oops, something went wrong.