Exemplo n.º 1
0
        public CssBundleManager(IOptions <CssBundlingOptions> options, IDynamicScriptManager scriptManager, IWebHostEnvironment hostEnvironment,
                                IHttpContextAccessor contextAccessor = null, IExceptionLogger logger = null)
        {
            this.options         = (options ?? throw new ArgumentNullException(nameof(options))).Value;
            this.scriptManager   = scriptManager ?? throw new ArgumentNullException(nameof(scriptManager));
            this.hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment));
            this.contextAccessor = contextAccessor;
            this.logger          = logger;

            Reset();
            scriptManager.ScriptChanged += name =>
            {
                HashSet <string> bundleKeys;
                lock (sync)
                {
                    if (bundleKeysBySourceUrl == null ||
                        !bundleKeysBySourceUrl.TryGetValue("dynamic://" + name, out bundleKeys))
                    {
                        bundleKeys = null;
                    }
                    {
                    }
                }

                if (bundleKeys != null)
                {
                    foreach (var bundleKey in bundleKeys)
                    {
                        scriptManager.Changed("Bundle." + bundleKey);
                    }
                }
            };
        }
Exemplo n.º 2
0
        public static void RegisterFormScripts(IDynamicScriptManager scriptManager,
                                               ITypeSource typeSource, IPropertyItemProvider propertyProvider, IServiceProvider serviceProvider)
        {
            if (scriptManager == null)
            {
                throw new ArgumentNullException(nameof(scriptManager));
            }

            if (typeSource == null)
            {
                throw new ArgumentNullException(nameof(typeSource));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var scripts = new List <Func <string> >();

            foreach (var type in typeSource.GetTypesWithAttribute(typeof(FormScriptAttribute)))
            {
                var attr   = type.GetCustomAttribute <FormScriptAttribute>();
                var key    = attr.Key ?? type.FullName;
                var script = new FormScript(key, type, propertyProvider, serviceProvider);
                scriptManager.Register(script);
                scripts.Add(script.GetScript);
            }

            scriptManager.Register("FormBundle", new ConcatenatedScript(scripts));
        }
        public static void RegisterDataScripts(IDynamicScriptManager scriptManager,
                                               ITypeSource typeSource, IServiceProvider serviceProvider)
        {
            if (scriptManager == null)
            {
                throw new ArgumentNullException(nameof(scriptManager));
            }

            if (typeSource == null)
            {
                throw new ArgumentNullException(nameof(typeSource));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            DataScriptAttribute attr;

            foreach (var type in typeSource.GetTypes())
            {
                if (type.IsAbstract ||
                    type.IsInterface ||
                    type.IsGenericTypeDefinition ||
                    !type.IsPublic)
                {
                    continue;
                }

                attr = type.GetCustomAttribute <DataScriptAttribute>();
                if (attr != null)
                {
                    var script = (INamedDynamicScript)ActivatorUtilities
                                 .CreateInstance(serviceProvider, type);
                    scriptManager.Register(script);
                    continue;
                }
            }
        }
        public static void RegisterLookupScripts(IDynamicScriptManager scriptManager,
                                                 ITypeSource typeSource, IServiceProvider serviceProvider)
        {
            if (scriptManager == null)
            {
                throw new ArgumentNullException(nameof(scriptManager));
            }

            if (typeSource == null)
            {
                throw new ArgumentNullException(nameof(typeSource));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var registeredType = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);

            foreach (var type in typeSource.GetTypesWithAttribute(typeof(LookupScriptAttribute)))
            {
                var          attr = type.GetCustomAttribute <LookupScriptAttribute>();
                LookupScript script;

                if (typeof(IRow).IsAssignableFrom(type))
                {
                    if (attr.LookupType == null)
                    {
                        script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider,
                                                                                 typeof(RowLookupScript <>).MakeGenericType(type));
                    }
                    else if (attr.LookupType.IsGenericType)
                    {
                        script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider,
                                                                                 attr.LookupType.MakeGenericType(type));
                    }
                    else if (attr.LookupType.GetCustomAttribute <LookupScriptAttribute>() == null)
                    {
                        script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider,
                                                                                 attr.LookupType);
                    }
                    else
                    {
                        // lookup script type already has a LookupScript attribute,
                        // so it's dynamic script will be generated on itself
                        continue;
                    }
                }
                else if (!typeof(LookupScript).IsAssignableFrom(type) ||
                         type.IsAbstract)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      "Type {0} can't be registered as a lookup script!", type.FullName));
                }
                else
                {
                    script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider, type);
                }

                script.LookupKey = attr.Key ??
                                   LookupScriptAttribute.AutoLookupKeyFor(type);

                if (registeredType.TryGetValue(script.LookupKey, out Type otherType))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      "Types {0} and {1} has the same lookup key (\"{2}\"). " +
                                                                      "\r\n\r\nPlease remove LookupScript attribute from one of them or change the lookup key!",
                                                                      type.FullName, otherType.FullName, script.LookupKey));
                }

                registeredType[script.LookupKey] = type;

                if (attr.Permission != null)
                {
                    script.Permission = attr.Permission;
                }

                if (attr.Expiration != 0)
                {
                    script.Expiration = TimeSpan.FromSeconds(attr.Expiration);
                }

                scriptManager.Register(script.ScriptName, script);
            }
        }
Exemplo n.º 5
0
        public static void RegisterDistinctValueScripts(IDynamicScriptManager scriptManager,
                                                        ITypeSource typeSource, IServiceProvider serviceProvider)
        {
            if (scriptManager == null)
            {
                throw new ArgumentNullException(nameof(scriptManager));
            }

            if (typeSource == null)
            {
                throw new ArgumentNullException(nameof(typeSource));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var list = new List <DistinctValuesEditorAttribute>();

            foreach (var type in typeSource.GetTypes())
            {
                bool isRow = typeof(IRow).IsAssignableFrom(type) &&
                             !type.IsInterface;

                if (!isRow &&
                    type.GetCustomAttribute <FormScriptAttribute>() == null &&
                    type.GetCustomAttribute <ColumnsScriptAttribute>() == null)
                {
                    continue;
                }

                if (isRow &&
                    type.IsAbstract)
                {
                    continue;
                }

                foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    var attr = property.GetCustomAttribute <DistinctValuesEditorAttribute>();
                    if (attr == null)
                    {
                        continue;
                    }

                    if (attr.RowType != null)
                    {
                        if (attr.RowType.IsInterface ||
                            attr.RowType.IsAbstract ||
                            !typeof(IRow).IsAssignableFrom(attr.RowType))
                        {
                            throw new Exception("DistinctValuesEditor can't be used with type: " +
                                                attr.RowType.FullName + " as it is not a row type. This attribute is specified " +
                                                "on " + property.Name + " property of " + type.FullName);
                        }

                        attr.PropertyName = attr.PropertyName.IsEmptyOrNull() ? property.Name :
                                            attr.PropertyName;
                    }
                    else
                    {
                        if (!isRow)
                        {
                            var basedOnRowAttr = type.GetCustomAttribute <BasedOnRowAttribute>();
                            if (basedOnRowAttr == null || basedOnRowAttr.RowType == null ||
                                basedOnRowAttr.RowType.IsAbstract ||
                                basedOnRowAttr.RowType.IsInterface ||
                                !typeof(IRow).IsAssignableFrom(basedOnRowAttr.RowType))
                            {
                                throw new Exception("Invalid usage of DistinctValuesEditor attribute on " +
                                                    "property " + property.Name + " of " + type.FullName + ". " +
                                                    "RowType has to be specified!");
                            }

                            attr.RowType = basedOnRowAttr.RowType;
                        }
                        else
                        {
                            attr.RowType = type;
                        }

                        attr.PropertyName = attr.PropertyName.IsEmptyOrNull() ? property.Name :
                                            attr.PropertyName;
                    }

                    list.Add(attr);
                }
            }

            var byRowProperty = list.ToLookup(x => new Tuple <Type, string>(x.RowType, x.PropertyName));

            foreach (var key in byRowProperty)
            {
                var row = (IRow)Activator.CreateInstance(key.Key.Item1);

                var script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider,
                                                                             typeof(DistinctValuesScript <>).MakeGenericType(key.Key.Item1),
                                                                             new object[] { key.Key.Item2 });

                script.LookupKey = "Distinct." + row.GetFields().LocalTextPrefix + "." +
                                   key.Key.Item2;

                var withPermission = key.FirstOrDefault(x => !string.IsNullOrEmpty(x.Permission));
                if (withPermission != null)
                {
                    script.Permission = withPermission.Permission;
                }

                var withExpiration = key.FirstOrDefault(x => x.Expiration != 0);
                if (withExpiration != null)
                {
                    script.Expiration = TimeSpan.FromSeconds(withExpiration.Expiration);
                }

                scriptManager.Register(script.ScriptName, script);
            }
        }
Exemplo n.º 6
0
 public RegisteredScripts(IDynamicScriptManager scriptManager)
 {
     Expiration         = TimeSpan.FromDays(-1);
     this.scriptManager = scriptManager;
 }