UNPKG

5.16 kBJavaScriptView Raw
1var log = require("phnq_log").create(__filename);
2var _ = require("underscore");
3var _fs = require("fs");
4var _path = require("path");
5var config = require("./config");
6var aggregator = require("./aggregator");
7var perf = require("./perf");
8
9exports.init = function(app, appRoot)
10{
11 var widgetManager = require("./widget_manager").instance();
12
13 /*
14 * Implicit addition of controllers. Looks for a folder named "controllers"
15 * in the app folder and calls init() on required js files.
16 */
17 var controllersPath = _path.join(appRoot, "controllers");
18 _fs.readdir(controllersPath, function(err, names)
19 {
20 _.each(names, function(name)
21 {
22 if(name.match(/^[^\.].*\.js$/))
23 {
24 var controller = require(_path.join(controllersPath, name));
25 if(typeof(controller.init) == "function")
26 controller.init(app);
27 }
28 })
29 });
30
31 /*
32 * Gets the client-side bootstrap JS. This includes jQuery and some
33 * other JS utilities to allow the loading of widgets.
34 */
35 // TODO: make this cacheable.
36 app.get(config.uriPrefix+"/boot", function(req, res)
37 {
38 res.type("js");
39 res.send(aggregator.getClientBoot());
40 });
41
42 /*
43 * Load widgets onto the client.
44 */
45 app.get(config.uriPrefix+"/load", function(req, res)
46 {
47 var result =
48 {
49 templates: {},
50 scripts: [],
51 styles: []
52 };
53
54 var classesOnly = req.query.co.split(",");
55 var fullyLoaded = req.query.fl.split(",");
56 var toLoad = req.query.t.split(",");
57
58 // Fill in toLoad with dependencies.
59 for(var i=0; i<toLoad.length; i++)
60 {
61 var type = toLoad[i];
62 var widget = widgetManager.getWidget(type);
63 if(widget)
64 {
65 var deps = widget.getDependencies();
66 for(var j=0; j<deps.length; j++)
67 {
68 if(_.indexOf(fullyLoaded, deps[j]) == -1)
69 toLoad.push(deps[j]);
70 }
71 }
72 }
73 toLoad = _.uniq(toLoad);
74
75 toLoad = widgetManager.sortDependencies(toLoad);
76
77 for(var i=0; i<toLoad.length; i++)
78 {
79 var type = toLoad[i];
80 var widget = widgetManager.getWidget(type);
81 if(widget)
82 {
83 result.templates[type] = widget.getCompiledMarkup();
84
85 if(_.indexOf(classesOnly, type) == -1)
86 {
87
88 var script = widget.getScript();
89 if(script)
90 result.scripts.push(script);
91
92 var style = widget.getStyle();
93 if(style)
94 result.styles.push(style);
95 }
96 }
97 }
98
99 var buf = [];
100 buf.push(req.query.jsoncallback);
101 buf.push("(");
102 buf.push(JSON.stringify(result));
103 buf.push(");");
104
105 res.contentType("js");
106 res.send(buf.join(""));
107 });
108
109 app.get(config.uriPrefix+"/agg/:aggFile.js", function(req, res)
110 {
111 var scriptAgg = aggregator.newScriptAggregator(req.params.aggFile);
112 scriptAgg.generate(function()
113 {
114 res.type("js");
115 res.sendfile(_path.join(appRoot, "static", "agg", req.params.aggFile+".js"));
116 });
117 });
118
119 app.get(config.uriPrefix+"/agg/:aggFile.js.gz", function(req, res)
120 {
121 var scriptAgg = aggregator.newScriptAggregator(req.params.aggFile);
122 scriptAgg.generate(function()
123 {
124 res.type("js");
125 res.header("Content-Encoding", "gzip");
126 res.sendfile(_path.join(appRoot, "static", "agg", req.params.aggFile+".js.gz"));
127 });
128 });
129
130 app.get(config.uriPrefix+"/agg/:aggFile.css", function(req, res)
131 {
132 var styleAgg = aggregator.newStyleAggregator(req.params.aggFile);
133 styleAgg.generate(function()
134 {
135 res.type("css");
136 res.sendfile(_path.join(appRoot, "static", "agg", req.params.aggFile+".css"));
137 });
138 });
139
140 app.get(config.uriPrefix+"/agg/:aggFile.css.gz", function(req, res)
141 {
142 var styleAgg = aggregator.newStyleAggregator(req.params.aggFile);
143 styleAgg.generate(function()
144 {
145 res.type("css");
146 res.header("Content-Encoding", "gzip");
147 res.sendfile(_path.join(appRoot, "static", "agg", req.params.aggFile+".css.gz"));
148 });
149 });
150
151 app.get(config.uriPrefix+"/:widgetType", function(req, res)
152 {
153 require("./phnq_widgets").renderWidget(req.params.widgetType, {}, req, res);
154 });
155
156 app.get(new RegExp(config.uriPrefix+"/([^/]*)/_?static/(.*)"), function(req, res)
157 {
158 var widgetType = req.params[0];
159 var staticPath = req.params[1];
160
161 var widget = widgetManager.getWidget(widgetType);
162 if(!widget)
163 return res.send(404);
164
165 res.sendfile(_path.join(widget.dir, "_static", staticPath));
166 });
167
168 app.post(config.uriPrefix+"/:widgetType/remote/:cmd", function(req, res)
169 {
170 var widget = widgetManager.getWidget(req.params.widgetType);
171 if(!widget)
172 return res.send(404);
173
174 var args = req.body;
175 args.push(function(resp)
176 {
177 res.json(resp);
178 });
179
180 var remoteHandlerFn = widget.getRemoteHandlers()["post"+req.params.cmd] || widget.getRemoteHandlers()[req.params.cmd];
181 remoteHandlerFn.apply(null, args);
182 });
183
184 app.get(new RegExp("^"+config.uriPrefix+"/([^/]*)/remote/([^/]*)/(.*)"), function(req, res)
185 {
186 var widgetType = req.params[0];
187 var cmd = req.params[1];
188 var args = req.params[2].split("/");
189
190 var widget = widgetManager.getWidget(widgetType);
191 if(!widget)
192 return res.send(404);
193
194 args.push(function(resp)
195 {
196 res.json(resp);
197 });
198 var remoteHandlerFn = widget.getRemoteHandlers()["get"+cmd];
199
200 if(remoteHandlerFn.maxAge)
201 res.header("Cache-Control", "max-age="+remoteHandlerFn.maxAge+", must-revalidate");
202
203 remoteHandlerFn.apply(null, args);
204 });
205};