|
@@ -1,9 +1,11 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Diagnostics.CodeAnalysis;
|
|
|
using System.Linq;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using InABox.Core;
|
|
|
+using InABox.WPF;
|
|
|
using NPOI.Util.Collections;
|
|
|
|
|
|
namespace InABox.DynamicGrid
|
|
@@ -74,12 +76,105 @@ namespace InABox.DynamicGrid
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public bool EditProperties(Type type, DFLayoutFieldProperties item)
|
|
|
+ private void CreateMenu(ContextMenu parent, string header, Type type)
|
|
|
+ {
|
|
|
+ parent.AddItem(header, null, type, (itemtype) =>
|
|
|
+ {
|
|
|
+ if(DynamicVariableUtils.CreateAndEdit(Item, Items, itemtype, out var variable))
|
|
|
+ {
|
|
|
+ SaveItem(variable);
|
|
|
+ Refresh(false, true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void DoAdd()
|
|
|
+ {
|
|
|
+ var menu = new ContextMenu();
|
|
|
+
|
|
|
+ foreach(var fieldType in DFUtils.GetFieldTypes())
|
|
|
+ {
|
|
|
+ var caption = fieldType.GetCaption();
|
|
|
+ if (string.IsNullOrWhiteSpace(caption))
|
|
|
+ {
|
|
|
+ caption = CoreUtils.Neatify(fieldType.Name);
|
|
|
+ }
|
|
|
+ CreateMenu(menu, caption, fieldType);
|
|
|
+ }
|
|
|
+
|
|
|
+ menu.IsOpen = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*protected override DigitalFormVariable LoadItem(CoreRow row)
|
|
|
+ {
|
|
|
+ return Items.FirstOrDefault(r => r.ID.Equals(row.Get<DigitalFormVariable, Guid>(c => c.ID)));
|
|
|
+ }*/
|
|
|
+
|
|
|
+ protected override void DoEdit()
|
|
|
+ {
|
|
|
+ if (!SelectedRows.Any())
|
|
|
+ return;
|
|
|
+ var variable = LoadItem(SelectedRows.First());
|
|
|
+ var properties = variable.CreateProperties();
|
|
|
+ if (DynamicVariableUtils.EditProperties(Item, Items, properties.GetType(), properties))
|
|
|
+ {
|
|
|
+ variable.SaveProperties(properties);
|
|
|
+ SaveItem(variable);
|
|
|
+ Refresh(false, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void DoDelete()
|
|
|
+ {
|
|
|
+ var rows = SelectedRows.ToArray();
|
|
|
+
|
|
|
+ if (rows.Any())
|
|
|
+ if (CanDeleteItems(rows))
|
|
|
+ if (MessageBox.Show("Are you sure you want to delete this variable? This will all cause data associated with this variable to be lost.\n(If you want to just hide the variable, set it to 'Hidden' instead.)", "Confirm Deletion", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
|
|
+ {
|
|
|
+ DeleteItems(rows);
|
|
|
+ SelectedRows = Array.Empty<CoreRow>();
|
|
|
+ OnChanged?.Invoke(this);
|
|
|
+ Refresh(false, true);
|
|
|
+ SelectItems(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override bool FilterRecord(CoreRow row)
|
|
|
+ {
|
|
|
+ return ShowHidden || !row.Get<DigitalFormVariable, bool>(x => x.Hidden);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class DynamicVariableUtils
|
|
|
+ {
|
|
|
+ public static bool CreateAndEdit(
|
|
|
+ DigitalForm form, IList<DigitalFormVariable> variables,
|
|
|
+ Type fieldType,
|
|
|
+ [NotNullWhen(true)] out DigitalFormVariable? variable)
|
|
|
+ {
|
|
|
+ var fieldBaseType = fieldType.GetSuperclassDefinition(typeof(DFLayoutField<>));
|
|
|
+ if (fieldBaseType != null)
|
|
|
+ {
|
|
|
+ var propertiesType = fieldBaseType.GetGenericArguments()[0];
|
|
|
+ var properties = Activator.CreateInstance(propertiesType) as DFLayoutFieldProperties;
|
|
|
+ if (DynamicVariableUtils.EditProperties(form, variables, propertiesType, properties))
|
|
|
+ {
|
|
|
+ variable = new DigitalFormVariable();
|
|
|
+ variable.SaveProperties(fieldType, properties);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ variable = null;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool EditProperties(DigitalForm form, IList<DigitalFormVariable> variables, Type type, DFLayoutFieldProperties item)
|
|
|
{
|
|
|
var editor = new DynamicEditorForm(type);
|
|
|
if (item is DFLayoutLookupFieldProperties)
|
|
|
{
|
|
|
- editor.OnFormCustomiseEditor += LookupEditor_OnFormCustomiseEditor;
|
|
|
+ editor.OnFormCustomiseEditor += (sender, items, column, editor) => LookupEditor_OnFormCustomiseEditor(sender, variables, items, column, editor);
|
|
|
editor.OnEditorValueChanged += (sender, name, value) =>
|
|
|
{
|
|
|
var result = DynamicGridUtils.UpdateEditorValue(new[] { item }, name, value);
|
|
@@ -95,7 +190,7 @@ namespace InABox.DynamicGrid
|
|
|
}
|
|
|
|
|
|
var propertiesEditor = grid.FindEditor(nameof(DFLayoutLookupFieldProperties.AdditionalProperties));
|
|
|
- if(propertiesEditor is MultiLookupEditorControl multi && multi.EditorDefinition is ComboMultiLookupEditor combo)
|
|
|
+ if (propertiesEditor is MultiLookupEditorControl multi && multi.EditorDefinition is ComboMultiLookupEditor combo)
|
|
|
{
|
|
|
combo.Clear();
|
|
|
multi.Configure();
|
|
@@ -106,7 +201,7 @@ namespace InABox.DynamicGrid
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- editor.OnFormCustomiseEditor += Editor_OnFormCustomiseEditor;
|
|
|
+ editor.OnFormCustomiseEditor += (sender, items, column, editor) => Editor_OnFormCustomiseEditor(sender, variables, column, editor);
|
|
|
}
|
|
|
editor.Items = new BaseObject[] { item };
|
|
|
editor.OnDefineLookups += o =>
|
|
@@ -118,9 +213,9 @@ namespace InABox.DynamicGrid
|
|
|
// so that I can get access to the "AppliesTo" property, and thus the list of properties that can be updated
|
|
|
// Nothing to see here, I promise!
|
|
|
CoreTable? values;
|
|
|
- if(o.ColumnName == "Property")
|
|
|
+ if (o.ColumnName == "Property")
|
|
|
{
|
|
|
- values = def.Values(colname, new object[] { Item });
|
|
|
+ values = def.Values(colname, new object[] { form });
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -132,12 +227,12 @@ namespace InABox.DynamicGrid
|
|
|
return editor.ShowDialog() == true;
|
|
|
}
|
|
|
|
|
|
- private void Editor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
|
|
|
+ private static void Editor_OnFormCustomiseEditor(IDynamicEditorForm sender, IList<DigitalFormVariable> vars, DynamicGridColumn column, BaseEditor editor)
|
|
|
{
|
|
|
if ((column.ColumnName == "Expression" || column.ColumnName == "ColourExpression") && editor is ExpressionEditor exp)
|
|
|
{
|
|
|
var variables = new List<string>();
|
|
|
- foreach (var variable in Items)
|
|
|
+ foreach (var variable in vars)
|
|
|
{
|
|
|
//variables.Add(variable.Code);
|
|
|
foreach (var col in variable.GetVariableColumns())
|
|
@@ -150,99 +245,17 @@ namespace InABox.DynamicGrid
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void LookupEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
|
|
|
+ private static void LookupEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, IList<DigitalFormVariable> vars, object[] items, DynamicGridColumn column, BaseEditor editor)
|
|
|
{
|
|
|
- if(column.ColumnName == "Filter" && editor is FilterEditor fe)
|
|
|
+ if (column.ColumnName == "Filter" && editor is FilterEditor fe)
|
|
|
{
|
|
|
- var properties = items[0] as DFLayoutLookupFieldProperties;
|
|
|
+ var properties = (items[0] as DFLayoutLookupFieldProperties)!;
|
|
|
var lookupType = properties.LookupType;
|
|
|
var entityType = CoreUtils.GetEntityOrNull(lookupType);
|
|
|
fe.Type = entityType;
|
|
|
}
|
|
|
- Editor_OnFormCustomiseEditor(sender, items, column, editor);
|
|
|
- }
|
|
|
-
|
|
|
- private void CreateMenu(ContextMenu parent, string header, Type type)
|
|
|
- {
|
|
|
- var menu = new MenuItem();
|
|
|
- menu.Header = header;
|
|
|
- menu.Tag = type;
|
|
|
- menu.Click += (o, e) =>
|
|
|
- {
|
|
|
- var itemtype = (o as MenuItem).Tag as Type;
|
|
|
-
|
|
|
- var fieldBaseType = itemtype.GetSuperclassDefinition(typeof(DFLayoutField<>));
|
|
|
- if(fieldBaseType != null)
|
|
|
- {
|
|
|
- var propertiesType = fieldBaseType.GetGenericArguments()[0];
|
|
|
- var properties = Activator.CreateInstance(propertiesType) as DFLayoutFieldProperties;
|
|
|
- if (EditProperties(propertiesType, properties))
|
|
|
- {
|
|
|
- var variable = CreateItem();
|
|
|
- variable.SaveProperties(itemtype, properties);
|
|
|
- SaveItem(variable);
|
|
|
- Refresh(false, true);
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- parent.Items.Add(menu);
|
|
|
- }
|
|
|
-
|
|
|
- protected override void DoAdd()
|
|
|
- {
|
|
|
- var menu = new ContextMenu();
|
|
|
-
|
|
|
- foreach(var fieldType in DFUtils.GetFieldTypes())
|
|
|
- {
|
|
|
- var caption = fieldType.GetCaption();
|
|
|
- if (string.IsNullOrWhiteSpace(caption))
|
|
|
- {
|
|
|
- caption = CoreUtils.Neatify(fieldType.Name);
|
|
|
- }
|
|
|
- CreateMenu(menu, caption, fieldType);
|
|
|
- }
|
|
|
-
|
|
|
- menu.IsOpen = true;
|
|
|
- }
|
|
|
-
|
|
|
- /*protected override DigitalFormVariable LoadItem(CoreRow row)
|
|
|
- {
|
|
|
- return Items.FirstOrDefault(r => r.ID.Equals(row.Get<DigitalFormVariable, Guid>(c => c.ID)));
|
|
|
- }*/
|
|
|
-
|
|
|
- protected override void DoEdit()
|
|
|
- {
|
|
|
- if (!SelectedRows.Any())
|
|
|
- return;
|
|
|
- var variable = LoadItem(SelectedRows.First());
|
|
|
- var properties = variable.CreateProperties();
|
|
|
- if (EditProperties(properties.GetType(), properties))
|
|
|
- {
|
|
|
- variable.SaveProperties(properties);
|
|
|
- SaveItem(variable);
|
|
|
- Refresh(false, true);
|
|
|
- }
|
|
|
+ Editor_OnFormCustomiseEditor(sender, vars, column, editor);
|
|
|
}
|
|
|
|
|
|
- protected override void DoDelete()
|
|
|
- {
|
|
|
- var rows = SelectedRows.ToArray();
|
|
|
-
|
|
|
- if (rows.Any())
|
|
|
- if (CanDeleteItems(rows))
|
|
|
- if (MessageBox.Show("Are you sure you want to delete this variable? This will all cause data associated with this variable to be lost.\n(If you want to just hide the variable, set it to 'Hidden' instead.)", "Confirm Deletion", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
|
|
- {
|
|
|
- DeleteItems(rows);
|
|
|
- SelectedRows = Array.Empty<CoreRow>();
|
|
|
- OnChanged?.Invoke(this);
|
|
|
- Refresh(false, true);
|
|
|
- SelectItems(null);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- protected override bool FilterRecord(CoreRow row)
|
|
|
- {
|
|
|
- return ShowHidden || !row.Get<DigitalFormVariable, bool>(x => x.Hidden);
|
|
|
- }
|
|
|
}
|
|
|
}
|