Example #1
0
        public TagBuilder GetTagBuilder(RequireSettings baseSettings, string appPath)
        {
            var tagBuilder = new TagBuilder(Resource.TagName);

            tagBuilder.MergeAttributes(Resource.TagBuilder.Attributes);
            if (!String.IsNullOrEmpty(Resource.FilePathAttributeName))
            {
                var resolvedUrl = GetResourceUrl(baseSettings, appPath);
                if (!String.IsNullOrEmpty(resolvedUrl))
                {
                    tagBuilder.MergeAttribute(Resource.FilePathAttributeName, resolvedUrl, true);
                }
            }
            return(tagBuilder);
        }
Example #2
0
        public string ResolveUrl(RequireSettings settings, string applicationPath)
        {
            string url;

            if (_urlResolveCache.TryGetValue(settings, out url))
            {
                return(url);
            }
            // Url priority:
            if (settings.DebugMode)
            {
                url = settings.CdnMode
                    ? Coalesce(UrlCdnDebug, UrlDebug, UrlCdn, Url)
                    : Coalesce(UrlDebug, Url, UrlCdnDebug, UrlCdn);
            }
            else
            {
                url = settings.CdnMode
                    ? Coalesce(UrlCdn, Url, UrlCdnDebug, UrlDebug)
                    : Coalesce(Url, UrlDebug, UrlCdn, UrlCdnDebug);
            }
            if (String.IsNullOrEmpty(url))
            {
                return(null);
            }
            if (!String.IsNullOrEmpty(settings.Culture))
            {
                string nearestCulture = FindNearestCulture(settings.Culture);
                if (!String.IsNullOrEmpty(nearestCulture))
                {
                    url = Path.ChangeExtension(url, nearestCulture + Path.GetExtension(url));
                }
            }
            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !VirtualPathUtility.IsAbsolute(url) && !VirtualPathUtility.IsAppRelative(url) && !String.IsNullOrEmpty(BasePath))
            {
                // relative urls are relative to the base path of the module that defined the manifest
                url = VirtualPathUtility.Combine(BasePath, url);
            }
            if (VirtualPathUtility.IsAppRelative(url))
            {
                url = applicationPath != null
                    ? VirtualPathUtility.ToAbsolute(url, applicationPath)
                    : VirtualPathUtility.ToAbsolute(url);
            }
            _urlResolveCache[settings] = url;
            return(url);
        }
Example #3
0
        public RequireSettings Combine(RequireSettings other)
        {
            var settings = (new RequireSettings {
                Name = Name,
                Type = Type
            }).AtLocation(Location).AtLocation(other.Location)
                           .WithBasePath(BasePath).WithBasePath(other.BasePath)
                           .UseCdn(CdnMode).UseCdn(other.CdnMode)
                           .UseDebugMode(DebugMode).UseDebugMode(other.DebugMode)
                           .UseCulture(Culture).UseCulture(other.Culture)
                           .UseCondition(Condition).UseCondition(other.Condition)
                           .Define(InlineDefinition).Define(other.InlineDefinition)
                           .AddDependencies(Dependencies).AddDependencies(other.Dependencies);

            settings._attributes = MergeAttributes(other);
            return(settings);
        }
Example #4
0
        private Dictionary <string, string> MergeAttributes(RequireSettings other)
        {
            // efficiently merge the two dictionaries, taking into account that one or both may not exist
            // and that attributes in 'other' should overridde attributes in this, even if the value is null.
            if (_attributes == null)
            {
                return(other._attributes == null ? null : new Dictionary <string, string>(other._attributes));
            }
            if (other._attributes == null)
            {
                return(new Dictionary <string, string>(_attributes));
            }
            var mergedAttributes = new Dictionary <string, string>(_attributes);

            foreach (var pair in other._attributes)
            {
                mergedAttributes[pair.Key] = pair.Value;
            }
            return(mergedAttributes);
        }
