/// <summary> /// Adds one or more CSS class names to the component. You can specify multiple class names delimited by spaces. If any /// class name already exists, it is not added twice. /// </summary> /// <typeparam name="T"> A component type. </typeparam> /// <param name="comp"> The comp to act on. </param> /// <param name="classNames"> . </param> /// <returns> A T. </returns> public static T AddClassName <T>(this T comp, params string[] classNames) where T : class, IWebViewComponent { if (classNames.Length > 0) { // (note: individual string items may already be space delimited, and will be parsed later using {string}.Split()) var classNamesStr = string.Join(" ", WebViewComponent.TrimClassNames(classNames)); if (!string.IsNullOrEmpty(classNamesStr)) { var currentClasses = comp.GetClassNames(); if (currentClasses.Length > 0) { // ... need to merge with existing values ... var itemsToAdd = classNamesStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(c => c.ToLower()); comp.Attributes["class"] = string.Join(" ", currentClasses.Union(itemsToAdd)); } else { comp.Attributes["class"] = classNamesStr; // (there is no existing class to merge with, so just set the values now) } } } return(comp); }
/// <summary> /// Sets the given attributes on this component by pulling all public fields and properties on the supplied object. /// </summary> /// <typeparam name="T"> A component type. </typeparam> /// <param name="comp"> The component to act on. </param> /// <param name="attributes"> A dictionary list of attributes to set. </param> /// <returns> this component instance. </returns> public static T SetAttributes <T>(this T comp, object attributes) where T : class, IWebViewComponent { if (attributes != null) { foreach (var prop in attributes.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { comp.Attributes[WebViewComponent.ModelMemberNameToAttributeName(prop.Name)] = prop.GetValue(attributes).ND(); } foreach (var field in attributes.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)) { comp.Attributes[WebViewComponent.ModelMemberNameToAttributeName(field.Name)] = field.GetValue(attributes).ND(); } } return(comp); }