| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Abp.Application.Features;
- using Abp.Dependency;
- using Abp.Runtime.Session;
- namespace Abp.Web.Features
- {
- public class FeaturesScriptManager : IFeaturesScriptManager, ITransientDependency
- {
- public IAbpSession AbpSession { get; set; }
- private readonly IFeatureManager _featureManager;
- private readonly IFeatureChecker _featureChecker;
- public FeaturesScriptManager(IFeatureManager featureManager, IFeatureChecker featureChecker)
- {
- _featureManager = featureManager;
- _featureChecker = featureChecker;
- AbpSession = NullAbpSession.Instance;
- }
- public async Task<string> GetScriptAsync()
- {
- var allFeatures = _featureManager.GetAll().ToList();
- var currentValues = new Dictionary<string, string>();
- if (AbpSession.TenantId.HasValue)
- {
- var currentTenantId = AbpSession.GetTenantId();
- foreach (var feature in allFeatures)
- {
- currentValues[feature.Name] = await _featureChecker.GetValueAsync(currentTenantId, feature.Name);
- }
- }
- else
- {
- foreach (var feature in allFeatures)
- {
- currentValues[feature.Name] = feature.DefaultValue;
- }
- }
- var script = new StringBuilder();
- script.AppendLine("(function() {");
- script.AppendLine();
- script.AppendLine(" abp.features = abp.features || {};");
- script.AppendLine();
- script.AppendLine(" abp.features.allFeatures = {");
- for (var i = 0; i < allFeatures.Count; i++)
- {
- var feature = allFeatures[i];
- script.AppendLine(" '" + feature.Name.Replace("'", @"\'") + "': {");
- script.AppendLine(" value: '" + currentValues[feature.Name].Replace(@"\", @"\\").Replace("'", @"\'") + "'");
- script.Append(" }");
- if (i < allFeatures.Count - 1)
- {
- script.AppendLine(",");
- }
- else
- {
- script.AppendLine();
- }
- }
- script.AppendLine(" };");
- script.AppendLine();
- script.Append("})();");
- return script.ToString();
- }
- }
- }
|