[add] block, component api
This commit is contained in:
parent
0b1de85033
commit
f0ccba6db4
6 changed files with 212 additions and 5 deletions
|
|
@ -23,7 +23,7 @@ var dsl = []byte(`
|
|||
"in": ["$param.id", "$param.template_id"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
"path": "/:id/template/:template_id/locale",
|
||||
"method": "GET",
|
||||
|
|
@ -37,6 +37,35 @@ var dsl = []byte(`
|
|||
"in": ["$param.id", "$param.template_id"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
},
|
||||
|
||||
{
|
||||
"path": "/:id/template/:template_id/block",
|
||||
"method": "GET",
|
||||
"process": "sui.Template.Block.Get",
|
||||
"in": ["$param.id", "$param.template_id"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
},{
|
||||
"path": "/:id/template/:template_id/block/:block_id",
|
||||
"method": "GET",
|
||||
"process": "sui.Template.Block.Find",
|
||||
"in": ["$param.id", "$param.template_id", "$param.block_id"],
|
||||
"out": { "status": 200, "type": "text/javascript" }
|
||||
},
|
||||
|
||||
{
|
||||
"path": "/:id/template/:template_id/component",
|
||||
"method": "GET",
|
||||
"process": "sui.Template.Component.Get",
|
||||
"in": ["$param.id", "$param.template_id"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
},{
|
||||
"path": "/:id/template/:template_id/component/:component_id",
|
||||
"method": "GET",
|
||||
"process": "sui.Template.Component.Find",
|
||||
"in": ["$param.id", "$param.template_id", "$param.component_id"],
|
||||
"out": { "status": 200, "type": "text/javascript" }
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
"path": "/:id/editor/render/:template_id/*route",
|
||||
|
|
|
|||
|
|
@ -8,10 +8,14 @@ import (
|
|||
|
||||
func init() {
|
||||
process.RegisterGroup("sui", map[string]process.Handler{
|
||||
"template.get": TemplateGet,
|
||||
"template.find": TemplateFind,
|
||||
"template.locale.get": TemplateLocaleGet,
|
||||
"template.theme.get": TemplateThemeGet,
|
||||
"template.get": TemplateGet,
|
||||
"template.find": TemplateFind,
|
||||
"template.locale.get": TemplateLocaleGet,
|
||||
"template.theme.get": TemplateThemeGet,
|
||||
"template.block.get": TemplateBlockGet,
|
||||
"template.block.find": TemplateBlockFind,
|
||||
"template.component.get": TemplateComponentGet,
|
||||
"template.component.find": TemplateComponentFind,
|
||||
|
||||
"editor.render": EditorRender,
|
||||
"editor.source": EditorSource,
|
||||
|
|
@ -70,6 +74,85 @@ func TemplateThemeGet(process *process.Process) interface{} {
|
|||
return template.Themes()
|
||||
}
|
||||
|
||||
// TemplateBlockGet handle the find Template request
|
||||
func TemplateBlockGet(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
|
||||
tmpl, err := sui.GetTemplate(templateID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
blocks, err := tmpl.Blocks()
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
return blocks
|
||||
}
|
||||
|
||||
// TemplateBlockFind handle the find Template request
|
||||
func TemplateBlockFind(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(3)
|
||||
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
blockID := process.ArgsString(2)
|
||||
|
||||
tmpl, err := sui.GetTemplate(templateID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
block, err := tmpl.Block(blockID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
return block.Source()
|
||||
}
|
||||
|
||||
// TemplateComponentGet handle the find Template request
|
||||
func TemplateComponentGet(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
|
||||
tmpl, err := sui.GetTemplate(templateID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
components, err := tmpl.Components()
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
return components
|
||||
}
|
||||
|
||||
// TemplateComponentFind handle the find Template request
|
||||
func TemplateComponentFind(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(3)
|
||||
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
componentID := process.ArgsString(2)
|
||||
|
||||
tmpl, err := sui.GetTemplate(templateID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
component, err := tmpl.Component(componentID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
return component.Source()
|
||||
}
|
||||
|
||||
// EditorRender handle the render page request
|
||||
func EditorRender(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(3)
|
||||
|
|
|
|||
|
|
@ -91,6 +91,89 @@ func TestTemplateThemeGet(t *testing.T) {
|
|||
assert.Equal(t, "light", res.([]core.SelectOption)[1].Value)
|
||||
}
|
||||
|
||||
func TestTemplateBlockGet(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.template.block.get", "demo", "tech-blue")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.IsType(t, []core.IBlock{}, res)
|
||||
assert.Equal(t, 4, len(res.([]core.IBlock)))
|
||||
assert.Equal(t, "ColumnsTwo", res.([]core.IBlock)[0].(*local.Block).ID)
|
||||
assert.Equal(t, "Hero", res.([]core.IBlock)[1].(*local.Block).ID)
|
||||
assert.Equal(t, "Section", res.([]core.IBlock)[2].(*local.Block).ID)
|
||||
assert.Equal(t, "Table", res.([]core.IBlock)[3].(*local.Block).ID)
|
||||
}
|
||||
|
||||
func TestTemplateBlockFind(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.template.block.find", "demo", "tech-blue", "ColumnsTwo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.IsType(t, "", res)
|
||||
assert.Contains(t, res.(string), "window.block__ColumnsTwo=")
|
||||
}
|
||||
|
||||
func TestTemplateComponentGet(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.template.component.get", "demo", "tech-blue")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.IsType(t, []core.IComponent{}, res)
|
||||
assert.Equal(t, 3, len(res.([]core.IComponent)))
|
||||
assert.Equal(t, "Box", res.([]core.IComponent)[0].(*local.Component).ID)
|
||||
assert.Equal(t, "Card", res.([]core.IComponent)[1].(*local.Component).ID)
|
||||
assert.Equal(t, "Nav", res.([]core.IComponent)[2].(*local.Component).ID)
|
||||
}
|
||||
|
||||
func TestTemplateComponentFind(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.template.component.find", "demo", "tech-blue", "Box")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.IsType(t, "", res)
|
||||
assert.Contains(t, res.(string), "window.component__Box=")
|
||||
}
|
||||
|
||||
func TestEditorRender(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
|
|
|||
|
|
@ -55,3 +55,8 @@ func (block *Block) Compile() (string, error) {
|
|||
block.Compiled = minified
|
||||
return minified, nil
|
||||
}
|
||||
|
||||
// Source get the compiled code
|
||||
func (block *Block) Source() string {
|
||||
return block.Compiled
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,3 +56,8 @@ func (component *Component) Compile() (string, error) {
|
|||
component.Compiled = minified
|
||||
return minified, nil
|
||||
}
|
||||
|
||||
// Source get the compiled code
|
||||
func (component *Component) Source() string {
|
||||
return component.Compiled
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,10 +50,12 @@ type IPage interface {
|
|||
type IBlock interface {
|
||||
Compile() (string, error)
|
||||
Load() error
|
||||
Source() string
|
||||
}
|
||||
|
||||
// IComponent is the interface for the component
|
||||
type IComponent interface {
|
||||
Compile() (string, error)
|
||||
Load() error
|
||||
Source() string
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue