Example #1
0
        public static FilePathResult GetJavaScriptResource(this Assembly resourceAssembly, string controllerName, string resourceName)
        {
            var cachedJavaScriptFile = string.Format("~/{0}/Resources/{1}/{2}.{3}.js", WebAppConfig.AppCacheDirectory, controllerName, resourceName, System.Threading.Thread.CurrentThread.CurrentUICulture.Name);
            var physicalPath = HttpContext.Current.Server.MapPath(cachedJavaScriptFile);

            if (!File.Exists(physicalPath))
            {
                var requestedType = resourceAssembly.GetTypes().SingleOrDefault(x => x.FullName.EndsWith(controllerName + "." + resourceName));

                if (requestedType == null)
                {
                    throw new FileNotFoundException();
                }

                var sb = new StringBuilder();

                sb.Add("if (typeof Views == 'undefined')");
                sb.Add("    window.Views = Views = {};");
                sb.Add("if (typeof Views.{0} == 'undefined')", controllerName);
                sb.Add("    window.Views.{0} = Views.{0} = {{}};", controllerName);
                sb.Add("if (typeof Views.{0}.{1} == 'undefined')", controllerName, resourceName);
                sb.Add("    window.Views.{0}.{1} = Views.{0}.{1} = ", controllerName, resourceName);
                sb.Add("    {");

                var properties = requestedType.GetProperties();
                for (var i = 0; i < properties.Length; i++)
                {
                    if (properties[i].CanRead)
                    {
                        sb.Add("        {0} : {1}{2}", properties[i].Name, properties[i].GetValue(null, null).ToEscapeString(), ((i == properties.Length - 1) ? "" : ","));
                    }
                }

                sb.Add("    };");
                sb.Add("var {1} = Views.{0}.{1};", controllerName, resourceName);

                WriteFileContent(physicalPath, sb.ToString());
            }
            else
            {
            #if DEBUG
                File.Delete(physicalPath);
            #endif
            }

            return new FilePathResult(cachedJavaScriptFile, "application/x-javascript");
        }
Example #2
0
        public string ToString(bool pretty = false)
        {
            var sb = new StringBuilder();
            //sb.Add(@"<?xml version=""1.0"" encoding=""utf-8""?>", pretty);

            var currentDepth = -1; // start at the same indenting as the <?xml..?> decalration

            Action<IXmlElement> recurse = null;
            recurse = e =>
            {
                currentDepth++;

                if (e is CDataElement)
                {
                    sb.Add("<![CDATA[ " + e.Value + " ]]>", pretty, currentDepth--);
                    return;
                }

                var xe = e as XmlElement;
                if (xe == null)
                    return;

                if (xe.Value == null && xe.Children.Count == 0)
                {
                    sb.Add("<" + xe.Name + GetAttributes(xe) + " />", pretty, currentDepth);
                }
                else
                {
                    // we keep text values on the same line, so only make it pretty if we don't have a value
                    sb.Add("<" + xe.Name + GetAttributes(xe) + ">", pretty && xe.Value == null, currentDepth);

                    if (xe.Value != null)
                        sb.Add(e.Value);
                    else
                        foreach (var child in xe.Children)
                            recurse(child);

                    sb.Add("</" + xe.Name + ">", pretty, xe.Value != null ? 0 : currentDepth);
                }

                currentDepth--;
            };

            recurse(RootElement);

            return pretty ? sb.ToString().Trim() : sb.ToString().Replace("\t", "");
        }
Example #3
0
        private void Create(string targetControlId)
        {
            var script = new StringBuilder();
            script.AppendFormat("{0} = window.{0} = popupMenu('{0}'", Id);

            if (MinWidth > 0)
                script.AppendFormat(", {0}", MinWidth);
            script.Add(");");

            foreach (var mi in Items)
            {
                mi.PrepareScript(this, script);
            }
            script.Add();

            HiggsScriptManager.InsertScript("_" + targetControlId, "_" + Id, script.ToString());
        }
Example #4
0
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Add("<div {0} class='RibbonGroup'>", !String.IsNullOrEmpty(Id) ? "id='" + Id + "'" : string.Empty);
            sb.Add("<div class='content'>");

            foreach(var c in Controls.Where(x => x.IsVisible))
            {
                sb.Add(c.ToString());
            }

            sb.Add("</div>");
            sb.Add("<div class='GroupName'>{0}</div>", Title);
            sb.Add("</div>");

            return sb.ToString();
        }
Example #5
0
        public static MvcHtmlString Create(int runningScriptDelay = 0)
        {
            var sb = new StringBuilder();

            if (RequiredScript != null)
            {
                foreach (var scriptPath in RequiredScript)
                {
                    sb.Add("<script type=\"text/javascript\" src=\"{0}\"></script>", VirtualPathUtility.ToAbsolute(scriptPath, HttpContext.Current.Request.ApplicationPath));
                }
                RequiredScript = new List<string>();
            }

            if (RequiredStyleSheet != null)
            {
                foreach (var styleSheetPath in RequiredStyleSheet)
                {
                    sb.Add("<link rel=\"Stylesheet\" href=\"{0}\">", VirtualPathUtility.ToAbsolute(styleSheetPath, HttpContext.Current.Request.ApplicationPath));
                }
                RequiredStyleSheet = new List<string>();
            }

            sb.Add();

            var oldLength = sb.Length;
            var hasScript = false;
            sb.Add("<script type=\"text/javascript\">");
            sb.Add("$(function()");
            sb.Add("{");

            if (runningScriptDelay > 0)
            {
                sb.Add("setTimeout(function()");
                sb.Add("{");
            }

            if (Script != null)
            {
                if (Script.Length > 0)
                {
                    sb.Add(Script.ToString());
                    Script = null;
                    hasScript = true;
                }
            }

            if (GroupingScripts != null && GroupingScripts.Count > 0)
            {
                foreach (var script in GroupingScripts.OrderBy(x => x.Key))
                {
                    sb.Add(script.Value.ToString());
                }

                GroupingScripts = null;
                hasScript = true;
            }

            if(runningScriptDelay > 0)
            {
                sb.Add("}}, {0});", runningScriptDelay.ToString());
            }

            sb.Add("});");
            sb.Add("</script>");

            if(!hasScript)
            {
                sb = sb.Remove(oldLength, sb.Length - oldLength);
            }

            SerializedType = null;

            return new MvcHtmlString(sb.ToString());
        }
Example #6
0
        // ReSharper restore MethodOverloadWithOptionalParameter
        // ReSharper disable MethodOverloadWithOptionalParameter
        public static void AddScript(string groupName, string script, params object[] args)
        {
            if (GroupingScripts == null)
                GroupingScripts = new Dictionary<string, StringBuilder>();

            if (GroupingScripts.ContainsKey(groupName))
            {
                GroupingScripts[groupName].Add(script, args);
            }
            else
            {
                var sb = new StringBuilder();
                sb.Add(script, args);

                GroupingScripts.Add(groupName, sb);
            }
        }