WebInspector.SourceView = function(resource)
{
WebInspector.ResourceView.call(this, resource);
this.element.addStyleClass("source");
var canEditScripts = WebInspector.panels.scripts.canEditScripts() && resource.type === WebInspector.Resource.Type.Script;
this.sourceFrame = new WebInspector.SourceFrame(this.contentElement, this._addBreakpoint.bind(this), this._removeBreakpoint.bind(this), canEditScripts ? this._editLine.bind(this) : null);
resource.addEventListener("finished", this._resourceLoadingFinished, this);
this._frameNeedsSetup = true;
}
WebInspector.SourceView.DefaultMIMETypeForResourceType = {
0: "text/html",
1: "text/css",
4: "text/javascript"
}
WebInspector.SourceView.prototype = {
show: function(parentElement)
{
WebInspector.ResourceView.prototype.show.call(this, parentElement);
this.sourceFrame.visible = true;
this.resize();
},
hide: function()
{
WebInspector.View.prototype.hide.call(this);
this.sourceFrame.visible = false;
this._currentSearchResultIndex = -1;
},
resize: function()
{
if (this.sourceFrame)
this.sourceFrame.resize();
},
setupSourceFrameIfNeeded: function()
{
if (!this._frameNeedsSetup)
return;
this.attach();
delete this._frameNeedsSetup;
WebInspector.getResourceContent(this.resource.identifier, this._contentLoaded.bind(this));
},
hasContentTab: function()
{
return true;
},
contentTabSelected: function()
{
this.setupSourceFrameIfNeeded();
},
_contentLoaded: function(content)
{
var mimeType = this._canonicalMimeType(this.resource);
this.sourceFrame.setContent(mimeType, content, this.resource.url);
this._sourceFrameSetupFinished();
},
_canonicalMimeType: function(resource)
{
return WebInspector.SourceView.DefaultMIMETypeForResourceType[resource.type] || resource.mimeType;
},
_resourceLoadingFinished: function(event)
{
this._frameNeedsSetup = true;
this._sourceFrameSetup = false;
if (this.visible)
this.setupSourceFrameIfNeeded();
this.resource.removeEventListener("finished", this._resourceLoadingFinished, this);
},
_addBreakpoint: function(line)
{
var sourceID = this._sourceIDForLine(line);
if (WebInspector.panels.scripts) {
var breakpoint = new WebInspector.Breakpoint(this.resource.url, line, sourceID);
WebInspector.panels.scripts.addBreakpoint(breakpoint);
}
},
_removeBreakpoint: function(breakpoint)
{
if (WebInspector.panels.scripts)
WebInspector.panels.scripts.removeBreakpoint(breakpoint);
},
_editLine: function(line, newContent)
{
var lines = [];
var textModel = this.sourceFrame.textModel;
for (var i = 0; i < textModel.linesCount; ++i) {
if (i === line)
lines.push(newContent);
else
lines.push(textModel.line(i));
}
var linesCountToShift = newContent.split("\n").length - 1;
WebInspector.panels.scripts.editScriptSource(this._sourceIDForLine(line), lines.join("\n"), line, linesCountToShift, this._editLineComplete.bind(this));
},
_editLineComplete: function(newBody)
{
this.sourceFrame.updateContent(newBody);
},
_sourceIDForLine: function(line)
{
var sourceID = null;
var closestStartingLine = 0;
var scripts = this.resource.scripts;
for (var i = 0; i < scripts.length; ++i) {
var script = scripts[i];
if (script.startingLine <= line && script.startingLine >= closestStartingLine) {
closestStartingLine = script.startingLine;
sourceID = script.sourceID;
}
}
return sourceID;
},
searchCanceled: function()
{
this._currentSearchResultIndex = -1;
this._searchResults = [];
this.sourceFrame.clearMarkedRange();
delete this._delayedFindSearchMatches;
},
performSearch: function(query, finishedCallback)
{
this.searchCanceled();
this._searchFinishedCallback = finishedCallback;
function findSearchMatches(query, finishedCallback)
{
this._searchResults = this.sourceFrame.findSearchMatches(query);
if (this._searchResults)
finishedCallback(this, this._searchResults.length);
}
if (!this._sourceFrameSetup) {
this._delayedFindSearchMatches = findSearchMatches.bind(this, query, finishedCallback);
this.setupSourceFrameIfNeeded();
return;
}
findSearchMatches.call(this, query, finishedCallback);
},
jumpToFirstSearchResult: function()
{
if (!this._searchResults || !this._searchResults.length)
return;
this._currentSearchResultIndex = 0;
this._jumpToSearchResult(this._currentSearchResultIndex);
},
jumpToLastSearchResult: function()
{
if (!this._searchResults || !this._searchResults.length)
return;
this._currentSearchResultIndex = (this._searchResults.length - 1);
this._jumpToSearchResult(this._currentSearchResultIndex);
},
jumpToNextSearchResult: function()
{
if (!this._searchResults || !this._searchResults.length)
return;
if (++this._currentSearchResultIndex >= this._searchResults.length)
this._currentSearchResultIndex = 0;
this._jumpToSearchResult(this._currentSearchResultIndex);
},
jumpToPreviousSearchResult: function()
{
if (!this._searchResults || !this._searchResults.length)
return;
if (--this._currentSearchResultIndex < 0)
this._currentSearchResultIndex = (this._searchResults.length - 1);
this._jumpToSearchResult(this._currentSearchResultIndex);
},
showingFirstSearchResult: function()
{
return (this._currentSearchResultIndex === 0);
},
showingLastSearchResult: function()
{
return (this._searchResults && this._currentSearchResultIndex === (this._searchResults.length - 1));
},
revealLine: function(lineNumber)
{
this.setupSourceFrameIfNeeded();
this.sourceFrame.revealLine(lineNumber);
},
highlightLine: function(lineNumber)
{
this.setupSourceFrameIfNeeded();
this.sourceFrame.highlightLine(lineNumber);
},
addMessage: function(msg)
{
this.sourceFrame.addMessage(msg);
},
clearMessages: function()
{
this.sourceFrame.clearMessages();
},
_jumpToSearchResult: function(index)
{
var foundRange = this._searchResults[index];
if (!foundRange)
return;
this.sourceFrame.markAndRevealRange(foundRange);
},
_sourceFrameSetupFinished: function()
{
this._sourceFrameSetup = true;
this.resize();
if (this._delayedFindSearchMatches) {
this._delayedFindSearchMatches();
delete this._delayedFindSearchMatches;
}
}
}
WebInspector.SourceView.prototype.__proto__ = WebInspector.ResourceView.prototype;