Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix duplicate;fix some layout #253

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
header := pb.RegisterContainer("MyContent").Group("Navigation").
RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
c := obj.(*MyContent)
return Div().Text(c.Text)
return Div().Text(c.Text).Style("height:200px")
})

ed := header.Model(&MyContent{}).Editing("Text", "Color")
Expand All @@ -146,7 +146,7 @@
pb.RegisterModelContainer("CampaignContent", campaignModelBuilder).Group("Campaign").
RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
c := obj.(*CampaignContent)
return Div(Text(c.Title))
return Div(Text(c.Title)).Style("height:200px")

Check warning on line 149 in docs/docsrc/examples/examples_admin/page_builder_with_campaign.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_admin/page_builder_with_campaign.go#L149

Added line #L149 was not covered by tests
}).Model(&CampaignContent{}).Editing("Title", "Banner")

campaignModelBuilder.Use(pb)
Expand All @@ -165,7 +165,7 @@
pb.RegisterModelContainer("ProductContent", productModelBuilder).Group("Product").
RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
c := obj.(*ProductContent)
return Div(Text(c.Name))
return Div(Text(c.Name)).Style("height:200px")

Check warning on line 168 in docs/docsrc/examples/examples_admin/page_builder_with_campaign.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_admin/page_builder_with_campaign.go#L168

Added line #L168 was not covered by tests
}).Model(&ProductContent{}).Editing("Name")

