WebInspector.SourcePanel = function(resource, views)
{
var allViews = [{ title: WebInspector.UIString("Source"), name: "source" }];
if (views)
allViews = allViews.concat(views);
WebInspector.ResourcePanel.call(this, resource, allViews);
this.currentView = this.views.source;
var sourceView = this.views.source;
sourceView.messages = [];
sourceView.frameNeedsSetup = true;
sourceView.frameElement = document.createElement("iframe");
sourceView.frameElement.setAttribute("viewsource", "true");
sourceView.contentElement.appendChild(sourceView.frameElement);
}
WebInspector.SourcePanel.prototype = {
show: function()
{
WebInspector.ResourcePanel.prototype.show.call(this);
this.setupSourceFrameIfNeeded();
},
setupSourceFrameIfNeeded: function()
{
if (this.views.source.frameNeedsSetup) {
this.attach();
InspectorController.addSourceToFrame(this.resource.identifier, this.views.source.frameElement);
WebInspector.addMainEventListeners(this.views.source.frameElement.contentDocument);
var length = this.views.source.messages;
for (var i = 0; i < length; ++i)
this._addMessageToSource(this.views.source.messages[i]);
delete this.views.source.frameNeedsSetup;
}
},
sourceRow: function(lineNumber)
{
this.setupSourceFrameIfNeeded();
var doc = this.views.source.frameElement.contentDocument;
var rows = doc.getElementsByTagName("table")[0].rows;
--lineNumber;
if (lineNumber >= rows.length)
lineNumber = rows.length - 1;
return rows[lineNumber];
},
showSourceLine: function(lineNumber)
{
var row = this.sourceRow(lineNumber);
if (!row)
return;
this.currentView = this.views.source;
row.scrollIntoView(true);
},
addMessageToSource: function(msg)
{
this.views.source.messages.push(msg);
if (!this.views.source.frameNeedsSetup)
this._addMessageToSource(msg);
},
_addMessageToSource: function(msg)
{
var row = this.sourceRow(msg.line);
if (!row)
return;
var doc = this.views.source.frameElement.contentDocument;
var cell = row.getElementsByTagName("td")[1];
var errorDiv = cell.lastChild;
if (!errorDiv || errorDiv.nodeName.toLowerCase() !== "div" || !errorDiv.hasStyleClass("webkit-html-message-bubble")) {
errorDiv = doc.createElement("div");
errorDiv.className = "webkit-html-message-bubble";
cell.appendChild(errorDiv);
}
var imageURL;
switch (msg.level) {
case WebInspector.ConsoleMessage.MessageLevel.Error:
errorDiv.addStyleClass("webkit-html-error-message");
imageURL = "Images/errorIcon.png";
break;
case WebInspector.ConsoleMessage.MessageLevel.Warning:
errorDiv.addStyleClass("webkit-html-warning-message");
imageURL = "Images/warningIcon.png";
break;
}
var lineDiv = doc.createElement("div");
lineDiv.className = "webkit-html-message-line";
errorDiv.appendChild(lineDiv);
var image = doc.createElement("img");
image.src = imageURL;
image.className = "webkit-html-message-icon";
lineDiv.appendChild(image);
lineDiv.appendChild(doc.createTextNode(msg.message));
}
}
WebInspector.SourcePanel.prototype.__proto__ = WebInspector.ResourcePanel.prototype;