1 /* See license.txt for terms of usage */
  2 
  3 FBL.ns(function() { with (FBL) {
  4 
  5 // ************************************************************************************************
  6 
  7 // List of JSON content types.
  8 var contentTypes =
  9 {
 10     "text/plain": 1,
 11     "text/javascript": 1,
 12     "text/x-javascript": 1,
 13     "text/json": 1,
 14     "text/x-json": 1,
 15     "application/json": 1,
 16     "application/x-json": 1,
 17     "application/javascript": 1,
 18     "application/x-javascript": 1
 19 };
 20 
 21 // ************************************************************************************************
 22 // Model implementation
 23 
 24 Firebug.JSONViewerModel = extend(Firebug.Module,
 25 {
 26     dispatchName: "jsonViewer",
 27     initialize: function()
 28     {
 29         Firebug.NetMonitor.NetInfoBody.addListener(this);
 30 
 31         // Used by Firebug.DOMPanel.DirTable domplate.
 32         this.toggles = {};
 33     },
 34 
 35     shutdown: function()
 36     {
 37         Firebug.NetMonitor.NetInfoBody.removeListener(this);
 38     },
 39 
 40     initTabBody: function(infoBox, file)
 41     {
 42         if (FBTrace.DBG_JSONVIEWER)
 43             FBTrace.sysout("jsonviewer.initTabBody", infoBox);
 44 
 45         // Let listeners to parse the JSON.
 46         dispatch(this.fbListeners, "onParseJSON", [file]);
 47 
 48         // The JSON is still no there, try to parse most common cases.
 49         if (!file.jsonObject)
 50         {
 51             if (this.isJSON(safeGetContentType(file.request)))
 52                 file.jsonObject = this.parseJSON(file);
 53         }
 54 
 55         // The jsonObject is created so, the JSON tab can be displayed.
 56         if (file.jsonObject && hasProperties(file.jsonObject))
 57         {
 58             Firebug.NetMonitor.NetInfoBody.appendTab(infoBox, "JSON",
 59                 $STR("jsonviewer.tab.JSON"));
 60 
 61             if (FBTrace.DBG_JSONVIEWER)
 62                 FBTrace.sysout("jsonviewer.initTabBody; JSON object available " +
 63                     (typeof(file.jsonObject) != "undefined"), file.jsonObject);
 64         }
 65     },
 66 
 67     isJSON: function(contentType)
 68     {
 69         if (!contentType)
 70             return false;
 71 
 72         contentType = contentType.split(";")[0];
 73         contentType = trim(contentType);
 74         return contentTypes[contentType];
 75     },
 76 
 77     // Update listener for TabView
 78     updateTabBody: function(infoBox, file, context)
 79     {
 80         var tab = infoBox.selectedTab;
 81         var tabBody = infoBox.getElementsByClassName("netInfoJSONText").item(0);
 82         if (!hasClass(tab, "netInfoJSONTab") || tabBody.updated)
 83             return;
 84 
 85         tabBody.updated = true;
 86 
 87         if (file.jsonObject) {
 88             Firebug.DOMPanel.DirTable.tag.replace(
 89                  {object: file.jsonObject, toggles: this.toggles}, tabBody);
 90         }
 91     },
 92 
 93     parseJSON: function(file)
 94     {
 95         var jsonString = new String(file.responseText);
 96         return parseJSONString(jsonString, "http://" + file.request.originalURI.host);
 97     },
 98 });
 99 
100 // ************************************************************************************************
101 // Registration
102 
103 Firebug.registerModule(Firebug.JSONViewerModel);
104 
105 // ************************************************************************************************
106 }});
107