dotnode
Version:
.NET-like MVC framework for Node.js
150 lines (91 loc) • 3.79 kB
JavaScript
var path = require('path');
var UrlHelper = function (appContext, controllerContext) {
this._appContext = appContext;
this._controllerContext = controllerContext;
};
// #region public methods
UrlHelper.prototype.action = function (actionName, controllerName, routeValues) {
var currentRoute = this._getCurrentRoute();
return this._buildActionFromRoute(currentRoute, actionName, controllerName, routeValues);
};
UrlHelper.prototype.route = function (routeName, actionName, controllerName, routeValues) {
var route = this._getRouteByName(routeName);
return this._buildActionFromRoute(route, actionName, controllerName, routeValues);
};
UrlHelper.prototype.content = function (contentPath) {
var currentRoute = this._getCurrentRoute();
var publicDirectory = this._appContext.publicDirectory || 'public';
};
// #endregion
// #region private methods
UrlHelper.prototype._getCurrentRoute = function () {
var routes = this._appContext.routes,
routePattern = this._controllerContext.request.route.path;
var currentRoute, defaultRoute;
for (var i in routes) {
if (routes[i].isDefault) {
defaultRoute = routes[i];
}
if (routes[i].route === routePattern) {
currentRoute = routes[i];
break;
}
}
if (!currentRoute) {
currentRoute = defaultRoute;
}
return currentRoute;
};
UrlHelper.prototype._getRouteByName = function (routeName) {
var routes = this._appContext.routes;
var route;
for (var i in routes) {
if (routes[i].name === routeName) {
route = routes[i];
break;
}
}
return route;
};
UrlHelper.prototype._buildActionFromRoute = function (route, actionName, controllerName, routeValues) {
var pattern = route.route,
actionVariable = route.routeValues.action,
controllerVariable = route.routeValues.controller,
areaVariable = route.routeValues.area;
var url = pattern;
url = url.replace(actionVariable, this._routeComponentIsVariable(actionVariable) ? actionName : actionVariable);
url = url.replace(controllerVariable, this._routeComponentIsVariable(controllerVariable) ? controllerName : controllerVariable);
if (routeValues) {
if (typeof routeValues.area == 'string') {
url = url.replace(areaVariable, this._routeComponentIsVariable(areaVariable) ? routeValues.area : areaVariable);
}
}
if (route.routeValues.params) {
for (var paramName in route.routeValues.params) {
if (route.routeValues.params.hasOwnProperty(paramName)) {
if (this._routeComponentIsVariable(route.routeValues.params[paramName]) && routeValues.params) {
url = url.replace(route.routeValues.params[paramName], routeValues.params[paramName]);
}
}
}
} else {
if (routeValues && routeValues.params) {
var paramsList = [];
for (var paramName in routeValues.params) {
if (routeValues.params.hasOwnProperty(paramName)) {
paramsList.push(paramName + '=' + routeValues.params[paramName]);
}
}
var queryString = paramsList.reduce(function (acc, curr) {
return acc + '&' + curr;
});
url = url + '?' + queryString;
}
}
return url;
};
UrlHelper.prototype._routeComponentIsVariable = function (component) {
return component.indexOf(':') === 0;
};
// #endregion
module.exports = UrlHelper;