You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by td...@apache.org on 2012/10/25 03:45:15 UTC
[6/6] DRILL-11 - Literal copy. Looks like we have a few author tags
that should be removed.
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/408168c4/sandbox/web-frontend/lib/jquery.renderjson.js
----------------------------------------------------------------------
diff --git a/sandbox/web-frontend/lib/jquery.renderjson.js b/sandbox/web-frontend/lib/jquery.renderjson.js
new file mode 100644
index 0000000..e5c3640
--- /dev/null
+++ b/sandbox/web-frontend/lib/jquery.renderjson.js
@@ -0,0 +1,61 @@
+/**
+ * RenderJSON plugin for jQuery
+ * Quick HTML visualisation of JSON data
+ * github.com/marekweb/jquery-renderjson
+ */
+(function ($) {
+
+ /**
+ * Renders the given JSON argument as HTML. Recursive for arrays and mapping objects.
+ * @param obj JSON data
+ * @param path Base path to use for building paths to elements (displayed in title attribute)
+ */
+ function renderJSON(obj, path) {
+ path = path || "root";
+ var elem = $('<div class="renderjson-value" title="' + path + '">');
+
+ if (obj instanceof Array) {
+ elem.addClass("renderjson-array");
+ for (var i = 0; i < obj.length; i++) {
+ var pairElem = $('<div class="renderjson-pair">').appendTo(elem);
+ $('<div class="renderjson-key">' + i + ' →</div>').appendTo(pairElem);
+ renderJSON(obj[i], path + "[" + i + "]").appendTo(pairElem);
+
+ }
+ } else if (typeof obj == "string") {
+ elem.addClass("renderjson-scalar renderjson-string").text(obj);
+ elem.html(""" + elem.html() + """);
+
+ } else if (typeof obj == "number") {
+ elem.addClass("renderjson-scalar renderjson-number").text(obj);
+
+ } else if (typeof obj == "boolean") {
+ elem.addClass("renderjson-scalar renderjson-boolean").text(obj);
+
+ } else if (obj === null) {
+ elem.addClass("renderjson-scalar renderjson-null").text("null");
+
+ } else {
+ // Object
+ elem.addClass("renderjson-object");
+ for (var key in obj) {
+
+ if (obj.hasOwnProperty(key)) {
+ var pairElem = $('<div class="renderjson-pair">').appendTo(elem);
+ $('<div class="renderjson-key">' + key + ' →</div>').appendTo(pairElem);
+ renderJSON(obj[key], path + '["' + key + '"]').appendTo(pairElem);
+ }
+ }
+ }
+
+ return elem;
+ }
+
+ /**
+ * Render the given JSON data as HTML inserted into the calling jQuery element.
+ * @param obj JSON data
+ */
+ $.fn.renderJSON = function (obj) {
+ return this.append(renderJSON(obj)).addClass("renderjson-container");
+ };
+})(jQuery);
\ No newline at end of file