8000 Fix media by zhangshanwen · Pull Request #421 · qor5/admin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix media #421

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

Merged
merged 3 commits into from
Aug 5, 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
11 changes: 9 additions & 2 deletions example/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package admin
import (
_ "embed"
"fmt"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/qor5/admin/v3/example/models"
"github.com/qor5/admin/v3/role"
"github.com/qor5/x/v3/login"
"github.com/qor5/x/v3/sitemap"
"gorm.io/gorm"
"net/http"
)

//go:embed assets/favicon.ico
Expand Down Expand Up @@ -38,6 +37,14 @@ func TestHandler(db *gorm.DB, u *models.User) http.Handler {
mux.Handle("/", m(c.pb))
return mux
}
func TestL18nHandler(db *gorm.DB) http.Handler {
mux := http.NewServeMux()
c := NewConfig(db)
c.loginSessionBuilder.Secret("test")
c.loginSessionBuilder.Mount(mux)
mux.Handle("/", c.pb)
return mux
}

func Router(db *gorm.DB) http.Handler {
c := NewConfig(db)
Expand Down
49 changes: 49 additions & 0 deletions example/integration/l18n_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package integration_test

import (
"net/http"
"testing"

"github.com/qor5/admin/v3/example/admin"
"github.com/qor5/web/v3/multipartestutils"
)

func TestL18n(t *testing.T) {
h := admin.TestL18nHandler(TestDB)

dbr, _ := TestDB.DB()
profileData.TruncatePut(dbr)

cases := []multipartestutils.TestCase{
{
Name: "view by zh",
Debug: true,
ReqFunc: func() *http.Request {
req := multipartestutils.NewMultipartBuilder().
PageURL("/auth/login").
BuildEventFuncRequest()
req.Header.Add("accept-language", "zh")
return req
},
ExpectPageBodyContainsInOrder: []string{`邮箱`},
},
{
Name: "view by ja",
Debug: true,
ReqFunc: func() *http.Request {
req := multipartestutils.NewMultipartBuilder().
PageURL("/auth/login").
BuildEventFuncRequest()
req.Header.Add("accept-language", "ja")
return req
},
ExpectPageBodyContainsInOrder: []string{`メールアドレス`},
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
multipartestutils.RunCase(t, c, h)
})
}
}
4 changes: 4 additions & 0 deletions login/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func (b *SessionBuilder) Activity(amb *activity.ModelBuilder) *SessionBuilder {
b.amb = amb
return b
}
func (b *SessionBuilder) Secret(v string) *SessionBuilder {
b.lb.Secret(v)
return b
}

func (b *SessionBuilder) IsPublicUser(f func(user any) bool) *SessionBuilder {
b.isPublicUser = f
Expand Down
30 changes: 20 additions & 10 deletions media/filechooser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Attr("v-model", "vars.showFileChooser"),
).VSlot("{form,locals}"),
})
r.RunScript = `setTimeout(function(){ vars.showFileChooser = true }, 100)`
r.RunScript = `setTimeout(function(){ vars.showFileChooser = true; }, 100)`

Check warning on line 56 in media/filechooser.go

View check run for this annotation

Codecov / codecov/patch

media/filechooser.go#L56

Added line #L56 was not covered by tests
return
}
}
Expand Down Expand Up @@ -159,6 +159,7 @@
}

renderFileChooserDialogContent(ctx, &r, field, mb, cfg)
r.RunScript = `vars.searchMsg = ""`

Check warning on line 162 in media/filechooser.go

View check run for this annotation

Codecov / codecov/patch

media/filechooser.go#L162

