diff options
| author | Jake Mannens <jake@asger.xyz> | 2023-08-16 14:19:33 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2025-08-18 10:59:32 +1000 |
| commit | acd4ca24259da98839847130bf34a27d10b0cf9f (patch) | |
| tree | a493ec45586f4b80e4125a066e842b8e86546341 | |
| parent | 38c60cee378b9c2ad42fc9dc79bc492b919a68f5 (diff) | |
Fixed most existing dialogs
| -rw-r--r-- | Pages/Component/MediaTagTable.razor | 69 | ||||
| -rw-r--r-- | Pages/Component/TagSelectDialog.razor | 49 | ||||
| -rw-r--r-- | Pages/TagDefinitions.razor | 11 | ||||
| -rw-r--r-- | Pages/ViewMedia.razor | 21 | ||||
| -rw-r--r-- | Pages/ViewMedia.razor.css | 4 | ||||
| -rw-r--r-- | Services/TagService.cs | 21 | ||||
| -rw-r--r-- | Tag.cs | 5 | ||||
| -rw-r--r-- | wwwroot/css/site.css | 2 |
8 files changed, 116 insertions, 66 deletions
diff --git a/Pages/Component/MediaTagTable.razor b/Pages/Component/MediaTagTable.razor index 1bd4de4..d4a4283 100644 --- a/Pages/Component/MediaTagTable.razor +++ b/Pages/Component/MediaTagTable.razor @@ -9,24 +9,27 @@ <th>Tag Name</th> <th></th> </tr> - @foreach(var tag in userTags) { - bool isImplicit = IsImplicit(tag); + @foreach(var tagDef in tagDefs) { <tr> <td> - @if(isImplicit) { - <i>@tag.Namespace</i> + @if(implicitTags[tagDef.Guid]) { + <i>@tagDef.Namespace</i> } else { - @tag.Namespace + @tagDef.Namespace } </td> <td> - @if(isImplicit) { - <i>@tag.Name</i> + @if(implicitTags[tagDef.Guid]) { + <i>@tagDef.Name</i> } else { - @tag.Name + @tagDef.Name + } + </td> + <td> + @if(!implicitTags[tagDef.Guid]) { + <a href="javascript:;" @onclick=@(() => Delete(tagDef))>Delete</a> } </td> - <td><a href="javascript:;" @onclick=@(() => Delete(tag))>Delete</a></td> </tr> } </table> @@ -35,32 +38,38 @@ [Parameter] public Media Media { get; set; } - private IEnumerable<TagDefinition> userTags { - get { - using var db = dbFactory.CreateDbContext(); - if(db.Entry(Media).State == EntityState.Detached) - db.Attach(Media); - return GetTagRecursive( - Media.Tags - .Select(t => t.TagDefinition)) - .Where(td => td.Source == TagSource.UserTag) - .OrderBy(td => td.Namespace) - .ThenBy(td => td.Name) - .ToArray(); - } - } + private TagDefinition[] tagDefs; + private Dictionary<Guid, bool> implicitTags; - public void Refresh() => StateHasChanged(); + protected override void OnInitialized() => LoadTagDefs(); - private void Delete(TagDefinition tagDef) { - tagService.RemoveTag(Media, tagDef); + public void Refresh() { + LoadTagDefs(); StateHasChanged(); } - private bool IsImplicit(TagDefinition tagDef) => - !Media.Tags - .Select(t => t.TagDefinition.Guid) - .Contains(tagDef.Guid); + private void Delete(TagDefinition tagDef) { + tagService.RemoveTag(Media.Guid, tagDef.Guid); + Refresh(); + } + + private void LoadTagDefs() { + using var db = dbFactory.CreateDbContext(); + var media = db.Media.First(m => m.ObjectId == Media.ObjectId); + + tagDefs = GetTagRecursive( + media.Tags + .Select(t => t.TagDefinition)) + .Where(td => td.Source == TagSource.UserTag) + .OrderBy(td => td.Namespace) + .ThenBy(td => td.Name) + .ToArray(); + + implicitTags = new(tagDefs + .Select(td => new KeyValuePair<Guid, bool>( + td.Guid, + !media.Tags.Select(t => t.TagDefinition.Guid).Contains(td.Guid)))); +} private IEnumerable<TagDefinition> GetTagRecursive(IEnumerable<TagDefinition> tagDefs) => tagDefs diff --git a/Pages/Component/TagSelectDialog.razor b/Pages/Component/TagSelectDialog.razor index 590c8f2..ebf6d58 100644 --- a/Pages/Component/TagSelectDialog.razor +++ b/Pages/Component/TagSelectDialog.razor @@ -1,13 +1,20 @@ -@inject HBContext db +@inject IDbContextFactory<HBContext> dbFactory +@implements IDisposable <link rel="stylesheet" href="@(nameof(HyperBooru)).styles.css"/> <Dialog Title=@(Title ?? "Select one or more tag(s)") @ref=dialog> <input type="text" placeholder="Search"/> <div class="tag-definitions"> - @foreach(var tagDef in tagDefinitions) { - <input type="checkbox" id="tagDef-@tagDef.Guid" @onchange=@(e => Checked(tagDef, e.Value))/> - <label for="tagDef-@tagDef.Guid">@tagDef.Name</label> + @for(int i = 0; i < tagDefinitions.Count(); i++) { + var local = i; + <input + type="checkbox" + id="tagDef-@tagDefinitions[i].tagDefinition.Guid" + @bind=tagDefinitions[local].selected /> + <label for="tagDef-@tagDefinitions[i].tagDefinition.Guid"> + @tagDefinitions[i].tagDefinition.Name + </label> } </div> <div class="button-container"> @@ -28,32 +35,34 @@ set => visible = dialog.Visible = value; } - private bool visible; + private (TagDefinition tagDefinition, bool selected)[] tagDefinitions; - private Dialog dialog; + private HBContext db; - private IEnumerable<TagDefinition> tagDefinitions => db.TagDefinitions - .Where(td => td.Source == TagSource.UserTag) - .OrderBy(td => td.Name); + private Dialog dialog; - private List<TagDefinition> selected = new(); + private bool visible; public void Show() => Visible = true; public void Hide() => Visible = false; + protected override void OnInitialized() { + db = dbFactory.CreateDbContext(); + tagDefinitions = db.TagDefinitions + .Where(td => td.Source == TagSource.UserTag) + .OrderBy(td => td.Name) + .Select(td => new Tuple<TagDefinition, bool>(td, false).ToValueTuple()) + .ToArray(); + } + private async void Submit() { - await OnSubmit.InvokeAsync(selected.ToArray()); - selected.Clear(); + await OnSubmit.InvokeAsync( + tagDefinitions.Select(e => e.tagDefinition).ToArray()); + for(int i = 0; i < tagDefinitions.Count(); i++) + tagDefinitions[i].selected = false; Hide(); StateHasChanged(); } - private void Checked(TagDefinition tagDef, object? isChecked) { - if(isChecked is bool && (bool) isChecked == true) - if (!selected.Contains(tagDef)) - selected.Add(tagDef); - else - if (selected.Contains(tagDef)) - selected.Remove(tagDef); - } + public void Dispose() => db.Dispose(); } diff --git a/Pages/TagDefinitions.razor b/Pages/TagDefinitions.razor index e48402b..b28ef0c 100644 --- a/Pages/TagDefinitions.razor +++ b/Pages/TagDefinitions.razor @@ -8,18 +8,19 @@ <table id="tag-definitions" class="data-table"> <tr> - <th>Guid</th> <th>Source</th> <th>Namespace</th> <th>Name</th> + <th>Implicit Tags</th> <th></th> </tr> @foreach(var tagDef in tagDefinitions) { + TagSourceMap.TryGetValue(tagDef.Source, out var source); <tr data-guid="@tagDef.Guid"> - <td>@tagDef.Guid</td> - <td>@tagDef.Source</td> + <td>@(source ?? tagDef.Source.ToString())</td> <td>@tagDef.Namespace</td> <td>@tagDef.Name</td> + <td></td> <td> <a href="javascript:showDeleteDialog('@tagDef.Guid');" @onclick=@(() => PromptToDelete(tagDef))> Delete @@ -54,6 +55,10 @@ </Dialog> @code { + private readonly Dictionary<TagSource, string> TagSourceMap = new() { + { TagSource.UserTag, "User" } + }; + private Dialog createTagDialog; private Dialog deleteTagDialog; diff --git a/Pages/ViewMedia.razor b/Pages/ViewMedia.razor index 8436159..68cc313 100644 --- a/Pages/ViewMedia.razor +++ b/Pages/ViewMedia.razor @@ -1,6 +1,7 @@ @page "/ViewMedia" @inject IDbContextFactory<HBContext> dbFactory @inject ITagService tagService +@implements IDisposable <PageTitle>@title</PageTitle> @@ -14,7 +15,7 @@ <div id="metadata-fileinfo"> <p>Title: <i>@(@media.ShortDescription ?? "None")</i></p> <p>Description: <i>@(media.LongDescription ?? "None")</i></p> - <p>Upload history</p> + <p id="metadata-uploadhistory">Upload history</p> <hr /> <table class="data-table"> <tr> @@ -84,9 +85,11 @@ private TagSelectDialog tagDialog; private MediaTagTable mediaTagTable; + private HBContext db; + protected override void OnInitialized() { - using var db = dbFactory.CreateDbContext(); - media = db.Media.AsNoTracking().First(m => m.Guid == MediaId); + db = dbFactory.CreateDbContext(); + media = db.Media.First(m => m.Guid == MediaId); if(media is null) throw new ArgumentException("Media not found!"); @@ -101,6 +104,18 @@ } private void SetIngest(bool ingest) { + var ingestTag = db.TagDefinitions + .First(td => td.Source == TagSource.Internal && td.Name == "ingest"); + + if(!ingest) + media.Tags.RemoveAll(t => t.TagDefinition.Guid == ingestTag.Guid); + else + if(!media.IsIngest) + media.Tags.Add(new(ingestTag)); + + db.SaveChanges(); StateHasChanged(); } + + public void Dispose() => db.Dispose(); } diff --git a/Pages/ViewMedia.razor.css b/Pages/ViewMedia.razor.css index abf8e08..8a59041 100644 --- a/Pages/ViewMedia.razor.css +++ b/Pages/ViewMedia.razor.css @@ -24,6 +24,10 @@ div#metadata-fileinfo > table td { font-size: 8pt; } +p#metadata-uploadhistory { + margin-top: 30px; +} + div.button-container { display: flex; justify-content: flex-end; diff --git a/Services/TagService.cs b/Services/TagService.cs index e0dcf64..e0d1072 100644 --- a/Services/TagService.cs +++ b/Services/TagService.cs @@ -4,7 +4,9 @@ using Microsoft.EntityFrameworkCore; namespace HyperBooru.Services; public interface ITagService { + public void AddTag(Guid obj, Guid tagDef); public void AddTag(HBObject obj, TagDefinition tagDef); + public void RemoveTag(Guid obj, Guid tagDef); public void RemoveTag(HBObject obj, TagDefinition tagDef); public void AddImplicitTag(TagDefinition tagDef, TagDefinition implicitTagDef); public void RemoveImplicitTag(TagDefinition tagDef, TagDefinition implicitTagDef); @@ -18,22 +20,23 @@ public class TagService : ITagService { public TagService(IDbContextFactory<HBContext> dbFactory) => this.dbFactory = dbFactory; - public void AddTag(HBObject obj, TagDefinition tagDef) { + public void AddTag(Guid obj, Guid tagDef) { using var db = dbFactory.CreateDbContext(); - db.Entry(obj).State = EntityState.Unchanged; - bool alreadyTagged = obj.Tags - .Select(t => t.TagDefinition.Guid) - .Contains(tagDef.Guid); + var tag = db.TagDefinitions.First(td => td.Guid == tagDef); - obj.Tags.Add(new() { - TagDefinition = tagDef, - Target = obj - }); + db.Objects + .Where(o => !o.Tags.Select(t => t.TagDefinition.Guid).Contains(tagDef)) + .FirstOrDefault(o => o.Guid == obj)? + .Tags + .Add(new(tag)); db.SaveChanges(); } + public void AddTag(HBObject obj, TagDefinition tagDef) => + AddTag(obj.Guid, tagDef.Guid); + public void RemoveTag(Guid obj, Guid tagDef) { using var db = dbFactory.CreateDbContext(); @@ -19,4 +19,9 @@ public class Tag : HBObject { public virtual TagDefinition TagDefinition { get; set; } public DateTime CreateTime { get; set; } = DateTime.Now; public virtual HBObject Target { get; set; } + + public Tag() {} + + public Tag(TagDefinition tagDef) => + this.TagDefinition = tagDef; }
\ No newline at end of file diff --git a/wwwroot/css/site.css b/wwwroot/css/site.css index 65147c2..21f9a94 100644 --- a/wwwroot/css/site.css +++ b/wwwroot/css/site.css @@ -1,5 +1,5 @@ #blazor-error-ui { - background: lightyellow; + background: #555; bottom: 0; box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2); display: none; |
