| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var translator = require('../../public/src/modules/translator'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(middleware) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	middleware.processRender = function(req, res, next) { | 
					
						
							|  |  |  | 		// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
 | 
					
						
							|  |  |  | 		var render = res.render; | 
					
						
							|  |  |  | 		res.render = function(template, options, fn) { | 
					
						
							|  |  |  | 			var self = this, | 
					
						
							|  |  |  | 				req = this.req, | 
					
						
							|  |  |  | 				defaultFn = function(err, str){ | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							| 
									
										
										
										
											2016-03-20 15:11:32 -05:00
										 |  |  | 						return next(err); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					self.send(str); | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 			options = options || {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ('function' === typeof options) { | 
					
						
							|  |  |  | 				fn = options; | 
					
						
							|  |  |  | 				options = {}; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-04 11:22:35 +02:00
										 |  |  | 			options.loggedIn = !!req.uid; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			options.relative_path = nconf.get('relative_path'); | 
					
						
							|  |  |  | 			options.template = {name: template}; | 
					
						
							|  |  |  | 			options.template[template] = true; | 
					
						
							| 
									
										
										
										
											2015-10-16 18:43:40 -04:00
										 |  |  | 			options.bodyClass = buildBodyClass(req); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			res.locals.template = template; | 
					
						
							| 
									
										
										
										
											2015-11-17 12:25:46 -05:00
										 |  |  | 			options._locals = undefined; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (res.locals.isAPI) { | 
					
						
							|  |  |  | 				if (req.route && req.route.path === '/api/') { | 
					
						
							|  |  |  | 					options.title = '[[pages:home]]'; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return res.json(options); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ('function' !== typeof fn) { | 
					
						
							|  |  |  | 				fn = defaultFn; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-13 17:45:23 -05:00
										 |  |  | 			var ajaxifyData = JSON.stringify(options); | 
					
						
							| 
									
										
										
										
											2015-11-23 10:57:36 -05:00
										 |  |  | 			ajaxifyData = ajaxifyData.replace(/<\//g, '<\\/'); | 
					
						
							| 
									
										
										
										
											2015-11-17 12:25:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			render.call(self, template, options, function(err, str) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return fn(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-11-17 12:25:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				str = (res.locals.postHeader ? res.locals.postHeader : '') + str + (res.locals.preFooter ? res.locals.preFooter : ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (res.locals.footer) { | 
					
						
							|  |  |  | 					str = str + res.locals.footer; | 
					
						
							|  |  |  | 				} else if (res.locals.adminFooter) { | 
					
						
							|  |  |  | 					str = str + res.locals.adminFooter; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (res.locals.renderHeader || res.locals.renderAdminHeader) { | 
					
						
							|  |  |  | 					var method = res.locals.renderHeader ? middleware.renderHeader : middleware.admin.renderHeader; | 
					
						
							|  |  |  | 					method(req, res, options, function(err, template) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return fn(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						str = template + str; | 
					
						
							|  |  |  | 						var language = res.locals.config ? res.locals.config.userLang || 'en_GB' : 'en_GB'; | 
					
						
							|  |  |  | 						language = req.query.lang || language; | 
					
						
							|  |  |  | 						translator.translate(str, language, function(translated) { | 
					
						
							| 
									
										
										
										
											2016-03-17 11:38:21 +02:00
										 |  |  | 							translated = translator.unescape(translated); | 
					
						
							| 
									
										
										
										
											2015-11-23 10:57:36 -05:00
										 |  |  | 							translated = translated + '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>'; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 							fn(err, translated); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2015-11-23 10:57:36 -05:00
										 |  |  | 					str = str + '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>'; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 					fn(err, str); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		next(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 18:43:40 -04:00
										 |  |  | 	function buildBodyClass(req) { | 
					
						
							|  |  |  | 		var clean = req.path.replace(/^\/api/, '').replace(/^\//, ''); | 
					
						
							|  |  |  | 		var parts = clean.split('/').slice(0, 3); | 
					
						
							|  |  |  | 		parts.forEach(function(p, index) { | 
					
						
							| 
									
										
										
										
											2015-10-30 15:43:04 -04:00
										 |  |  | 			parts[index] = index ? parts[0] + '-' + p : 'page-' + (p || 'home'); | 
					
						
							| 
									
										
										
										
											2015-10-16 18:43:40 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 		return parts.join(' '); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 15:11:32 -05:00
										 |  |  | }; |