Comparing version 0.1.0-beta-38 to 0.1.0-beta-39
@@ -205,3 +205,3 @@ "use strict"; | ||
renderFile: function Controller_renderFile(pathName, locals) { | ||
return view.renderFile(pathName, locals, this._config.themesPath, this._config.viewsPath); | ||
return view.renderFile(pathName, locals, this._config.viewsPath); | ||
}, | ||
@@ -208,0 +208,0 @@ /** |
@@ -143,3 +143,3 @@ "use strict"; | ||
date = new Date().toISOString(); | ||
logs += date + '\n'; | ||
logs += date + "\n"; | ||
try { | ||
@@ -149,3 +149,3 @@ throw new Error(); | ||
url = core.trim(e.stack.split('\n').slice(3, 4).shift()); | ||
logs += url + '\n'; | ||
logs += url + "\n"; | ||
} | ||
@@ -165,3 +165,3 @@ args.forEach(function (item) { | ||
logs = logs.replace(/\\'/g, "'"); | ||
logs = logs.replace(/\\n/g, '\n'); | ||
logs = logs.replace(/\\n/g, "\n"); | ||
logs = logs.replace(/\\u001b/g, '\u001b'); | ||
@@ -168,0 +168,0 @@ |
@@ -77,14 +77,2 @@ "use strict"; | ||
return this.getModulePath() + '/views/'; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Module#getThemesPath | ||
* | ||
* @description | ||
* Get themes path | ||
* @return {string} | ||
*/ | ||
getThemesPath: function Module_getThemesPath() { | ||
return this.getModulePath() + '/themes/'; | ||
} | ||
@@ -91,0 +79,0 @@ }); |
@@ -10,3 +10,2 @@ "use strict"; | ||
logger = component.get('core/logger'), | ||
view = component.get('core/view'), | ||
URLParser = di.load('url'), | ||
@@ -63,3 +62,2 @@ Type = di.load('typejs'), | ||
this.id = this._uuid(); | ||
view.setPaths(); | ||
}, | ||
@@ -584,3 +582,3 @@ /** | ||
*/ | ||
_handleController: function Request_handleController(controllersPath, viewsPath, themesPath) { | ||
_handleController: function Request_handleController(controllersPath, viewsPath) { | ||
var controllerToLoad = controllersPath + this.controller, | ||
@@ -606,4 +604,3 @@ LoadedController, | ||
module: this.module, | ||
viewsPath: viewsPath, | ||
themesPath: themesPath | ||
viewsPath: viewsPath | ||
}); | ||
@@ -693,3 +690,3 @@ | ||
return this._handleController(module.getControllersPath() + '/', module.getViewsPath(), module.getThemesPath()); | ||
return this._handleController(module.getControllersPath(), module.getViewsPath()); | ||
}, | ||
@@ -717,6 +714,6 @@ /** | ||
if (!!this.module) { | ||
return this._handleModule(di.getAlias('modulesPath') + '/'); | ||
return this._handleModule(di.getAlias('modulesPath')); | ||
} | ||
return this._handleController(di.getAlias('controllersPath') + '/'); | ||
return this._handleController(di.getAlias('controllersPath')); | ||
} | ||
@@ -723,0 +720,0 @@ |
@@ -6,2 +6,3 @@ "use strict"; | ||
ViewInterface = di.load('interface/view'), | ||
ModuleInterface = di.load('interface/module'), | ||
swig = di.load('swig'), | ||
@@ -27,3 +28,7 @@ error = di.load('error'), | ||
swig: Type.OBJECT, | ||
preloaded: Type.OBJECT | ||
preloaded: Type.OBJECT, | ||
paths: Type.ARRAY, | ||
aliasRegex: Type.REGEX, | ||
defaultThemeRegex: Type.REGEX, | ||
themeRegex: Type.REGEX | ||
}, | ||
@@ -34,3 +39,5 @@ { | ||
// extend | ||
this.aliasRegex = /@{.*}/g; | ||
this.preloaded = {}; | ||
this.paths = []; | ||
this.config = core.extend({ | ||
@@ -44,13 +51,18 @@ cache: false, | ||
cacheComponent: false, | ||
themes: '@{appPath}/themes/', | ||
views: '@{appPath}/views/', | ||
suffix: '.twig', | ||
extensions: false, | ||
defaultTheme: 'default', | ||
theme: false | ||
}, config); | ||
this.defaultThemeRegex = new RegExp('^' + this.config.defaultTheme); | ||
this.themeRegex = new RegExp('^' + this.config.theme); | ||
di.setAlias('viewsPath', this.config.views); | ||
di.setAlias('themesPath', this.config.themes); | ||
this.paths.push(this.config.views); | ||
this.setModulesViewsPath(di.getAlias('modulesPath')); | ||
if (Type.assert(Type.STRING, this.config.suffix)) { | ||
@@ -92,2 +104,44 @@ this.suffix = new RegExp(this.config.suffix + '$'); | ||
* @author Igor Ivanovic | ||
* @method View#setModulesViewsPath | ||
* | ||
* @description | ||
* Set modules path | ||
*/ | ||
setModulesViewsPath: function View_setModulesViewsPath(dir) { | ||
var list, name, moduleToLoad, LoadedModule, module; | ||
if (this.isDir(dir)) { | ||
list = this.readDir(dir); | ||
while (true) { | ||
name = list.shift(); | ||
if (!name) { | ||
break; | ||
} | ||
moduleToLoad = dir + '/' + name; | ||
try { | ||
LoadedModule = di.load(moduleToLoad); | ||
} catch (e) { | ||
throw new error.HttpError(500, {path: moduleToLoad}, 'Missing module', e); | ||
} | ||
if (!Type.assert(Type.FUNCTION, LoadedModule)) { | ||
throw new error.HttpError(500, {path: moduleToLoad}, 'Module must be function type'); | ||
} | ||
module = new LoadedModule(name); | ||
if (!(module instanceof ModuleInterface)) { | ||
throw new error.HttpError(500, module, 'Module must be instance of ModuleInterface "core/module"'); | ||
} | ||
this.paths.push(module.getViewsPath()); | ||
} | ||
} | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#setPreloaded | ||
@@ -98,3 +152,3 @@ * | ||
*/ | ||
setPreloaded: function (key, value) { | ||
setPreloaded: function View_setPreloaded(key, value) { | ||
logger.log('View.setPreloaded: ', key + '\n' + value); | ||
@@ -110,4 +164,5 @@ this.preloaded[key] = value; | ||
* Get preloaded template | ||
* @return {string|boolean} | ||
*/ | ||
getPreloaded: function (key) { | ||
getPreloaded: function View_getPreloaded(key) { | ||
if (this.preloaded.hasOwnProperty(key)) { | ||
@@ -121,2 +176,50 @@ return this.preloaded[key]; | ||
* @author Igor Ivanovic | ||
* @method View#isFile | ||
* | ||
* @description | ||
* Is file | ||
* @return {string|boolean} | ||
*/ | ||
isFile: function View_isFile(path) { | ||
try { | ||
path = di.normalizePath(path); | ||
return fs.statSync(path).isFile(); | ||
} catch (e) { | ||
logger.print('View.isFile path is not valid file', {path: path}); | ||
} | ||
return false; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#isDir | ||
* | ||
* @description | ||
* Is directory | ||
* @return {string|boolean} | ||
*/ | ||
isDir: function View_isDir(path) { | ||
try { | ||
path = di.normalizePath(path); | ||
return fs.statSync(path).isDirectory(); | ||
} catch (e) { | ||
logger.print('View.isDir path is not valid path', {path: path}); | ||
} | ||
return false; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#readDir | ||
* | ||
* @description | ||
* Read directory | ||
*/ | ||
readDir: function View_readDir(path) { | ||
path = di.normalizePath(path); | ||
return fs.readdirSync(path); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#preloadTemplates | ||
@@ -129,4 +232,4 @@ * | ||
var list, name; | ||
if (isDir(dir)) { | ||
list = readDir(dir); | ||
if (this.isDir(dir)) { | ||
list = this.readDir(dir); | ||
while (true) { | ||
@@ -140,17 +243,5 @@ name = list.shift(); | ||
} else if (isFile(dir) && this.suffix.test(dir)) { | ||
} else if (this.isFile(dir) && this.suffix.test(dir)) { | ||
this.setPreloaded(dir, this.swig.compileFile(dir)); | ||
} | ||
function readDir(path) { | ||
return fs.readdirSync(path); | ||
} | ||
function isDir(path) { | ||
return fs.statSync(path).isDirectory(); | ||
} | ||
function isFile(path) { | ||
return fs.statSync(path).isFile(); | ||
} | ||
}, | ||
@@ -168,3 +259,3 @@ /** | ||
this.config.theme = name; | ||
} else if(Type.isNull(name)) { | ||
} else if (Type.isNull(name)) { | ||
this.config.theme = null; | ||
@@ -181,28 +272,15 @@ } else { | ||
* @description | ||
* Get path | ||
*/ | ||
getPath: function View_getPath(skipTheme) { | ||
var path = '@{viewsPath}/'; | ||
if (Type.isString(this.config.theme) && !skipTheme) { | ||
path = '@{themesPath}/' + this.config.theme + '/'; | ||
} | ||
return di.normalizePath(path); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#normalizeResolveValue | ||
* | ||
* @description | ||
* Normalize resolve value | ||
* Get the path | ||
* @return {string} | ||
*/ | ||
normalizeResolveValue: function View_normalizeResolveValue(value) { | ||
var theme = this.getPath(), view = this.getPath(true); | ||
if (Type.isString(value) && value.match(theme)) { | ||
return value.replace(theme, "").replace(this.suffix, ""); | ||
} else if (Type.isString(value) && value.match(view)) { | ||
return value.replace(view, "").replace(this.suffix, ""); | ||
getPath: function View_getPath(path) { | ||
var paths = this.paths.slice(), | ||
item; | ||
while (paths.length) { | ||
item = di.normalizePath(paths.pop()); | ||
if (path && path.indexOf(item) === 0) { | ||
return item; | ||
} | ||
} | ||
return value; | ||
return ''; | ||
}, | ||
@@ -219,3 +297,40 @@ /** | ||
resolve: function View_resolve(to, from) { | ||
return this.getPath() + this.normalizeResolveValue(to) + this.config.suffix; | ||
var path, file; | ||
to = di.normalizePath(to); | ||
path = this.getPath(to); | ||
file = to.replace(path, ''); | ||
file = file.replace(this.suffix, ''); | ||
if (!path) { | ||
path = this.getPath(from); | ||
} | ||
file = file.replace(this.defaultThemeRegex, ''); | ||
if (!!this.config.theme) { | ||
file = file.replace(this.themeRegex, ''); | ||
if (this.isFile(path + this.config.theme + '/' + file + this.config.suffix)) { | ||
return path + this.config.theme + '/' + file + this.config.suffix; | ||
} | ||
} | ||
if (this.isFile(path + this.config.defaultTheme + '/' + file + this.config.suffix)) { | ||
return path + this.config.defaultTheme + '/' + file + this.config.suffix; | ||
} | ||
if (this.config.theme) { | ||
throw new error.HttpError(500, { | ||
themeFile: path + this.config.theme + '/' + file + this.config.suffix, | ||
defaultFile: path + this.config.defaultTheme + '/' + file + this.config.suffix | ||
}, "View.resolve: template don't exists"); | ||
} else { | ||
throw new error.HttpError(500, { | ||
defaultFile: path + this.config.defaultTheme + '/' + file + this.config.suffix | ||
}, "View.resolve: template don't exists"); | ||
} | ||
}, | ||
@@ -231,13 +346,15 @@ /** | ||
*/ | ||
load: function View_load(identifier, cb) { | ||
load: function View_load(path, cb) { | ||
var template = ''; | ||
try { | ||
template = di.readFileSync(identifier); | ||
template = di.readFileSync(path); | ||
} catch (e) { | ||
identifier = this.normalizeResolveValue(identifier); | ||
identifier = this.getPath(true) + identifier + this.config.suffix; | ||
template = di.readFileSync(identifier); | ||
logger.print('ViewLoader.load.error', { | ||
path: path, | ||
cb: cb, | ||
e: e | ||
}); | ||
} finally { | ||
logger.print('ViewLoader.load', { | ||
identifier: identifier, | ||
path: path, | ||
template: template, | ||
@@ -316,24 +433,2 @@ cb: cb | ||
* @author Igor Ivanovic | ||
* @method View#setPaths | ||
* | ||
* @description | ||
* Set default paths | ||
* @return {string} | ||
*/ | ||
setPaths: function View_setPaths(themesPath, viewsPath) { | ||
if (Type.isString(themesPath) && !!themesPath) { | ||
di.setAlias('themesPath', themesPath); | ||
} else { | ||
di.setAlias('themesPath', this.config.themes); | ||
} | ||
if (Type.isString(viewsPath) && !!viewsPath) { | ||
di.setAlias('viewsPath', viewsPath); | ||
} else { | ||
di.setAlias('viewsPath', this.config.views); | ||
} | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#renderFile | ||
@@ -345,5 +440,7 @@ * | ||
*/ | ||
renderFile: function View_renderFile(pathName, locals, themesPath, viewsPath) { | ||
this.setPaths(themesPath, viewsPath); | ||
return this.swig.renderFile(pathName, locals); | ||
renderFile: function View_renderFile(templateName, locals, viewsPath) { | ||
if (!viewsPath) { | ||
viewsPath = this.config.views; | ||
} | ||
return this.swig.renderFile(viewsPath + templateName, locals); | ||
} | ||
@@ -350,0 +447,0 @@ } |
@@ -25,4 +25,3 @@ "use strict"; | ||
"getControllersPath", | ||
"getViewsPath", | ||
"getThemesPath" | ||
"getViewsPath" | ||
].forEach(function (method) { | ||
@@ -29,0 +28,0 @@ if (!(method in this)) { |
@@ -25,4 +25,4 @@ "use strict"; | ||
"setLoader", "setFilter", "setTag", "setExtension", | ||
"render", "renderFile", "setTheme", "getPath", | ||
"normalizeResolveValue", "resolve", "load" | ||
"render", "renderFile", "setTheme", | ||
"resolve", "load", "getPath" | ||
].forEach( | ||
@@ -29,0 +29,0 @@ function (method) { |
@@ -5,3 +5,3 @@ { | ||
"description": "Powerful lightweight mvc framework for nodejs", | ||
"version": "0.1.0-beta-38", | ||
"version": "0.1.0-beta-39", | ||
"dependencies" : { | ||
@@ -8,0 +8,0 @@ "mongoose": "3.8.x", |
127411
4066