Skip to content

Instantly share code, notes, and snippets.

Created December 29, 2014 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/d83c3cf7d1c685389f08 to your computer and use it in GitHub Desktop.
Save anonymous/d83c3cf7d1c685389f08 to your computer and use it in GitHub Desktop.
Formatter = function(simpleFormatter)
{
this._simpleFormatter = simpleFormatter;
}
Formatter.prototype = {
header: function(object)
{
if ((object instanceof Node))
return null;
var header = new JsonMLElement("span");
header.createTextChild(this._simpleFormatter.description(object));
return header.toJsonML();
},
hasBody: function(object)
{
if (object instanceof Array)
return false;
return this._simpleFormatter.hasChildren(object);
},
body: function(object)
{
var body = new JsonMLElement("ol");
body.setStyle("list-style-type:none; padding-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-left: 12px");
var children = this._simpleFormatter.children(object);
for (var i = 0; i < children.length; ++i) {
var child = children[i];
var li = body.createChild("li");
var objectTag;
if (typeof child.value === "object")
objectTag = li.createObjectTag(child.value);
else
objectTag = li.createChild("span");
var nameSpan = objectTag.createChild("span");
nameSpan.createTextChild(child.key + ": ");
nameSpan.setStyle("color: rgb(136, 19, 145);");
if (child.value instanceof Node) {
var node = child.value;
objectTag.createTextChild(node.nodeName.toLowerCase());
if (node.id)
objectTag.createTextChild("#" + node.id)
else
objectTag.createTextChild("." + node.className)
}
if (typeof child.value !== "object")
objectTag.createTextChild("" + child.value);
}
return body.toJsonML();
},
_arrayFormatter: function(array)
{
var j = new JsonMLElement();
j.createTextChild("[");
for (var i = 0; i < array.length; ++i) {
if (i != 0)
j.createTextChild(", ")
j.createObjectTag(array[i]);
}
j.createTextChild("]");
return j;
}
}
JsonMLElement = function(tagName)
{
this._attributes = {};
this._jsonML = [tagName, this._attributes];
}
JsonMLElement.prototype = {
createChild: function(tagName)
{
var c = new JsonMLElement(tagName);
this._jsonML.push(c.toJsonML());
return c;
},
createObjectTag: function(object)
{
var tag = this.createChild("object");
tag.addAttribute("object", object);
return tag;
},
setStyle: function(style)
{
this._attributes["style"] = style;
},
addAttribute: function(key, value)
{
this._attributes[key] = value;
},
createTextChild: function(text)
{
this._jsonML.push(text + "");
},
toJsonML: function()
{
return this._jsonML;
}
}
function SimpleFormatter()
{
}
SimpleFormatter.prototype = {
description: function(object)
{
if ((typeof object === "object") && object)
return object.constructor.name;
return object;
},
hasChildren: function(object)
{
return (typeof object === "object");
},
children: function(object)
{
var result = [];
for (var key in object)
result.push({key: key, value: object[key]});
return result;
}
}
window["devtoolsFormatter"] = new Formatter(new SimpleFormatter());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment