소스 검색

ComboMultiLookup editor

Kenric Nugteren 2 년 전
부모
커밋
2e387d188a

+ 1 - 1
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutFieldProperties.cs

@@ -64,7 +64,7 @@ namespace InABox.Core
             SetProperty("Expression", Expression);
         }
 
-        protected class PropertyLookupGenerator : LookupGenerator<object>
+        private class PropertyLookupGenerator : LookupGenerator<object>
         {
             public PropertyLookupGenerator(object[] items) : base(items)
             {

+ 26 - 1
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutLookupField/DFLayoutLookupFieldProperties.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 
 namespace InABox.Core
 {
@@ -20,7 +21,8 @@ namespace InABox.Core
         /// <summary>
         /// A list of comma-separated property names to also pull with the lookup.
         /// </summary>
-        [TextBoxEditor(ToolTip = "A comma-separated list of property names.")]
+        //[TextBoxEditor(ToolTip = "A comma-separated list of property names.")]
+        [ComboMultiLookupEditor(typeof(AdditionalPropertyLookupGenerator))]
         public string AdditionalProperties
         {
             get => string.Join(',', AdditionalPropertiesList);
@@ -74,5 +76,28 @@ namespace InABox.Core
             SetProperty("Filter", Filter);
             SetProperty("AdditionalProperties", AdditionalProperties);
         }
+
+
+        private class AdditionalPropertyLookupGenerator : LookupGenerator<object>
+        {
+            public AdditionalPropertyLookupGenerator(object[] items) : base(items)
+            {
+            }
+
+            protected override void DoGenerateLookups()
+            {
+                if (!(Items?.FirstOrDefault() is DFLayoutLookupFieldProperties properties))
+                    return;
+
+                if (!CoreUtils.TryGetEntity(properties.LookupType, out var type))
+                    return;
+
+                var props = CoreUtils.PropertyList(type, x => x.GetCustomAttribute<DoNotSerialize>() == null, true);
+                foreach (var prop in props.Keys)
+                {
+                    AddValue(prop, prop);
+                }
+            }
+        }
     }
 }

+ 18 - 0
InABox.Core/Editors/ComboMultiLookupEditor.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace InABox.Core
+{
+    public class ComboMultiLookupEditor : BaseComboEditor
+    {
+        public ComboMultiLookupEditor(Type generator) : base(generator)
+        {
+        }
+
+        protected override BaseEditor DoClone()
+        {
+            return CloneBaseComboEditor();
+        }
+    }
+}

+ 16 - 1
InABox.DynamicGrid/DynamicEditorGrid.xaml.cs

@@ -17,6 +17,7 @@ using InABox.Clients;
 using InABox.Configuration;
 using InABox.Core;
 using InABox.WPF;
+using NPOI.SS.Formula.Functions;
 
 namespace InABox.DynamicGrid
 {
@@ -260,6 +261,15 @@ namespace InABox.DynamicGrid
             lookup.OnLookupsDefined += sender => { OnLookupsDefined?.Invoke(sender); };
         }
 
+        private void ConfigureMultiLookupEditor(MultiLookupEditorControl lookup, DynamicGridColumn column, ComboMultiLookupEditor editor)
+        {
+            if (editor.LookupWidth != int.MaxValue)
+                lookup.Width = editor.LookupWidth;
+            lookup.ColumnName = column.ColumnName;
+            lookup.OnDefineLookups += sender => { OnDefineLookups?.Invoke(sender); };
+            lookup.OnLookupsDefined += sender => { OnLookupsDefined?.Invoke(sender); };
+        }
+
         private void ConfigureCheckListEditor(CheckListBoxEditorControl checks, DynamicGridColumn column, CheckListEditor editor)
         {
             checks.Width = editor.LookupWidth;
@@ -349,6 +359,11 @@ namespace InABox.DynamicGrid
                                 ConfigureComboEditor(lookupControl, column, comboEditor);
                         }
 
+                        else if(Editor is MultiLookupEditorControl multiLookupEditor && editor is ComboMultiLookupEditor comboMultiLookup)
+                        {
+                            ConfigureMultiLookupEditor(multiLookupEditor, column, comboMultiLookup);
+                        }
+
                         else if (Editor is CheckListBoxEditorControl checkBoxControl && editor is CheckListEditor checkListEditor)
                         {
                             ConfigureCheckListEditor(checkBoxControl, column, checkListEditor);
@@ -382,7 +397,6 @@ namespace InABox.DynamicGrid
                 }
         }
 
