2 * Copyright 2010-2013 Ben Birch
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this software except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 var ui = app.ns("ui");
20 ui.JsonPretty = ui.AbstractWidget.extend({
24 init: function(parent) {
26 this.el = $(this._main_template());
28 this.el.click(this._click_handler);
31 _click_handler: function(jEv) {
32 var t = $(jEv.target).closest(".uiJsonPretty-name").closest("LI");
33 if(t.length === 0 || t.parents(".uiJsonPretty-minimised").length > 0) { return; }
34 t.toggleClass("uiJsonPretty-minimised");
35 jEv.stopPropagation();
38 _main_template: function() {
40 return { tag: "DIV", cls: "uiJsonPretty", children: this.pretty.parse(this.config.obj) };
42 throw "JsonPretty error: " + error.message;
46 pretty: { // from https://github.com/RyanAmos/Pretty-JSON/blob/master/pretty_json.js
47 "expando" : function(value) {
48 return (value && (/array|object/i).test(value.constructor.name)) ? "expando" : "";
50 "parse": function (member) {
51 return this[(member == null) ? 'null' : member.constructor.name.toLowerCase()](member);
53 "null": function (value) {
54 return this['value']('null', 'null');
56 "array": function (value) {
58 var lastItem = value.length - 1;
59 value.forEach(function( v, i ) {
60 results.push({ tag: "LI", cls: this.expando(v), children: [ this['parse'](v) ] });
61 if( i !== lastItem ) {
65 return [ "[ ", ((results.length > 0) ? { tag: "UL", cls: "uiJsonPretty-array", children: results } : null), "]" ];
67 "object": function (value) {
69 var keys = Object.keys( value );
70 var lastItem = keys.length - 1;
71 keys.forEach( function( key, i ) {
72 var children = [ this['value']( 'name', '"' + key + '"' ), ": ", this['parse']( value[ key ]) ];
73 if( i !== lastItem ) {
76 results.push( { tag: "LI", cls: this.expando( value[ key ] ), children: children } );
78 return [ "{ ", ((results.length > 0) ? { tag: "UL", cls: "uiJsonPretty-object", children: results } : null ), "}" ];
80 "number": function (value) {
81 return this['value']('number', value.toString());
83 "string": function (value) {
84 if (/^(http|https|file):\/\/[^\s]+$/.test(value)) {
85 return this['link']( value );
87 return this['value']('string', '"' + value.toString() + '"');
90 "boolean": function (value) {
91 return this['value']('boolean', value.toString());
93 "link": function( value ) {
94 return this['value']("string", { tag: "A", href: value, target: "_blank", text: '"' + value + '"' } );
96 "value": function (type, value) {
97 if (/^(http|https|file):\/\/[^\s]+$/.test(value)) {
99 return { tag: "SPAN", cls: "uiJsonPretty-" + type, text: value };
104 })( this.jQuery, this.app );