Ejemplo n.º 1
0
        public HtmlDocument Example(ExampleHtmlRequest exampleHtmlRequest)
        {
            var modelPath         = exampleHtmlRequest.Model ?? typeof(ExampleViewModel).FullName + "-Person";
            var tags              = new List <HtmlTag>();
            var propertyPath      = new List <string>(modelPath.Split('-'));
            var rootModelTypeName = propertyPath[0];

            tags.Add(new HtmlTag("h3").AddClass("viewmodel").Text(propertyPath.Join(".")));
            propertyPath.RemoveAt(0);

            Type scannedModelType     = getTypeFromName(rootModelTypeName);
            var  scannedModelInstance = createInstance(scannedModelType);
            var  tagGeneratorType     = typeof(TagGenerator <>).MakeGenericType(scannedModelType);
            var  tagGenerator         = (ITagGenerator)_serviceLocator.GetInstance(tagGeneratorType);

            tagGenerator.SetModel(scannedModelInstance);

            var propertyChainParts = new List <PropertyInfo>();

            while (propertyPath.Count > 0)
            {
                var parentPropertyInfo = scannedModelType.GetProperty(propertyPath[0]);
                propertyChainParts.Add(parentPropertyInfo);
                var currentModelType = parentPropertyInfo.PropertyType;
                var currentModel     = createInstance(currentModelType);
                setProperty(parentPropertyInfo, scannedModelInstance, currentModel);

                scannedModelType     = currentModelType;
                scannedModelInstance = currentModel;
                propertyPath.RemoveAt(0);
            }


            var modelProperties  = scannedModelType.GetProperties();
            var propertiesToLink = modelProperties.Where(p => !TypeDescriptor.GetConverter(p.PropertyType).CanConvertFrom(typeof(string)));
            var propertiesToShow = modelProperties.Except(propertiesToLink);

            // show links to deeper properties
            var linkList = new HtmlTag("ul").AddClass("subproperties");

            foreach (var propertyInfo in propertiesToLink)
            {
                var linkTag = new LinkTag("", _examplePageUrl + "?model=" + modelPath + "-" + propertyInfo.Name);
                linkTag.Append(new HtmlTag("code").Text(getPropertySourceCode(propertyInfo)));
                var listItem = new HtmlTag("li").Append(linkTag);
                linkList.Append(listItem);
            }
            if (linkList.Children.Count > 0)
            {
                tags.Add(linkList);
            }

            // show examples
            populateInstance(scannedModelInstance, propertiesToShow);
            foreach (var propertyInfo in propertiesToShow)
            {
                var property = propertyChainParts.Count > 0 ?
                               (Accessor) new PropertyChain(propertyChainParts.Concat(new[] { propertyInfo }).Select(x => new PropertyValueGetter(x)).ToArray()) :
                               new SingleProperty(propertyInfo);

                var propertyExpression = "x => x." + property.PropertyNames.Join(".");

                var propertySource = getPropertySourceCode(propertyInfo);

                var example = new HtmlTag("div").AddClass("example");
                example.Append(new HtmlTag("code").AddClass("property").Text(propertySource));
                example.Append(createExample(tagGenerator.LabelFor(tagGenerator.GetRequest(property)), "LabelFor({0})".ToFormat(propertyExpression)));
                example.Append(createExample(tagGenerator.DisplayFor(tagGenerator.GetRequest(property)), "DisplayFor({0})".ToFormat(propertyExpression)));
                example.Append(createExample(tagGenerator.InputFor(tagGenerator.GetRequest(property)), "InputFor({0})".ToFormat(propertyExpression)));
                tags.Add(example);
            }

            var doc = DiagnosticHtml.BuildDocument(_urlRegistry, "FubuMVC.UI Examples", tags.ToArray());

            doc.AddStyle(DiagnosticHtml.GetResourceText(GetType(), "examples.css"));
            return(doc);
        }
Ejemplo n.º 2
0
        public HtmlDocument Example(ExampleHtmlRequest exampleHtmlRequest)
        {
            var modelPath = exampleHtmlRequest.Model ?? typeof(ExampleViewModel).FullName + "-Person";
            var tags = new List<HtmlTag>();
            var propertyPath = new List<string>(modelPath.Split('-'));
            var rootModelTypeName = propertyPath[0];
            tags.Add(new HtmlTag("h3").AddClass("viewmodel").Text(propertyPath.Join(".")));
            propertyPath.RemoveAt(0);

            Type scannedModelType = getTypeFromName(rootModelTypeName);
            var scannedModelInstance = createInstance(scannedModelType);
            var tagGeneratorType = typeof(TagGenerator<>).MakeGenericType(scannedModelType);
            var tagGenerator = (ITagGenerator)_serviceLocator.GetInstance(tagGeneratorType);
            tagGenerator.SetModel(scannedModelInstance);

            var propertyChainParts = new List<PropertyInfo>();

            while (propertyPath.Count > 0)
            {
                var parentPropertyInfo = scannedModelType.GetProperty(propertyPath[0]);
                propertyChainParts.Add(parentPropertyInfo);
                var currentModelType = parentPropertyInfo.PropertyType;
                var currentModel = createInstance(currentModelType);
                setProperty(parentPropertyInfo, scannedModelInstance, currentModel);

                scannedModelType = currentModelType;
                scannedModelInstance = currentModel;
                propertyPath.RemoveAt(0);
            }

            var modelProperties = scannedModelType.GetProperties();
            var propertiesToLink = modelProperties.Where(p => !TypeDescriptor.GetConverter(p.PropertyType).CanConvertFrom(typeof(string)));
            var propertiesToShow = modelProperties.Except(propertiesToLink);

            // show links to deeper properties
            var linkList = new HtmlTag("ul").AddClass("subproperties");
            foreach (var propertyInfo in propertiesToLink)
            {
                var linkTag = new LinkTag("", _examplePageUrl + "?model=" + modelPath + "-" + propertyInfo.Name);
                linkTag.Child(new HtmlTag("code").Text(getPropertySourceCode(propertyInfo)));
                var listItem = new HtmlTag("li").Child(linkTag);
                linkList.Child(listItem);
            }
            if (linkList.Children.Count > 0) tags.Add(linkList);

            // show examples
            populateInstance(scannedModelInstance, propertiesToShow);
            foreach (var propertyInfo in propertiesToShow)
            {
                var property = propertyChainParts.Count > 0 ?
                    (Accessor) new PropertyChain(propertyChainParts.Concat(new[]{propertyInfo}).ToArray()) :
                    new SingleProperty(propertyInfo);

                var propertyExpression = "x => x." + property.PropertyNames.Join(".");

                var propertySource = getPropertySourceCode(propertyInfo);

                var example = new HtmlTag("div").AddClass("example");
                example.AddChildren(new HtmlTag("code").AddClass("property").Text(propertySource));
                example.AddChildren(createExample(tagGenerator.LabelFor(tagGenerator.GetRequest(property)), "LabelFor({0})".ToFormat(propertyExpression)));
                example.AddChildren(createExample(tagGenerator.DisplayFor(tagGenerator.GetRequest(property)), "DisplayFor({0})".ToFormat(propertyExpression)));
                example.AddChildren(createExample(tagGenerator.InputFor(tagGenerator.GetRequest(property)), "InputFor({0})".ToFormat(propertyExpression)));
                tags.Add(example);
            }

            var doc = DiagnosticHtml.BuildDocument(_urlRegistry, "FubuMVC.UI Examples", tags.ToArray());
            doc.AddStyle(DiagnosticHtml.GetResourceText(GetType(), "examples.css"));
            return doc;
        }