Browse Source

Making template optional for pagebuilder

Chenyang 1 year ago
parent
commit
11cb4b0e33
1 changed files with 25 additions and 7 deletions
  1. 25 7
      pagebuilder/builder.go

+ 25 - 7
pagebuilder/builder.go

@@ -78,6 +78,7 @@ type Builder struct {
 	defaultDevice     string
 	publishBtnColor   string
 	duplicateBtnColor string
+	templateEnabled   bool
 }
 
 const (
@@ -130,6 +131,7 @@ create unique index if not exists uidx_page_builder_demo_containers_model_name_l
 		defaultDevice:     DeviceComputer,
 		publishBtnColor:   "primary",
 		duplicateBtnColor: "primary",
+		templateEnabled:   true,
 	}
 	r.ps = presets.New().
 		BrandTitle("Page Builder").
@@ -190,18 +192,29 @@ func (b *Builder) PublishBtnColor(v string) (r *Builder) {
 	b.publishBtnColor = v
 	return b
 }
+
 func (b *Builder) DuplicateBtnColor(v string) (r *Builder) {
 	b.duplicateBtnColor = v
 	return b
 }
 
+func (b *Builder) TemplateEnabled(v bool) (r *Builder) {
+	b.templateEnabled = v
+	return b
+}
+
 func (b *Builder) Configure(pb *presets.Builder, db *gorm.DB, l10nB *l10n.Builder, activityB *activity.ActivityBuilder, publisher *publish.Builder, seoCollection *seo.Collection) (pm *presets.ModelBuilder) {
 	pb.I18n().
 		RegisterForModule(language.English, I18nPageBuilderKey, Messages_en_US).
 		RegisterForModule(language.SimplifiedChinese, I18nPageBuilderKey, Messages_zh_CN).
 		RegisterForModule(language.Japanese, I18nPageBuilderKey, Messages_ja_JP)
 	pm = pb.Model(&Page{})
-	templateM := b.ConfigTemplate(pb, db)
+
+	templateM := presets.NewModelBuilder(pb, &Template{})
+	if b.templateEnabled {
+		templateM = b.ConfigTemplate(pb, db)
+	}
+
 	b.mb = pm
 	pm.Listing("ID", "Online", "Title", "Slug")
 	dp := pm.Detailing("Overview")
@@ -234,7 +247,7 @@ func (b *Builder) Configure(pb *presets.Builder, db *gorm.DB, l10nB *l10n.Builde
 			}
 
 			isPage := strings.Contains(ctx.R.RequestURI, "/"+pm.Info().URIName()+"/")
-			isTempate := strings.Contains(ctx.R.RequestURI, "/"+templateM.Info().URIName()+"/")
+			isTemplate := strings.Contains(ctx.R.RequestURI, "/"+templateM.Info().URIName()+"/")
 			var obj interface{}
 			var dmb *presets.ModelBuilder
 			if isPage {
@@ -245,7 +258,7 @@ func (b *Builder) Configure(pb *presets.Builder, db *gorm.DB, l10nB *l10n.Builde
 			obj = dmb.NewModel()
 			obj, err = dmb.Detailing().GetFetchFunc()(obj, id, ctx)
 			if err != nil {
-				if err == presets.ErrRecordNotFound {
+				if errors.Is(err, presets.ErrRecordNotFound) {
 					return pb.DefaultNotFoundPageFunc(ctx)
 				}
 				return
@@ -281,7 +294,7 @@ func (b *Builder) Configure(pb *presets.Builder, db *gorm.DB, l10nB *l10n.Builde
 				if l, ok := obj.(l10n.L10nInterface); ok {
 					ctx.R.Form.Set("locale", l.GetLocale())
 				}
-				if isTempate {
+				if isTemplate {
 					ctx.R.Form.Set("tpl", "1")
 				}
 				tabContent, err = b.PageContent(ctx)
@@ -445,9 +458,11 @@ function(e){
 
 	configureVersionListDialog(db, b.ps, pm)
 
-	pm.RegisterEventFunc(openTemplateDialogEvent, openTemplateDialog(db, b.prefix))
-	pm.RegisterEventFunc(selectTemplateEvent, selectTemplate(db))
-	// pm.RegisterEventFunc(clearTemplateEvent, clearTemplate(db))
+	if b.templateEnabled {
+		pm.RegisterEventFunc(openTemplateDialogEvent, openTemplateDialog(db, b.prefix))
+		pm.RegisterEventFunc(selectTemplateEvent, selectTemplate(db))
+		// pm.RegisterEventFunc(clearTemplateEvent, clearTemplate(db))
+	}
 	pm.RegisterEventFunc(schedulePublishDialogEvent, schedulePublishDialog(db, pm))
 	pm.RegisterEventFunc(schedulePublishEvent, schedulePublish(db, pm))
 	pm.RegisterEventFunc(createNoteDialogEvent, createNoteDialog(db, pm))
@@ -499,6 +514,9 @@ function(e){
 	})
 
 	eb.Field("TemplateSelection").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
+		if !b.templateEnabled {
+			return nil
+		}
 		p := obj.(*Page)
 		msgr := i18n.MustGetModuleMessages(ctx.R, I18nPageBuilderKey, Messages_en_US).(*Messages)
 		// Display template selection only when creating a new page