Skip to content

Commit

Permalink
dispatch event siblings (#25)
Browse files Browse the repository at this point in the history
* dispatch events to siblings

* add comments

* update alpine js plugin
  • Loading branch information
adnaan authored Dec 25, 2022
1 parent bd6e359 commit 0db3d7a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
4 changes: 2 additions & 2 deletions alpinejs-plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion alpinejs-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@livefir/fir",
"version": "0.0.23",
"version": "0.0.24",
"repository": {
"type": "git",
"url": "https://github.com/livefir/fir"
Expand Down
17 changes: 15 additions & 2 deletions alpinejs-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,21 @@ const Plugin = (Alpine) => {
document.dispatchEvent(event)
return
}
const eventSourceElement = document.getElementById(operation.eid)
eventSourceElement.dispatchEvent(event)

const elem = document.getElementById(operation.eid)
const getSiblings = (elm) =>
elm &&
elm.parentNode &&
[...elm.parentNode.children].filter(
(node) =>
node != elm &&
(node.hasAttribute(`@${operation.selector}`) ||
node.hasAttribute(`x-on:${operation.selector}`))
)

const sibs = getSiblings(elem)
sibs.forEach((sib) => sib.dispatchEvent(event))
elem.dispatchEvent(event)
},
}

Expand Down
26 changes: 17 additions & 9 deletions examples/twitter/routes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
style="margin-top:1rem;margin-bottom:1rem;">
<input type="text" name="body" placeholder="a new tweet" />
<!-- validation error for the name=body set by ctx.FieldError(...) -->

<!-- $fir.replace replaces the content of the element on which it is invoked -->
<!-- $fir.replaceEl replaces the element on which it is invoked -->
<p
id="body-err"
@fir:create-tweet:body:error.window="$fir.replace()">
Expand All @@ -34,28 +35,35 @@
<div id="tweets" @fir:create-tweet.window="$fir.appendEl()">
{{ range .tweets }}
<!-- block "tweet*" matches the element id of event listeners:
@fir:like-tweet(tweet-{{ .ID }}),
@fir:delete-tweet(tweet-{{ .ID }})
@fir:create-tweet(tweets)
The block is re-rendered on the server aftter the event is emitted by $fir.xxxEl()
The output html is patched(replaceEl,removeEl,appendEl) to the element on the client.
Similary, block "like-tweet*" matches the element id of event listeners:
@fir:like-tweet(tweet-{{ .ID }})
The block is re-rendered on the server aftter the event is emitted by $fir.submit()
The output html is patched(removeEl,appendEl,replace,) to the element on the client.
-->
{{ block "tweet*" . }}
<div
id="tweet-{{ .ID }}"
@fir:like-tweet="$fir.replaceEl()"
@fir:delete-tweet="$fir.removeEl()"
style="display: flex;margin-top:1rem;margin-bottom:1rem;">
<div style="margin-right:1rem">
Tweet:
{{ .Body }}
</div>
<div style="margin-right:1rem">
Likes:
{{ .LikesCount }}
<div
id="like-tweet-{{ .ID }}"
@fir:like-tweet="$fir.replace()"
style="margin-right:1rem">
{{ block "like-tweet*" . }}
<div>
Likes:
{{ .LikesCount }}
</div>
{{ end }}
</div>
<form
id="tweet-{{ .ID }}"
id="update-tweet-{{ .ID }}"
method="post"
@submit.prevent="$fir.submit()"
style="margin-right:1rem">
Expand Down
5 changes: 3 additions & 2 deletions examples/twitter/routes/partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css" />
{{/* <script defer src="http://localhost:8000/cdn.js"></script> */}}
<script
<script defer src="http://localhost:8000/cdn.js"></script>
{{/* <script
defer
src="https://unpkg.com/@livefir/fir@latest/dist/fir.min.js"></script>
*/}}

<script
defer
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
entgo.io/ent v0.11.4
github.com/Masterminds/sprig/v3 v3.2.3
github.com/PuerkitoBio/goquery v1.8.0
github.com/alecthomas/chroma v0.10.0
github.com/alexedwards/scs/v2 v2.5.0
github.com/davecgh/go-spew v1.1.1
Expand All @@ -31,7 +32,6 @@ require (
ariga.io/atlas v0.7.3-0.20221011160332-3ca609863edd // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func parseTemplate(opt routeOpt) (*template.Template, error) {
return layoutSetContentSet(opt)
}

func parseEventRenderMapping(allTemplates []string, eventTemplateMap map[string]string, r io.Reader) {
func parseEventRenderMapping(rt *route, r io.Reader) {
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
panic(err)
Expand All @@ -140,9 +140,9 @@ func parseEventRenderMapping(allTemplates []string, eventTemplateMap map[string]
if strings.HasPrefix(a.Key, "@fir:") || strings.HasPrefix(a.Key, "x-on:fir:") {
templateName, exists := node.Attr("id")
if exists {
if !slices.Contains(allTemplates, templateName) {
if !slices.Contains(rt.allTemplates, templateName) {
// try to match the id with template as a pattern
for _, pattern := range allTemplates {
for _, pattern := range rt.allTemplates {
if !match.IsPattern(pattern) {
continue
}
Expand All @@ -162,7 +162,9 @@ func parseEventRenderMapping(allTemplates []string, eventTemplateMap map[string]
if len(parts) > 0 {
eventID = parts[0]
}
eventTemplateMap[eventID] = templateName
rt.Lock()
rt.eventTemplateMap[eventID] = templateName
rt.Unlock()
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/golang/glog"
"github.com/google/uuid"
Expand Down Expand Up @@ -152,6 +153,7 @@ type route struct {
allTemplates []string
eventTemplateMap map[string]string
routeOpt
sync.RWMutex
}

func newRoute(cntrl *controller, routeOpt *routeOpt) *route {
Expand Down Expand Up @@ -605,7 +607,7 @@ func (rt *route) buildEventRenderMapping() {
pagePath := filepath.Join(opt.publicDir, page)
// is layout html content or a file/directory
if isFileOrString(pagePath, opt) {
parseEventRenderMapping(rt.allTemplates, rt.eventTemplateMap, strings.NewReader(page))
parseEventRenderMapping(rt, strings.NewReader(page))
} else {
// compile layout
commonFiles := []string{pagePath}
Expand All @@ -619,15 +621,15 @@ func (rt *route) buildEventRenderMapping() {
if err != nil {
panic(err)
}
parseEventRenderMapping(rt.allTemplates, rt.eventTemplateMap, r)
parseEventRenderMapping(rt, r)
}
} else {
for _, v := range commonFiles {
r, err := os.OpenFile(v, os.O_RDONLY, 0644)
if err != nil {
panic(err)
}
parseEventRenderMapping(rt.allTemplates, rt.eventTemplateMap, r)
parseEventRenderMapping(rt, r)
}

}
Expand Down

0 comments on commit 0db3d7a

Please sign in to comment.