@page "/admin/menu-lang"
@using Syncfusion.EJ2.Blazor.Grids
@using Newtonsoft.Json.Linq
@using System.Linq;
@using Microsoft.AspNetCore.Hosting;
@using System.IO;
@inject TreeViewLanguageContext _treeViewLangContext
@inject IWebHostEnvironment _webHostEnviroment
@attribute [Authorize(Roles = "Admin")]
<div>
<EjsGrid ID="menu_lang_grid" DataSource="@TreeViewLanguages" ModelType="@Model" AllowPaging="true" Height="600" Toolbar="@(new List<string> { "Add", "Edit", "Delete" })" OnActionComplete="@OnActionComplete">
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Dialog" ShowDeleteConfirmDialog="true">
<Template>
@{
var lang = (context as TreeViewLanguage);
<input class="e-input e-field" name="Description" value=@lang.Description type="text" required />
<br />
<Syncfusion.EJ2.Blazor.DropDowns.EjsDropDownList ID="LanguageFlag" Width="200" CssClass="dropdown" DataSource=@DropDownLanguages Value="@lang.LanguageFlag" AllowFiltering="true">
<DropDownListFieldSettings Text="Text" />
</Syncfusion.EJ2.Blazor.DropDowns.EjsDropDownList>
<img style="background-color:white;width:30px;height:30px;padding:4px;" src="@($"images/flags/{@lang.LanguageFlag}.png")" />
<br />
}
</Template>
</GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(TreeViewLanguage.Id) Width="0" IsPrimaryKey="true" />
<GridColumn Field=@nameof(TreeViewLanguage.Description) HeaderText="@Resources.DESCRIPTION" Width="300" />
<GridColumn Field=@nameof(TreeViewLanguage.LanguageFlag) HeaderText="@Resources.LANGUAGEFLAG" Width="50" />
<GridColumn HeaderText="@Resources.LANGUAGFLAGIMAGE" Width="50">
<Template>
@{
var lang = (context as TreeViewLanguage);
<div>
<img style="background-color:white;width:30px;height:30px;padding:4px;" src="@($"images/flags/{@lang.LanguageFlag}.png")" />
</div>
}
</Template>
</GridColumn>
</GridColumns>
</EjsGrid>
</div>
@code {
public List<TreeViewLanguage> TreeViewLanguages { get; set; }
public List<DropDownListItems> DropDownLanguages { get; set; }
public TreeViewLanguage Model = new TreeViewLanguage();
protected override void OnInit()
{
TreeViewLanguages = _treeViewLangContext.treeviewlanguages.OrderBy(p => p.SortOrder).ToList();
FillDropDownListLanguages();
base.OnInit();
}
void FillDropDownListLanguages()
{
DropDownLanguages = new List<DropDownListItems>();
var path = _webHostEnviroment.WebRootPath + @"\images\flags";
var images = Directory.GetFiles(path);
foreach (var item in images)
{
DropDownLanguages.Add(new DropDownListItems() { Text = Path.GetFileNameWithoutExtension(item) });
}
}
async void OnActionComplete(ActionEventArgs args)
{
TreeViewLanguage item = null;
int sortOrder;
if (args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.BeginEdit || args.Data == null)
{
return;
}
if (args.Data.GetType() == typeof(JObject))
{
item = ((JObject)args.Data).ToObject<TreeViewLanguage>();
}
else
{
item = ((JArray)args.Data)[0].ToObject<TreeViewLanguage>();
}
switch (args.RequestType)
{
case Syncfusion.EJ2.Blazor.Grids.Action.Save:
if (args.Action == "add")
{
if (_treeViewLangContext.treeviewlanguages.Count() > 0)
{
sortOrder = _treeViewLangContext.treeviewlanguages.Select(p => p.SortOrder).Max();
}
else
{
sortOrder = 10;
}
item.Id = Guid.NewGuid().ToString();
item.SortOrder = sortOrder + 10;
await _treeViewLangContext.AddAsync(item);
await _treeViewLangContext.SaveChangesAsync();
}
if (args.Action == "edit")
{
var dbItem = _treeViewLangContext.treeviewlanguages.FirstOrDefault(p => p.Id == item.Id);
dbItem.Description = item.Description;
dbItem.LanguageFlag = item.LanguageFlag;
dbItem.SortOrder = item.SortOrder;
_treeViewLangContext.Update(dbItem);
await _treeViewLangContext.SaveChangesAsync();
}
break;
case Syncfusion.EJ2.Blazor.Grids.Action.Delete:
var delItem = _treeViewLangContext.treeviewlanguages.FirstOrDefault(p => p.Id == item.Id);
_treeViewLangContext.Remove(delItem);
await _treeViewLangContext.SaveChangesAsync();
TreeViewLanguages = _treeViewLangContext.treeviewlanguages.OrderBy(p => p.SortOrder).ToList();
StateHasChanged();
break;
default:
break;
}
}
}