Exemple #1
0
        /// <summary>
        /// Add new assembly into vue handler components
        /// </summary>
        public void AddAssembly(Assembly assembly)
        {
            foreach (var resourceName in assembly
                     .GetManifestResourceNames()
                     .Where(x => Path.GetExtension(x) == Extension))
            {
                var file = new HtmlFile(assembly.GetManifestResourceStream(resourceName));
                var name = file.Name ?? ComponentLoader.GetName(resourceName);

                _discovers[name] = new ComponentDiscover
                {
                    Name     = name,
                    Assembly = assembly,
                    File     = file
                };
            }
        }
Exemple #2
0
        /// <summary>
        /// Add new assembly into vue handler components
        /// </summary>
        private void AddWebFiles(string root, Assembly assembly)
        {
            foreach (var path in Directory.GetFiles(root, "*" + Extension, SearchOption.AllDirectories))
            {
                var file = new HtmlFile(File.OpenRead(path));
                var name = file.Name ?? ComponentLoader.GetName(path.Replace(@"\", "."));

                _discovers[name] = new ComponentDiscover
                {
                    Name     = name,
                    Assembly = assembly,
                    File     = file
                };

                // remove component to be re-loaded on next Load method (only for debug)
                _components.Remove(name);
            }
        }
Exemple #3
0
        public ComponentInfo Load(Assembly assembly, string resourceName, StringBuilder globalScripts)
        {
            // try read file from local disk (for hot reload)
            var localFile = Path.Combine(_root + @"\..\",
                                         assembly.GetName().Name +
                                         resourceName.Substring(assembly.GetName().Name.Length, resourceName.Length - assembly.GetName().Name.Length - 4).Replace(".", "\\")) +
                            ".vue";

            var content = File.Exists(localFile) ?
                          File.ReadAllText(localFile) :
                          new StreamReader(assembly.GetManifestResourceStream(resourceName)).ReadToEnd();

            var html      = new HtmlFile(content, globalScripts);
            var name      = html.GetDirective("name") ?? Path.GetFileNameWithoutExtension(localFile);
            var component = new ComponentInfo(name);

            if (html.Directives.TryGetValue("viewmodel", out var viewModel))
            {
                component.ViewModelType = assembly.GetType(viewModel, true);
            }
            else
            {
                // try guess viewModel based on resource name
                component.ViewModelType = assembly.GetType(resourceName.Substring(0, resourceName.Length - 4)) ?? typeof(ViewModel);
            }

            component.IsAsync = html.Directives.ContainsKey("async");

            if (html.Directives.TryGetValue("page", out var route))
            {
                component.IsPage = true;
                component.Route  = route;
            }

            component.Template = html.Template.ToString().Trim();
            component.Styles   = html.Styles.ToString();
            component.Scripts  = html.Scripts.ToString() + this.GetScriptAttr(component.ViewModelType);
            component.Mixins   = html.Mixins;

            using (var instance = (ViewModel)ActivatorUtilities.CreateInstance(_service, component.ViewModelType))
            {
                component.JsonData = this.GetJsonData(instance);
                component.Methods  = this.GetMethods(component.ViewModelType).ToDictionary(x => x.Method.Name, x => x, StringComparer.OrdinalIgnoreCase);

                component.Props       = this.GetField <PropAttribute>(component.ViewModelType, instance);
                component.RouteParams = this.GetField <RouteParamAttribute>(component.ViewModelType, instance);
                component.QueryString = this.GetField <QueryStringAttribute>(component.ViewModelType, instance);

                component.LocalStorage = component.ViewModelType
                                         .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                         .Where(x => x.GetCustomAttribute <LocalStorageAttribute>() != null)
                                         .ToDictionary(x => x.Name, x => x.GetCustomAttribute <LocalStorageAttribute>().Key ?? x.Name.ToCamelCase(), StringComparer.OrdinalIgnoreCase);
            }

            component.InheritAttrs = !component.Template.Contains("v-bind=\"$attrs\"");

            component.IsAuthenticated = component.ViewModelType.GetCustomAttribute <AutorizeAttribute>() != null;

            if (component.IsAuthenticated)
            {
                component.Roles = component.ViewModelType.GetCustomAttribute <AutorizeAttribute>().Roles;
            }

            return(component);
        }