123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace InABox.DynamicGrid
- {
- public abstract class DynamicFormLayoutGrid<T> : DynamicOneToManyGrid<DigitalForm, DigitalFormLayout> where T : Entity, IRemotable, IPersistent, new()
- {
- private readonly BitmapImage design = Properties.Resources.design.AsBitmapImage();
- public DynamicFormLayoutGrid()
- {
- Options.AddRange(DynamicGridOption.RecordCount);
- ActionColumns.Add(new DynamicImageColumn(DesignImage, DesignClick));
- //AddButton("Design", PRSDesktop.Resources.design.AsBitmapImage(), DesignClick);
- HiddenColumns.Add(x => x.Layout);
- AddButton("Auto Generate", null, AutoGenerate_Click);
- AddButton("Duplicate", null, Duplicate_Click);
- }
- private bool Duplicate_Click(Button btn, CoreRow[] rows)
- {
- if (!rows.Any()) return false;
- SaveItems(rows.Select(x =>
- {
- var layout = x.ToObject<DigitalFormLayout>();
- layout.ID = Guid.Empty;
- return layout;
- }).ToArray());
- return true;
- }
- private bool AutoGenerate_Click(Button btn, CoreRow[] rows)
- {
- var menu = new ContextMenu();
- menu.AddItem("Desktop Layout", null, AddDesktop_Click);
- menu.AddItem("Mobile Layout", null, AddMobile_Click);
- menu.IsOpen = true;
- return false;
- }
- private BitmapImage? DesignImage(CoreRow? row)
- {
- return row != null ? design : null;
- }
- private void AddMobile_Click()
- {
- var item = CreateItem();
- item.Layout = DFLayout.GenerateAutoMobileLayout(GetVariables()).SaveLayout();
- item.Type = DFLayoutType.Mobile;
- if (EditItems(new[] { item }))
- {
- SaveItem(item);
- Refresh(false, true);
- OnChanged?.Invoke(this);
- }
- }
- private void AddDesktop_Click()
- {
- var item = CreateItem();
- item.Layout = DFLayout.GenerateAutoDesktopLayout(GetVariables()).SaveLayout();
- item.Type = DFLayoutType.Desktop;
- if (EditItems(new[] { item }))
- {
- SaveItem(item);
- Refresh(false, true);
- OnChanged?.Invoke(this);
- }
- }
- private DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>? GetVariableGrid()
- => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>)
- as DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>;
- private List<DigitalFormVariable> GetVariables()
- => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
- private void Design(DigitalFormLayout layout)
- {
- var variables = GetVariables();
- var newVariables = new List<DigitalFormVariable>();
- var form = new DynamicFormDesignWindow
- {
- Type = layout.Type
- };
- form.OnCreateVariable += (fieldType) =>
- {
- if (DynamicVariableUtils.CreateAndEdit(Item, GetVariables(), fieldType, out var variable))
- {
- newVariables.Add(variable);
- return variable;
- }
- return null;
- };
- form.LoadLayout(layout, variables);
- form.Initialize();
- if (form.ShowDialog() == true)
- {
- layout.Layout = form.SaveLayout();
- SaveItem(layout);
- var grid = GetVariableGrid();
- if (grid is not null)
- {
- grid.SaveItems(newVariables.ToArray());
- grid.Refresh(false, true);
- }
- }
- }
- private bool DesignClick(CoreRow? row)
- {
- if (row == null)
- return false;
- Design(LoadItem(row));
- return false;
- }
- //public override void SaveItem(DigitalFormLayout item)
- //{
- // bool bActive = item.Active;
- // foreach (var other in Items.Where(x=>(x != item) && (x.Type == item.Type)))
- // {
- // if (item.Active)
- // {
- // if (other.Active)
- // other.Active = false;
- // }
- // else
- // bActive = bActive || other.Active;
- // }
- // if (!bActive)
- // item.Active = true;
- // base.SaveItem(item);
- //}
- protected override void DoDoubleClick(object sender)
- {
- DesignClick(SelectedRows.FirstOrDefault());
- }
- }
- }
|