An EcmaScriptWriter which also generates a markup version of the data.
Inheritance: JsonFx.Json.EcmaScriptWriter
示例#1
0
        /// <summary>
        /// Renders the data items as a block of JavaScript
        /// </summary>
        /// <param name="writer"></param>
        public void Write(TextWriter writer, IDictionary <string, object> data)
        {
            if (data == null || data.Count < 1)
            {
                // emit nothing when empty
                return;
            }

            List <string> namespaces = new List <string>();

            StringWriter     markup;
            EcmaScriptWriter jsWriter;

            if (this.AutoMarkup == AutoMarkupType.Data)
            {
                markup   = new StringWriter();
                jsWriter = new JsonMarkupWriter(writer, markup);
            }
            else
            {
                markup   = null;
                jsWriter = new EcmaScriptWriter(writer);
            }

            if (this.IsDebug)
            {
                jsWriter.Settings.PrettyPrint = true;
                jsWriter.Settings.NewLine     = Environment.NewLine;
                jsWriter.Settings.Tab         = "\t";
            }

            writer.Write(DataBlockWriter.ScriptOpen);

            foreach (string key in data.Keys)
            {
                if (markup != null)
                {
                    if (this.IsDebug)
                    {
                        markup.WriteLine();
                    }
                    markup.Write("<div title=\"");
                    HttpUtility.HtmlAttributeEncode(key, markup);
                    markup.Write("\">");
                }

                string declaration;
                if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, key, namespaces, this.IsDebug))
                {
                    declaration = "var " + key;
                }
                else
                {
                    declaration = key;
                }

                if (this.IsDebug)
                {
                    writer.Write(DataBlockWriter.VarAssignmentDebug, declaration);
                    if (data[key] != null &&
                        data[key].GetType().IsClass)
                    {
                        writer.WriteLine();
                    }
                }
                else
                {
                    writer.Write(DataBlockWriter.VarAssignment, declaration);
                }

                // emit the value as JSON
                jsWriter.Write(data[key]);
                writer.Write(DataBlockWriter.VarAssignmentEnd);

                if (markup != null)
                {
                    markup.Write("</div>");
                }

                if (this.IsDebug)
                {
                    writer.WriteLine();
                }
            }

            writer.Write(DataBlockWriter.ScriptClose);

            if (markup != null)
            {
                writer.Write(DataBlockWriter.NoScriptOpen);
                writer.Write(markup.ToString());
                writer.Write(DataBlockWriter.NoScriptClose);
            }
        }
        /// <summary>
        /// Renders the data items as a block of JavaScript
        /// </summary>
        /// <param name="writer"></param>
        public void Write(TextWriter writer, IDictionary<string, object> data)
        {
            if (data == null || data.Count < 1)
            {
                // emit nothing when empty
                return;
            }

            List<string> namespaces = new List<string>();

            StringWriter markup;
            EcmaScriptWriter jsWriter;
            if (this.AutoMarkup == AutoMarkupType.Data)
            {
                markup = new StringWriter();
                jsWriter = new JsonMarkupWriter(writer, markup);
            }
            else
            {
                markup = null;
                jsWriter = new EcmaScriptWriter(writer);
            }

            if (this.IsDebug)
            {
                jsWriter.Settings.PrettyPrint = true;
                jsWriter.Settings.NewLine = Environment.NewLine;
                jsWriter.Settings.Tab = "\t";
            }

            writer.Write(DataBlockWriter.ScriptOpen);

            foreach (string key in data.Keys)
            {
                if (markup != null)
                {
                    if (this.IsDebug)
                    {
                        markup.WriteLine();
                    }
                    markup.Write("<div title=\"");
                    HttpUtility.HtmlAttributeEncode(key, markup);
                    markup.Write("\">");
                }

                string declaration;
                if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, key, namespaces, this.IsDebug))
                {
                    declaration = "var "+key;
                }
                else
                {
                    declaration = key;
                }

                if (this.IsDebug)
                {
                    writer.Write(DataBlockWriter.VarAssignmentDebug, declaration);
                    if (data[key] != null &&
                        data[key].GetType().IsClass)
                    {
                        writer.WriteLine();
                    }
                }
                else
                {
                    writer.Write(DataBlockWriter.VarAssignment, declaration);
                }

                // emit the value as JSON
                jsWriter.Write(data[key]);
                writer.Write(DataBlockWriter.VarAssignmentEnd);

                if (markup != null)
                {
                    markup.Write("</div>");
                }

                if (this.IsDebug)
                {
                    writer.WriteLine();
                }
            }

            writer.Write(DataBlockWriter.ScriptClose);

            if (markup != null)
            {
                writer.Write(DataBlockWriter.NoScriptOpen);
                writer.Write(markup.ToString());
                writer.Write(DataBlockWriter.NoScriptClose);
            }
        }
        /// <summary>
        /// Renders the JBST control reference and any stored data to be used.
        /// </summary>
        /// <param name="writer">output</param>
        /// <param name="data">data to be bound as an object which will be serialized</param>
        /// <param name="index">the data index</param>
        /// <param name="count">the total data count</param>
        /// <param name="inner">a callback for writing inner placeholder content</param>
        internal void Write(TextWriter writer, object data, int index, int count, InnerCallback inner)
        {
            if (String.IsNullOrEmpty(this.JbstName))
            {
                throw new ArgumentNullException("JBST Name must be specified.");
            }

            // generate an ID for controls which do not have explicit
            if (String.IsNullOrEmpty(this.ID))
            {
                // happens with no parents
                this.ID = "_"+Guid.NewGuid().ToString("n");
            }

            bool hasInner = (inner != null);
            string placeholder = hasInner ? "div" : "noscript";

            // render the placeholder hook
            writer.Write('<');
            writer.Write(placeholder);
            writer.Write(" id=\"");
            writer.Write(this.ID);
            writer.Write("\">");

            if (hasInner)
            {
                // render inner as loading/error markup
                inner(writer);
            }

            string inlineData = null;
            if (data != null && !(data is EcmaScriptIdentifier) && this.AutoMarkup == AutoMarkupType.Data)
            {
                if (hasInner)
                {
                    writer.Write("<noscript>");
                }

                // serialize InlineData as a JavaScript literal
                StringBuilder builder = new StringBuilder();

                JsonMarkupWriter jsWriter = new JsonMarkupWriter(builder, writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);

                if (hasInner)
                {
                    writer.Write("</noscript>");
                }

                inlineData = builder.ToString();
            }

            writer.Write("</");
            writer.Write(placeholder);
            writer.Write('>');

            // render the binding script
            writer.Write("<script type=\"text/javascript\">");

            writer.Write(this.JbstName);
            writer.Write(".replace(\"");
            writer.Write(this.ID);
            writer.Write("\",");

            if (!String.IsNullOrEmpty(inlineData))
            {
                writer.Write(inlineData);
            }
            else if (data != null)
            {
                // serialize InlineData as a JavaScript literal
                EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);
            }
            else
            {
                // smallest most innocuous default data
                writer.Write("{}");
            }

            if (index >= 0)
            {
                writer.Write(",(");
                writer.Write(index);
                writer.Write(')');

                if (count >= 0)
                {
                    writer.Write(",(");
                    writer.Write(count);
                    writer.Write(')');
                }
            }
            writer.Write(");");

            writer.Write("</script>");
        }