[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / public / red / ui / tab-info.js
1 /**
2  * Copyright 2013 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file 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 RED.sidebar.info = (function() {
17     
18     var content = document.createElement("div");
19     content.id = "tab-info";
20     content.style.paddingTop = "4px";
21     content.style.paddingLeft = "4px";
22     content.style.paddingRight = "4px";
23
24     RED.sidebar.addTab("info",content);
25     
26     function jsonFilter(key,value) {
27         if (key === "") {
28             return value;
29         }
30         var t = typeof value;
31         if ($.isArray(value)) {
32             return "[array:"+value.length+"]";
33         } else if (t === "object") {
34             return "[object]"
35         } else if (t === "string") {
36             if (value.length > 30) {
37                 return value.substring(0,30)+" ...";
38             }
39         }
40         return value;
41     }
42     
43     function refresh(node) {
44         var table = '<table class="node-info"><tbody>';
45
46         table += '<tr class="blank"><td colspan="2">Node</td></tr>';
47         table += "<tr><td>Type</td><td>&nbsp;"+node.type+"</td></tr>";
48         table += "<tr><td>ID</td><td>&nbsp;"+node.id+"</td></tr>";
49         table += '<tr class="blank"><td colspan="2">Properties</td></tr>';
50         for (var n in node._def.defaults) {
51             if (node._def.defaults.hasOwnProperty(n)) {
52                 var val = node[n]||"";
53                 var type = typeof val;
54                 if (type === "string") {
55                     if (val.length > 30) { 
56                         val = val.substring(0,30)+" ...";
57                     }
58                     val = val.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
59                 } else if (type === "number") {
60                     val = val.toString();
61                 } else if ($.isArray(val)) {
62                     val = "[<br/>";
63                     for (var i=0;i<Math.min(node[n].length,10);i++) {
64                         var vv = JSON.stringify(node[n][i],jsonFilter," ").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
65                         val += "&nbsp;"+i+": "+vv+"<br/>";
66                     }
67                     if (node[n].length > 10) {
68                         val += "&nbsp;... "+node[n].length+" items<br/>";
69                     }
70                     val += "]";
71                 } else {
72                     val = JSON.stringify(val,jsonFilter," ");
73                     val = val.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
74                 }
75                 
76                 table += "<tr><td>&nbsp;"+n+"</td><td>"+val+"</td></tr>";
77             }
78         }
79         table += "</tbody></table><br/>";
80         table  += '<div class="node-help">'+($("script[data-help-name|='"+node.type+"']").html()||"")+"</div>";
81         $("#tab-info").html(table);
82     }
83     
84     return {
85         refresh:refresh,
86         clear: function() {
87             $("#tab-info").html("");
88         }
89     }
90 })();