/*****************************************
* View Manager
*****************************************
* Author: mikael.lofjard@gmail.com
* Website: http://lofjard.se
* License: MIT License
****************************************/
var
ViewManager = (
function
() {
fs = require(
'fs'
);
path = require(
'path'
mu = require(
'mustache'
env = require(
'./environmentManager'
).EnvironmentManager;
cm = require(
'./cacheManager'
).CacheManager;
misc = require(
'./misc'
).Misc;
templatesCached =
false
;
templates = {};
partials = {};
versionCode =
null
lengthInUtf8Bytes(str) {
// Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
m = encodeURIComponent(str).match(/%[89ABab]/g);
return
str.length + (m ? m.length : 0);
}
{
init :
(templatePath) {
versionCode = misc.getGuid();
if
(!templatesCached) {
console.log(
'ViewManager: Populating template cache'
allTemplateFiles = fs.readdirSync(templatePath);
for
(
file
in
allTemplateFiles) {
' - Adding '
+ allTemplateFiles[file] +
' to template store'
filePath = path.resolve(templatePath, allTemplateFiles[file]);
templates[allTemplateFiles[file]] = fs.readFileSync(filePath,
'utf-8'
partials.header = templates[
'header.partial.mu'
];
partials.footer = templates[
'footer.partial.mu'
true
},
renderView :
(response, viewName, model, contentType) {
typeof
(model.header) ==
'undefined'
) {
model.header = {};
contentType = contentType ||
'text/html'
model.header.trackingCode = env.trackingCode();
model.header.versionCode = versionCode;
model.footer = {};
model.footer.versionCode = versionCode;
env.info(
'ViewManager: Rendering view '
+ viewName);
html = mu.to_html(templates[viewName +
'.mu'
], model, partials);
contentLength = lengthInUtf8Bytes(html);
headers = {
"Content-Type"
: contentType +
';charset=utf-8'
,
"Content-Length"
: contentLength
};
responseData = { headers: headers, content: html };
responseData;
}());
(exports) !=
? exports.ViewManager = ViewManager :
/*****************************************
* View Manager
*****************************************
* Author: mikael.lofjard@gmail.com
* Website: http://lofjard.se
* License: MIT License
****************************************/
var
ViewManager = (
function
() {
var
fs = require(
'fs'
);
var
path = require(
'path'
);
var
mu = require(
'mustache'
);
var
env = require(
'./environmentManager'
).EnvironmentManager;
var
cm = require(
'./cacheManager'
).CacheManager;
var
misc = require(
'./misc'
).Misc;
var
templatesCached =
false
;
var
templates = {};
var
partials = {};
var
versionCode =
null
;
function
lengthInUtf8Bytes(str) {
// Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
var
m = encodeURIComponent(str).match(/%[89ABab]/g);
return
str.length + (m ? m.length : 0);
}
return
{
init :
function
(templatePath) {
versionCode = misc.getGuid();
if
(!templatesCached) {
console.log(
'ViewManager: Populating template cache'
);
templates = {};
var
allTemplateFiles = fs.readdirSync(templatePath);
for
(
var
file
in
allTemplateFiles) {
console.log(
' - Adding '
+ allTemplateFiles[file] +
' to template store'
);
var
filePath = path.resolve(templatePath, allTemplateFiles[file]);
templates[allTemplateFiles[file]] = fs.readFileSync(filePath,
'utf-8'
);
}
partials.header = templates[
'header.partial.mu'
];
partials.footer = templates[
'footer.partial.mu'
];
templatesCached =
true
;
}
},
renderView :
function
(response, viewName, model, contentType) {
if
(
typeof
(model.header) ==
'undefined'
) {
model.header = {};
}
contentType = contentType ||
'text/html'
;
model.header.trackingCode = env.trackingCode();
model.header.versionCode = versionCode;
model.footer = {};
model.footer.versionCode = versionCode;
env.info(
'ViewManager: Rendering view '
+ viewName);
var
html = mu.to_html(templates[viewName +
'.mu'
], model, partials);
var
contentLength = lengthInUtf8Bytes(html);
var
headers = {
"Content-Type"
: contentType +
';charset=utf-8'
,
"Content-Length"
: contentLength
};
var
responseData = { headers: headers, content: html };
return
responseData;
}
};
}());
typeof
(exports) !=
'undefined'
? exports.ViewManager = ViewManager :
null
;