-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPage.js
249 lines (198 loc) · 6.66 KB
/
Page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var _ = require("underscore");
var Backbone = require("backbone");
var path = require("path");
var async = require("async");
var Page = function(attributes, options) {
attributes = attributes || {};
if(this.defaults)
attributes = _.extend({}, attributes, this.cloneDefaults());
if(this.extendDefaults)
attributes = this.extendDefaultAttributes(attributes, this.extendDefaults)
this.attributes = attributes;
this.cid = this.attributes.name || _.uniqueId('page');
this.initialize.apply(this, arguments);
this.packageme = require("packageme");
// prepend root to any of given paths as well as calculate the root if it is missing.
this.rootPaths();
// convert code, views, style attributes to packageme options
this.packagemeOptions();
}
_.extend(Page.prototype, Backbone.Events, {
initialize: function() {},
cloneDefaults: function() {
var result = _.clone(this.defaults);
result.views = _.clone(this.defaults.views);
result.code = _.clone(this.defaults.code);
result.style = _.clone(this.defaults.style);
return result;
},
extendDefaultAttributes: function(input, attrs){
// mix input with attrs
for(var key in attrs)
if(typeof input[key] == "undefined")
input[key] = attrs[key];
else
if(Array.isArray(input[key]))
for(var i = 0; i<attrs[key].length; i++)
input[key].push(attrs[key][i]);
else
if(typeof input[key] == "object")
_.extend(input[key], attrs[key]);
else
input[key] = attrs[key];
return input;
},
rootPaths : function(){
var root = this.attributes.root;
var content = this.attributes.content;
var layout = this.attributes.layout;
var style = this.attributes.style;
var code = this.attributes.code;
var views = this.attributes.views;
// in case no content and no root are given, it is not possible to root any
// of the other properties
if(!root && !content)
return;
if(!root)
root = this.attributes.root = path.dirname(content);
if(content.indexOf(".") == 0 || content.indexOf("/") != 0)
this.attributes.content = content = path.normalize(root+"/"+content);
if(typeof layout == "string" && layout.indexOf(".") == 0 && layout.indexOf("/") != 0)
this.attributes.layout = path.normalize(root+"/"+layout);
var prependRoot = function(v){
// packageme driven paths can be objects of packageme/lib/File structure,
// threfore respecting that usage
if(typeof v == "object") {
if(v.fullPath.indexOf(".") == 0 || v.fullPath.indexOf("/") != 0)
v.fullPath = path.normalize(root+"/"+v.fullPath);
return v;
}
// just prepend root if given path is relative or it is missing start /
if(v.indexOf(".") == 0 || v.indexOf("/") != 0)
return path.normalize(root+"/"+v);
else
return v;
};
if(style)
this.attributes.style = style = _.map(style, prependRoot);
if(code)
this.attributes.code = code = _.map(code, prependRoot);
if(views)
this.attributes.views = views = _.map(views, prependRoot);
},
packagemeOptions : function(){
if(typeof this.attributes.packageme == "undefined")
this.attributes.packageme = {};
if(this.attributes.code)
this.attributes.code = _.extend({
source: this.attributes.code,
format: "js",
prefix: "/"+this.cid+"/",
compile: this.attributes.compile
}, this.attributes.packageme);
if(this.attributes.style)
this.attributes.style = _.extend({
source: this.attributes.style,
format: "css",
prefix: "/"+this.cid+"/"
}, this.attributes.packageme);
if(this.attributes.views)
this.attributes.views = _.extend({
sourceFolder: this.attributes.views,
format: this.attributes.viewsEngine || "html"
}, this.attributes.packageme);
},
get: function(name){
return this.attributes[name];
},
set: function(name, value) {
return this.attributes[name] = value;
},
registerStyleHandlers : function(app){
if(this.attributes.style)
this.packageme(this.attributes.style).serveFilesToExpress(app);
return this;
},
registerCodeHandlers: function(app) {
if(this.attributes.code)
this.packageme(this.attributes.code).serveFilesToExpress(app);
return this;
},
compileViews : function(callback) {
if(this.attributes.views)
this.packageme(this.attributes.views).toString(function(data){
callback(null, data);
});
else
callback(null, undefined);
return this;
},
compileCode : function(callback){
if(this.attributes.code) {
this.packageme(this.attributes.code).toScriptTags(function(data){
callback(null, data);
});
} else
callback(null, []);
return this;
},
generateCode : function(callback){
if(this.attributes.code) {
this.packageme(this.attributes.code).toString(function(data){
callback(null, data);
});
} else
callback(null, "");
return this;
},
compileStyle : function(callback){
if(this.attributes.style) {
this.packageme(this.attributes.style).toStyleTags(function(data){
callback(null, data);
});
} else
callback(null, []);
return this;
},
generateStyle : function(callback){
if(this.attributes.style) {
this.packageme(this.attributes.style).toString(function(data){
callback(null, data);
});
} else
callback(null, "");
return this;
},
render : function(req, res, next){
var renderData = {};
if(this.attributes.variables)
renderData = _.extend(renderData, this.attributes.variables);
// provide params and query to templates
renderData.params = JSON.stringify(req.params ? req.params : {});
renderData.query = JSON.stringify(req.query ? req.query : {});
// compile the this and render the output
var page = this;
async.parallel([
function(done){ page.compileCode(done) },
function(done){ page.compileStyle(done) },
function(done){ page.compileViews(done) }
], function(err, results){
renderData.javascripts = results[0].join("\n");
renderData.stylesheets = results[1].join("\n");
renderData.views = results[2];
res.render(page.attributes.content, renderData, function(err, bodyData){
if(err)
throw err;
if(page.attributes.layout) {
renderData.content = bodyData;
res.render(page.attributes.layout, renderData);
}
else
res.send(bodyData);
});
})
return this;
}
});
Page.extend = Backbone.View.extend;
module.exports = Page;