Merge pull request #469 from trheyi/main
[add] template locale, theme, block, component api
This commit is contained in:
commit
04e4f316a5
6 changed files with 300 additions and 8 deletions
|
|
@ -11,19 +11,62 @@ var dsl = []byte(`
|
||||||
"group": "__yao/sui/v1",
|
"group": "__yao/sui/v1",
|
||||||
"paths": [
|
"paths": [
|
||||||
{
|
{
|
||||||
"path": "/:id/template/:template_id",
|
|
||||||
"method": "GET",
|
|
||||||
"process": "sui.Template.Find",
|
|
||||||
"in": ["$param.id", "$param.template_id"],
|
|
||||||
"out": { "status": 200, "type": "application/json" }
|
|
||||||
},{
|
|
||||||
"path": "/:id/template",
|
"path": "/:id/template",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"process": "sui.Template.Get",
|
"process": "sui.Template.Get",
|
||||||
"in": ["$param.id"],
|
"in": ["$param.id"],
|
||||||
"out": { "status": 200, "type": "application/json" }
|
"out": { "status": 200, "type": "application/json" }
|
||||||
|
},{
|
||||||
|
"path": "/:id/template/:template_id",
|
||||||
|
"method": "GET",
|
||||||
|
"process": "sui.Template.Find",
|
||||||
|
"in": ["$param.id", "$param.template_id"],
|
||||||
|
"out": { "status": 200, "type": "application/json" }
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"path": "/:id/template/:template_id/locale",
|
||||||
|
"method": "GET",
|
||||||
|
"process": "sui.Template.Locale.Get",
|
||||||
|
"in": ["$param.id", "$param.template_id"],
|
||||||
|
"out": { "status": 200, "type": "application/json" }
|
||||||
|
},{
|
||||||
|
"path": "/:id/template/:template_id/theme",
|
||||||
|
"method": "GET",
|
||||||
|
"process": "sui.Template.Theme.Get",
|
||||||
|
"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",
|
"path": "/:id/editor/render/:template_id/*route",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,14 @@ import (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
process.RegisterGroup("sui", map[string]process.Handler{
|
process.RegisterGroup("sui", map[string]process.Handler{
|
||||||
"template.get": TemplateGet,
|
"template.get": TemplateGet,
|
||||||
"template.find": TemplateFind,
|
"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.render": EditorRender,
|
||||||
"editor.source": EditorSource,
|
"editor.source": EditorSource,
|
||||||
|
|
@ -42,6 +48,111 @@ func TemplateFind(process *process.Process) interface{} {
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TemplateLocaleGet handle the find Template request
|
||||||
|
func TemplateLocaleGet(process *process.Process) interface{} {
|
||||||
|
process.ValidateArgNums(2)
|
||||||
|
|
||||||
|
sui := get(process)
|
||||||
|
template, err := sui.GetTemplate(process.ArgsString(1))
|
||||||
|
if err != nil {
|
||||||
|
exception.New(err.Error(), 500).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
return template.Locales()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TemplateThemeGet handle the find Template request
|
||||||
|
func TemplateThemeGet(process *process.Process) interface{} {
|
||||||
|
process.ValidateArgNums(2)
|
||||||
|
|
||||||
|
sui := get(process)
|
||||||
|
template, err := sui.GetTemplate(process.ArgsString(1))
|
||||||
|
if err != nil {
|
||||||
|
exception.New(err.Error(), 500).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
// EditorRender handle the render page request
|
||||||
func EditorRender(process *process.Process) interface{} {
|
func EditorRender(process *process.Process) interface{} {
|
||||||
process.ValidateArgNums(3)
|
process.ValidateArgNums(3)
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,132 @@ func TestTemplateFind(t *testing.T) {
|
||||||
assert.Equal(t, "tech-blue", res.(*local.Template).ID)
|
assert.Equal(t, "tech-blue", res.(*local.Template).ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTemplateLocaleGet(t *testing.T) {
|
||||||
|
load(t)
|
||||||
|
defer clean()
|
||||||
|
|
||||||
|
// test demo
|
||||||
|
p, err := process.Of("sui.template.locale.get", "demo", "tech-blue")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := p.Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.IsType(t, []core.SelectOption{}, res)
|
||||||
|
assert.Equal(t, 3, len(res.([]core.SelectOption)))
|
||||||
|
assert.Equal(t, "ar", res.([]core.SelectOption)[0].Value)
|
||||||
|
assert.Equal(t, "zh-cn", res.([]core.SelectOption)[1].Value)
|
||||||
|
assert.Equal(t, "zh-tw", res.([]core.SelectOption)[2].Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTemplateThemeGet(t *testing.T) {
|
||||||
|
load(t)
|
||||||
|
defer clean()
|
||||||
|
|
||||||
|
// test demo
|
||||||
|
p, err := process.Of("sui.template.theme.get", "demo", "tech-blue")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := p.Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.IsType(t, []core.SelectOption{}, res)
|
||||||
|
assert.Equal(t, 2, len(res.([]core.SelectOption)))
|
||||||
|
assert.Equal(t, "dark", res.([]core.SelectOption)[0].Value)
|
||||||
|
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) {
|
func TestEditorRender(t *testing.T) {
|
||||||
load(t)
|
load(t)
|
||||||
defer clean()
|
defer clean()
|
||||||
|
|
|
||||||
|
|
@ -55,3 +55,8 @@ func (block *Block) Compile() (string, error) {
|
||||||
block.Compiled = minified
|
block.Compiled = minified
|
||||||
return minified, nil
|
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
|
component.Compiled = minified
|
||||||
return minified, nil
|
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 {
|
type IBlock interface {
|
||||||
Compile() (string, error)
|
Compile() (string, error)
|
||||||
Load() error
|
Load() error
|
||||||
|
Source() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// IComponent is the interface for the component
|
// IComponent is the interface for the component
|
||||||
type IComponent interface {
|
type IComponent interface {
|
||||||
Compile() (string, error)
|
Compile() (string, error)
|
||||||
Load() error
|
Load() error
|
||||||
|
Source() string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue