1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var _ = require('lodash');
|
10 | var HttpViewHelper = require('../helpers').HtmlViewHelper;
|
11 | var HttpNotFoundError = require('@themost/common/errors').HttpNotFoundError;
|
12 | var ejs = require('ejs');
|
13 | var path = require('path');
|
14 | var fs = require('fs');
|
15 | var Symbol = require('symbol');
|
16 | var DirectiveEngine = require('./../handlers/directive').DirectiveEngine;
|
17 | var PostExecuteResultArgs = require('./../handlers/directive').PostExecuteResultArgs;
|
18 | var HttpViewContext = require('./../mvc').HttpViewContext;
|
19 | var layoutFileProperty = Symbol();
|
20 | var viewProperty = Symbol();
|
21 | var scriptsProperty = Symbol();
|
22 | var stylesheetsProperty = Symbol();
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function postRender(result, data, callback) {
|
31 | var directiveHandler = new DirectiveEngine();
|
32 | var viewContext = new HttpViewContext(this.context);
|
33 | viewContext.body = result;
|
34 | viewContext.data = data;
|
35 | var args = _.assign(new PostExecuteResultArgs(), {
|
36 | "context": this.context,
|
37 | "target": viewContext
|
38 | });
|
39 | directiveHandler.postExecuteResult(args, function(err) {
|
40 | if (err) {
|
41 | return callback(err);
|
42 | }
|
43 | return callback(null, viewContext.body);
|
44 | });
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 | function EjsLocals() {
|
50 | this[scriptsProperty] = new Array();
|
51 | this[stylesheetsProperty] = new Array();
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 | EjsLocals.prototype.layout = function(view) {
|
57 |
|
58 | if (typeof view !== 'string') {
|
59 | throw new TypeError('Include view must be a string');
|
60 | }
|
61 |
|
62 | if (!/\.html\.ejs$/ig.test(view)) {
|
63 |
|
64 | this[layoutFileProperty] = view + '.html.ejs';
|
65 | }
|
66 | else {
|
67 | this[layoutFileProperty] = view;
|
68 | }
|
69 | };
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | EjsLocals.prototype.partial = function(view, data){
|
75 | if (typeof view !== 'string') {
|
76 | throw new TypeError('Include view must be a string');
|
77 | }
|
78 |
|
79 | if (!/\.html\.ejs$/ig.test(view)) {
|
80 |
|
81 | view = view + '.html.ejs';
|
82 | }
|
83 | if (typeof this[viewProperty] !== 'string') {
|
84 | throw new TypeError('Current view must be a string');
|
85 | }
|
86 |
|
87 | var includeFile = path.resolve(path.dirname(this[viewProperty]), view);
|
88 |
|
89 | var source;
|
90 |
|
91 | if (process.env.NODE_ENV === 'development') {
|
92 |
|
93 | source = fs.readFileSync(includeFile, 'utf-8');
|
94 | }
|
95 | else {
|
96 |
|
97 | source = ejs.cache.get(includeFile);
|
98 |
|
99 | if (typeof source === 'undefined') {
|
100 |
|
101 | source = fs.readFileSync(includeFile, 'utf-8');
|
102 |
|
103 | ejs.cache.set(includeFile, source);
|
104 | }
|
105 | }
|
106 |
|
107 | if (typeof data === 'undefined') {
|
108 |
|
109 | return;
|
110 | }
|
111 | else {
|
112 |
|
113 | var context;
|
114 | if (this.html && this.html.context) {
|
115 | context = this.html.context;
|
116 | }
|
117 |
|
118 | if (_.isArray(data)) {
|
119 | return _.map(data, function(item) {
|
120 |
|
121 | var locals = _.assign(new EjsLocals(), {
|
122 |
|
123 | model: item,
|
124 |
|
125 | html: new HttpViewHelper(context)
|
126 | });
|
127 |
|
128 | return ejs.render(source, locals);
|
129 | }).join('\n');
|
130 | }
|
131 | else {
|
132 |
|
133 | var locals = _.assign(new EjsLocals(), {
|
134 |
|
135 | model: data,
|
136 |
|
137 | html: new HttpViewHelper(context)
|
138 | });
|
139 |
|
140 | return ejs.render(source, locals);
|
141 | }
|
142 |
|
143 | }
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | EjsLocals.prototype.include = function(view, data){
|
151 | if (typeof view !== 'string') {
|
152 | throw new TypeError('Include view must be a string');
|
153 | }
|
154 |
|
155 | if (!/\.html\.ejs$/ig.test(view)) {
|
156 |
|
157 | view = view + '.html.ejs';
|
158 | }
|
159 | if (typeof this[viewProperty] !== 'string') {
|
160 | throw new TypeError('Current view must be a string');
|
161 | }
|
162 |
|
163 | var includeFile = path.resolve(path.dirname(this[viewProperty]), view);
|
164 |
|
165 | var source;
|
166 |
|
167 | if (process.env.NODE_ENV === 'development') {
|
168 |
|
169 | source = fs.readFileSync(includeFile, 'utf-8');
|
170 | }
|
171 | else {
|
172 |
|
173 | source = ejs.cache.get(includeFile);
|
174 |
|
175 | if (typeof source === 'undefined') {
|
176 |
|
177 | source = fs.readFileSync(includeFile, 'utf-8');
|
178 |
|
179 | ejs.cache.set(includeFile, source);
|
180 | }
|
181 | }
|
182 |
|
183 | if (typeof data === 'undefined') {
|
184 |
|
185 | return ejs.render(source, this);
|
186 | }
|
187 | else {
|
188 |
|
189 | var context;
|
190 | if (this.html && this.html.context) {
|
191 | context = this.html.context;
|
192 | }
|
193 |
|
194 | var locals = _.assign(new EjsLocals(), {
|
195 |
|
196 | model: data,
|
197 |
|
198 | html: new HttpViewHelper(context)
|
199 | });
|
200 |
|
201 | return ejs.render(source, locals);
|
202 | }
|
203 | };
|
204 |
|
205 | EjsLocals.prototype.script = function(path, type) {
|
206 | if (path) {
|
207 | this[scriptsProperty].push('<script src="'+path+'"'+(type ? 'type="'+type+'"' : '')+'></script>');
|
208 | }
|
209 | return this;
|
210 | };
|
211 |
|
212 | EjsLocals.prototype.stylesheet = function(path, media) {
|
213 | if (path) {
|
214 | this[stylesheetsProperty].push('<link rel="stylesheet" href="'+path+'"'+(media ? 'media="'+media+'"' : '')+' />');
|
215 | }
|
216 | return this;
|
217 | };
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | function EjsEngine(context) {
|
225 |
|
226 | |
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | |
233 |
|
234 |
|
235 | var ctx = context;
|
236 | Object.defineProperty(this,'context', {
|
237 | get: function() {
|
238 | return ctx;
|
239 | },
|
240 | set: function(value) {
|
241 | ctx = value;
|
242 | },
|
243 | configurable:false,
|
244 | enumerable:false
|
245 | });
|
246 | }
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | EjsEngine.prototype.getContext = function() {
|
252 | return this.context;
|
253 | };
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | EjsEngine.prototype.filter = function(name, filterFunc) {
|
261 | ejs.filters[name] = filterFunc;
|
262 | };
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | EjsEngine.prototype.render = function(filename, data, callback) {
|
271 | var self = this;
|
272 | var locals;
|
273 | var source;
|
274 | try {
|
275 | if (process.env.NODE_ENV === 'development') {
|
276 | source = fs.readFileSync(filename,'utf-8');
|
277 | }
|
278 | else {
|
279 | source = ejs.cache.get(filename);
|
280 | if (typeof source === 'undefined') {
|
281 |
|
282 | source = fs.readFileSync(filename,'utf-8');
|
283 |
|
284 | ejs.cache.set(filename, source);
|
285 | }
|
286 | }
|
287 |
|
288 |
|
289 | locals = _.assign(new EjsLocals(), {
|
290 | model: data,
|
291 | html:new HttpViewHelper(self.context)
|
292 | });
|
293 |
|
294 | locals[viewProperty] = filename;
|
295 |
|
296 | var matcher = /^(\s*)<%#(.*?)%>/;
|
297 | var properties = {
|
298 | |
299 |
|
300 |
|
301 | layout:null
|
302 | };
|
303 | if (matcher.test(source)) {
|
304 | var matches = matcher.exec(source);
|
305 | properties = JSON.parse(matches[2]);
|
306 |
|
307 | source = source.replace(matcher,'');
|
308 |
|
309 | console.log('INFO', 'Layout syntax e.g. <%# { "layout":"../shared/master.html.ejs" } %> is deprecated and it\'s going to be removed in a future version. Use layout() method instead e.g. <% layout(\'../shared/master\')%>.');
|
310 | }
|
311 | if (properties.layout) {
|
312 | var layout;
|
313 | if (/^\//.test(properties.layout)) {
|
314 |
|
315 | layout = self.context.getApplication().mapExecutionPath(properties.layout);
|
316 | }
|
317 | else {
|
318 |
|
319 | layout = path.resolve(path.dirname(filename), properties.layout);
|
320 | }
|
321 |
|
322 | var body = ejs.render(source, locals);
|
323 |
|
324 | _.assign(locals, {
|
325 | body: body
|
326 | });
|
327 |
|
328 | return ejs.renderFile(layout, locals, {
|
329 | cache: process.env.NODE_ENV !== 'development'
|
330 | }, function(err, result) {
|
331 | try {
|
332 | if (err) {
|
333 | if (err.code === 'ENOENT') {
|
334 | return callback(new HttpNotFoundError('Master view layout cannot be found'));
|
335 | }
|
336 | return callback(err);
|
337 | }
|
338 | return postRender.bind(self)(result, locals.model, function(err, finalResult) {
|
339 | if (err) {
|
340 | return callback(err);
|
341 | }
|
342 | return callback(null, finalResult);
|
343 | });
|
344 | }
|
345 | catch (err) {
|
346 | callback(err);
|
347 | }
|
348 | });
|
349 | }
|
350 | else {
|
351 |
|
352 | var htmlResult = ejs.render(source, locals);
|
353 |
|
354 | if (typeof locals[layoutFileProperty] === 'string') {
|
355 |
|
356 | var layoutFile = path.resolve(path.dirname(filename), locals[layoutFileProperty]);
|
357 |
|
358 | delete locals[layoutFileProperty];
|
359 |
|
360 | _.assign(locals, {
|
361 | body: htmlResult,
|
362 | scripts: locals[scriptsProperty].join('\n'),
|
363 | stylesheets: locals[stylesheetsProperty].join('\n'),
|
364 | });
|
365 |
|
366 | return ejs.renderFile(layoutFile, locals, {
|
367 | cache: process.env.NODE_ENV !== 'development'
|
368 | }, function(err, result) {
|
369 | if (err) {
|
370 | return callback(err);
|
371 | }
|
372 |
|
373 | return postRender.bind(self)(result, locals.model, function(err, finalResult) {
|
374 | if (err) {
|
375 | return callback(err);
|
376 | }
|
377 | return callback(null, finalResult);
|
378 | });
|
379 | });
|
380 | }
|
381 | return postRender.bind(self)(htmlResult, locals.model, function(err, finalResult) {
|
382 | if (err) {
|
383 | return callback(err);
|
384 | }
|
385 | return callback(null, finalResult);
|
386 | });
|
387 | }
|
388 | }
|
389 | catch (err) {
|
390 | if (err.code === 'ENOENT') {
|
391 |
|
392 | return callback(new HttpNotFoundError('View layout cannot be found.'));
|
393 | }
|
394 | return callback(err);
|
395 | }
|
396 | };
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | EjsEngine.createInstance = function(context) {
|
404 | return new EjsEngine(context);
|
405 | };
|
406 |
|
407 | if (typeof exports !== 'undefined') {
|
408 | module.exports.EjsEngine = EjsEngine;
|
409 | |
410 |
|
411 |
|
412 |
|
413 | module.exports.createInstance = function(context) {
|
414 | return EjsEngine.createInstance(context);
|
415 | };
|
416 | }
|