예제 #1
0
        protected FlowStepComponent(
            string viewName,
            IPlatformSettings platformSettings,
            string customLoadingViewName          = null,
            [CallerFilePath] string componentPath = null)
            : this($"~{GetCustomViewPath(componentPath, viewName, "cshtml")}")
        {
            PlatformSettings = platformSettings;

            UpdateCachedLoaderView();
            SetComponentId();

            _loadingViewContent = new Lazy <string>(() =>
            {
                string customLoadingViewFullPath = null;
                if (customLoadingViewName != null)
                {
                    customLoadingViewFullPath = GetCustomViewPath(componentPath, customLoadingViewName).TrimStart('~', '/');
                }

                return(customLoadingViewFullPath != null
                                        ? ResolveView(customLoadingViewFullPath)
                                        : string.Empty);
            });

            void SetComponentId()
            {
                var componentPrefix = ViewName.Replace(".cshtml", string.Empty, StringComparison.InvariantCultureIgnoreCase)
                                      .Replace('\\', '_')
                                      .Replace('/', '_')
                                      .Replace('~', '_')
                                      .Replace('.', '_')
                                      .TrimStart('_');

                ComponentId = $"{componentPrefix}_{Guid.NewGuid()}";
            }

            string ResolveView(string customLoadingViewFullPath)
            {
#if DEBUG || FRAMEWORKDEVELOPMENT
                //so it can be changed in the same debugging session
                return(DoResolve());
#else
                return(LoadingViewsCache.GetOrAdd(customLoadingViewFullPath, k => DoResolve()));
#endif
                string DoResolve()
                {
                    var path = Path.Combine(HttpContext.Resolve <IWebHostEnvironment>().ContentRootPath,
                                            customLoadingViewFullPath);

                    if (!File.Exists(path))
                    {
                        Logger.Error(() => $"Could not find loading view at :{path}");
                        return(string.Empty);
                    }
                    return(File.ReadAllText(path).Replace("\"~/", $"\"{HttpContext.GetBaseUrl()}"));
                }
            }

            void UpdateCachedLoaderView()
            {
                if (platformSettings.DeferredComponentLoaderViewEmbeddedResourceName != null &&
                    Template.Key != platformSettings.DeferredComponentLoaderViewEmbeddedResourceName)
                {
                    var textResource = EmbeddedResourceReader.ReadTextResource(
                        platformSettings.DeferredComponentLoaderViewEmbeddedResourceName,
                        GetType().Assembly);
                    Template = new KeyValuePair <string, string>(
                        platformSettings.DeferredComponentLoaderViewEmbeddedResourceName,
                        textResource);
                }
            }
        }