// Copyright © 2013-2017 David Caldwell // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // Usage // ----- // The module exports one entry point, the `renderjson()` function. It takes in // the JSON you want to render as a single argument and returns an HTML // element. // // Options // ------- // renderjson.set_icons("+", "-") // This Allows you to override the disclosure icons. // // renderjson.set_show_to_level(level) // Pass the number of levels to expand when rendering. The default is 0, which // starts with everything collapsed. As a special case, if level is the string // "all" then it will start with everything expanded. // // renderjson.set_max_string_length(length) // Strings will be truncated and made expandable if they are longer than // `length`. As a special case, if `length` is the string "none" then // there will be no truncation. The default is "none". // // renderjson.set_sort_objects(sort_bool) // Sort objects by key (default: false) // // renderjson.set_replacer(replacer_function) // Equivalent of JSON.stringify() `replacer` argument when it's a function // // renderjson.set_collapse_msg(collapse_function) // Accepts a function (len:number):string => {} where len is the length of the // object collapsed. Function should return the message displayed when a // object is collapsed. The default message is "X items" // // renderjson.set_property_list(property_list) // Equivalent of JSON.stringify() `replacer` argument when it's an array // // Theming // ------- // The HTML output uses a number of classes so that you can theme it the way // you'd like: // .disclosure ("⊕", "⊖") // .syntax (",", ":", "{", "}", "[", "]") // .string (includes quotes) // .number // .boolean // .key (object key) // .keyword ("null", "undefined") // .object.syntax ("{", "}") // .array.syntax ("[", "]") var module, window, define, renderjson=(function() { var themetext = function(/* [class, text]+ */) { var spans = []; while (arguments.length) spans.push(append(span(Array.prototype.shift.call(arguments)), text(Array.prototype.shift.call(arguments)))); return spans; }; var append = function(/* el, ... */) { var el = Array.prototype.shift.call(arguments); for (var a=0; a 0 && type != "string") show(); return el; }; if (json === null) return themetext(null, my_indent, "keyword", "null"); if (json === void 0) return themetext(null, my_indent, "keyword", "undefined"); if (typeof(json) == "string" && json.length > options.max_string_length) return disclosure('"', json.substr(0,options.max_string_length)+" ...", '"', "string", function () { return append(span("string"), themetext(null, my_indent, "string", JSON.stringify(json))); }); if (typeof(json) != "object" || [Number, String, Boolean, Date].indexOf(json.constructor) >= 0) // Strings, numbers and bools return themetext(null, my_indent, typeof(json), JSON.stringify(json)); if (json.constructor == Array) { if (json.length == 0) return themetext(null, my_indent, "array syntax", "[]"); return disclosure("[", options.collapse_msg(json.length), "]", "array", function () { var as = append(span("array"), themetext("array syntax", "[", null, "\n")); for (var i=0; i