Base class for live preview servers
Configuration parameters for this server:
function BaseServer(config) {
this._baseUrl = config.baseUrl;
this._root = config.root; // ProjectManager.getProjectRoot().fullPath
this._pathResolver = config.pathResolver; // ProjectManager.makeProjectRelativeIfPossible(doc.file.fullPath)
this._liveDocuments = {};
}
BaseServer.prototype._setDocInfo = function (liveDocument) {
var parentUrl,
matches,
doc = liveDocument.doc;
// FUTURE: some of these things should just be moved into core Document; others should
// be in a LiveDevelopment-specific object attached to the doc.
matches = /^(.*\/)(.+\.([^.]+))$/.exec(doc.file.fullPath);
if (!matches) {
return;
}
doc.extension = matches[3];
parentUrl = this.pathToUrl(matches[1]);
doc.url = parentUrl + encodeURI(matches[2]);
// the root represents the document that should be displayed in the browser
// for live development (the file for HTML files)
// TODO: Issue #2033 Improve how default page is determined
doc.root = { url: doc.url };
// TODO: Better workflow of liveDocument.doc.url assignment
// Force sync the browser after a URL is assigned
if (doc.isDirty && liveDocument._updateBrowser) {
liveDocument._updateBrowser();
}
};
Adds a live document to server
BaseServer.prototype.add = function (liveDocument) {
if (!liveDocument) {
return;
}
// use the project relative path as a key to lookup requests
var key = this._documentKey(liveDocument.doc.file.fullPath);
this._setDocInfo(liveDocument);
this._liveDocuments[key] = liveDocument;
};
Determines if this server can serve local file. LiveDevServerManager calls this method when determining if a server can serve a file.
BaseServer.prototype.canServe = function (localPath) {
return true;
};
BaseServer.prototype._documentKey = function (absolutePath) {
return "/" + encodeURI(this._pathResolver(absolutePath));
};
Clears all live documents currently attached to the server
BaseServer.prototype.clear = function () {
this._liveDocuments = {};
};
Lookup a live document using it's full path key
BaseServer.prototype.get = function (path) {
return this._liveDocuments[this._documentKey(path)];
};
Returns a base url for current project.
BaseServer.prototype.getBaseUrl = function () {
return this._baseUrl;
};
Returns a URL for a given path
BaseServer.prototype.pathToUrl = function (path) {
var baseUrl = this.getBaseUrl(),
relativePath = this._pathResolver(path);
// See if base url has been specified and path is within project
if (relativePath !== path) {
// Map to server url. Base url is already encoded, so don't encode again.
var encodedDocPath = encodeURI(path);
var encodedProjectPath = encodeURI(this._root);
return encodedDocPath.replace(encodedProjectPath, baseUrl);
}
return null;
};
Called by LiveDevelopment before to prepare the server before navigating to the project's base URL. The provider returns a jQuery promise. The Live Development launch process waits until the promise is resolved or rejected. If the promise is rejected, an error window is shown and Live Development does not start..
BaseServer.prototype.readyToServe = function () {
// Base implementation always resolves
return $.Deferred().resolve().promise();
};
Removes a live document from the server
BaseServer.prototype.remove = function (liveDocument) {
if (!liveDocument) {
return;
}
var key = this._liveDocuments[this._documentKey(liveDocument.doc.file.fullPath)];
if (key) {
delete this._liveDocuments[key];
}
};
Stop the server
BaseServer.prototype.stop = function () {
// do nothing
};
exports.BaseServer = BaseServer;
});
Convert a URL to a local full file path
BaseServer.prototype.urlToPath = function (url) {
var path,
baseUrl = "";
baseUrl = this.getBaseUrl();
if (baseUrl !== "" && url.indexOf(baseUrl) === 0) {
// Use base url to translate to local file path.
// Need to use encoded project path because it's decoded below.
path = url.replace(baseUrl, encodeURI(this._root));
return decodeURI(path);
}
return null;
};