Exemple #1
0
        /// <summary>
        /// Render a partial view with AngularJS components.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="viewName">The view name.</param>
        /// <param name="model">The model.</param>
        /// <param name="viewDataDictionary">The view data dictionary.</param>
        /// <param name="ngJsDeps">The Angular JS dependencies related to this view.</param>
        /// <returns>The HTML string to print.</returns>
        /// <seealso cref="PartialExtensions.Partial(HtmlHelper, string, object, ViewDataDictionary)"/>
        public static MvcHtmlString PartialNgJs(
            this HtmlHelper htmlHelper,
            string viewName,
            object model,
            ViewDataDictionary viewDataDictionary,
            NgJsViewDeps ngJsDeps = null)
        {
            string       sanitizedViewName = GetSanitizedNameFrom(viewName);
            NgJsViewData viewData          = NgJsContext.Current.Provider.GetViewData(sanitizedViewName);

            NgJsViewDeps actualNgDeps = new NgJsViewDeps();

            NgJsViewDependenciesContext.Current.Dependencies.Add(sanitizedViewName, actualNgDeps);
            actualNgDeps.Merge(ngJsDeps);
            LoadView(actualNgDeps, viewData);

            string controllerName = viewData.JsName;
            string formName       = sanitizedViewName + "Frm"; // FIXME: Use proper init from provider

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat(@"<div ng-controller=""{0} as _context"" ng-form=""{1}"">", controllerName, formName);
            sb.AppendLine(htmlHelper.Partial(viewData.TemplatePath, model, viewDataDictionary).ToHtmlString());
            sb.AppendLine("</div>");
            return(new MvcHtmlString(sb.ToString()));
        }
        /// <summary>
        /// Gets the default AngularJS dependencies (modules), to be inherited by all defined components.
        /// </summary>
        /// <returns>The default AngularJS deps.</returns>
        public static NgJsViewDeps GetDefaultDependencies()
        {
            NgJsViewDeps res = new NgJsViewDeps();

            if (!TryGetConfigFromAppSettings(DefaultDepsDirConfigKey, out string defaultDeps))
            {
                return(res);
            }

            res.Modules = new System.Collections.Generic.List <string>(defaultDeps.Split(','));
            return(res);
        }
Exemple #3
0
        /// <summary>
        /// Merges the specified dependencies object into this one. This object will be modified in-place.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="other">The other object.</param>
        public static void Merge(this NgJsViewDeps obj, NgJsViewDeps other)
        {
            if (other == null)
            {
                return;
            }

            MergeNotPresent(obj.Modules, other.Modules);
            MergeNotPresent(obj.Services, other.Services);
            MergeNotPresent(obj.Directives, other.Directives);
            MergeNotPresent(obj.Components, other.Components);
            MergeNotPresent(obj.Filters, other.Filters);
        }
Exemple #4
0
        public Dictionary <string, NgJsViewDeps> GetAllDependencies()
        {
            Dictionary <string, NgJsViewDeps> deps = GetCurrentViewSpecificDependencies();

            NgJsViewDeps defaultDeps = GetDefaultDependencies();

            if (defaultDeps != null)
            {
                deps.Add("__DEFAULT__", defaultDeps);
            }

            return(deps);
        }
Exemple #5
0
        private void RegisterServices(StringBuilder script, Dictionary <string, NgJsViewDeps> dependencies)
        {
            script.AppendLine();
            script.AppendLine("/** Register services **/");
            List <string> services = new List <string>();

            foreach (string s in dependencies.Keys)
            {
                NgJsViewDeps dep = dependencies[s];

                foreach (string service in dep.Services)
                {
                    if (service != null)
                    {
                        services.Add(service.Trim());
                    }
                }
            }

            // include services one time only
            foreach (string service in services.Distinct())
            {
                NgJsBaseData serviceData = Provider.GetServiceData(service);

                script.AppendLine();
                if (IncludeMode == IncludeMode.Load)
                {
                    string mappedPath = NgJsHelper.MapPath(serviceData.Path);
                    if (File.Exists(mappedPath))
                    {
                        script.Append(File.ReadAllText(mappedPath));
                    }
                }
                else
                {
                    script.AppendLine("Include mode: link not implemented");
                }

                script.AppendLine();
                script.AppendFormat("app.service('{1}', {0});", serviceData.NgName, serviceData.JsName);
            }

            script.AppendLine("/** End Register services **/");
            script.AppendLine();
        }
Exemple #6
0
        private static void LoadView(NgJsViewDeps target, NgJsViewData viewData)
        {
            if (target.View != null)
            {
                throw new ArgumentException("Unable to load " + viewData.JsName + ": view has already been loaded!");
            }

            string diskPath = NgJsHelper.MapPath(viewData.Path);

            target.View = "/** INCLUDE JS " + viewData.JsName + " @" + diskPath + "**/";

            if (File.Exists(diskPath))
            {
                target.View += File.ReadAllText(diskPath);
            }
            else
            {
                target.View = "/** js at '" + diskPath + "' not found **/";
            }
        }
Exemple #7
0
        private void RegisterViewControllers(StringBuilder script, Dictionary <string, NgJsViewDeps> dependencies)
        {
            script.AppendLine();
            script.AppendLine("/** Register view controllers **/");

            foreach (string c in dependencies.Keys)
            {
                NgJsViewDeps dep = dependencies[c];

                if (!string.IsNullOrEmpty(dep.View))
                {
                    NgJsViewData viewData = Provider.GetViewData(c);

                    script.AppendFormat("app.controller(\"{0}\", {0}); ", viewData.JsName);
                    script.AppendLine();
                }
            }

            script.AppendLine("/** End Register view controllers **/");
            script.AppendLine();
        }
Exemple #8
0
        private void AppendViews(StringBuilder script, Dictionary <string, NgJsViewDeps> deps)
        {
            foreach (string c in deps.Keys)
            {
                NgJsViewDeps dep = deps[c];

                if (!string.IsNullOrEmpty(dep.View))
                {
                    script.AppendLine();
                    if (IncludeMode == IncludeMode.Load)
                    {
                        script.AppendLine(dep.View);
                    }
                    else
                    {
                        script.AppendLine("/** TODO:modalità non implementata **/");
                    }

                    script.AppendLine();
                }
            }
        }