149 lines
5.6 KiB
C#
149 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Sarand.Net.Standard.Base.ItemOptions
|
|
{
|
|
//=======================================================================
|
|
public class SaItemOptionDfn
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Item Option Action.
|
|
public delegate bool ItemOptionActionFunction (SaItemOption itemOption, List<SaItemOption> itemOptions);
|
|
public class Action
|
|
{
|
|
public Action (String caption, ItemOptionActionFunction itemOptionActionFn)
|
|
{
|
|
Caption = caption;
|
|
ItemOptionActionFn = itemOptionActionFn;
|
|
}
|
|
public String Caption { get; set; } = "";
|
|
public ItemOptionActionFunction ItemOptionActionFn { get; set; } = null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item Option display options.
|
|
public class Display
|
|
{
|
|
public String Group { get; set; } = "";
|
|
public String Caption { get; set; } = "";
|
|
/// <summary>Sort sequence for display.</summary>
|
|
public int Sequence { get; set; } = 0;
|
|
/// <summary>Allowed contexts ('Value' aganst each context defines default value).</summary>
|
|
public List<CodeDesc> Contexts { get; } = new List<CodeDesc>();
|
|
/// <summary>Allowed values.</summary>
|
|
public List<CodeDesc> Values { get; } = new List<CodeDesc>();
|
|
// Maximum value is defined by database.
|
|
public int MaxLength { get; set; } = 0;
|
|
// User must pick from the supplied list.
|
|
public bool ExclusiveValues { get; set; } = true;
|
|
public bool RightText { get; set; } = false;
|
|
public bool Uppercase { get; set; } = false;
|
|
public bool Lowercase { get; set; } = false;
|
|
public bool Numeric { get; set; } = false;
|
|
public bool Password { get; set; } = false;
|
|
public bool Encrypted { get; set; } = false;
|
|
public bool ReadOnly { get; set; } = false;
|
|
public decimal MinValue { get; set; } = 0.0m;
|
|
public decimal MaxValue { get; set; } = 0.0m;
|
|
public bool RequiresContext { get; set; } = false;
|
|
public bool PopulateWithContext { get; set; } = false;
|
|
/// <summary>Defines available contexts and other rules, but should not be shown in list.</summary>
|
|
public bool NonDisplayTemplate { get; set; } = false;
|
|
/// <summary>User can add additional options for other contexts.</summary>
|
|
public bool CanAddOption { get; set; } = false;
|
|
/// <summary>Use can remove the option from the list.</summary>
|
|
public bool CanRemoveOption { get; set; } = false;
|
|
/// <summary>Browse for filename/folder etc.</summary>
|
|
public Action BrowseAction { get; set; } = null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
/// <summary>Option Name - identifying code.</summary>
|
|
public String Name { get; set; } = "";
|
|
public String DisplayName { get { return StringUtil.OnEmpty(DisplayOptions.Caption,Name); } }
|
|
public String DefaultValue { get; set; } = "";
|
|
public String DefaultNotes { get; set; } = "";
|
|
public Display DisplayOptions { get; } = new Display();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Utilities.
|
|
public String GetContextDesc (String context)
|
|
{
|
|
return DisplayOptions.Contexts.GetDescForCode(context, context);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
public static class SaItemOptionDfnList
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// List value properties.
|
|
public static List<String> GetNames (this List<SaItemOptionDfn> values)
|
|
{
|
|
List<String> names = new List<string>();
|
|
foreach (var itemOptionDfn in values)
|
|
{
|
|
if (!StringUtil.IsEmpty(itemOptionDfn.Name) && !names.ContainsCI(itemOptionDfn.Name))
|
|
names.Add(itemOptionDfn.Name);
|
|
}
|
|
return names;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Retrieve available Item Option Definitions, ie. those that aren't fully defined in the list of item options.
|
|
public static List<SaItemOptionDfn> GetAvailableItemOptionDfns (this List<SaItemOptionDfn> values, List<SaItemOption> itemOptions)
|
|
{
|
|
List<SaItemOptionDfn> availableItemOptionDfns = new List<SaItemOptionDfn>();
|
|
foreach (SaItemOptionDfn itemOptionDfn in values)
|
|
{
|
|
if (itemOptionDfn.DisplayOptions.Contexts.Count == 0)
|
|
{
|
|
// Option has no context.
|
|
if (!itemOptions.IsOptionDefined(itemOptionDfn.Name, ""))
|
|
availableItemOptionDfns.Add(itemOptionDfn);
|
|
}
|
|
else
|
|
{
|
|
List<String> definedOptionContexts = itemOptions.GetContextsForName(itemOptionDfn.Name);
|
|
if (itemOptionDfn.DisplayOptions.Contexts.Exists(item => !definedOptionContexts.ContainsCI(item.Code)))
|
|
availableItemOptionDfns.Add(itemOptionDfn);
|
|
}
|
|
}
|
|
return availableItemOptionDfns;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Retrieve Item Option Definition
|
|
public static SaItemOptionDfn GetDfnForItemOption (this List<SaItemOptionDfn> values, SaItemOption itemOption)
|
|
{
|
|
return values.GetItemOptionDfnByName(itemOption.Name);
|
|
}
|
|
public static SaItemOptionDfn GetItemOptionDfnByName (this List<SaItemOptionDfn> values, String name)
|
|
{
|
|
if (StringUtil.IsEmpty(name)) return null;
|
|
foreach (var itemOptionDfn in values)
|
|
{
|
|
if (itemOptionDfn.Name.EqualsCI(name))
|
|
return itemOptionDfn;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Sorting
|
|
public enum SortBy
|
|
{
|
|
Name,
|
|
DisplayName,
|
|
}
|
|
public static void Sort (this List<SaItemOptionDfn> values)
|
|
{
|
|
}
|
|
}
|
|
//=======================================================================
|
|
}
|