1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var LangUtils = require('@themost/common/utils').LangUtils;
|
11 | var HttpViewEngine = require('../types').HttpViewEngine;
|
12 | var _ = require('lodash');
|
13 | var fs = require('fs');
|
14 | var DirectiveEngine = require('./../handlers/directive').DirectiveEngine;
|
15 | var PostExecuteResultArgs = require('./../handlers/directive').PostExecuteResultArgs;
|
16 | var HttpViewContext = require('./../mvc').HttpViewContext;
|
17 | var HttpViewResult = require('./../mvc').HttpViewResult;
|
18 | var HttpNotFoundError = require('@themost/common').HttpNotFoundError;
|
19 | var Args = require('@themost/common').Args;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function NgApplication() {
|
25 |
|
26 | Object.defineProperty(this, 'services', {
|
27 | value: {}
|
28 | });
|
29 | }
|
30 |
|
31 | NgApplication.prototype.hasService = function(serviceCtor) {
|
32 | Args.notFunction(serviceCtor,"Service constructor");
|
33 | return this.services.hasOwnProperty(serviceCtor.name);
|
34 | };
|
35 |
|
36 | NgApplication.prototype.useService = function(serviceCtor) {
|
37 | Args.notFunction(serviceCtor,"Service constructor");
|
38 | this.services[serviceCtor.name] = new serviceCtor(this);
|
39 | return this;
|
40 | };
|
41 |
|
42 | NgApplication.prototype.getService = function(serviceCtor) {
|
43 | Args.notFunction(serviceCtor,"Service constructor");
|
44 | return this.services[serviceCtor.name];
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function NgContext() {
|
54 | this.application = null;
|
55 | this.request = null;
|
56 | this.response = null;
|
57 | }
|
58 |
|
59 | NgContext.prototype.getApplication = function() {
|
60 | return this.application;
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function NgEngine(context) {
|
71 | NgEngine.super_.bind(this)(context);
|
72 | }
|
73 | LangUtils.inherits(NgEngine, HttpViewEngine);
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | NgEngine.prototype.render = function(filename, data, callback) {
|
82 | var self = this;
|
83 |
|
84 | var template = (self.context.request && self.context.request.route && self.context.request.route.template) ||
|
85 | (self.context.request && self.context.request.routeData && self.context.request.routeData.template);
|
86 | var controller = self.context.request && self.context.request.route && self.context.request.route.controller;
|
87 |
|
88 | function renderFile(file, view, data, done) {
|
89 | fs.readFile(file,'utf-8', function(err, str) {
|
90 | try {
|
91 | if (err) {
|
92 | if (err.code === 'ENOENT') {
|
93 |
|
94 | return done(new HttpNotFoundError('View cannot be found.'));
|
95 | }
|
96 | return done(err);
|
97 | }
|
98 | var viewContext = new HttpViewContext(self.getContext());
|
99 | viewContext.body = str;
|
100 | viewContext.data = data;
|
101 | viewContext.templatePath = view;
|
102 | var directiveHandler = new DirectiveEngine();
|
103 | var args = _.assign(new PostExecuteResultArgs(), {
|
104 | "context": self.getContext(),
|
105 | "target":viewContext
|
106 | });
|
107 |
|
108 | directiveHandler.hasBootstrap = this.hasBootstrap;
|
109 | directiveHandler.postExecuteResult(args, function(err) {
|
110 | if (err) {
|
111 | return done(err);
|
112 | }
|
113 | return done(null, viewContext.body);
|
114 | });
|
115 | }
|
116 | catch (err) {
|
117 | return done(err);
|
118 | }
|
119 | });
|
120 | }
|
121 |
|
122 | if (typeof template === 'string' && typeof controller === 'string') {
|
123 | return HttpViewResult.resolveViewPath(self.context, controller, template, {
|
124 | extension: "ng"
|
125 | }, function(err, layout) {
|
126 | if (layout) {
|
127 | return renderFile(layout, filename, data, callback);
|
128 | }
|
129 | else {
|
130 | return renderFile(filename, null, data, callback);
|
131 | }
|
132 | });
|
133 | }
|
134 | return renderFile(filename, null, data, callback);
|
135 |
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | NgEngine.prototype.renderString = function(str, data) {
|
145 | const self = this;
|
146 | return new Promise((resolve, reject) => {
|
147 | var viewContext = new HttpViewContext(self.getContext());
|
148 | viewContext.body = str;
|
149 | viewContext.data = data || {};
|
150 | var directiveHandler = new DirectiveEngine();
|
151 | var args = Object.assign(new PostExecuteResultArgs(), {
|
152 | "context": self.getContext(),
|
153 | "target":viewContext
|
154 | });
|
155 |
|
156 | directiveHandler.hasBootstrap = this.hasBootstrap;
|
157 | directiveHandler.postExecuteResult(args, function(err) {
|
158 | if (err) {
|
159 | return reject(err);
|
160 | }
|
161 | return resolve(viewContext.body);
|
162 | });
|
163 | });
|
164 |
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | NgEngine.prototype.bootstrap = function(bootstrapFunc) {
|
176 | Args.notFunction(bootstrapFunc, 'Application bootstrap should be a function');
|
177 | this.hasBootstrap = bootstrapFunc;
|
178 | return this;
|
179 | };
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | NgEngine.createInstance = function(context) {
|
186 | return new NgEngine(context);
|
187 | };
|
188 |
|
189 | module.exports.NgEngine = NgEngine;
|
190 | module.exports.NgApplication = NgApplication;
|
191 | module.exports.NgContext = NgContext;
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | module.exports.createInstance = function(context) {
|
197 | return NgEngine.createInstance(context);
|
198 | };
|