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

fix media choose file select #493

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 1 commit into from
Aug 20, 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
31 changes: 31 additions & 0 deletions example/integration/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,37 @@ func TestMedia(t *testing.T) {
return
},
},
{
Name: "Pages ChooseFileEvent Dialog no selected image",
Debug: true,
ReqFunc: func() *http.Request {
pageBuilderData.TruncatePut(dbr)
mediaTestData.TruncatePut(dbr)
req := NewMultipartBuilder().
PageURL("/pages").
Query(web.EventFuncIDName, media.OpenFileChooserEvent).
Query(media.ParamField, "media").
BuildEventFuncRequest()
return req
},
ExpectPortalUpdate0ContainsInOrder: []string{`showFileChooser:true`, "select_ids:[]", "v-checkbox"},
},
{
Name: "Pages ChooseFileEvent Dialog selected image",
Debug: true,
ReqFunc: func() *http.Request {
pageBuilderData.TruncatePut(dbr)
mediaTestData.TruncatePut(dbr)
req := NewMultipartBuilder().
PageURL("/pages").
Query(web.EventFuncIDName, media.OpenFileChooserEvent).
Query(media.ParamField, "media").
Query(media.ParamSelectIDS, "1").
BuildEventFuncRequest()
return req
},
ExpectPortalUpdate0ContainsInOrder: []string{`showFileChooser:true`, "select_ids:[1]", "v-checkbox"},
},
}

for _, c := range cases {
Expand Down
47 changes: 35 additions & 12 deletions media/filechooser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func fileChooser(mb *Builder) web.EventFunc {
Attr("v-model", "vars.showFileChooser"),
).VSlot("{form,locals}"),
})
r.RunScript = `setTimeout(function(){ vars.showFileChooser = true; }, 100)`
r.RunScript = `setTimeout(function(){ vars.showFileChooser = true}, 100)`
return
}
}
Expand Down Expand Up @@ -83,7 +83,10 @@ type selectItem struct {
func fileChooserDialogContent(mb *Builder, field string, ctx *web.EventContext,
cfg *media_library.MediaBoxConfig,
) h.HTMLComponent {
msgr := i18n.MustGetModuleMessages(ctx.R, I18nMediaLibraryKey, Messages_en_US).(*Messages)
var (
msgr = i18n.MustGetModuleMessages(ctx.R, I18nMediaLibraryKey, Messages_en_US).(*Messages)
inMediaLibrary = strings.Contains(ctx.R.RequestURI, "/"+MediaLibraryURIName)
)
return h.Div(
imageDialog(),
VSnackbar(h.Text(msgr.DescriptionUpdated)).
Expand All @@ -104,7 +107,7 @@ func fileChooserDialogContent(mb *Builder, field string, ctx *web.EventContext,
).Class("d-flex align-center justify-center pt-2"),
).Attr("v-if", "vars.mediaName").Attr("@click", "vars.mediaName = null").ZIndex(10),
).Attr(web.VAssign("vars",
`{snackbarShow: false, mediaShow: null, mediaName: null, isImage: false,imagePreview:false,imageSrc:""}`)...)
fmt.Sprintf(`{snackbarShow: false, mediaShow: null, mediaName: null, isImage: false,imagePreview:false,imageSrc:"",showFileChooser:%v}`, !inMediaLibrary))...)
}