Example #5
0
        public string GetTemplateNotCache(string resourceName, string viewVirtualPath)
        {
            string resourceType    = "template";
            var    defaultSettings = new RequireSettings
            {
                DebugMode = false,
                CdnMode   = false,
                Culture   = "" //_workContext.Value.CurrentCulture,
            };

            Require("template", resourceName);
            var httpContext = HttpContext.Current;
            var appPath     = httpContext == null || httpContext.Request == null
                ? null
                : httpContext.Request.ApplicationPath;

            var requiredResources = this.GetResources(resourceType);

            requiredResources = requiredResources.Where(t => t.Resource.Name == resourceName).ToList();
            var listTemplateTmp = new List <string>();

            foreach (var context in requiredResources)
            {
                if (viewVirtualPath != null)
                {
                    context.Settings.WithBasePath(ResourceDefinition.GetBasePathFromViewPath(resourceType, viewVirtualPath));
                }
                var path       = context.GetResourceUrl(defaultSettings, appPath);
                var condition  = context.Settings.Condition;
                var attributes = context.Settings.HasAttributes ? context.Settings.Attributes : null;

                listTemplateTmp.Add(path);
            }
            if (listTemplateTmp == null || !listTemplateTmp.Any())
            {
                throw new Exception($"no found {resourceName} View Template");
            }


            return(listTemplateTmp.FirstOrDefault());
        }
Example #6
0
        public virtual RequireSettings Require(string resourceType, string resourceName)
        {
            if (resourceType == null)
            {
                throw new ArgumentNullException("resourceType");
            }
            if (resourceName == null)
            {
                throw new ArgumentNullException("resourceName");
            }
            RequireSettings settings;
            var             key = new Tuple <string, string>(resourceType, resourceName);

            if (!_required.TryGetValue(key, out settings))
            {
                settings = new RequireSettings {
                    Type = resourceType, Name = resourceName
                };
                _required[key] = settings;
            }
            _builtResources[resourceType] = null;
            return(settings);
        }
Example #7
0
        private ResourceDefinition FindResource(RequireSettings settings, bool resolveInlineDefinitions)
        {
            // find the resource with the given type and name
            // that has at least the given version number. If multiple,
            // return the resource with the greatest version number.
            // If not found and an inlineDefinition is given, define the resource on the fly
            // using the action.
            var name     = settings.Name ?? "";
            var type     = settings.Type;
            var resource = (from p in ResourceProviders
                            from r in p.GetResources(type)
                            where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase)
                            let version = r.Value.Version != null ? new Version(r.Value.Version) : null
                                          orderby version descending
                                          select r.Value).FirstOrDefault();

            if (resource == null && _dynamicManifest != null)
            {
                resource = (from r in _dynamicManifest.GetResources(type)
                            where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase)
                            let version = r.Value.Version != null ? new Version(r.Value.Version) : null
                                          orderby version descending
                                          select r.Value).FirstOrDefault();
            }
            if (resolveInlineDefinitions && resource == null)
            {
                // Does not seem to exist, but it's possible it is being
                // defined by a Define() from a RequireSettings somewhere.
                if (ResolveInlineDefinitions(settings.Type))
                {
                    // if any were defined, now try to find it
                    resource = FindResource(settings, false);
                }
            }
            return(resource);
        }
Example #8
0
        public void WriteResources()
        {
            var defaultSettings = new RequireSettings {
                DebugMode = _setttingManager.GetSettingValue(typeof(DebugState).Name) == DebugState.Debug.ToString(),
                CdnMode   = false,
                Culture   = ""//_workContext.Value.CurrentCulture,
            };

            var httpContext = HttpContext.Current;
            var appPath     = httpContext == null || httpContext.Request == null
                ? null
                : httpContext.Request.ApplicationPath;

            var requiredResources = this.BuildRequiredResources("script");

            requiredResources?.AddRange(this.BuildRequiredResources("stylesheet"));
            requiredResources?.AddRange(this.BuildRequiredResources("template"));

            foreach (var context in requiredResources)
            {
                var path       = context.GetResourceUrl(defaultSettings, appPath);
                var condition  = context.Settings.Condition;
                var attributes = context.Settings.HasAttributes ? context.Settings.Attributes : null;

                if (context.Resource.Type == "script")
                {
                    var multLanguageResource = _manifests.SelectMany(m => m.GetResources("script").Values)
                                               .Where(rName => rName.Name == LocalizeNamed.GetName(context.Resource.Name, _languageManager.CurrentLanguage.Name))
                                               .ToList();

                    if (multLanguageResource.Count > 0)
                    {
                        foreach (var languageResource in multLanguageResource)
                        {
                            var languageContext = new ResourceRequiredContext()
                            {
                                Resource = languageResource,
                                Settings = context.Settings.AutoMapTo <RequireSettings>()
                            };
                            RegisterJavascript(languageContext, languageContext.GetResourceUrl(defaultSettings, appPath), attributes);
                        }
                        var resources = multLanguageResource.Select(r => r.Name);
                        context.Resource.Dependencies = context.Resource.Dependencies != null?
                                                        context.Resource.Dependencies.Concat(resources) : resources;
                    }

                    RegisterJavascript(context, path, attributes);
                }
                else if (context.Resource.Type == "stylesheet")
                {
                    this.RegisterLink(new LinkEntry()
                    {
                        Rel = "stylesheet", Type = "text/css", Href = path,
                    }.SetAttributes(attributes));
                }

                else if (context.Resource.Type == "template")
                {
                    this.Templates.Add(path);
                }
//                IHtmlString result;
//                if (resourceType == "stylesheet") {
//                    result = Display.Style(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
//                }
//                else if (resourceType == "script") {
//                    result = Display.Script(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
//                }
//                else {
//                    result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
//                }
//                Output.Write(result);
            }
        }
