public virtual IViewModel BuildViewModel(Type type, IModel modelData, IContextModel contextModel = null) { IViewModel viewModel = null; viewModel = _resolver.ResolveModel(type, modelData); viewModel.ModelData = modelData; ProcessViewModel(viewModel, type, contextModel); return(viewModel); }
public virtual IViewModel BuildViewModelByAttribute <T>(IModel modelData, IContextModel contextModel = null) where T : IModelAttribute { IViewModel result = null; Type type = FindViewModelByAttribute <T>(modelData); if (type != null) { _logger.Debug("Building ViewModel based on type " + type.FullName); result = BuildViewModel(type, modelData, contextModel); } return(result); }
public async Task <string> Transform(string content, string key, IContextModel model) { var html = Regex.Replace(content, @"^@page(\r\n|\r|\n)", ""); if (!string.IsNullOrEmpty(html)) { html = await m_RazorEngine.CompileRenderStringAsync( key, html, model); } return(html); }
private void ProcessViewModel(IViewModel viewModel, Type type, IContextModel contextModel) { //PropertyInfo[] props = type.GetProperties(); var props = _resolver.GetModelProperties(type); IPropertyAttribute propAttribute; object propertyValue = null; foreach (var prop in props) { propAttribute = prop.PropertyAttribute; //prop.GetCustomAttributes(typeof(FieldAttributeBase), true).FirstOrDefault() as FieldAttributeBase; if (propAttribute != null) // this property is an IPropertyAttribute { IEnumerable values; //ILinkablePropertyAttribute is implemented, we have to pass context data to property. if (propAttribute is ILinkablePropertyAttribute) { values = ((ILinkablePropertyAttribute)propAttribute).GetPropertyValues(viewModel.ModelData, prop, this, contextModel); //delegate work to the Property Attribute object itself. Allows for custom attribute types to easily be added } else { values = propAttribute.GetPropertyValues(viewModel.ModelData, prop, this); //delegate work to the Property Attribute object itself. Allows for custom attribute types to easily be added } if (values != null) { try { propertyValue = GetPropertyValue(prop, values); prop.Set(viewModel, propertyValue); } catch (Exception e) { if (e is TargetException || e is InvalidCastException) { throw new PropertyTypeMismatchException(prop, propAttribute, propertyValue); } else { throw e; } } } } } }
/// <summary> /// Finds the root page from this data /// </summary> /// <param name="model">Context model</param> /// <returns>Root page</returns> /// <remarks>Menu's root page can be defined with root-page attribute specifying the url of the root page</remarks> public static IContextPage GetRootPage(IContextModel model) { var rootPageUrl = model.Data.GetOrDefault <string>(ROOT_PAGE_ATT); if (!string.IsNullOrEmpty(rootPageUrl)) { var rootPage = PageHelper.GetAllPages(model.Site.MainPage).Prepend(model.Site.MainPage) .FirstOrDefault(p => string.Equals(p.Url, rootPageUrl, StringComparison.CurrentCultureIgnoreCase)); if (rootPage != null) { return(rootPage); } else { throw new RootPageNotFoundException(rootPageUrl); } } else { return(model.Site.MainPage); } }
public override IEnumerable GetPresentationValues(IList<IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel) { return cps.Where(cp => { bool result = false; if (cp.ComponentTemplate != null && cp.ComponentTemplate.MetadataFields != null && cp.ComponentTemplate.MetadataFields.ContainsKey("region")) { var region = cp.ComponentTemplate.MetadataFields["region"].Values.Cast<string>().FirstOrDefault(); if (!string.IsNullOrEmpty(region) && region.Contains(Region)) { result = true; } } return result; }) .Select(cp => { object model = null; if (ComplexTypeMapping != null) { model = factory.BuildMappedModel(cp, ComplexTypeMapping); } else model = factory.BuildViewModel(cp, contextModel); return model; }); }
public override IEnumerable GetPresentationValues(IList<IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel) { return cps.Where(cp => { bool result = false; if (cp.ComponentTemplate != null && cp.ComponentTemplate.MetadataFields != null && cp.ComponentTemplate.MetadataFields.ContainsKey("view")) { var view = cp.ComponentTemplate.MetadataFields["view"].Values.FirstOrDefault(); if (view != null && view.StartsWith(ViewPrefix)) { result = true; } } return result; }) .Select(cp => { object model = null; if (ComplexTypeMapping != null) { model = factory.BuildMappedModel(cp, ComplexTypeMapping); } else model = factory.BuildViewModel((cp)); return model; }); }
public override IEnumerable GetPresentationValues(IList<IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel) { return cps.Select(cp => { object model = null; if (ComplexTypeMapping != null) { model = factory.BuildMappedModel(cp, ComplexTypeMapping); } else model = factory.BuildViewModel((cp)); return model; }); }
public IEnumerable GetPropertyValues(IModel modelData, IModelProperty property, IViewModelFactory builder, IContextModel contextModel) { _contextModel = contextModel; return(base.GetPropertyValues(modelData, property, builder)); }
public DefaultContextResolver() { _contextModel = new ContextModel(); }
public override IEnumerable GetPresentationValues(IList <IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel) { return(cps.Where(cp => { bool result = false; if (cp.ComponentTemplate != null && cp.ComponentTemplate.MetadataFields != null && cp.ComponentTemplate.MetadataFields.ContainsKey("region")) { var region = cp.ComponentTemplate.MetadataFields["region"].Values.Cast <string>().FirstOrDefault(); if (!string.IsNullOrEmpty(region) && region.Contains(Region)) { result = true; } } return result; }) .Select(cp => { object model = null; if (ComplexTypeMapping != null) { model = factory.BuildMappedModel(cp, ComplexTypeMapping); } else { model = factory.BuildViewModel(cp, contextModel); } return model; })); }
//Really leaving the bulk of the work to implementer -- they must both find out if the CP matches this attribute and then construct an object with it /// <summary> /// When overriden in a derived class, this gets a set of values representing Component Presentations of a Page /// </summary> /// <param name="cps">Component Presentations for the Page Model</param> /// <param name="propertyType">Actual return type of the Property</param> /// <param name="factory">A View Model factory</param> /// <returns>The Property value</returns> public abstract IEnumerable GetPresentationValues(IList <IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel);
public override IEnumerable GetPresentationValues(IList <IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel) { return(cps.Select(cp => { object model = null; if (ComplexTypeMapping != null) { model = factory.BuildMappedModel(cp, ComplexTypeMapping); } else { model = factory.BuildViewModel((cp)); } return model; })); }
public virtual IViewModel BuildViewModel(IModel modelData, IContextModel contextModel = null) { return(BuildViewModelByAttribute <IModelAttribute>(modelData, contextModel)); }
//Really leaving the bulk of the work to implementer -- they must both find out if the CP matches this attribute and then construct an object with it /// <summary> /// When overriden in a derived class, this gets a set of values representing Component Presentations of a Page /// </summary> /// <param name="cps">Component Presentations for the Page Model</param> /// <param name="propertyType">Actual return type of the Property</param> /// <param name="factory">A View Model factory</param> /// <returns>The Property value</returns> public abstract IEnumerable GetPresentationValues(IList<IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel);
public override IEnumerable GetPresentationValues(IList <IComponentPresentation> cps, IModelProperty property, IViewModelFactory factory, IContextModel contextModel) { return(cps.Where(cp => { bool result = false; if (cp.ComponentTemplate != null && cp.ComponentTemplate.MetadataFields != null && cp.ComponentTemplate.MetadataFields.ContainsKey("view")) { var view = cp.ComponentTemplate.MetadataFields["view"].Values.FirstOrDefault(); if (view != null && view.StartsWith(ViewPrefix)) { result = true; } } return result; }) .Select(cp => { object model = null; if (ComplexTypeMapping != null) { model = factory.BuildMappedModel(cp, ComplexTypeMapping); } else { model = factory.BuildViewModel((cp)); } return model; })); }
public IEnumerable GetPropertyValues(IModel modelData, IModelProperty property, IViewModelFactory builder, IContextModel contextModel) { _contextModel = contextModel; return base.GetPropertyValues(modelData, property, builder); }