Added line #L162 was not covered by tests
return
}
}
Expand Down Expand Up @@ -269,6 +270,7 @@
initCroppingVars []string,
event *string,
menus *[]h.HTMLComponent,
inMediaLibrary bool,
) (title h.HTMLComponent, content h.HTMLComponent) {
_, needCrop := mergeNewSizes(f, cfg)
croppingVar := fileCroppingVarName(f.ID)
Expand Down Expand Up @@ -298,8 +300,15 @@
Query(searchKeywordName(field), ctx.Param(searchKeywordName(field))).
Go()),
)
if base.IsImageFormat(f.File.FileName) {
*event = fmt.Sprintf(`vars.imageSrc="%s";vars.imagePreview=true;`, src)
clickEvent := fmt.Sprintf(`vars.imageSrc="%s";vars.imagePreview=true;`, src)
if base.IsImageFormat(f.File.FileName) && inMediaLibrary {
*event = clickEvent
}
fileNameComp := VTextField().
Attr(web.VField("name", f.File.FileName)...).
Readonly(true).Variant(VariantPlain)
if !inMediaLibrary {
fileNameComp.Attr("@click", clickEvent)

Check warning on line 311 in media/filechooser.go

View check run for this annotation

Codecov / codecov/patch

media/filechooser.go#L311

Added line #L311 was not covered by tests
}
title = h.Div(
h.If(
Expand Down Expand Up @@ -330,7 +339,7 @@
content = h.Components(
web.Slot(
web.Scope(
VTextField().Attr(web.VField("name", f.File.FileName)...).Readonly(true).Variant(VariantPlain),
fileNameComp,
).VSlot(`{form}`),
).Name("title"),
web.Slot(h.If(base.IsImageFormat(f.File.FileName),
Expand All @@ -353,7 +362,7 @@
) h.HTMLComponent {
var (
title, content h.HTMLComponent
checkEvent = fmt.Sprintf(`let arr=locals.select_ids;let find_id=%v; arr.includes(find_id)?arr.splice(arr.indexOf(find_id), 1):arr.push(find_id);`, f.ID)
checkEvent = fmt.Sprintf(`let arr=locals.select_ids;let find_id=%v;arr.includes(find_id)?arr.splice(arr.indexOf(find_id), 1):arr.push(find_id);`, f.ID)
clickCardWithoutMoveEvent = "null"
)
menus := &[]h.HTMLComponent{
Expand All @@ -366,7 +375,7 @@
Query(searchKeywordName(field), ctx.Param(searchKeywordName(field))).
Query(ParamMediaIDS, fmt.Sprint(f.ID)).
Go()),
VListItem(h.Text("Move to")).Attr("@click", fmt.Sprintf("locals.select_ids=[%v]", f.ID)),
VListItem(h.Text("Move to")).Attr("@click", fmt.Sprintf("locals.select_ids.push(%v)", f.ID)),
h.If(mb.deleteIsAllowed(ctx.R, f) == nil, VListItem(h.Text(msgr.Delete)).Attr("@click",
web.Plaid().
EventFunc(DeleteConfirmationEvent).
Expand All @@ -392,12 +401,13 @@
clickCardWithoutMoveEvent += ";" + web.Plaid().PushState(true).MergeQuery(true).Query(ParamParentID, f.ID).RunPushState()
}
} else {
title, content = fileComponent(mb, field, tab, ctx, f, msgr, cfg, initCroppingVars, &clickCardWithoutMoveEvent, menus)
title, content = fileComponent(mb, field, tab, ctx, f, msgr, cfg, initCroppingVars, &clickCardWithoutMoveEvent, menus, inMediaLibrary)

}

return VCard(
VCheckbox().
Color(ColorPrimary).
Attr(":model-value", fmt.Sprintf(`locals.select_ids.includes(%v)`, f.ID)).
Attr("@update:model-value", checkEvent).
Attr("style", "z-index:2").
Expand Down Expand Up @@ -426,7 +436,7 @@
).Class("pa-0"),
).Class("position-relative").
Hover(true).
Attr("@click", fmt.Sprintf("if( locals.select_ids.length>0){%s}else{%s}", checkEvent, clickCardWithoutMoveEvent))
Attr("@click", fmt.Sprintf("if(locals.select_ids.length>0){%s}else{%s}", checkEvent, clickCardWithoutMoveEvent))
}

func folderComponent(
Expand Down Expand Up @@ -796,10 +806,10 @@
Query(ParamCfg, h.JSONString(cfg)).
Query(ParamMediaIDS, web.Var(`locals.select_ids.join(",")`)).Go()),
),
).Class("d-flex align-center").Attr("v-if", "locals.select_ids && locals.select_ids.length>0"),
).Class("d-flex align-center").Attr("v-if", "(locals.select_ids && locals.select_ids.length>0)"),
).Fluid(true),
).Init(fmt.Sprintf(`{fileChooserUploadingFiles: [], %s}`, strings.Join(initCroppingVars, ", "))).
VSlot("{ locals,form}").Init("{select_ids:[]}")
VSlot("{ locals,form}").Init(`{select_ids:[]}`)
}

