示例#1
0
        public UiStratum(string componentName, string rootPath = "", string baseId = "", object modelData = null, UiStratumType type = null)
        {
            _componentName = componentName;

            if (type != null)
            {
                _t = type;
            }
            if (!String.IsNullOrWhiteSpace(rootPath))
            {
                _t.RootPath = rootPath;
            }

            var page = HttpContext.Current.Request.Path;

            if (page != null && page.StartsWith("/"))
            {
                _virtualBundlePath = "~/bundles" + page.Substring(1);
            }
            //_virtualBundlePath = "~/bundles/components/" + _componentName;

            if (String.IsNullOrWhiteSpace(baseId))
            {
                string rand = Guid.NewGuid().ToString().Substring(0, 5);
                _containerId = componentName + rand + _t.ContainerSuffix;
            }
            else
            {
                _containerId = baseId;// + _t.ContainerSuffix;
            }
            _templateId = componentName + _t.TemplateSuffix;

            _modelData = modelData;

            try
            {
                string componentFile = HttpContext.Current.Server.MapPath(_t.RootPath + _componentName + "/" + _t.DepsFile);
                _dependencies      = JsonConvert.DeserializeObject <ComponentDependencies>(File.ReadAllText(componentFile));
                _backboneModelName = _dependencies.BackboneModelName;
                _templateFilePath  = ConstructFilePath(_componentName, _t.TemplateExtension);
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Something went wrong finding the component '{0}'. Did you give me the right component name? \n {1}", _componentName, ex.InnerException));
            }

            if (_dependencies != null)
            {
                ExtractDependencies();
                UpdateTemplatesCache(_dependentTemplatePaths.Values.ToArray());
            }
        }
示例#2
0
        /// <summary>
        /// Render and register a Stratum UI component in the page
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="componentName">name of the component (should correlate the folder in /Components/)</param>
        /// <param name="data"></param>
        /// <param name="withScripts"></param>
        /// <returns></returns>
        public static IHtmlString Stratum <T>(string componentName, string rootPath = "", string myId = "", object data = null, bool withScripts = false, bool withStyles = true, bool withTemplates = true, bool withViewInit = false) where T : UiStratumType
        {
            UiStratumType newType   = (T)Activator.CreateInstance(typeof(T));
            UiStratum     component = new UiStratum(componentName, rootPath, myId, data, newType);
            List <string> loaded    = new List <string>();
            string        pageKey   = "stratum:" + HttpContext.Current.Request.Path;

            if (HttpContext.Current.Application[pageKey] != null)
            {
                loaded = HttpContext.Current.Application[pageKey] as List <string>;
            }
            loaded.Add(componentName);
            HttpContext.Current.Application[pageKey] = loaded;
            string output = "";

            if (withStyles)
            {
                output += component.RenderStyles();
            }
            output += component.RenderComponentHtml(withTemplates, withScripts, withViewInit);
            return(new HtmlString(output));
        }