StylesSourceMapping.js [plain text]
WebInspector.StylesSourceMapping = function(cssModel, workspace)
{
this._cssModel = cssModel;
this._workspace = workspace;
this._workspace.addEventListener(WebInspector.Workspace.Events.ProjectWillReset, this._projectWillReset, this);
this._workspace.addEventListener(WebInspector.UISourceCodeProvider.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);
WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameCreatedOrNavigated, this._mainFrameCreatedOrNavigated, this);
WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, this._resourceAdded, this);
this._initialize();
}
WebInspector.StylesSourceMapping.prototype = {
rawLocationToUILocation: function(rawLocation)
{
var location = (rawLocation);
var uiSourceCode = this._workspace.uiSourceCodeForURL(location.url);
return new WebInspector.UILocation(uiSourceCode, location.lineNumber, 0);
},
uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
{
return new WebInspector.CSSLocation(uiSourceCode.url || "", lineNumber);
},
isIdentity: function()
{
return true;
},
_resourceAdded: function(event)
{
var resource = (event.data);
if (resource.contentType() !== WebInspector.resourceTypes.Stylesheet)
return;
if (!resource.url)
return;
var uiSourceCode = this._workspace.uiSourceCodeForURL(resource.url);
if (!uiSourceCode)
return;
this._bindUISourceCode(uiSourceCode);
},
_uiSourceCodeAddedToWorkspace: function(event)
{
var uiSourceCode = (event.data);
if (uiSourceCode.contentType() !== WebInspector.resourceTypes.Stylesheet)
return;
if (!uiSourceCode.url || !WebInspector.resourceForURL(uiSourceCode.url))
return;
this._bindUISourceCode(uiSourceCode);
},
_bindUISourceCode: function(uiSourceCode)
{
if (this._mappedURLs[uiSourceCode.url])
return;
this._mappedURLs[uiSourceCode.url] = true;
uiSourceCode.setSourceMapping(this);
var styleFile = new WebInspector.StyleFile(uiSourceCode);
uiSourceCode.setStyleFile(styleFile);
this._cssModel.setSourceMapping(uiSourceCode.url, this);
},
_projectWillReset: function(event)
{
var project = event.data;
var uiSourceCodes = project.uiSourceCodes();
for (var i = 0; i < uiSourceCodes; ++i)
delete this._mappedURLs[uiSourceCodes[i].url];
},
_initialize: function()
{
this._mappedURLs = {};
},
_mainFrameCreatedOrNavigated: function(event)
{
for (var mappedURL in this._mappedURLs) {
var uiSourceCode = this._workspace.uiSourceCodeForURL(mappedURL);
if (!uiSourceCode)
continue;
uiSourceCode.styleFile().dispose();
uiSourceCode.setStyleFile(null);
uiSourceCode.setSourceMapping(null);
}
this._initialize();
}
}
WebInspector.StyleFile = function(uiSourceCode)
{
this._uiSourceCode = uiSourceCode;
this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
}
WebInspector.StyleFile.updateTimeout = 200;
WebInspector.StyleFile.prototype = {
_workingCopyCommitted: function(event)
{
if (this._isAddingRevision)
return;
this._commitIncrementalEdit(true);
},
_workingCopyChanged: function(event)
{
if (this._isAddingRevision)
return;
if (WebInspector.StyleFile.updateTimeout >= 0) {
this._incrementalUpdateTimer = setTimeout(this._commitIncrementalEdit.bind(this, false), WebInspector.StyleFile.updateTimeout)
} else
this._commitIncrementalEdit(false);
},
_commitIncrementalEdit: function(majorChange)
{
this._clearIncrementalUpdateTimer();
WebInspector.styleContentBinding.setStyleContent(this._uiSourceCode, this._uiSourceCode.workingCopy(), majorChange, this._styleContentSet.bind(this));
},
_styleContentSet: function(error)
{
if (error)
WebInspector.showErrorMessage(error);
},
_clearIncrementalUpdateTimer: function()
{
if (!this._incrementalUpdateTimer)
return;
clearTimeout(this._incrementalUpdateTimer);
delete this._incrementalUpdateTimer;
},
addRevision: function(content)
{
this._isAddingRevision = true;
this._uiSourceCode.addRevision(content);
delete this._isAddingRevision;
},
dispose: function()
{
this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
}
}
WebInspector.StyleContentBinding = function(cssModel, workspace)
{
this._cssModel = cssModel;
this._workspace = workspace;
this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetChanged, this._styleSheetChanged, this);
}
WebInspector.StyleContentBinding.prototype = {
setStyleContent: function(uiSourceCode, content, majorChange, userCallback)
{
var resource = WebInspector.resourceForURL(uiSourceCode.url);
if (!resource) {
userCallback("No resource found: " + uiSourceCode.url);
return;
}
this._cssModel.resourceBinding().requestStyleSheetIdForResource(resource, callback.bind(this));
function callback(styleSheetId)
{
if (!styleSheetId) {
userCallback("No stylesheet found: " + resource.frameId + ":" + resource.url);
return;
}
this._innerSetContent(styleSheetId, content, majorChange, userCallback, null);
}
},
_innerSetContent: function(styleSheetId, content, majorChange, userCallback)
{
this._isSettingContent = true;
function callback(error)
{
userCallback(error);
delete this._isSettingContent;
}
this._cssModel.setStyleSheetText(styleSheetId, content, majorChange, callback.bind(this));
},
_styleSheetChanged: function(event)
{
if (this._isSettingContent)
return;
if (!event.data.majorChange)
return;
function callback(error, content)
{
if (!error)
this._innerStyleSheetChanged(event.data.styleSheetId, content);
}
CSSAgent.getStyleSheetText(event.data.styleSheetId, callback.bind(this));
},
_innerStyleSheetChanged: function(styleSheetId, content)
{
function callback(styleSheetURL)
{
if (typeof styleSheetURL !== "string")
return;
var uiSourceCode = this._workspace.uiSourceCodeForURL(styleSheetURL);
if (!uiSourceCode)
return;
if (uiSourceCode.styleFile())
uiSourceCode.styleFile().addRevision(content);
}
this._cssModel.resourceBinding().requestResourceURLForStyleSheetId(styleSheetId, callback.bind(this));
},
}
WebInspector.styleContentBinding = null;