Example #9
0
 public virtual ResourceDefinition FindResource(RequireSettings settings)
 {
     return(FindResource(settings, true));
 }
Example #10
0
        public void WriteResources()
        {
            var defaultSettings = new RequireSettings {
                DebugMode = _setttingManager.GetSettingValue(typeof(DebugState).Name) == DebugState.Debug.ToString(),
                CdnMode   = false,
                Culture   = ""//_workContext.Value.CurrentCulture,
            };

            var httpContext = HttpContext.Current;
            var appPath     = httpContext == null || httpContext.Request == null
                ? null
                : httpContext.Request.ApplicationPath;

            var requiredResources = this.BuildRequiredResources("script");

            requiredResources?.AddRange(this.BuildRequiredResources("stylesheet"));
            requiredResources?.AddRange(this.BuildRequiredResources("template"));

            foreach (var context in requiredResources)
            {
                var path       = context.GetResourceUrl(defaultSettings, appPath);
                var condition  = context.Settings.Condition;
                var attributes = context.Settings.HasAttributes ? context.Settings.Attributes : null;

                if (context.Resource.Type == "script")
                {
                    switch (context.Settings.Location)
                    {
                    case ResourceLocation.Foot: this.RegisterFootScript(new ScriptEntry()
                        {
                            Src          = path, Type = "text/javascript", Name = context.Resource.Name,
                            Dependencies = context.Resource.Dependencies?.ToList(), IsAMD = context.Resource.IsAMD
                        }.SetAttributes(attributes)); break;

                    case ResourceLocation.Head: this.RegisterHeadScript(new ScriptEntry()
                        {
                            Src = path, Type = "text/javascript", Name = context.Resource.Name, Dependencies = context.Resource.Dependencies?.ToList(), IsAMD = context.Resource.IsAMD
                        }.SetAttributes(attributes)); break;

                    default: this.RegisterDisplayNonoScript(new ScriptEntry()
                        {
                            Src = path, Type = "text/javascript", Name = context.Resource.Name, Dependencies = context.Resource.Dependencies?.ToList(), IsAMD = context.Resource.IsAMD
                        }.SetAttributes(attributes)); break;
                    }
                }
                else if (context.Resource.Type == "stylesheet")
                {
                    this.RegisterLink(new LinkEntry()
                    {
                        Rel = "stylesheet", Type = "text/css", Href = path,
                    }.SetAttributes(attributes));
                }

                else if (context.Resource.Type == "template")
                {
                    this.Templates.Add(path);
                }
//                IHtmlString result;
//                if (resourceType == "stylesheet") {
//                    result = Display.Style(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
//                }
//                else if (resourceType == "script") {
//                    result = Display.Script(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
//                }
//                else {
//                    result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
//                }
//                Output.Write(result);
            }
        }
Example #11
0
 public string GetResourceUrl(RequireSettings baseSettings, string appPath)
 {
     return(Resource.ResolveUrl(baseSettings == null ? Settings : baseSettings.Combine(Settings), appPath));
 }