productModelBuilder.Use(pb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ INSERT INTO public.campaigns (id, created_at, updated_at, deleted_at, title, sta
INSERT INTO public.products (id, created_at, updated_at, deleted_at, name, status, online_url, scheduled_start_at, scheduled_end_at, actual_start_at, actual_end_at, version, version_name, parent_version) VALUES (1, '2024-05-19 22:11:53.645941 +00:00', '2024-05-19 22:11:53.645941 +00:00', null, 'Hello Product', 'draft', '', null, null, null, null, '2024-05-20-v01', '2024-05-20-v01','');
INSERT INTO public.my_contents (id,text) values (1,'my-contents');
INSERT INTO public.campaign_contents (id,title,banner) values (1,'campaign-contents','banner');
INSERT INTO public.page_builder_containers (id, created_at, updated_at, deleted_at, page_id, page_version, page_model_name, model_name, model_id, display_order, shared, hidden, display_name, locale_code, localize_from_model_id) VALUES (1, '2024-06-05 07:20:58.435363 +00:00', '2024-06-05 07:20:58.435363 +00:00', null, 1, '2024-05-20-v01', 'campaigns', 'MyContent', 1, 1, false, false, 'MyContent', '', 0);

`, []string{"campaigns", "products", "my_contents", "campaign_contents", "page_builder_containers"}))

func TestPageBuilderCampaign(t *testing.T) {
Expand Down Expand Up @@ -129,7 +131,7 @@ func TestPageBuilderCampaign(t *testing.T) {
},
EventResponseMatch: func(t *testing.T, er *TestEventResponse) {
var container pagebuilder.Container
if err := TestDB.First(&container).Error; err != nil {
if err := TestDB.Order("id desc").First(&container).Error; err != nil {
t.Error("containers not add", er)
}
if container.ModelName != "CampaignContent" {
Expand All @@ -140,8 +142,52 @@ func TestPageBuilderCampaign(t *testing.T) {
}
},
},
}

{
Name: "Add a new campaigns",
Debug: true,
ReqFunc: func() *http.Request {
pageBuilderData.TruncatePut(dbr)
req := NewMultipartBuilder().
PageURL("/campaigns?__execute_event__=presets_Update").
AddField("Title", "Hello 4").
BuildEventFuncRequest()
return req
},
EventResponseMatch: func(t *testing.T, er *TestEventResponse) {
var campaign Campaign
if err := TestDB.First(&campaign, "title = ?", "Hello 4").Error; err != nil {
t.Error(err)
}
},
},

{
Name: "Page Builder Editor Duplicate A Campaign",
Debug: true,
ReqFunc: func() *http.Request {
pageBuilderData.TruncatePut(dbr)
req := NewMultipartBuilder().
PageURL("/page_builder/campaigns/editors/1_2024-05-20-v01?__execute_event__=publish_EventDuplicateVersion").
BuildEventFuncRequest()

return req
},
EventResponseMatch: func(t *testing.T, er *TestEventResponse) {
var campaigns []*Campaign
TestDB.Order("id DESC, version DESC").Find(&campaigns)
if len(campaigns) != 2 {
t.Fatal("Campaign not duplicated", campaigns)
}
var containers []*pagebuilder.Container
TestDB.Find(&containers, "page_id = ? AND page_version = ?", campaigns[0].ID,
campaigns[0].Version.Version)
if len(containers) == 0 {
t.Error("Container not duplicated", containers)
}
},
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
RunCase(t, c, b)
Expand Down
24 changes: 16 additions & 8 deletions docs/docsrc/generated/g1.go

Large diffs are not rendered by default.

36 changes: 24 additions & 12 deletions docs/docsrc/generated/g2.go

Large diffs are not rendered by default.

876 changes: 584 additions & 292 deletions docs/docsrc/generated/g3.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/integration/pagebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestPageBuilder(t *testing.T) {
},
EventResponseMatch: func(t *testing.T, er *TestEventResponse) {
var page pagebuilder.Page
TestDB.First(&page, "slug = ?", "/hello4")
TestDB.First(&page, "slug = ?", "hello4")
if page.LocaleCode != "International" {
t.Errorf("wrong locale code, expected International, got %#+v", page)
}
Expand Down
155 changes: 10 additions & 145 deletions pagebuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"github.com/qor5/x/v3/i18n"
"github.com/qor5/x/v3/perm"
. "github.com/qor5/x/v3/ui/vuetify"
vx "github.com/qor5/x/v3/ui/vuetifyx"
"github.com/sunfmin/reflectutils"
h "github.com/theplant/htmlgo"
"golang.org/x/text/language"
Expand Down Expand Up @@ -268,6 +267,8 @@
b.models = append(b.models, r)
r.setName()
r.registerFuncs()
r.configDuplicate(r.mb)
r.configDuplicate(r.editor)

Check warning on line 271 in pagebuilder/builder.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/builder.go#L270-L271

Added lines #L270 - L271 were not covered by tests
return r
}

Expand Down Expand Up @@ -324,7 +325,7 @@

func (b *Builder) configEditor(m *ModelBuilder) (err error) {
b.useAllPlugin(m.editor)
md := m.editor.Detailing()
md := m.editor.Detailing().Drawer(false)

Check warning on line 328 in pagebuilder/builder.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/builder.go#L328

Added line #L328 was not covered by tests
md.PageFunc(b.Editor(m))
return
}
Expand Down Expand Up @@ -391,145 +392,6 @@
pm.RegisterEventFunc(selectTemplateEvent, selectTemplate(db))
// pm.RegisterEventFunc(clearTemplateEvent, clearTemplate(db))
}

eb := pm.Editing("TemplateSelection", "Title", "CategoryID", "Slug")
eb.ValidateFunc(func(obj interface{}, ctx *web.EventContext) (err web.ValidationErrors) {
c := obj.(*Page)
err = pageValidator(ctx.R.Context(), c, db, b.l10n)
return
})

eb.Field("Slug").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
var vErr web.ValidationErrors
if ve, ok := ctx.Flash.(*web.ValidationErrors); ok {
vErr = *ve
}

return VTextField().
Variant(FieldVariantUnderlined).
Attr(web.VField(field.Name, strings.TrimPrefix(field.Value(obj).(string), "/"))...).
Prefix("/").
ErrorMessages(vErr.GetFieldErrors("Page.Slug")...)
}).SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
m := obj.(*Page)
m.Slug = path.Join("/", m.Slug)
return nil
})
eb.Field("CategoryID").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
p := obj.(*Page)
categories := []*Category{}
locale, _ := l10n.IsLocalizableFromContext(ctx.R.Context())
if err := db.Model(&Category{}).Where("locale_code = ?", locale).Find(&categories).Error; err != nil {
panic(err)
}

var vErr web.ValidationErrors
if ve, ok := ctx.Flash.(*web.ValidationErrors); ok {
vErr = *ve
}

msgr := i18n.MustGetModuleMessages(ctx.R, I18nPageBuilderKey, Messages_en_US).(*Messages)

return vx.VXAutocomplete().Label(msgr.Category).
Attr(web.VField(field.Name, p.CategoryID)...).
Multiple(false).Chips(false).
Items(categories).ItemText("Path").ItemValue("ID").
ErrorMessages(vErr.GetFieldErrors("Page.Category")...)
})

eb.Field("TemplateSelection").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
if !b.templateEnabled {
return nil
}

p := obj.(*Page)

selectedID := ctx.R.FormValue(templateSelectedID)
body, err := getTplPortalComp(ctx, db, selectedID)
if err != nil {
panic(err)
}

// Display template selection only when creating a new page
if p.ID == 0 {
return h.Div(
web.Portal().Name(templateSelectPortal),
web.Portal(
body,
).Name(selectedTemplatePortal),
).Class("my-2").
Attr(web.VAssign("vars", `{showTemplateDialog: false}`)...)
}
return nil
})

eb.SaveFunc(func(obj interface{}, id string, ctx *web.EventContext) (err error) {
localeCode, _ := l10n.IsLocalizableFromContext(ctx.R.Context())
p := obj.(*Page)
if p.Slug != "" {
p.Slug = path.Clean(p.Slug)
}
if p.LocaleCode == "" {
p.LocaleCode = localeCode
}
funcName := ctx.R.FormValue(web.EventFuncIDName)
if funcName == publish.EventDuplicateVersion {
id := ctx.Param(presets.ParamID)
var fromPage Page
eb.Fetcher(&fromPage, id, ctx)
p.SEO = fromPage.SEO
}

err = db.Transaction(func(tx *gorm.DB) (inerr error) {
if inerr = gorm2op.DataOperator(tx).Save(obj, id, ctx); inerr != nil {
return
}

if strings.Contains(ctx.R.RequestURI, publish.EventDuplicateVersion) {
if inerr = b.copyContainersToNewPageVersion(tx, int(p.ID), p.LocaleCode, p.ParentVersion, p.Version.Version); inerr != nil {
return
}
return
}

if v := ctx.R.FormValue(templateSelectedID); v != "" {
var tplID int
tplID, inerr = strconv.Atoi(v)
if inerr != nil {
return
}
if b.l10n == nil {
localeCode = ""
}
if inerr = b.copyContainersToAnotherPage(tx, tplID, templateVersion, localeCode, int(p.ID), p.Version.Version, localeCode); inerr != nil {
panic(inerr)
}
}
if b.l10n != nil && strings.Contains(ctx.R.RequestURI, l10n.DoLocalize) {
fromID := ctx.R.Context().Value(l10n.FromID).(string)
fromVersion := ctx.R.Context().Value(l10n.FromVersion).(string)
fromLocale := ctx.R.Context().Value(l10n.FromLocale).(string)

var fromIDInt int
fromIDInt, err = strconv.Atoi(fromID)
if err != nil {
return
}

if inerr = b.localizeCategory(tx, p.CategoryID, fromLocale, p.LocaleCode); inerr != nil {
panic(inerr)
}

if inerr = b.localizeContainersToAnotherPage(tx, fromIDInt, fromVersion, fromLocale, int(p.ID), p.Version.Version, p.LocaleCode); inerr != nil {
panic(inerr)
}
return
}
return
})

return
})
return
}

Expand Down Expand Up @@ -1754,9 +1616,12 @@

func (b *Builder) setDefaultDevices() {
b.devices = []Device{
{Name: DeviceComputer, Width: "", Icon: "mdi-laptop"},
{Name: DeviceTablet, Width: "768px", Icon: "mdi-tablet"},
//{Name: DeviceComputer, Width: "", Icon: "mdi-desktop-mac"},
//{Name: DevicePhone, Width: "414px", Icon: "mdi-tablet-android"},
//{Name: DeviceTablet, Width: "768px", Icon: "mdi-tablet"},
{Name: DeviceComputer, Width: "", Icon: "mdi-monitor"},

Check warning on line 1622 in pagebuilder/builder.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/builder.go#L1622

Added line #L1622 was not covered by tests
{Name: DevicePhone, Width: "414px", Icon: "mdi-cellphone"},
{Name: DeviceTablet, Width: "768px", Icon: "mdi-tablet"},

Check warning on line 1624 in pagebuilder/builder.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/builder.go#L1624

Added line #L1624 was not covered by tests
}
}

Expand All @@ -1765,14 +1630,14 @@
ctx.R.Form.Del(web.EventFuncIDName)
for _, d := range b.getDevices() {
comps = append(comps,
VBtn("").Icon(d.Icon).Color(ColorPrimary).Variant(VariantText).Class("mr-4").
VBtn("").Icon(d.Icon).Color(ColorPrimary).BaseColor(ColorPrimary).Variant(VariantText).Class("mr-2").

Check warning on line 1633 in pagebuilder/builder.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/builder.go#L1633

Added line #L1633 was not covered by tests
Attr("@click", web.Plaid().EventFunc(ReloadRenderPageOrTemplateEvent).
PushState(true).Queries(ctx.R.Form).Query(paramsDevice, d.Name).Go()).Value(d.Name),
)
}
return web.Scope(
VBtnToggle(
comps...,
).Class("pa-2 rounded-lg ").Attr("v-model", "toggleLocals.activeDevice").Density(DensityCompact),
).Class("pa-2 rounded-lg ").Attr("v-model", "toggleLocals.activeDevice"),

Check warning on line 1641 in pagebuilder/builder.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/builder.go#L1641

Added line #L1641 was not covered by tests
).VSlot("{ locals : toggleLocals}").Init(fmt.Sprintf(`{activeDevice: "%s"}`, ctx.Param(paramsDevice)))
}
58 changes: 32 additions & 26 deletions pagebuilder/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
tabContent web.PageResponse
pageAppbarContent []h.HTMLComponent
exitHref string
readonly bool

Check warning on line 77 in pagebuilder/editor.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/editor.go#L77

Added line #L77 was not covered by tests

containerDataID = ctx.R.FormValue(paramContainerDataID)
obj = m.mb.NewModel()
Expand All @@ -97,6 +98,7 @@
}
if p, ok := obj.(publish.StatusInterface); ok {
ctx.R.Form.Set(paramStatus, p.EmbedStatus().Status)
readonly = p.EmbedStatus().Status == publish.StatusDraft

Check warning on line 101 in pagebuilder/editor.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/editor.go#L101

Added line #L101 was not covered by tests
}
versionComponent = publish.DefaultVersionComponentFunc(m.editor, publish.VersionComponentConfig{Top: true})(obj, &presets.FieldContext{ModelInfo: m.editor.Info()}, ctx)
exitHref = m.mb.Info().DetailingHref(ctx.Param(presets.ParamID))
Expand All @@ -118,16 +120,18 @@
pageAppbarContent...,
).Class("d-flex align-center justify-space-between w-100 px-6").Style("height: 36px"),
).Elevation(0).Density(DensityCompact).Height(96).Class("align-center border-b"),
VNavigationDrawer(
navigatorDrawer,
).Location(LocationLeft).
Permanent(true).
Width(350),
VNavigationDrawer(
web.Portal(editContainerDrawer).Name(pageBuilderRightContentPortal),
).Location(LocationRight).
Permanent(true).
Width(350),
h.If(readonly,
VNavigationDrawer(
navigatorDrawer,
).Location(LocationLeft).
Permanent(true).
Width(350),
VNavigationDrawer(
web.Portal(editContainerDrawer).Name(pageBuilderRightContentPortal),
).Location(LocationRight).
Permanent(true).
Width(350),
),

Check warning on line 134 in pagebuilder/editor.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/editor.go#L123-L134

Added lines #L123 - L134 were not covered by tests
VMain(
vx.VXMessageListener().ListenFunc(b.generateEditorBarJsFunction(ctx)),
tabContent.Body.(h.HTMLComponent),
Expand Down Expand Up @@ -185,22 +189,24 @@
}
}
r = h.Components(
VTabs(
VTab().Text("Add").
Value(EditorTabElements).Attr("@click",
web.Plaid().PushState(true).MergeQuery(true).
Query(paramTab, EditorTabElements).RunPushState()),
VTab().Text("Layers").Value(EditorTabLayers).Attr("@click",
web.Plaid().PushState(true).MergeQuery(true).
Query(paramTab, EditorTabLayers).RunPushState()+
";"+
web.Plaid().
URL(ctx.R.URL.Path).
EventFunc(ShowSortedContainerDrawerEvent).
Query(paramStatus, ctx.Param(paramStatus)).
MergeQuery(true).
Go()),
).Attr("v-model", "vars.containerTab").FixedTabs(true),
web.Slot(
VTabs(
VTab().Text("Add").
Value(EditorTabElements).Attr("@click",
web.Plaid().PushState(true).MergeQuery(true).
Query(paramTab, EditorTabElements).RunPushState()),
VTab().Text("Layers").Value(EditorTabLayers).Attr("@click",
web.Plaid().PushState(true).MergeQuery(true).
Query(paramTab, EditorTabLayers).RunPushState()+
";"+
web.Plaid().
URL(ctx.R.URL.Path).
EventFunc(ShowSortedContainerDrawerEvent).
Query(paramStatus, ctx.Param(paramStatus)).
MergeQuery(true).
Go()),
).Attr("v-model", "vars.containerTab").FixedTabs(true),
).Name(VSlotPrepend),

Check warning on line 209 in pagebuilder/editor.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/editor.go#L192-L209

Added lines #L192 - L209 were not covered by tests
VTabsWindow(
VTabsWindowItem(m.renderContainersList(ctx)).Value(EditorTabElements),
VTabsWindowItem(web.Portal(listContainers).Name(pageBuilderLayerContainerPortal)).Value(EditorTabLayers),
Expand Down
Loading
Loading