/// <summary> /// Called by the framework when the component needs to be rendered as HTML. /// </summary> /// <param name="model">The model being rendered by the component.</param> /// <returns>The component rendered as HTML.</returns> public async Task <string> RenderAsync(object model) { Type enumType = model.GetType(); // get all enum values in supported list List <int> supported = new List <int>(); object obj = GetSiblingProperty <object>($"{PropertyName}_Supported"); IEnumerable ienumerable = obj as IEnumerable; IEnumerator ienum = ienumerable.GetEnumerator(); while (ienum.MoveNext()) { supported.Add(Convert.ToInt32(ienum.Current)); } List <SelectionItem <int> > list = new List <SelectionItem <int> >(); EnumData enumData = ObjectSupport.GetEnumData(enumType); bool showValues = UserSettings.GetProperty <bool>("ShowEnumValue"); bool showSelect = PropData.GetAdditionalAttributeValue("ShowSelect", false); if (showSelect) { list.Add(new SelectionItem <int> { Text = __ResStr("enumSelect", "(select)"), Value = 0, Tooltip = __ResStr("enumPlsSelect", "Please select one of the available options"), }); } foreach (EnumDataEntry entry in enumData.Entries) { int enumVal = Convert.ToInt32(entry.Value); if (enumVal == 0 && showSelect) { continue; } if (supported.Contains(enumVal)) { string caption = entry.Caption; if (showValues) { caption = __ResStr("enumFmt", "{0} - {1}", enumVal, caption); } list.Add(new SelectionItem <int> { Text = caption, Value = enumVal, Tooltip = entry.Description, }); } } return(await DropDownListIntComponent.RenderDropDownListAsync(this, (int)model, list, "yt_enumsupported")); }
/// <summary> /// Given an enum type, returns a collection suitable for use in a DropDownList component. /// </summary> /// <param name="enumType">The type of the enum.</param> /// <param name="showSelect">Defines whether the first entry "(select)" should be generated.</param> /// <returns>Returns a collection suitable for use in a DropDownList component.</returns> public static List <SelectionItem <int> > GetEnumSelectionList(Type enumType, bool showSelect = false) { List <SelectionItem <int> > list = new List <SelectionItem <int> >(); EnumData enumData = ObjectSupport.GetEnumData(enumType); bool showValues = UserSettings.GetProperty <bool>("ShowEnumValue"); if (showSelect) { list.Add(new SelectionItem <int> { Text = __ResStr("enumSelect", "(select)"), Value = 0, Tooltip = __ResStr("enumPlsSelect", "Please select one of the available options"), }); } foreach (EnumDataEntry entry in enumData.Entries) { int enumVal = Convert.ToInt32(entry.Value); if (enumVal == 0 && showSelect) { continue; } string caption = entry.Caption; if (showValues) { caption = __ResStr("enumFmt", "{0} - {1}", enumVal, caption); } list.Add(new SelectionItem <int> { Text = caption, Value = enumVal, Tooltip = entry.Description, }); } return(list); }