-
         private bool LoadLayout(string xaml)
         {
             if (!string.IsNullOrWhiteSpace(xaml))
@@ -502,6 +516,7 @@ namespace InABox.DynamicGrid
                         PopupEditor => ClientFactory.IsSupported(((PopupEditor)editor).Type) ? new PopupEditorControl() : null,
                         CodePopupEditor => ClientFactory.IsSupported(((CodePopupEditor)editor).Type) ? new CodePopupEditorControl() : null,
                         EnumLookupEditor or ComboLookupEditor => new LookupEditorControl(),
+                        ComboMultiLookupEditor => new MultiLookupEditorControl(),
                         EmbeddedImageEditor imageEditor => new EmbeddedImageEditorControl
                         {
                             MaximumHeight = imageEditor.MaximumHeight,

+ 250 - 0
InABox.DynamicGrid/Editors/MultiLookupEditorControl.cs

@@ -0,0 +1,250 @@
+using InABox.Core;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Controls;
+using System.Windows.Media;
+using System.Windows;
+using Syncfusion.Windows.Tools.Controls;
+using Syncfusion.Data.Extensions;
+
+namespace InABox.DynamicGrid
+{
+    public class MultiLookupEditorControl : DynamicEditorControl<object>, ILookupEditorControl
+    {
+        private ComboBoxAdv Editor;
+
+        private CoreTable LookupTable;
+
+        public MultiLookupEditorControl()
+        {
+            OtherColumns = new Dictionary<string, string>();
+            Lookups = new Dictionary<object, string>();
+            Width = int.MaxValue;
+        }
+
+        public int Width { get; set; }
+
+        public Dictionary<object, string> Lookups { get; set; }
+
+        public Dictionary<string, string> OtherColumns { get; }
+
+        private List<Tuple<object?, string>> Items;
+
+        public override void Configure()
+        {
+            base.Configure();
+            Editor.HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left;
+            if (!Width.Equals(int.MaxValue))
+                Editor.Width = Width;
+            Editor.IsEnabled = false;
+
+            OnDefineLookups?.Invoke(this);
+        }
+
+        public void LoadLookups(CoreTable values)
+        {
+            var keycol = ColumnName.Split('.').Last();
+            //var prefix = string.Join(".", ColumnName.Split('.').Reverse().Skip(1).Reverse());
+
+            /*foreach (var col in values.Columns)
+                if (!string.Equals(col.ColumnName, keycol) && !string.Equals(col.ColumnName, "Display"))
+                    if (!OtherColumns.ContainsKey(col.ColumnName))
+                        OtherColumns[col.ColumnName] = string.IsNullOrWhiteSpace(prefix) ? col.ColumnName : string.Join(".", prefix, col.ColumnName);*/
+
+
+            var value = RetrieveValue();
+
+            //Lookups = lookups;
+            LookupTable = values;
+
+            Lookups.Clear();
+
+            Items = new List<Tuple<object?, string>>();
+
+            //if (!IsEnumEditor())
+            //    Editor.Items.Add("");
+
+            foreach (var row in values.Rows)
+            {
+                Items.Add(new(row[keycol], $"{row["Display"]}"));
+                Lookups[row[keycol]] = string.Format("{0}", row["Display"]);
+            }
+            Editor.ItemsSource = Items;
+            Editor.DisplayMemberPath = "Item2";
+
+            var sel = values.Rows.FirstOrDefault(r => r[keycol].Equals(value));
+            //if (IsEnumEditor())
+                Editor.SelectedIndex = sel != null ? sel.Index : 0;
+            //else
+            //    Editor.SelectedIndex = sel != null ? sel.Index + 1 : 0;
+
+            //foreach (var key in lookups.Keys)
+            //    Editor.Items.Add(lookups[key]);
+            //if ((value != null) && Lookups.ContainsKey(value))
+            //    Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
+
+            Editor.IsEnabled = EditorDefinition.Editable == Editable.Enabled;
+
+            OnLookupsDefined?.Invoke(this);
+        }
+
+        public event OnDefineLookup OnDefineLookups;
+
+        public event OnLookupsDefined OnLookupsDefined;
+
+        public override int DesiredHeight()
+        {
+            return 25;
+        }
+
+        public override void SetFocus()
+        {
+            Editor.Focus();
+        }
+
+        private bool IsEnumEditor()
+        {
+            return EditorDefinition is EnumLookupEditor;
+        }
+
+        public List<Button> Buttons { get; private set; }
+
+        protected override FrameworkElement CreateEditor()
+        {
+            var dock = new DockPanel();
+
+
+            Buttons = CreateButtons(out var DisableEditor);
+            foreach (var button in Buttons)
+            {
+                button.SetValue(DockPanel.DockProperty, Dock.Right);
+                dock.Children.Add(button);
+                dock.Width += button.Width + 5;
+            }
+
+            Editor = new ComboBoxAdv
+            {
+                VerticalAlignment = VerticalAlignment.Stretch,
+                VerticalContentAlignment = VerticalAlignment.Center,
+                HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left,
+                IsEditable = false,
+                AllowMultiSelect = true
+            };
+
+            Editor.SelectionChanged += Combobox_SelectionChanged;
+            Editor.IsEnabled = false;
+
+            if (DisableEditor)
+            {
+                Editor.Background = new SolidColorBrush(Colors.Silver);
+                Editor.IsEnabled = false;
+            }
+
+            dock.Children.Add(Editor);
+
+            return dock;
+        }
+
+        private void Combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            if (!Editor.IsEnabled)
+                return;
+
+            /*if (LookupTable != null)
+            {
+                var keycol = ColumnName.Split('.').Last();
+
+                var row = LookupTable.Rows.FirstOrDefault(r => object.Equals(r[keycol], RetrieveValue()));
+                foreach (var field in LookupTable.Columns.Where(x => OtherColumns.ContainsKey(x.ColumnName)))
+                {
+                    var othervalue = row?[field.ColumnName];
+                    OtherValues[OtherColumns[field.ColumnName]] = othervalue;
+                }
+            }*/
+
+            CheckChanged();
+        }
+
+        public event OnUpdateOtherEditorHandler OnUpdateOtherEditor;
+
+        public override int DesiredWidth()
+        {
+            return int.MaxValue;
+        }
+
+        protected override object? RetrieveValue()
+        {
+            if (Editor.SelectedIndex >= 0 && Editor.SelectedIndex < Lookups.Keys.Count)
+                return string.Join(',', Editor.SelectedItems.Cast<Tuple<object?, string>>().Select(x => x.Item1));
+
+            return "";
+        }
+
+        protected override void UpdateValue(object? value)
+        {
+            if (Editor == null)
+                return;
+
+            if (value == null)
+            {
+                Editor.SelectedItem = null;
+                return;
+            }
+
+            if (!Loaded && !Editor.IsEnabled)
+            {
+                Lookups.Clear();
+                Lookups[value] = "Loading";
+                Editor.Items.Clear();
+                Editor.Items.Add("");
+                Editor.Items.Add("Loading...");
+                Editor.SelectedIndex = IsEnumEditor() ? 0 : 1;
+                return;
+            }
+
+            if(value is not string str)
+            {
+                return;
+            }
+
+            IEnumerable<Tuple<object?, string>> selected;
+            if (!string.IsNullOrWhiteSpace(str))
+            {
+                var values = str.Split(',');
+                selected = values.Select(x => Items.FirstOrDefault(y => y.Item1?.ToString() == x)).Where(x => x is not null)!;
+            }
+            else
+            {
+                selected = Enumerable.Empty<Tuple<object?, string>>();
+            }
+
+            Editor.SelectedItems = selected.ToObservableCollection();
+            /*if (Lookups.ContainsKey(value))
+            {
+                if (IsEnumEditor())
+                    Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
+                else
+                    Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value) + 1;
+            }
+            else
+            {
+                Editor.SelectedIndex = 0;
+            }*/
+        }
+
+        public override void SetColor(Color color)
+        {
+            //Editor.Background = new SolidColorBrush(color);
+        }
+
+        public override void SetEnabled(bool enabled)
+        {
+            base.SetEnabled(enabled);
+
+            Editor.IsEnabled = enabled;
+        }
+    }
+}