Autocomplete.cs
- Last UpdatedMar 12, 2021
- 4 minute read
namespace Kendo.Custom.Controls
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Xml;
using Kendo.Custom.ControlProperty;
using Skelta.Forms.Core;
using Skelta.Forms.Core.Controls;
using Skelta.Forms.Core.Interfaces;
[DesignerControl(992201, CoreDesigner.NextGenDesigner, DesignerCategory.Custom)]
[SuppressedPropertiesNextGen("DefaultValue;ReadOnly;EnableResponsiveView")]
public class AutoComplete : BaseDataExternalControl
{
public enum FilterConditionsEnum
{
startswith = 0,
endswith,
contains
}
public override BaseControl GetNewInstanceForClone()
{
var auto = new AutoComplete
{
Enabled = this.Enabled,
Visible = this.Visible,
ToolTip = this.ToolTip,
AutoCompleteLength = this.AutoCompleteLength,
Filter = this.Filter,
HeaderTemplate = this.HeaderTemplate,
DataTextField = this.DataTextField,
MessageWhenEmpty = this.MessageWhenEmpty,
Data = this.Data,
URL = this.URL
};
return auto;
}
/// <summary>
/// Get the Display name
/// </summary>
public static string TypeDisplayName
{
get
{
return "AutoComplete";
}
}
public override string TypeAssociation
{
get
{
return "Custom";
}
}
/// <summary>
/// Set the MinLength for TextInput
/// </summary>
[DesignerProperty(PropertyOrder.MinimumLength, Categories.Validation, CoreDesigner.NextGenDesigner)]
[Binding(882930, "Skelta.Forms.Core.CommonObjects.IntegerBindingProperty")]
public int AutoCompleteLength
{
get;
set;
}
/// <summary>
/// Get or set the FilterConditionsEnum position
/// </summary>
[Scriptable(550600)]
[DesignerProperty(553000, Categories.Appearance, CoreDesigner.FormDesigner)]
[DesignerProperty(PropertyOrder.LabelPosition + 1, Categories.Appearance, CoreDesigner.NextGenDesigner)]
[Binding(883100, "Skelta.Forms.Core.CommonObjects.CommonEnumBindingProperty")]
public FilterConditionsEnum Filter
{
get;
set;
}
/// <summary>
/// Get or set the FilterConditionsEnum position
/// </summary>
[Scriptable(550600)]
[DesignerProperty(376567, Categories.Behaviour, typeof(PopupProperty), "")]
[Binding(883100, "Skelta.Forms.Core.CommonObjects.CommonStringBindingProperty")]
public string URL
{
get;
set;
}
/// <summary>
/// Set the MinLength for TextInput
/// </summary>
[DesignerProperty(PropertyOrder.MinimumLength + 1, Categories.Appearance, CoreDesigner.NextGenDesigner)]
[Binding(882930, "Skelta.Forms.Core.CommonObjects.CommonStringBindingProperty")]
public string HeaderTemplate
{
get;
set;
}
string _dataTextField = "name";
/// <summary>
/// Set the MinLength for TextInput
/// </summary>
[DesignerProperty(PropertyOrder.MinimumLength + 2, Categories.Appearance, CoreDesigner.NextGenDesigner)]
[Binding(882940, "Skelta.Forms.Core.CommonObjects.CommonStringBindingProperty")]
public string DataTextField
{
get;
set;
}
public string DataFormXml
{
get
{
// Returns the resource form definition XML present in the assembly
return Helper.GetResourceTextFile("DataXml.xml");
}
}
[DesignerProperty(PropertyOrder.MinimumLength + 2, Categories.Behaviour, typeof(GridProperty), "DataFormXml")]
public Dictionary<string, string> Data
{
get;
set;
}
public override string SetDisplayValueHandlerMethod
{
get
{
return Common.ViewUtilitiesVarName + ".updateDisplayValue;";
}
}
public override string GetViewModelDefinition()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(base.GetViewModelDefinition());
if (string.IsNullOrEmpty(this.URL) && this.Data != null && this.Data.Count > 0)
{
var jArray = this.Data.Aggregate("[", (current, eachItem) => current + ("{Name:'" + eachItem.Value + "',SurName:'" + eachItem.Key + "'},"));
jArray = jArray.TrimEnd(',');
jArray += "]";
stringBuilder.AppendLine("_" + this.ParentForm.Id + "." + this.Id + ".items=ko.observableArray(" + jArray + ");");
}
else
{
stringBuilder.AppendLine("_" + this.ParentForm.Id + "." + this.Id + ".items=autoComplete.getData(_" + this.ParentForm.Id + "." + this.Id + ");");
}
return stringBuilder.ToString();
}
/// <summary>
/// Method to render the control, How it has to look in designer as well as in the preview
/// </summary>
/// <returns>Controls view </returns>
public override string GetControlHtml()
{
var stringBuilder = new StringBuilder();
var dataBindAttribute = new StringBuilder();
var headerTemplate = string.IsNullOrEmpty(this.HeaderTemplate) ? string.Empty : " headerTemplate:'" + this.HeaderTemplate + "', ";
var dataTextField = string.IsNullOrEmpty(this.DataTextField) ? string.Empty : " dataTextField:'" + this.DataTextField + "', ";
var placeHolder = string.IsNullOrEmpty(this.MessageWhenEmpty) ? string.Empty : " placeholder:'" + this.MessageWhenEmpty + "', ";
dataBindAttribute.Append(" " + DataBindAttribute + "=" + "\"kendoAutoComplete : {" + placeHolder + headerTemplate + dataTextField + "data: " + this.Id + ".items, enabled: " + this.Id + "._enable, filter:'" + this.Filter + "', readOnly: " + this.Id + "._readOnly, value: " + this.Id + "._value, minLength:" + this.AutoCompleteLength + "}\"");
stringBuilder.Append("<input " + DataAttributes.Role + "=\"skAutoComplete\" " + this.GetControlIdAttribute + dataBindAttribute);
stringBuilder.Append(GetCommonViewForControls(this));
stringBuilder.Append("/>");
return stringBuilder.ToString();
}
/// <summary>
/// Method to add the custom JS files associated with the custom control
/// </summary>
private void AddJsFiles()
{
if (string.IsNullOrEmpty(this.TopLevelForm.RuntimeJS["kendo.autocomplete.min.js"]))
{
// "this.TopLevelForm.RuntimeCss" is the collection which holds your custom CSS files.
// It is a key-value pair collection where the key is the file name and the value is the file path(absolute or relative).
// Ensure the JS files are present in the "BPMUITemplates\Default\NextGenForms\js" folder. Otherwise, it will not be rendered.
this.TopLevelForm.RuntimeJS.Add("kendo.autocomplete.min.js", "js/kendo.autocomplete.min.js");
}
if (string.IsNullOrEmpty(this.TopLevelForm.RuntimeJS["knockout-kendo.autocompletebinding.min.js"]))
{
// "this.TopLevelForm.RuntimeCss" is the collection which holds your custom CSS files.
// It is a key-value pair collection where the key is the file name and the value is the file path(absolute or relative).
// Ensure the JS files are present in the "BPMUITemplates\Default\NextGenForms\js" folder. Otherwise, it will not be rendered.
this.TopLevelForm.RuntimeJS.Add("knockout-kendo.autocompletebinding.min.js", "js/knockout-kendo.autocompletebinding.min.js");
}
if (string.IsNullOrEmpty(this.TopLevelForm.RuntimeJS["AutoComplete.js"]))
{
this.TopLevelForm.RuntimeJS.Add("AutoComplete.js", "js/AutoComplete.js");
}
}
/// <summary>
/// Adding JS and CSS files and setting the view model value
/// </summary>
/// <returns></returns>
public override string GetView()
{
AddJsFiles();
return base.GetView();
}
public override IBindingProperty GetBindingClassObject(string fullyQualifiedClassPath)
{
try
{
return (IBindingProperty)Activator.CreateInstance(Type.GetType(fullyQualifiedClassPath));
}
catch (Exception ex)
{
return null;
}
}
internal static string GetCommonViewForControls(object control)
{
var stringBuilder = new StringBuilder();
var cultureInfo = CultureInfo.InvariantCulture;
var baseControl = control as BaseControl;
if (baseControl.ToolTip.Length > 0)
{
stringBuilder.Append(" title = \"" + HttpUtility.HtmlEncode(baseControl.ToolTip) + "\" ");
}
if (baseControl.Height > 0 && baseControl.Width > 0)
{
stringBuilder.Append(" style=\"height:" + baseControl.Height.ToString(cultureInfo) + "px;width:" + baseControl.Width.ToString(cultureInfo) + "px;max-width:" + baseControl.Width.ToString(cultureInfo) + "px;\" ");
}
else
{
if (baseControl.Height > 0)
{
stringBuilder.Append(" style=\"height:" + baseControl.Height.ToString(cultureInfo) + "px;\" ");
}
else if (baseControl.Width > 0)
{
stringBuilder.Append(" style=\"width:" + baseControl.Width.ToString(cultureInfo) + "px;max-width:" + baseControl.Width.ToString(cultureInfo) + "px;\" ");
}
}
return stringBuilder.ToString();
}
}
}