-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (137 loc) · 3.9 KB
/
Copy pathindex.js
File metadata and controls
165 lines (137 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var ejs = require('ejs'),
path = require('path'),
async = require('async');
var renderExtended = ejs;
function addEjsExtension(viewPath){
return path.extname(viewPath) === '.ejs' ? viewPath : viewPath + '.ejs';
}
function getTemplateText(url, success, fail) {
var request = new XMLHttpRequest();
request.open("GET", addEjsExtension(url));
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status >= 200 && request.status < 400) {
//var type = request.getResponseHeader("Content-Type");
//if (type.match(/^text/))
success(request.responseText);
}
else {
fail(request.statusText);
}
}
};
request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
request.send(null);
}
renderExtended.renderSmart = function(view, data, callback) {
//Running on node.js
if (typeof window == 'undefined') {
renderInNode(view.server, data, callback);
} else {
this.renderAsync(view.client, data, callback);
}
};
renderExtended.renderAsync = function(view, data, callback) {
getAllTemplateFileList(view, data, function(err, pathData) {
if(!err){
var rendered = renderIncludeRecursive(view, data, pathData);
callback(null, rendered);
}
else {
callback(err, null);
}
});
};
function renderInNode(view, data, callback) {
ejs.renderFile(
addEjsExtension(view),
data,
function(err, result) {
callback(err, result);
}
);
}
function getEjsTextFromPathData(templateData, filePath) {
for(var key in templateData){
if(templateData[key].filePath == filePath){
return templateData[key].templateText;
}
}
}
function renderIncludeRecursive(view, data, templateData) {
var ejsText = getEjsTextFromPathData(templateData, view);
var fn = ejs.compile(
ejsText, {
client: true,
filename: view
}
);
var includeFilePaths = [];
var rendered = fn(data, null, function(includeFilePath, includeData) {
return renderIncludeRecursive(path.join(path.dirname(view), includeFilePath), includeData, templateData);
});
return rendered;
}
function getAllTemplateFileList(view, data, callback) {
var returnPathsData = [];
var currentPathData = {
filePath: view,
data: data,
templateText: null
};
getTemplateText(
view,
function success(ejsText) {
currentPathData.templateText = ejsText;
returnPathsData.push(currentPathData);
var fn = ejs.compile(
ejsText, {
client: true,
filename: view
}
);
var includeFilePaths = [];
fn(data, null, function(includeFilePath, includeData) {
includeFilePaths.push({
filePath: includeFilePath,
data: includeData
});
});
if (includeFilePaths.length > 0) {
var createAsyncFunc = function(filePath, data) {
return function(callback) {
getAllTemplateFileList(path.join(path.dirname(view), filePath), data, function(err, pathData) {
callback(err, pathData);
});
};
};
var funcs = {};
includeFilePaths.forEach(function(pathData, index) {
funcs['data' + index] = createAsyncFunc(pathData.filePath, pathData.data);
});
async.parallel(
funcs,
function(err, result) {
if (err) {
callback(err, null);
} else {
var pathDataFromChild = [];
for (var key in result) {
result[key].forEach(function(obj) {
returnPathsData.push(obj);
});
}
callback(null, returnPathsData);
}
}
);
} else {
callback(null, returnPathsData);
}
},
function fail(statusText){
callback(new Error(statusText), returnPathsData);
}
);
}
module.exports = renderExtended;