refactor: Update namespace handling in SUI page builder
This commit is contained in:
parent
7ea9953b69
commit
78d8d9fbc2
3 changed files with 60 additions and 16 deletions
|
|
@ -39,6 +39,10 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.sequence++
|
ctx.sequence++
|
||||||
|
|
||||||
|
namespace := Namespace(page.Name, ctx.sequence, option.ScriptMinify)
|
||||||
|
page.namespace = namespace
|
||||||
|
|
||||||
html, err := page.BuildHTML(option)
|
html, err := page.BuildHTML(option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.warnings = append(ctx.warnings, err.Error())
|
ctx.warnings = append(ctx.warnings, err.Error())
|
||||||
|
|
@ -48,6 +52,7 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ctx.warnings, err
|
return nil, ctx.warnings, err
|
||||||
}
|
}
|
||||||
|
doc.Find("body").SetAttr("s:ns", namespace)
|
||||||
|
|
||||||
err = page.buildComponents(doc, ctx, option)
|
err = page.buildComponents(doc, ctx, option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -55,7 +60,6 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scripts
|
// Scripts
|
||||||
namespace := Namespace(page.Name, ctx.sequence, option.ScriptMinify)
|
|
||||||
scripts, err := page.BuildScripts(ctx, option, "__page", namespace)
|
scripts, err := page.BuildScripts(ctx, option, "__page", namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ctx.warnings, err
|
return nil, ctx.warnings, err
|
||||||
|
|
@ -101,10 +105,12 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
||||||
|
|
||||||
namespace := Namespace(name, ctx.sequence, option.ScriptMinify)
|
namespace := Namespace(name, ctx.sequence, option.ScriptMinify)
|
||||||
component := ComponentName(name, option.ScriptMinify)
|
component := ComponentName(name, option.ScriptMinify)
|
||||||
|
page.namespace = namespace
|
||||||
attrs := []html.Attribute{
|
attrs := []html.Attribute{
|
||||||
{Key: "s:ns", Val: namespace},
|
{Key: "s:ns", Val: namespace},
|
||||||
{Key: "s:cn", Val: component},
|
{Key: "s:cn", Val: component},
|
||||||
{Key: "s:ready", Val: component + "()"},
|
{Key: "s:ready", Val: component + "()"},
|
||||||
|
{Key: "s:parent", Val: page.parent.namespace},
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.sequence++
|
ctx.sequence++
|
||||||
|
|
@ -264,6 +270,7 @@ func (page *Page) buildComponents(doc *goquery.Document, ctx *BuildContext, opti
|
||||||
// Check if Just-In-Time Component ( "is" has variable )
|
// Check if Just-In-Time Component ( "is" has variable )
|
||||||
if ctx.isJitComponent(name) {
|
if ctx.isJitComponent(name) {
|
||||||
sel.SetAttr("s:jit", "true")
|
sel.SetAttr("s:jit", "true")
|
||||||
|
sel.SetAttr("s:parent", page.namespace)
|
||||||
sel.SetAttr("s:root", public.Root)
|
sel.SetAttr("s:root", public.Root)
|
||||||
ctx.addJitComponent(name)
|
ctx.addJitComponent(name)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -143,18 +143,47 @@ func (parser *TemplateParser) isComponent(sel *goquery.Selection) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) componentProps(sel *goquery.Selection) (map[string]interface{}, error) {
|
func (parser *TemplateParser) componentProps(sel *goquery.Selection) (map[string]interface{}, error) {
|
||||||
|
|
||||||
|
parentProps := map[string]string{}
|
||||||
props := map[string]string{}
|
props := map[string]string{}
|
||||||
parentSel := sel.Parent()
|
parent := sel.AttrOr("s:parent", "")
|
||||||
if parentSel == nil {
|
parentSel := sel.Parents().Find(fmt.Sprintf(`[s\:ns="%s"]`, parent))
|
||||||
return map[string]interface{}{}, nil
|
|
||||||
|
if parentSel != nil && parentSel.Length() > 0 {
|
||||||
|
for _, attr := range parentSel.Nodes[0].Attr {
|
||||||
|
|
||||||
|
if !strings.HasPrefix(attr.Key, "s:prop") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
key := strings.TrimPrefix(attr.Key, "s:prop:")
|
||||||
|
parentProps[key] = attr.Val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, attr := range parentSel.Nodes[0].Attr {
|
for _, attr := range sel.Nodes[0].Attr {
|
||||||
if !strings.HasPrefix(attr.Key, "s:prop") {
|
if strings.HasPrefix(attr.Key, "s:") || attr.Key == "is" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
key := strings.TrimPrefix(attr.Key, "s:prop:")
|
|
||||||
props[key] = attr.Val
|
if strings.HasPrefix(attr.Key, "...$props") {
|
||||||
|
data := Data{"$props": parentProps}
|
||||||
|
values, err := data.Exec(fmt.Sprintf("{{ %s }}", strings.TrimPrefix(attr.Key, "...")))
|
||||||
|
if err != nil {
|
||||||
|
return map[string]interface{}{}, err
|
||||||
|
}
|
||||||
|
if values == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := values.(map[string]string); ok {
|
||||||
|
for key, val := range values.(map[string]string) {
|
||||||
|
props[key] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
props[attr.Key] = attr.Val
|
||||||
}
|
}
|
||||||
|
|
||||||
return parser.parseComponentProps(props)
|
return parser.parseComponentProps(props)
|
||||||
|
|
@ -164,7 +193,7 @@ func (parser *TemplateParser) parseComponentProps(props map[string]string) (map[
|
||||||
result := map[string]interface{}{}
|
result := map[string]interface{}{}
|
||||||
for key, val := range props {
|
for key, val := range props {
|
||||||
if strings.HasPrefix(key, "...") {
|
if strings.HasPrefix(key, "...") {
|
||||||
values, err := parser.data.Exec(fmt.Sprintf("{{ %s }}", val))
|
values, err := parser.data.Exec(fmt.Sprintf("{{ %s }}", strings.TrimPrefix(key, "...")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -173,17 +202,24 @@ func (parser *TemplateParser) parseComponentProps(props map[string]string) (map[
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range values.(map[string]interface{}) {
|
if _, ok := values.(map[string]interface{}); ok {
|
||||||
result[k] = v
|
for k, v := range values.(map[string]interface{}) {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
value, err := parser.data.Exec(val)
|
|
||||||
if err != nil {
|
if strings.HasPrefix(val, "{{") && strings.HasSuffix(val, "}}") {
|
||||||
return nil, err
|
value, err := parser.data.Exec(val)
|
||||||
|
if err != nil {
|
||||||
|
return map[string]interface{}{}, err
|
||||||
|
}
|
||||||
|
result[key] = value
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
result[key] = value
|
|
||||||
|
result[key] = val
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ type Page struct {
|
||||||
Attrs map[string]string `json:"-"`
|
Attrs map[string]string `json:"-"`
|
||||||
Attributes []html.Attribute `json:"-"`
|
Attributes []html.Attribute `json:"-"`
|
||||||
Translations []Translation `json:"-"` // will be deprecated
|
Translations []Translation `json:"-"` // will be deprecated
|
||||||
|
namespace string `json:"-"`
|
||||||
parent *Page `json:"-"`
|
parent *Page `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue