Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add syntax highlighting of JSON code in Futon.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@733897 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cmlenz committed Jan 12, 2009
1 parent 6682f09 commit 2031b1464136c5a1168994658d2a0b8e0fdf79a2
Showing 13 changed files with 156 additions and 109 deletions.
@@ -23,7 +23,6 @@
<script src="script/jquery.cookies.js?0.9.0"></script>
<script src="script/jquery.couch.js?0.9.0"></script>
<script src="script/futon.js?0.9.0"></script>
<script src="script/pprint.js?0.9.0"></script>
<script>
$(document).ready(function() {
$(document.body).addClass("loading");
@@ -24,7 +24,6 @@
<script src="script/jquery.couch.js?0.9.0"></script>
<script src="script/couch.js?0.9.0"></script>
<script src="script/futon.js?0.9.0"></script>
<script src="script/pprint.js?0.9.0"></script>
<script src="script/couch_test_runner.js"></script>
<script>
$(function() {
@@ -27,7 +27,7 @@
<script src="script/jquery.suggest.js?0.9.0"></script>
<script src="script/futon.js?0.9.0"></script>
<script src="script/browse.js?0.9.0"></script>
<script src="script/pprint.js?0.9.0"></script>
<script src="script/futon.format.js?0.9.0"></script>
<script>
var page = new CouchDatabasePage();

@@ -27,7 +27,7 @@
<script src="script/jquery.resizer.js?0.9.0"></script>
<script src="script/futon.js?0.9.0"></script>
<script src="script/browse.js?0.9.0"></script>
<script src="script/pprint.js?0.9.0"></script>
<script src="script/futon.format.js?0.9.0"></script>
<script>
var page = new CouchDocumentPage();

@@ -59,7 +59,7 @@
$("#fields thead th:first").text("Source").attr("colspan", 2).next().hide();
$("#fields tbody.content").hide();
$("#fields tbody.source").find("td").each(function() {
$(this).html($("<code></code>").text(prettyPrintJSON(page.doc)));
$(this).html($("<pre></pre>").html($.futon.formatJSON(page.doc, {html: true})));
}).end().show();
});

@@ -25,7 +25,7 @@
<script src="script/jquery.dialog.js?0.9.0"></script>
<script src="script/futon.js?0.9.0"></script>
<script src="script/browse.js?0.9.0"></script>
<script src="script/pprint.js?0.9.0"></script>
<script src="script/futon.format.js?0.9.0"></script>
<script>
var page = new CouchIndexPage();
$(document).ready(function() {
@@ -23,7 +23,6 @@
<script src="script/jquery.cookies.js?0.9.0"></script>
<script src="script/jquery.couch.js?0.9.0"></script>
<script src="script/futon.js?0.9.0"></script>
<script src="script/pprint.js?0.9.0"></script>
<script>
$(document).ready(function() {
$("fieldset input[type=radio]").click(function() {
@@ -59,7 +59,7 @@ function CouchIndexPage() {
$.couch.db(dbName).info({
success: function(info) {
$("#databases tbody.content tr:eq(" + idx + ")")
.find("td.size").text(prettyPrintSize(info.disk_size)).end()
.find("td.size").text($.futon.formatSize(info.disk_size)).end()
.find("td.count").text(info.doc_count).end()
.find("td.seq").text(info.update_seq);
if (idx == dbsOnPage.length - 1) {
@@ -457,22 +457,28 @@ function CouchDatabasePage() {
for (var i = 0; i < resp.rows.length; i++) {
var row = resp.rows[i];
var tr = $("<tr></tr>");
var key = row.key;
var key = "null";
if (row.key !== null) {
key = $.futon.formatJSON(row.key, {indent: 0, linesep: ""});
}
if (row.id) {
$("<td class='key'><a href='document.html?" + encodeURIComponent(db.name) +
"/" + encodeDocId(row.id) + "'><strong></strong><br>" +
"<span class='docid'>ID:&nbsp;" + row.id + "</span></a></td>")
.find("strong").text(key !== null ? prettyPrintJSON(key, 0, "") : "null").end()
.find("strong").text(key).end()
.appendTo(tr);
} else {
$("<td class='key'><strong></strong></td>")
.find("strong").text(key !== null ? prettyPrintJSON(key, 0, "") : "null").end()
.find("strong").text(key).end()
.appendTo(tr);
}
var value = row.value;
$("<td class='value'></td>").text(
value !== null ? prettyPrintJSON(value, 0, "") : "null"
).appendTo(tr).dblclick(function() {
var value = "null";
if (row.value !== null) {
value = $.futon.formatJSON(row.value, {
html: true, indent: 0, linesep: "", quoteKeys: false
});
}
$("<td class='value'><div></div></td>").find("div").html(value).end().appendTo(tr).dblclick(function() {
location.href = this.previousSibling.firstChild.href;
});
tr.appendTo("#documents tbody.content");
@@ -736,7 +742,7 @@ function CouchDocumentPage() {
_initValue(value);
}
}
$("#fields tbody tr").removeClass("odd").filter(":odd").addClass("odd");
$("#fields tbody.content tr").removeClass("odd").filter(":odd").addClass("odd");
return row;
}

@@ -838,7 +844,7 @@ function CouchDocumentPage() {
cancelChange();
}).appendTo(tools);
tools.appendTo(td);
input.val(prettyPrintJSON(value)).appendTo(td);
input.val($.futon.formatJSON(value)).appendTo(td);
input.each(function() { this.focus(); this.select(); });
if (needsTextarea) input.makeResizable({vertical: true});
}
@@ -849,7 +855,7 @@ function CouchDocumentPage() {
delete doc[fieldName];
row.remove();
page.isDirty = true;
$("#fields tbody tr").removeClass("odd").filter(":odd").addClass("odd");
$("#fields tbody.content tr").removeClass("odd").filter(":odd").addClass("odd");
}).prependTo(row.find("th"));
}
}
@@ -894,7 +900,7 @@ function CouchDocumentPage() {
$("<a href='' title='Download file' target='_top'></a>").text(name)
.attr("href", attachmentHref)
.wrapInner("<tt></tt>").appendTo(li);
$("<span>()</span>").text("" + prettyPrintSize(attachment.length) +
$("<span>()</span>").text("" + $.futon.formatSize(attachment.length) +
", " + attachment.content_type).addClass("info").appendTo(li);
if (name == "tests.js") {
li.find('span.info').append(', <a href="/_utils/couch_tests.html?'
@@ -0,0 +1,118 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

(function($) {
$.futon = $.futon || {};
$.extend($.futon, {

// JSON pretty printing
formatJSON: function(val, options) {
options = options || {};
if (options.indent === undefined) {
options.indent = 4;
}
options.indent = options.indent !== undefined ? options.indent : 4;
options.linesep = options.linesep !== undefined ? options.linesep : "\n";
options.quoteKeys = options.quoteKeys !== undefined ? options.quoteKeys : true;
var itemsep = options.linesep.length ? "," + options.linesep : ", ";

function escape(string) {
return string.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}

function format(val, depth) {
var tab = [];
for (var i = 0; i < options.indent * depth; i++) tab.push("");
tab = tab.join(" ");

var type = typeof val;
switch (type) {
case "boolean":
case "number":
case "string":
var retval = JSON.stringify(val);
if (options.html) {
retval = "<code class='" + type + "'>" + escape(retval) + "</code>";
}
return retval;

case "object": {
if (val === null) {
if (options.html) {
return "<code class='null'>null</code>";
}
return "null";
}
if (val.constructor == Date) {
return JSON.stringify(val);
}

var buf = [];

if (val.constructor == Array) {
buf.push("[");
for (var index = 0; index < val.length; index++) {
buf.push(index > 0 ? itemsep : options.linesep);
buf.push(tab, format(val[index], depth + 1));
}
if (index >= 0) buf.push(options.linesep, tab.substr(options.indent));
buf.push("]");

} else {
buf.push("{");
var index = 0;
for (var key in val) {
if (!val.hasOwnProperty(key)) continue;
buf.push(index > 0 ? itemsep : options.linesep);
var keyDisplay = options.quoteKeys ? JSON.stringify(key) : key;
if (options.html) {
if (options.quoteKeys) {
keyDisplay = keyDisplay.substr(1, keyDisplay.length - 2);
}
keyDisplay = "<code class='key'>" + escape(keyDisplay) + "</code>";
if (options.quoteKeys) {
keyDisplay = '"' + keyDisplay + '"';
}
}
buf.push(tab, keyDisplay,
": ", format(val[key], depth + 1));
index++;
}
if (index >= 0) buf.push(options.linesep, tab.substr(options.indent));
buf.push("}");
}

return buf.join("");
}
}
}

return format(val, 1);
},

// File size pretty printing
formatSize: function(size) {
var jump = 512;
if (size < jump) return size + " bytes";
var units = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
var i = 0;
while (size >= jump && i < units.length) {
i += 1;
size /= 1024
}
return size.toFixed(1) + ' ' + units[i - 1];
}

});
})(jQuery);
@@ -103,7 +103,7 @@
}

$.futon = $.futon || {};
$.fn.extend($.futon, {
$.extend($.futon, {
navigation: new Navigation()
});

@@ -12,7 +12,7 @@

(function($) {
$.cookies = $.cookies || {}
$.fn.extend($.cookies, {
$.extend($.cookies, {

/* Return the value of a cookie. */
get: function(name, defaultValue) {
@@ -12,7 +12,7 @@

(function($) {
$.couch = $.couch || {};
$.fn.extend($.couch, {
$.extend($.couch, {

allDbs: function(options) {
options = options || {};

This file was deleted.

0 comments on commit 2031b14

Please sign in to comment.