func fileChips(f *media_library.MediaLibrary) h.HTMLComponent {
Expand Down Expand Up @@ -304,10 +307,9 @@ func fileComponent(
if base.IsImageFormat(f.File.FileName) && inMediaLibrary {
*event = clickEvent
}
fileNameComp := VTextField().
Attr(web.VField("name", f.File.FileName)...).
Readonly(true).Variant(VariantPlain)
fileNameComp := h.Span(f.File.FileName).Class("text-body-2")
if !inMediaLibrary {
fileNameComp.Class("text-"+ColorPrimary, "text-decoration-underline")
fileNameComp.Attr("@click.stop", clickEvent)
*event = web.Plaid().
BeforeScript(fmt.Sprintf("locals.%s = true", croppingVar)).
Expand Down Expand Up @@ -361,8 +363,9 @@ func fileOrFolderComponent(
inMediaLibrary bool,
) 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)
title, content h.HTMLComponent
checkEvent = fmt.Sprintf(`let arr=locals.select_ids;let find_id=%v;if(vars.showFileChooser){locals.select_ids=[find_id]}
else{arr.includes(find_id)?arr.splice(arr.indexOf(find_id), 1):arr.push(find_id);}`, f.ID)
clickCardWithoutMoveEvent = "null"
)
menus := &[]h.HTMLComponent{
Expand Down Expand Up @@ -411,7 +414,7 @@ func fileOrFolderComponent(
Attr(":model-value", fmt.Sprintf(`locals.select_ids.includes(%v)`, f.ID)).
Attr("@update:model-value", checkEvent).
Attr("style", "z-index:2").
Class("position-absolute top-0 right-0").Attr("v-if", "locals.select_ids.length>0"),
Class("position-absolute top-0 right-0").Attr("v-if", "vars.showFileChooser||locals.select_ids.length>0"),
VCardText(
VCard(
title,
Expand All @@ -436,7 +439,7 @@ func fileOrFolderComponent(
).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(vars.showFileChooser||locals.select_ids.length>0){%s}else{%s}", checkEvent, clickCardWithoutMoveEvent))
}

func folderComponent(
Expand Down Expand Up @@ -539,7 +542,9 @@ func mediaLibraryContent(mb *Builder, field string, ctx *web.EventContext,
orderByVal = ctx.Param(paramOrderByKey)
typeVal = ctx.Param(paramTypeKey)
parentID = ctx.ParamAsInt(ParamParentID)
selectIDS = ctx.Param(ParamSelectIDS)
msgr = i18n.MustGetModuleMessages(ctx.R, I18nMediaLibraryKey, Messages_en_US).(*Messages)
pMsgr = i18n.MustGetModuleMessages(ctx.R, presets.CoreI18nModuleKey, Messages_en_US).(*presets.Messages)
inMediaLibrary = strings.Contains(ctx.R.RequestURI, "/"+MediaLibraryURIName)
wh = db.Model(&media_library.MediaLibrary{})
files []*media_library.MediaLibrary
Expand All @@ -553,6 +558,10 @@ func mediaLibraryContent(mb *Builder, field string, ctx *web.EventContext,
} else if mb.currentUserID != nil {
wh = wh.Where("user_id = ? ", mb.currentUserID(ctx))
}
var selectIDInit = "[]"
if selectIDS != "" && selectIDS != "0" {
selectIDInit = fmt.Sprintf("[%s]", selectIDS)
}
switch orderByVal {
case orderByCreatedAt:
wh = wh.Order("created_at")
Expand Down Expand Up @@ -819,10 +828,24 @@ func mediaLibraryContent(mb *Builder, field string, ctx *web.EventContext,
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", "!vars.showFileChooser&&(locals.select_ids && locals.select_ids.length>0)"),
VRow(
VCol(
VBtn(pMsgr.Save).Size(SizeSmall).Variant(VariantOutlined).
Attr(":disabled", "locals.select_ids.length==0").
Color(ColorPrimary).
Attr("@click", web.Plaid().
BeforeScript(fmt.Sprintf(`locals["fileChooser"+%v+"_cropping"] = true`, web.Var(`locals.select_ids.join(",")`))).
EventFunc(chooseFileEvent).
Query(ParamField, field).
Query(ParamMediaIDS, web.Var(`locals.select_ids.join(",")`)).
Query(ParamCfg, h.JSONString(cfg)).
Go()),
),
).Class("d-flex align-center").Attr("v-if", "vars.showFileChooser&&(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(fmt.Sprintf(`{select_ids:%s}`, selectIDInit))
}

func searchComponent(ctx *web.EventContext, field string, cfg *media_library.MediaBoxConfig, inMediaLibrary bool) h.HTMLComponent {
Expand Down
23 changes: 10 additions & 13 deletions media/media_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"path"
"slices"
"sort"
"strconv"
"strings"
"time"

"github.com/qor5/admin/v3/media/base"
"github.com/qor5/admin/v3/media/media_library"
"github.com/qor5/admin/v3/presets"
Expand All @@ -26,6 +18,12 @@ import (
h "github.com/theplant/htmlgo"
"golang.org/x/text/language"
"gorm.io/gorm"
"io"
"path"
"slices"
"sort"
"strconv"
"strings"
)

type MediaBoxConfigKey int
Expand Down Expand Up @@ -183,8 +181,7 @@ func (b *QMediaBoxBuilder) MarshalHTML(c context.Context) (r []byte, err error)
).Name(mediaBoxThumbnailsPortalName(b.fieldName)),
web.Portal().Name(portalName),
).Class("pb-4").
Rounded(true).
Attr(web.VAssign("vars", `{showFileChooser: false}`)...),
Rounded(true),
).MarshalHTML(c)
}

Expand All @@ -193,13 +190,13 @@ func mediaBoxThumb(msgr *Messages, cfg *media_library.MediaBoxConfig,
) h.HTMLComponent {
size := cfg.Sizes[thumb]
fileSize := f.FileSizes[thumb]
url := f.URL(thumb)
url := f.URLNoCached(thumb)
if thumb == base.DefaultSizeKey {
url = f.URL()
url = f.URLNoCached()
}
card := VCard(
h.If(base.IsImageFormat(f.FileName),
VImg().Src(fmt.Sprintf("%s?%d", url, time.Now().UnixNano())).Height(150),
VImg().Src(url).Height(150),
).Else(
h.Div(
fileThumb(f.FileName),
Expand Down
Loading
11 0