Comparing version
@@ -29,3 +29,2 @@ "use strict"; | ||
server: Type.OBJECT, | ||
logs: Type.ARRAY, | ||
config: Type.OBJECT | ||
@@ -38,3 +37,2 @@ }, | ||
this.server = null; | ||
this.logs = []; | ||
this.config = { | ||
@@ -119,3 +117,2 @@ publish: false, | ||
logs += '\n'; | ||
this.logs.push(logs); | ||
try { | ||
@@ -122,0 +119,0 @@ replace.forEach(function (value) { |
@@ -8,2 +8,3 @@ "use strict"; | ||
error = di.load('error'), | ||
fs = di.load('fs'), | ||
component = di.load('core/component'), | ||
@@ -25,8 +26,10 @@ logger = component.get('core/logger'), | ||
{ | ||
swig: Type.OBJECT | ||
swig: Type.OBJECT, | ||
preloaded: Type.OBJECT | ||
}, | ||
{ | ||
_construct: function View_construct(config) { | ||
var cache; | ||
var defaults; | ||
// extend | ||
this.preloaded = {}; | ||
this.config = core.extend({ | ||
@@ -43,21 +46,6 @@ cache: false, | ||
suffix: '.twig', | ||
theme: false | ||
theme: false, | ||
prelaod: false | ||
}, config); | ||
// set cache component | ||
if (Type.isString(this.config.cacheComponent)) { | ||
cache = component.get(this.config.cacheComponent); | ||
} else { | ||
cache = component.get('cache/memory'); | ||
} | ||
if (this.config.cache) { | ||
this.config.cache = { | ||
set: function (key, value) { | ||
cache.set('SWIG_TEMPLATE_' + key, value); | ||
}, | ||
get: function (key) { | ||
return cache.get('SWIG_TEMPLATE_' + key); | ||
} | ||
}; | ||
} | ||
@@ -73,3 +61,2 @@ di.setAlias('viewsPath', this.config.views); | ||
// create new swig env | ||
@@ -81,4 +68,11 @@ this.config.loader = { | ||
this.swig = new swig.Swig(this.config); | ||
if (this.config.cache) { | ||
this.preloadTemplates(di.getAlias('viewsPath')); | ||
this.preloadTemplates(di.getAlias('themesPath')); | ||
} | ||
defaults = core.extend({}, this.config); | ||
// don't use swig cache! | ||
defaults.cache = false; | ||
this.swig = new swig.Swig(defaults); | ||
@@ -90,8 +84,85 @@ logger.print("View.construct", this.config); | ||
* @author Igor Ivanovic | ||
* @method ViewLoader#setThemesPath | ||
* @method View#setPreloaded | ||
* | ||
* @description | ||
* Set preloaded template | ||
*/ | ||
setPreloaded: function (key, value) { | ||
logger.log('View.setPreloaded: ', key + '\n' + value); | ||
this.preloaded[key] = value; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#getPreloaded | ||
* | ||
* @description | ||
* Get preloaded template | ||
*/ | ||
getPreloaded: function (key) { | ||
if (this.hasPreloaded(key)) { | ||
return this.preloaded[key]; | ||
} | ||
return false; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#hasPreloaded | ||
* | ||
* @description | ||
* Check if have preloaded | ||
*/ | ||
hasPreloaded: function (key) { | ||
var isPreloaded = this.preloaded.hasOwnProperty(key); | ||
if (!!this.config.cache && !isPreloaded) { | ||
throw new error.DataError({key: key}, "ENOENT, no such file or directory "); | ||
} | ||
return isPreloaded; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#preloadTemplates | ||
* | ||
* @description | ||
* Preload all templates at bootstrap | ||
*/ | ||
preloadTemplates: function View_preloadTemplates(dir) { | ||
var list, name; | ||
if (isDir(dir)) { | ||
list = readDir(dir); | ||
while (true) { | ||
name = list.shift(); | ||
if (!name) { | ||
break; | ||
} | ||
this.preloadTemplates(di.normalizePath(dir + '/' + name)); | ||
} | ||
} else if (isFile(dir) && this.suffix.test(dir)) { | ||
this.setPreloaded(dir, di.readFileSync(dir)); | ||
} | ||
function readDir(path) { | ||
return fs.readdirSync(path); | ||
} | ||
function isDir(path) { | ||
return fs.statSync(path).isDirectory(); | ||
} | ||
function isFile(path) { | ||
return fs.statSync(path).isFile(); | ||
} | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#setThemesPath | ||
* | ||
* @description | ||
* Set theme path | ||
*/ | ||
setTheme: function ViewLoader_setTheme(name) { | ||
setTheme: function View_setTheme(name) { | ||
if (Type.assert(Type.STRING, name)) { | ||
@@ -106,3 +177,3 @@ this.config.theme = name; | ||
* @author Igor Ivanovic | ||
* @method ViewLoader#getPath | ||
* @method View#getPath | ||
* | ||
@@ -112,5 +183,5 @@ * @description | ||
*/ | ||
getPath: function ViewLoader_getPath() { | ||
getPath: function View_getPath(skipTheme) { | ||
var path = '@{viewsPath}/'; | ||
if (Type.isString(this.config.theme)) { | ||
if (Type.isString(this.config.theme) && !skipTheme) { | ||
path = '@{themesPath}/' + this.config.theme + '/'; | ||
@@ -123,3 +194,3 @@ } | ||
* @author Igor Ivanovic | ||
* @method ViewLoader#normalizeResolveValue | ||
* @method View#normalizeResolveValue | ||
* | ||
@@ -130,3 +201,3 @@ * @description | ||
*/ | ||
normalizeResolveValue: function ViewLoader_normalizeResolveValue(value) { | ||
normalizeResolveValue: function View_normalizeResolveValue(value) { | ||
if (Type.isString(value)) { | ||
@@ -140,3 +211,3 @@ return value.replace(this.getPath(), "").replace(this.suffix, ""); | ||
* @author Igor Ivanovic | ||
* @method ViewLoader#resolve | ||
* @method View#resolve | ||
* | ||
@@ -147,3 +218,3 @@ * @description | ||
*/ | ||
resolve: function ViewLoader_resolve(to, from) { | ||
resolve: function View_resolve(to, from) { | ||
return this.getPath() + this.normalizeResolveValue(to) + this.config.suffix; | ||
@@ -154,14 +225,39 @@ }, | ||
* @author Igor Ivanovic | ||
* @method ViewLoader#load | ||
* @method View#readTemplate | ||
* | ||
* @description | ||
* Read template | ||
* @return {string}; | ||
*/ | ||
readTemplate: function View_readTemplate(path) { | ||
if (this.hasPreloaded(path)) { | ||
return this.getPreloaded(path); | ||
} else { | ||
return di.readFileSync(path); | ||
} | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method View#load | ||
* | ||
* @description | ||
* Set load view | ||
* @return {string}; | ||
*/ | ||
load: function ViewLoader_load(identifier, cb) { | ||
var template = di.readFileSync(identifier); | ||
logger.print('ViewLoader.load', { | ||
identifier: identifier, | ||
template: template, | ||
cb: cb | ||
}); | ||
load: function View_load(identifier, cb) { | ||
var template = ''; | ||
try { | ||
template = this.readTemplate(identifier); | ||
} catch (e) { | ||
identifier = this.normalizeResolveValue(identifier); | ||
identifier = this.getPath(true) + identifier + this.config.suffix; | ||
template = this.readTemplate(identifier); | ||
} finally { | ||
logger.print('ViewLoader.load', { | ||
identifier: identifier, | ||
template: template, | ||
cb: cb | ||
}); | ||
} | ||
return template; | ||
@@ -168,0 +264,0 @@ }, |
@@ -5,3 +5,3 @@ { | ||
"description": "Powerful lightweight mvc framework for nodejs", | ||
"version": "0.1.0-alpha-6", | ||
"version": "0.1.0-alpha-7", | ||
"dependencies" : { | ||
@@ -8,0 +8,0 @@ "mongoose": "3.8.x", |
96940
2.95%3143
2.95%