using Abp.Localization; using VberZero; using VberZero.Tools.StringModel; namespace VberAdmin.Web.Models.Input; public abstract class VmInputBase { protected static string L(string name) { var str = LocalizationHelper.GetSource(VzConsts.LocalizationSourceName).GetString(name); return str; } #region 字段属性 protected VmInputBase(string name, string displayName, VmInputType inputType) { Name = name; DisplayName = displayName; InputType = inputType; IsHidden = false; IsReadOnly = false; IsDisabled = false; IsRequired = false; Other = ""; DefaultClass = "form-control form-control-sm form-control-solid"; DefaultValue = ""; LabelClass = "fs-6 fw-bolder mb-1"; InputClass = ""; } public string ModalId { get; set; } public bool UseId { get; set; } public string Name { get; set; } public string DisplayName { get; set; } public VmInputType InputType { get; set; } public string Style { get; set; } public string Class { get; set; } public virtual string DefaultClass { get; set; } protected string _other; public virtual string Other { get { if (MinLength != null) _other += $" minlength={MinLength}"; if (MaxLength != null) _other += $" maxlength={MaxLength}"; if (Min != null) _other += $" min={Min}"; if (Max != null) _other += $" max={Max}"; return _other; } set => _other = value; } public int? MinLength { get; set; } public int? MaxLength { get; set; } public int? Min { get; set; } public int? Max { get; set; } public string DefaultValue { get; set; } private string PlaceholderPrefix => InputType == VmInputType.Select ? L("PlaceholderSelectHeader") : L("PlaceholderHeader"); public string PlaceholderStr => Placeholder.Empty() ? $"{PlaceholderPrefix}{DisplayName}..." : Placeholder; public string Placeholder { get; set; } public bool IsHidden { get; set; } public bool IsRequired { get; set; } public bool IsDisabled { get; set; } public bool IsReadOnly { get; set; } public string Help { get; set; } protected virtual string ClassStr => $"{(IsHidden ? "" : DefaultClass)} {Class} {(IsRequired ? "required" : "")} {(DefaultValue.Empty() ? "" : "default-value")} {(IsDisabled ? "disabled" : "")} {(IsReadOnly ? "readonly" : "")}"; public string LabelClass { get; set; } public string InputClass { get; set; } public string LayoutClass { get; set; } public string Special { get; set; } public virtual string InputBaseString => $" class=\"{ClassStr}\" {(UseId ? $"id =\"{Name}\"" : "")} name=\"{Name}\" {(DefaultValue.Empty() ? "" : $"value=\"{DefaultValue}\"")} placeholder=\"{PlaceholderStr}\" {(IsDisabled ? "disabled" : "")} {(IsReadOnly ? "readonly" : "")} {Other} {(Style.Empty() ? "" : $"style=\"{Style}\"")}"; #endregion 字段属性 #region 方法 public virtual VmInputBase WithHide() { IsHidden = true; return this; } public virtual VmInputBase WithReadonly() { IsReadOnly = true; return this; } public virtual VmInputBase WithDisabled() { IsDisabled = true; return this; } public virtual VmInputBase WithRequired() { IsRequired = true; return this; } public virtual VmInputBase WithDefaultValue(string value) { DefaultValue = value; return this; } public virtual VmInputBase WithPlaceholder(string placeholder) { Placeholder = placeholder; return this; } public virtual VmInputBase WithStyle(string style) { Style = style; return this; } public virtual VmInputBase WithClass(string @class) { Class = @class; return this; } public virtual VmInputBase WithOther(string other) { Other += other; return this; } public virtual VmInputBase WithHelp(string help) { Help = help; return this; } public virtual VmInputBase WithSpecial(string special) { Special = special; return this; } public virtual VmInputBase WithMaxLength(int maxLength) { MaxLength = maxLength; return this; } public virtual VmInputBase WithMinLength(int minLength) { MinLength = minLength; return this; } public virtual VmInputBase WithHidden() { IsHidden = true; return this; } public virtual VmInputBase WithLayoutClass(string layout) { LayoutClass = layout; return this; } #endregion 方法 #region 配合查询模态框使用 /// /// 查询模态框ID /// public string QueryModalId { get; set; } public string DivSearchClass { get; set; } protected string _target; public string Target => string.IsNullOrEmpty(_target) ? DefaultTarget ?? "" : _target.StartsWith(".") ? _target : _target.StartsWith("#") ? _target : $"#{_target}"; public string DefaultTarget { get; set; } public string Clear { get; set; } /// /// 添加搜索按钮 /// /// /// public virtual VmInputBase WithQueryIcon(string modalId) { QueryModalId = modalId; return this; } /// /// 添加搜索按钮 /// /// /// /// /// public virtual VmInputBase WithQueryIcon(string modalId, string target, string clear = "") { QueryModalId = modalId; _target = target; if (clear.NotEmpty()) WithQueryClear(clear, true); return this; } /// /// 添加搜索范围Class /// /// /// public virtual VmInputBase WithQueryClass(string @class) { DivSearchClass = @class; return this; } /// /// 添加搜索按钮 /// /// /// /// /// public virtual VmInputBase WithQueryIconWithClass(string modalId, string @class, string clear = "") { QueryModalId = modalId; _target = "." + @class; DivSearchClass = @class; if (clear.NotEmpty()) WithQueryClear(clear, true); return this; } /// /// 配置需要清空的INPUT name /// /// /// /// public virtual VmInputBase WithQueryClear(string clear, bool isDefault = false) { Clear = isDefault ? $"{clear}No,{clear}Name" : clear; return this; } #endregion 配合查询模态框使用 }