f48247938acd84ba957b0f57bb5a28f564141995
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 (function( $, app ) {
17
18         var ui = app.ns("ui");
19
20         ui.JsonPretty = ui.AbstractWidget.extend({
21                 defaults: {
22                         obj: null
23                 },
24                 init: function(parent) {
25                         this._super();
26                         this.el = $(this._main_template());
27                         this.attach(parent);
28                         this.el.click(this._click_handler);
29                 },
30                 
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();
36                 },
37                 
38                 _main_template: function() {
39                         try {
40                                         return { tag: "DIV", cls: "uiJsonPretty", children: this.pretty.parse(this.config.obj) };
41                         }       catch (error) {
42                                         throw "JsonPretty error: " + error.message;
43                         }
44                 },
45                 
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" : "";
49                         },
50                         "parse": function (member) {
51                                 return this[(member == null) ? 'null' : member.constructor.name.toLowerCase()](member);
52                         },
53                         "null": function (value) {
54                                 return this['value']('null', 'null');
55                         },
56                         "array": function (value) {
57                                 var results = [];
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 ) {
62                                                 results.push(",");
63                                         }
64                                 }, this);
65                                 return [ "[ ", ((results.length > 0) ? { tag: "UL", cls: "uiJsonPretty-array", children: results } : null), "]" ];
66                         },
67                         "object": function (value) {
68                                 var results = [];
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 ) {
74                                                 children.push(",");
75                                         }
76                                         results.push( { tag: "LI", cls: this.expando( value[ key ] ), children: children } );
77                                 }, this);
78                                 return [ "{ ", ((results.length > 0) ? { tag: "UL", cls: "uiJsonPretty-object", children: results } : null ),  "}" ];
79                         },
80                         "number": function (value) {
81                                 return this['value']('number', value.toString());
82                         },
83                         "string": function (value) {
84                                 if (/^(http|https|file):\/\/[^\s]+$/.test(value)) {
85                                         return this['link']( value );
86                                 } else {
87                                         return this['value']('string', '"' + value.toString() + '"');
88                                 }
89                         },
90                         "boolean": function (value) {
91                                 return this['value']('boolean', value.toString());
92                         },
93                         "link": function( value ) {
94                                         return this['value']("string", { tag: "A", href: value, target: "_blank", text: '"' + value + '"' } );
95                         },
96                         "value": function (type, value) {
97                                 if (/^(http|https|file):\/\/[^\s]+$/.test(value)) {
98                                 }
99                                 return { tag: "SPAN", cls: "uiJsonPretty-" + type, text: value };
100                         }
101                 }
102         });
103
104 })( this.jQuery, this.app );