<commit_before>package admin

import (
	"errors"
	"fmt"
	"net/http"

	"html/template"

	"github.com/qor/admin"
	"github.com/qor/banner_editor"
	"github.com/qor/l10n"
	"github.com/qor/media/oss"
	"github.com/qor/qor"
	"github.com/qor/qor-example/app/models"
	"github.com/qor/qor-example/db"
	"github.com/qor/qor/resource"
	"github.com/qor/sorting"
	"github.com/qor/widget"
)

var Widgets *widget.Widgets

type QorWidgetSetting struct {
	widget.QorWidgetSetting
	l10n.Locale
	// publish.Status
	// DeletedAt *time.Time
}

func initWidgets() {
	if Widgets == nil {
		Widgets = widget.New(&widget.Config{DB: db.DB})
		Widgets.WidgetSettingResource = Admin.AddResource(&QorWidgetSetting{}, &admin.Config{Menu: []string{"Site Management"}, Priority: 3})
		Widgets.WidgetSettingResource.Meta(&admin.Meta{Name: "Name", FormattedValuer: func(widget interface{}, ctx *qor.Context) interface{} {
			setting := widget.(*QorWidgetSetting)
			return template.HTML(`<img src="/images/Widget-` + setting.WidgetType + `.png" width="80" height="35" style="margin-right: 12px;"/><span>` + setting.Name + `</span>`)
		}})

		Widgets.RegisterScope(&widget.Scope{
			Name: "From Google",
			Visible: func(context *widget.Context) bool {
				if request, ok := context.Get("Request"); ok {
					_, ok := request.(*http.Request).URL.Query()["from_google"]
					return ok
				}
				return false
			},
		})

		Admin.AddResource(Widgets)

		// Top Banner
		type bannerArgument struct {
			Title           string
			ButtonTitle     string
			Link            string
			BackgroundImage oss.OSS
			Logo            oss.OSS
		}

		Widgets.RegisterWidget(&widget.Widget{
			Name:      "NormalBanner",
			Templates: []string{"banner", "banner2"},
			Setting:   Admin.NewResource(&bannerArgument{}),
			Context: func(context *widget.Context, setting interface{}) *widget.Context {
				context.Options["Setting"] = setting
				return context
			},
		})

		// Banner Editor
		type bannerEditorArgument struct {
			Value string
		}
		type subHeaderSetting struct {
			Text  string
			Color string
		}
		type buttonSetting struct {
			Text string
			Link string
		}
		subHeaderRes := Admin.NewResource(&subHeaderSetting{})
		subHeaderRes.Meta(&admin.Meta{Name: "Text"})
		subHeaderRes.Meta(&admin.Meta{Name: "Color"})

		buttonRes := Admin.NewResource(&buttonSetting{})
		buttonRes.Meta(&admin.Meta{Name: "Text"})
		buttonRes.Meta(&admin.Meta{Name: "Link"})

		banner_editor.RegisterElement(&banner_editor.Element{
			Name:     "Sub Header",
			Template: "<em style=\"color: {{.Color}};\">{{.Text}}</em>",
			Resource: subHeaderRes,
			Context: func(c *banner_editor.Context, r interface{}) *banner_editor.Context {
				return c
			},
		})
		banner_editor.RegisterElement(&banner_editor.Element{
			Name:     "Button",
			Template: "<a href='{{.Link}}'>{{.Text}}</a>",
			Resource: buttonRes,
			Context: func(c *banner_editor.Context, r interface{}) *banner_editor.Context {
				return c
			},
		})
		bannerEditorResource := Admin.NewResource(&bannerEditorArgument{})
		bannerEditorResource.Meta(&admin.Meta{Name: "Value", Config: &banner_editor.BannerEditorConfig{}})

		Widgets.RegisterWidget(&widget.Widget{
			Name:      "BannerEditor",
			Templates: []string{"banner_editor"},
			Setting:   bannerEditorResource,
			Context: func(context *widget.Context, setting interface{}) *widget.Context {<commit_msg>update banner edit<commit_after>package admin

import (
	"errors"
	"fmt"
	"net/http"

	"html/template"

	"github.com/qor/admin"
	"github.com/qor/banner_editor"
	"github.com/qor/l10n"
	"github.com/qor/media/oss"
	"github.com/qor/qor"
	"github.com/qor/qor-example/app/models"
	"github.com/qor/qor-example/db"
	"github.com/qor/qor/resource"
	"github.com/qor/sorting"
	"github.com/qor/widget"
)

var Widgets *widget.Widgets

type QorWidgetSetting struct {
	widget.QorWidgetSetting
	l10n.Locale
	// publish.Status
	// DeletedAt *time.Time
}

func initWidgets() {
	if Widgets == nil {
		Widgets = widget.New(&widget.Config{DB: db.DB})
		Widgets.WidgetSettingResource = Admin.AddResource(&QorWidgetSetting{}, &admin.Config{Menu: []string{"Site Management"}, Priority: 3})
		Widgets.WidgetSettingResource.Meta(&admin.Meta{Name: "Name", FormattedValuer: func(widget interface{}, ctx *qor.Context) interface{} {
			setting := widget.(*QorWidgetSetting)
			return template.HTML(`<img src="/images/Widget-` + setting.WidgetType + `.png" width="80" height="35" style="margin-right: 12px;"/><span>` + setting.Name + `</span>`)
		}})

		Widgets.RegisterScope(&widget.Scope{
			Name: "From Google",
			Visible: func(context *widget.Context) bool {
				if request, ok := context.Get("Request"); ok {
					_, ok := request.(*http.Request).URL.Query()["from_google"]
					return ok
				}
				return false
			},
		})

		Admin.AddResource(Widgets)

		// Top Banner
		type bannerArgument struct {
			Title           string
			ButtonTitle     string
			Link            string
			BackgroundImage oss.OSS
			Logo            oss.OSS
		}

		Widgets.RegisterWidget(&widget.Widget{
			Name:      "NormalBanner",
			Templates: []string{"banner", "banner2"},
			Setting:   Admin.NewResource(&bannerArgument{}),
			Context: func(context *widget.Context, setting interface{}) *widget.Context {
				context.Options["Setting"] = setting
				return context
			},
		})

		// Banner Editor
		type bannerEditorArgument struct {
			Value string
		}
		type subHeaderSetting struct {
			Text  string
			Color string
		}
		type headerSetting struct {
			Text  string
			Color string
		}
		type buttonSetting struct {
			Text string
			Link string
		}

		headerRes := Admin.NewResource(&headerSetting{})
		headerRes.Meta(&admin.Meta{Name: "Text"})
		headerRes.Meta(&admin.Meta{Name: "Color"})

		subHeaderRes := Admin.NewResource(&subHeaderSetting{})
		subHeaderRes.Meta(&admin.Meta{Name: "Text"})
		subHeaderRes.Meta(&admin.Meta{Name: "Color"})

		buttonRes := Admin.NewResource(&buttonSetting{})
		buttonRes.Meta(&admin.Meta{Name: "Text"})
		buttonRes.Meta(&admin.Meta{Name: "Link"})

		banner_editor.RegisterElement(&banner_editor.Element{
			Name:     "Add Header",
			Template: "<h1 style=\"color: {{.Color}};\">{{.Text}}</h1>",
			Resource: headerRes,
			Context: func(c *banner_editor.Context, r interface{}) *banner_editor.Context {
				return c
			},
		})
		banner_editor.RegisterElement(&banner_editor.Element{
			Name:     "Add Sub Header",
			Template: "<h2 style=\"color: {{.Color}};\">{{.Text}}</h2>",
			Resource: subHeaderRes,
			Context: func(c *banner_editor.Context, r interface{}) *banner_editor.Context {
				return c
			},
		})
		banner_editor.RegisterElement(&banner_editor.Element{
			Name:     "Add Link",
			Template: "<a href='{{.Link}}'>{{.Text}}</a>",
			Resource: buttonRes,
			Context: func(c *banner_editor.Context, r interface{}) *banner_editor.Context {
				return c
			},
		})
		bannerEditorResource := Admin.NewResource(&bannerEditorArgument{})
		bannerEditorResource.Meta(&admin.Meta{Name: "Value", Config: &banner_editor.BannerEditorConfig{}})

		Widgets.RegisterWidget(&widget.Widget{
			Name:      "BannerEditor",
			Templates: []string{"banner_editor"},
			Setting:   bannerEditorResource,
			Context: func(context *widget.Context, setting interface{}) *widget.Context {
{'fraction_non_alphanumeric': 0.15420754456266408, 'fraction_numerical': 0.0038690064944037583, 'mean_word_length': 4.005532503457815, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4000330', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2371, 'n_words': 441}