1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | var pug = require('pug'),
|
8 | path = require('path'),
|
9 | fs = require('fs'),
|
10 | paths = require('../configs/paths'),
|
11 | config = require('../configs/config')
|
12 |
|
13 | module.exports = class {
|
14 |
|
15 | constructor() {
|
16 | this.templateActive = true;
|
17 | this.templateOptions = null;
|
18 | this.renderContent = null;
|
19 |
|
20 | if (config('template')) {
|
21 | this.templateName = this.templateNameDefault = config('template');
|
22 |
|
23 | if (!path.extname(this.config.template.layout)) {
|
24 | this.config.template.layout += '.pug';
|
25 | }
|
26 |
|
27 | this.templateLayout = this.templateLayoutDefault = config('template_layout');
|
28 | }
|
29 | else {
|
30 | this.templateName = this.templateNameDefault = 'default';
|
31 | this.templateLayout = this.templateLayoutDefault = 'default.pug';
|
32 | }
|
33 |
|
34 | this.errors = {
|
35 | "100": "Continue",
|
36 | "101": "Switching Protocols",
|
37 | "102": "Processing",
|
38 | "200": "OK",
|
39 | "201": "Created",
|
40 | "202": "Accepted",
|
41 | "203": "Non-Authoritative Information",
|
42 | "204": "No Content",
|
43 | "205": "Reset Content",
|
44 | "206": "Partial Content",
|
45 | "207": "Multi-Status",
|
46 | "208": "Already Reported",
|
47 | "226": "IM Used",
|
48 | "300": "Multiple Choices",
|
49 | "301": "Moved Permanently",
|
50 | "302": "Found",
|
51 | "303": "See Other",
|
52 | "304": "Not Modified",
|
53 | "305": "Use Proxy",
|
54 | "306": "Switch Proxy",
|
55 | "307": "Temporary Redirect",
|
56 | "308": "Permanent Redirect",
|
57 | "400": "Bad Request",
|
58 | "401": "Unauthorized",
|
59 | "402": "Payment Required",
|
60 | "403": "Forbidden",
|
61 | "404": "Not Found",
|
62 | "405": "Method Not Allowed",
|
63 | "406": "Not Acceptable",
|
64 | "407": "Proxy Authentication Required",
|
65 | "408": "Request Timeout",
|
66 | "409": "Conflict",
|
67 | "410": "Gone",
|
68 | "411": "Length Required",
|
69 | "412": "Precondition Failed",
|
70 | "413": "Payload Too Large",
|
71 | "414": "URI Too Long",
|
72 | "415": "Unsupported Media Type",
|
73 | "416": "Range Not Satisfiable",
|
74 | "417": "Expectation Failed",
|
75 | "418": "I'm a teapot",
|
76 | "421": "Misdirected Request",
|
77 | "422": "Unprocessable Entity",
|
78 | "423": "Locked",
|
79 | "424": "Failed Dependency",
|
80 | "426": "Upgrade Required",
|
81 | "428": "Precondition Required",
|
82 | "429": "Too Many Requests",
|
83 | "431": "Request Header Fields Too Large",
|
84 | "451": "Unavailable For Legal Reasons/Redirect",
|
85 | "103": "Checkpoint",
|
86 | "420": "Method Failure/Enhance Your Calm",
|
87 | "450": "Blocked by Windows Parental Controls",
|
88 | "498": "Invalid Token",
|
89 | "499": "Token Required/Request has been forbidden by antivirus",
|
90 | "509": "Bandwidth Limit Exceeded",
|
91 | "530": "Site is frozen",
|
92 | "440": "Login Timeout",
|
93 | "449": "Retry With"
|
94 | }
|
95 |
|
96 | }
|
97 |
|
98 | templatePath(template, layout) {
|
99 | return path.join(paths.app.views, 'templates', this.templateName, this.templateLayout);
|
100 | }
|
101 |
|
102 | controller(controllerName, action) {
|
103 | if (controllerName) {
|
104 | this.controllerName = controllerName;
|
105 | }
|
106 |
|
107 | if (action) {
|
108 | this.actionName = action;
|
109 | }
|
110 |
|
111 | return this;
|
112 | }
|
113 |
|
114 | http(request, response) {
|
115 | this.response = response;
|
116 |
|
117 | this.request = request;
|
118 |
|
119 | return this;
|
120 | }
|
121 |
|
122 | setConfig({response, request}) {
|
123 | if (response) {
|
124 | this.response = response;
|
125 | }
|
126 |
|
127 | if (response) {
|
128 | this.request = request;
|
129 | }
|
130 |
|
131 | if (paths) {
|
132 | paths = paths;
|
133 | }
|
134 |
|
135 | if (config) {
|
136 | this.config = config;
|
137 | }
|
138 |
|
139 | return this;
|
140 | }
|
141 |
|
142 | error(error, message, options = {}) {
|
143 | if (typeof error == 'object' && error['error']) {
|
144 | options = error;
|
145 | error = error.error;
|
146 | message = null;
|
147 | }
|
148 |
|
149 | if (typeof message == 'object') {
|
150 | options = message;
|
151 | message = null;
|
152 | }
|
153 |
|
154 | this.response.status(error);
|
155 |
|
156 | if (!options.error) {
|
157 | options.error = error;
|
158 | }
|
159 |
|
160 | if (!options.message && !message) {
|
161 | if (this.errors[error]) {
|
162 | options.message = this.errors[error]
|
163 | }
|
164 | else {
|
165 | options.message = ""
|
166 | }
|
167 | }
|
168 |
|
169 | if (this.request.accepts('html')) {
|
170 | return this.template(null, 'errors').render('errors/errors', options).send();
|
171 | }
|
172 |
|
173 | if (this.request.accepts('json')) {
|
174 | return this.response.send(options);
|
175 | }
|
176 |
|
177 | return this.response.type('txt').send(options.error + ' - ' + options.message);
|
178 | }
|
179 |
|
180 | template(name, layout = 'default.pug', options = null) {
|
181 | if (name === false) {
|
182 | this.templateActive = false;
|
183 | return this;
|
184 | }
|
185 | else if (name === null) {
|
186 | name = this.templateName;
|
187 | }
|
188 |
|
189 | if (typeof name == 'object') {
|
190 | this.templateOptions = name;
|
191 | }
|
192 | else if (typeof layout == 'object' && typeof name == 'string') {
|
193 | this.templateOptions = layout;
|
194 | this.templateName = name;
|
195 | }
|
196 | else {
|
197 | if (!path.extname(layout)) {
|
198 | layout += '.pug';
|
199 | }
|
200 |
|
201 | this.templateLayout = layout;
|
202 | this.templateName = name;
|
203 | this.templateOptions = options;
|
204 | }
|
205 |
|
206 | this.templateActive = true;
|
207 |
|
208 | return this;
|
209 | }
|
210 |
|
211 | render(name, options) {
|
212 | var viewPaths;
|
213 |
|
214 | if (typeof name == 'object') {
|
215 | options = name;
|
216 | name = this.actionName;
|
217 | }
|
218 |
|
219 | if (!name) {
|
220 | name = this.actionName;
|
221 | }
|
222 |
|
223 | if(!this.controllerName){
|
224 | return console.error('ERROR: Controller name not defined')
|
225 | }
|
226 |
|
227 | if (!path.extname(name)) {
|
228 | viewPaths = [
|
229 | path.join(paths.app.views, this.controllerName, name + '.pug'),
|
230 | path.join(paths.app.views, this.controllerName, name + '.jade'),
|
231 | path.join(paths.app.views, this.controllerName, name + '.html'),
|
232 | path.join(paths.app.views, name + '.pug'),
|
233 | path.join(paths.app.views, name + '.jade'),
|
234 | path.join(paths.app.views, name + '.html')
|
235 | ]
|
236 |
|
237 | }
|
238 | else {
|
239 | viewPaths = [
|
240 | path.join(paths.app.views, this.controllerName, this.actionName, name),
|
241 | path.join(paths.app.views, this.controllerName, name),
|
242 | path.join(paths.app.views, name)
|
243 | ]
|
244 | }
|
245 |
|
246 | for (var key in viewPaths) {
|
247 | try {
|
248 | if (path.extname(viewPaths[key]) == '.pug') {
|
249 | return this.renderTemplate(pug.renderFile(viewPaths[key], options))
|
250 | }
|
251 |
|
252 | var html = fs.readFileSync(viewPaths[key]).toString();
|
253 |
|
254 | if (options) {
|
255 | html = pug.render('script var view = ' + JSON.stringify(options)) + "\n\n" + html;
|
256 | }
|
257 |
|
258 | return this.renderTemplate(html);
|
259 | }
|
260 | catch (e) {
|
261 |
|
262 | }
|
263 | }
|
264 |
|
265 | console.error(`Error: View /${this.controllerName}/${name} and /${name} not found`);
|
266 | return this.error(404, options);
|
267 | }
|
268 |
|
269 | renderTemplate(content) {
|
270 | if (!this.templateActive) {
|
271 | this.renderContent = content;
|
272 | }
|
273 | else if (path.extname(this.templateLayout) == '.pug' || path.extname(this.templateLayout) == '.jade') {
|
274 | if (this.templateOptions) {
|
275 | this.templateOptions.content = content;
|
276 | content = this.templateOptions;
|
277 | this.templateOptions = null;
|
278 | }
|
279 | else {
|
280 | content = {content}
|
281 | }
|
282 |
|
283 | this.renderContent = pug.renderFile(this.templatePath(), content)
|
284 | }
|
285 | else {
|
286 | var html = fs.readFileSync(this.templatePath()).toString();
|
287 |
|
288 | this.renderContent = pug.render('script var view = ' + JSON.stringify(content)) + "\n\n" + html;
|
289 | }
|
290 |
|
291 | this.templateName = this.templateNameDefault;
|
292 | this.templateLayout = this.templateLayoutDefault;
|
293 |
|
294 | return this;
|
295 | }
|
296 |
|
297 | send(content) {
|
298 | if (content == undefined && this.renderContent) {
|
299 | content = this.renderContent;
|
300 | this.renderContent = null;
|
301 | }
|
302 |
|
303 | this.response.send(content);
|
304 | }
|
305 | }
|