159 lines
5.9 KiB
C#
159 lines
5.9 KiB
C#
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static Sarand.Net.Standard.Base.Bus.SaErrorStateItem;
|
|
|
|
namespace Sacs.Web.Components
|
|
{
|
|
//=======================================================================
|
|
public class SaModel : SacsDisposable
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private readonly List<String> infoMessages = new List<string>();
|
|
private readonly List<String> warningMessages = new List<string>();
|
|
private readonly List<String> errorMessages = new List<string>();
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Protected construction - use derived objects.
|
|
/// </summary>
|
|
protected SaModel()
|
|
{
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
}
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
// Properties.
|
|
public bool DebugMode { get; } = SacsSystem.DebugMode;
|
|
public bool OK { get { return !HasMessages(); } }
|
|
public virtual bool ProhibitCache { get; set; } = true;
|
|
public String PageCaption { get; set; }
|
|
public bool IncludeGoogleAnalytics { get; set; } = false;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Page configuration.
|
|
// Views can be registered to avoid multiple inclusions in a page.
|
|
private HashSet<String> registeredViews = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
|
|
/// <summary>
|
|
/// Views can be registered to avoid multiple inclusions in a page.
|
|
/// <para>Only generate view content if this method returns true.</para>
|
|
/// </summary>
|
|
public bool RegisterView(String viewName)
|
|
{
|
|
#if DEBUG
|
|
if (registeredViews.Contains(viewName))
|
|
SaLogger.Warn(this.GetType().Name + ".RegisterView View already registered: " + viewName);
|
|
#endif
|
|
return registeredViews.Add(viewName);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Version parameter for includes.
|
|
public String HrefVersion
|
|
{
|
|
get
|
|
{
|
|
DateTime versionDate = SacsSystem.AssemblyBuildAt;
|
|
if (!DateUtil.IsValid(versionDate) || SacsSystem.DebugMode)
|
|
versionDate = DateTime.Now;
|
|
return DateUtil.Format(versionDate, "yyyyMMdd" /*+ (SacsSystem.Debug ? "HHmm" : "")*/);
|
|
}
|
|
}
|
|
// Additional includes.
|
|
public HashSet<String> AdditionalFiles_CSS { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
public HashSet<String> AdditionalFiles_JS { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
//-------------------------------------------------------------------
|
|
// Redirection locations.
|
|
public class RedirectLocationData
|
|
{
|
|
// Properties populated in Controller_Base.OnActionExecuted.
|
|
public String Base { get; set; }
|
|
public String Home { get; set; }
|
|
public String Home_SessionExpired { get; set; }
|
|
}
|
|
public RedirectLocationData RedirectLocation { get; } = new RedirectLocationData();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Paging properties.
|
|
public class DataPaging
|
|
{
|
|
private DbQuery.Paging paging { get; } = new DbQuery.Paging();
|
|
public void PopulatePagingTo(DbQuery.Paging resultsPaging)
|
|
{
|
|
resultsPaging.PageLength = paging.PageLength;
|
|
resultsPaging.PageNo = paging.PageNo;
|
|
}
|
|
public bool IsPaged { get { return paging.Paged; } }
|
|
public uint PageNo { get { return paging.PageNo; } set { paging.PageNo = value; } }
|
|
public String PageNoValue { get { return paging.PageNo.ToString(); } set { paging.PageNo = (uint)StringUtil.ToInt(value); } }
|
|
public ushort PageLength { get { return paging.PageLength; } set { paging.PageLength = value; } }
|
|
public String PageLengthValue { get { return paging.PageLength.ToString(); } set { paging.PageLength = (ushort)StringUtil.ToShort(value); } }
|
|
public uint PageCount { get { return (PageLength > 0 ? (uint)Math.Ceiling((decimal)ItemTotal / paging.PageLength) : 0); } }
|
|
public ushort ItemCount { get; set; }
|
|
private ulong itemTotal = 0;
|
|
public ulong ItemTotal
|
|
{
|
|
get { return itemTotal; }
|
|
set
|
|
{
|
|
itemTotal = value;
|
|
// Check whether the total number of items is less than the current page and reduce page no if needed.
|
|
if (IsPaged && (PageNo - 1) * PageLength > itemTotal)
|
|
PageNo = (uint)((itemTotal - 1) / PageLength + 1);
|
|
}
|
|
}
|
|
}
|
|
public DataPaging Paging { get; } = new DataPaging();
|
|
public void PopulatePagingTo(DbQuery.Paging resultsPaging)
|
|
{
|
|
Paging.PopulatePagingTo(resultsPaging);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Messages.
|
|
//-------------------------------------------------------------------
|
|
public bool HasMessages()
|
|
{
|
|
return infoMessages.Count > 0 || warningMessages.Count > 0 || errorMessages.Count > 0;
|
|
}
|
|
public List<String> InfoMessages { get { return new List<string>(infoMessages); } }
|
|
public List<String> WarningMessages { get { return new List<string>(warningMessages); } }
|
|
public List<String> ErrorMessages { get { return new List<string>(errorMessages); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Form messages.
|
|
public void AddInfoMessage(String message)
|
|
{
|
|
if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return;
|
|
infoMessages.Add(message);
|
|
}
|
|
public void AddWarningMessage(String message)
|
|
{
|
|
if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return;
|
|
warningMessages.Add(message);
|
|
}
|
|
public void AddErrorMessage(String message)
|
|
{
|
|
if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return;
|
|
errorMessages.Add(message);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|