func searchComponent(ctx *web.EventContext, field string, cfg *media_library.MediaBoxConfig, inMediaLibrary bool) h.HTMLComponent {
Expand Down
8 changes: 5 additions & 3 deletions media/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ func configList(b *presets.Builder, mb *Builder) {
searchComponent(ctx, filed, cfg, true),
).Class(W100, "d-flex align-center"))
r.Body = h.Components(
web.Portal(
fileChooserDialogContent(mb, filed, ctx, cfg),
).Name(dialogContentPortalName(filed)),
h.Div(
web.Portal(
fileChooserDialogContent(mb, filed, ctx, cfg),
).Name(dialogContentPortalName(filed)),
),
)
return
})
Expand Down
3 changes: 3 additions & 0 deletions media/media_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func mediaBoxThumbnails(ctx *web.EventContext, mediaBox *media_library.MediaBox,
Attr("@click", web.Plaid().EventFunc(openFileChooserEvent).
Query(ParamField, field).
Query(ParamCfg, h.JSONString(cfg)).
Query(ParamSelectIDS, mediaBox.ID).
Go(),
).Disabled(disabled),
)
Expand Down Expand Up @@ -838,7 +839,9 @@ func moveToFolder(mb *Builder) web.EventFunc {
}
r.RunScript = web.Plaid().
EventFunc(imageJumpPageEvent).
AfterScript(`vars.searchMsg=""`).
Queries(queries).
Query(ParamSelectIDS, "").
Query(ParamParentID, selectFolderID).
Go()
return
Expand Down
4 changes: 3 additions & 1 deletion pagebuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ func (b *Builder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b.ps.ServeHTTP(w, r)
}

func (b *Builder) generateEditorBarJsFunction(_ *web.EventContext) string {
func (b *Builder) generateEditorBarJsFunction(ctx *web.EventContext) string {
editAction := fmt.Sprintf(`vars.%s=container_data_id;`, paramContainerDataID) +
web.Plaid().
PushState(true).
Expand Down Expand Up @@ -1548,6 +1548,8 @@ func (b *Builder) generateEditorBarJsFunction(_ *web.EventContext) string {
Query(paramContainerID, web.Var("container_id")).
Query(paramMoveDirection, web.Var("msg_type")).
Query(paramModelID, web.Var("model_id")).
Query(paramStatus, ctx.Param(paramStatus)).
Query(paramContainerDataID, ctx.Param(paramContainerDataID)).
Go()
return fmt.Sprintf(`
function(e){
Expand Down
6 changes: 5 additions & 1 deletion
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,11 @@ func (b *ModelBuilder) moveUpDownContainer(ctx *web.EventContext) (r web.EventRe
}
return
})
r.RunScript = web.Plaid().EventFunc(ReloadRenderPageOrTemplateEvent).MergeQuery(true).Go()
web.AppendRunScripts(&r,
web.Plaid().EventFunc(ReloadRenderPageOrTemplateEvent).MergeQuery(true).Go(),
web.Plaid().EventFunc(ShowSortedContainerDrawerEvent).Query(paramStatus, ctx.Param(paramStatus)).
Query(paramContainerDataID, ctx.Param(paramContainerDataID)).Go(),
)
return
}

Expand Down
2 changes: 2 additions & 0 deletions pagebuilder/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pagebuilder

import (
"fmt"
"path"
"strconv"
"strings"

Expand Down Expand Up @@ -172,6 +173,7 @@ func detailPageEditor(dp *presets.DetailingBuilder, b *Builder) {
Editing("Title", "Slug", "CategoryID").
ValidateFunc(func(obj interface{}, ctx *web.EventContext) (err web.ValidationErrors) {
c := obj.(*Page)
c.Slug = path.Join("/", c.Slug)
err = pageValidator(ctx.R.Context(), c, db, b.l10n)
return
}).
Expand Down
Loading
0