Comparing version 0.1.0-beta-51 to 0.1.0-beta-52
@@ -30,3 +30,3 @@ "use strict"; | ||
aliasRegex: Type.REGEX, | ||
defaultThemeRegex: Type.REGEX | ||
normalizers: Type.ARRAY | ||
}, | ||
@@ -40,2 +40,3 @@ { | ||
this.paths = []; | ||
this.normalizers = []; | ||
this.config = core.extend({ | ||
@@ -56,7 +57,17 @@ cache: false, | ||
this.defaultThemeRegex = new RegExp('^' + this.config.defaultTheme + '/'); | ||
this.normalizers.push(this.config.views); | ||
di.setAlias('viewsPath', this.config.views); | ||
this.paths.push(this.config.views); | ||
if (Type.isArray(this.config.themes)) { | ||
if (this.config.themes.indexOf(this.config.defaultTheme) === -1) { | ||
this.config.themes.push(this.config.defaultTheme); | ||
} | ||
this.config.themes.forEach(function (name) { | ||
this.paths.push(this.config.views + name + '/'); | ||
}.bind(this)); | ||
} else { | ||
throw new error.HttpError(500, this.config, 'View.construct: themes are not array type'); | ||
} | ||
@@ -107,3 +118,3 @@ this.setModulesViewsPath(di.getAlias('modulesPath')); | ||
setModulesViewsPath: function View_setModulesViewsPath(dir) { | ||
var list, name, moduleToLoad, LoadedModule, module; | ||
var list, name, moduleToLoad, LoadedModule, moduleInstance; | ||
if (this.isDir(dir)) { | ||
@@ -130,9 +141,13 @@ list = this.readDir(dir); | ||
module = new LoadedModule(name); | ||
moduleInstance = new LoadedModule(name); | ||
if (!(module instanceof ModuleInterface)) { | ||
throw new error.HttpError(500, module, 'Module must be instance of ModuleInterface "core/module"'); | ||
if (!(moduleInstance instanceof ModuleInterface)) { | ||
throw new error.HttpError(500, moduleInstance, 'Module must be instance of ModuleInterface "core/module"'); | ||
} | ||
this.paths.push(module.getViewsPath()); | ||
this.normalizers.push(moduleInstance.getViewsPath()); | ||
this.config.themes.forEach(function (name) { | ||
this.paths.push(moduleInstance.getViewsPath() + name + '/'); | ||
}.bind(this)); | ||
} | ||
@@ -265,78 +280,74 @@ | ||
* @author Igor Ivanovic | ||
* @method View#lookUpFile | ||
* @method View#resolve | ||
* | ||
* @description | ||
* Look up file | ||
* Resolve view | ||
* @return {string} | ||
*/ | ||
lookUpFile: function View_lookUpFile(path, file, to, from) { | ||
var themes = this.config.themes.slice(), | ||
resolve: function View_resolve(toPath, fromPath) { | ||
var file = di.normalizePath(toPath), | ||
themes = this.config.themes.slice(), | ||
theme, | ||
re, | ||
filePath, | ||
normalizers = this.normalizers.slice(), | ||
isNormalized = false, | ||
path, | ||
trace = []; | ||
while (themes.length) { | ||
theme = themes.shift(); | ||
re = new RegExp('^' + theme + '/'); | ||
file = file.replace(re, ''); | ||
filePath = di.normalizePath(path + theme + '/' + file + this.config.suffix); | ||
if (this.isFile(filePath)) { | ||
return filePath; | ||
// file name normalizers | ||
while (normalizers.length) { | ||
path = di.normalizePath(normalizers.shift()); | ||
if (file.match(path)) { | ||
file = file.replace(path, '').replace(this.suffix, ''); | ||
isNormalized = true; | ||
break; | ||
} | ||
trace.push({ | ||
theme: theme, | ||
path: path, | ||
filePath: filePath | ||
}); | ||
} | ||
filePath = di.normalizePath(path + this.config.defaultTheme + '/' + file + this.config.suffix); | ||
if (this.isFile(filePath)) { | ||
return filePath; | ||
// try normalize fromPath | ||
if (!isNormalized && !!fromPath) { | ||
normalizers = this.normalizers.slice(); | ||
fromPath = di.normalizePath(fromPath); | ||
// file name normalizers | ||
while (normalizers.length) { | ||
path = di.normalizePath(normalizers.shift()); | ||
if (fromPath.match(path)) { | ||
isNormalized = true; | ||
break; | ||
} | ||
} | ||
} | ||
throw new error.HttpError(500, { | ||
from: from, | ||
load: to, | ||
trace: trace | ||
}, "View.resolve: template don't exists"); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#resolve | ||
* | ||
* @description | ||
* Resolve view | ||
* @return {string} | ||
*/ | ||
resolve: function View_resolve(to, from) { | ||
var path, file; | ||
// check themes | ||
if (isNormalized) { | ||
while (themes.length) { | ||
theme = themes.shift(); | ||
re = new RegExp('^' + theme + '/'); | ||
file = file.replace(re, ''); | ||
to = di.normalizePath(to); | ||
path = this.getPath(to); | ||
file = to.replace(path, ''); | ||
file = file.replace(this.suffix, ''); | ||
if (!path) { | ||
path = this.getPath(from); | ||
filePath = di.normalizePath(path + theme + '/' + file + this.config.suffix); | ||
if (this.isFile(filePath)) { | ||
return filePath; | ||
} | ||
trace.push({ | ||
theme: theme, | ||
path: path, | ||
filePath: filePath | ||
}); | ||
} | ||
} | ||
if (!path) { | ||
throw new error.HttpError(500, { | ||
path: path, | ||
to: to, | ||
from: from, | ||
file: file, | ||
paths: this.paths | ||
}, "View.resolve: view path is not registered in system and mvc was not able to detect path, please check your path configs"); | ||
} | ||
file = file.replace(this.defaultThemeRegex, ''); | ||
return this.lookUpFile(path, file, to, from); | ||
throw new error.HttpError(500, { | ||
from: fromPath, | ||
load: toPath, | ||
filePath: filePath, | ||
paths: this.paths, | ||
isNormalized: isNormalized, | ||
file: file, | ||
path: path, | ||
trace: trace, | ||
themes: this.config.themes | ||
}, "View.resolve: template don't exists"); | ||
}, | ||
@@ -343,0 +354,0 @@ /** |
@@ -26,3 +26,3 @@ "use strict"; | ||
"render", "renderFile", | ||
"resolve", "load", "getPath" | ||
"resolve", "load" | ||
].forEach( | ||
@@ -29,0 +29,0 @@ function (method) { |
@@ -5,3 +5,3 @@ { | ||
"description": "Powerful lightweight mvc framework for nodejs", | ||
"version": "0.1.0-beta-51", | ||
"version": "0.1.0-beta-52", | ||
"dependencies" : { | ||
@@ -8,0 +8,0 @@ "mongoose": "3.8.x", |
130695
4167