WebInspector.SourceView = function(resource)
{
this.sourceFrame = new WebInspector.SourceFrame(null, this._addBreakpoint.bind(this));
WebInspector.ResourceView.call(this, resource);
resource.addEventListener("finished", this._resourceLoadingFinished, this);
this.element.addStyleClass("source");
this._frameNeedsSetup = true;
this.contentElement.appendChild(this.sourceFrame.element);
var gutterElement = document.createElement("div");
gutterElement.className = "webkit-line-gutter-backdrop";
this.element.appendChild(gutterElement);
}
WebInspector.SourceView.prototype = {
set headersVisible(x)
{
if (x === this._headersVisible)
return;
var superSetter = WebInspector.ResourceView.prototype.__lookupSetter__("headersVisible");
if (superSetter)
superSetter.call(this, x);
this.sourceFrame.autoSizesToFitContentHeight = x;
},
show: function(parentElement)
{
WebInspector.ResourceView.prototype.show.call(this, parentElement);
this.setupSourceFrameIfNeeded();
},
hide: function()
{
WebInspector.View.prototype.hide.call(this);
this._currentSearchResultIndex = -1;
},
resize: function()
{
if (this.sourceFrame.autoSizesToFitContentHeight)
this.sourceFrame.sizeToFitContentHeight();
},
detach: function()
{
WebInspector.ResourceView.prototype.detach.call(this);
this._frameNeedsSetup = true;
this._sourceFrameSetup = false;
},
setupSourceFrameIfNeeded: function()
{
if (!this._frameNeedsSetup)
return;
this.attach();
delete this._frameNeedsSetup;
this.sourceFrame.addEventListener("content loaded", this._contentLoaded, this);
InspectorController.addResourceSourceToFrame(this.resource.identifier, this.sourceFrame.element);
},
_contentLoaded: function()
{
delete this._frameNeedsSetup;
this.sourceFrame.removeEventListener("content loaded", this._contentLoaded, this);
if (this.resource.type === WebInspector.Resource.Type.Script) {
this.sourceFrame.addEventListener("syntax highlighting complete", this._syntaxHighlightingComplete, this);
this.sourceFrame.syntaxHighlightJavascript();
} else
this._sourceFrameSetupFinished();
},
_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 = 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;
}
}
if (WebInspector.panels.scripts) {
var breakpoint = new WebInspector.Breakpoint(this.resource.url, line, sourceID);
WebInspector.panels.scripts.addBreakpoint(breakpoint);
}
},
searchCanceled: function()
{
this._currentSearchResultIndex = -1;
this._searchResults = [];
delete this._delayedFindSearchMatches;
},
performSearch: function(query, finishedCallback)
{
this.searchCanceled();
var lineQueryRegex = /(^|\s)(?:#|line:\s*)(\d+)(\s|$)/i;
var lineQueryMatch = query.match(lineQueryRegex);
if (lineQueryMatch) {
var lineToSearch = parseInt(lineQueryMatch[2]);
var lineQueryReplacement = (lineQueryMatch[1] && lineQueryMatch[3] ? " " : "");
var filterlessQuery = query.replace(lineQueryRegex, lineQueryReplacement);
}
this._searchFinishedCallback = finishedCallback;
function findSearchMatches(query, finishedCallback)
{
if (isNaN(lineToSearch)) {
this._searchResults = (InspectorController.search(this.sourceFrame.element.contentDocument, query) || []);
} else {
var sourceRow = this.sourceFrame.sourceRow(lineToSearch);
if (sourceRow) {
if (filterlessQuery) {
this._searchResults = (InspectorController.search(sourceRow, filterlessQuery) || []);
} else {
var rowRange = this.sourceFrame.element.contentDocument.createRange();
rowRange.selectNodeContents(sourceRow);
this._searchResults = [rowRange];
}
}
var wholeQueryMatches = InspectorController.search(this.sourceFrame.element.contentDocument, query);
if (wholeQueryMatches)
this._searchResults = this._searchResults.concat(wholeQueryMatches);
}
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;
var selection = this.sourceFrame.element.contentWindow.getSelection();
selection.removeAllRanges();
selection.addRange(foundRange);
if (foundRange.startContainer.scrollIntoViewIfNeeded)
foundRange.startContainer.scrollIntoViewIfNeeded(true);
else if (foundRange.startContainer.parentNode)
foundRange.startContainer.parentNode.scrollIntoViewIfNeeded(true);
},
_sourceFrameSetupFinished: function()
{
this._sourceFrameSetup = true;
if (this._delayedFindSearchMatches) {
this._delayedFindSearchMatches();
delete this._delayedFindSearchMatches;
}
},
_syntaxHighlightingComplete: function(event)
{
this._sourceFrameSetupFinished();
this.sourceFrame.removeEventListener("syntax highlighting complete", null, this);
}
}
WebInspector.SourceView.prototype.__proto__ = WebInspector.ResourceView.prototype;