forked from cliftonc/calipso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.js
252 lines (205 loc) · 5.9 KB
/
Helpers.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
250
251
252
/*!
* Calipso Core Library
*
* Copyright(c) 2011 Clifton Cunningham <[email protected]>
* MIT Licensed
*
* Dynamic helpers for insertion into the templating engine
* They all need to take in a req,res pair, these are then
* interpreted during the request stack and passed into the
* view engine (so for example 'request' is accessible).
*/
/**
* removes any trailing query string or hash values
* @method stripUrlToConvert
* @param url {string} The url to convert
* @return {String} Converted url, if applicable
*/
function stripUrlToConvert(url) {
var qs = url.search(/\?|#/);
if (qs > -1) {
url = url.substring(0, qs);
}
return url;
}
/**
* Exports
*/
exports = module.exports = {
/**
* Request shortcut
*/
request: function(req, res, calipso){
return req;
},
/**
* Translation shortcut
*/
t: function(req, res, calipso){
return req.t;
},
/**
* User shortcut
*/
user: function(req, res, calipso){
return req.session && req.session.user || { username: '', anonymous: true };
},
/**
* Pretty date helper
*/
prettyDate: function(req,res,calipso) {
var prettyFn = calipso.lib.prettyDate.prettyDate;
return prettyFn;
},
/**
* Hot date helper
*/
hotDate: function(req,res, calipso) {
var hotFn = calipso.lib.prettyDate.hotDate;
return hotFn;
},
/**
* Get block data not included preloaded in the theme configuration (in blockData)
*/
getBlock: function(req, res, calipso){
return function(block, next) {
// TODO : Allow block to be passed as a regex (e.g. to include all scripts.* blocks)
var output = "";
res.renderedBlocks.get(block,function(err,blocks) {
blocks.forEach(function(content) {
output += content;
});
if(typeof next === 'function')
next(null,output);
});
};
},
/**
* Generate a cache key for current url - should only be used in theme templates.
*/
getCacheKey: function(req, res, calipso) {
return function(type,name,params) {
var cacheKey = type + '::' + name, paramCount = 0;
if(params) {
cacheKey += "::";
calipso.lib._.each(res.params,function(param,key) {
if(param) {
cacheKey += (paramCount > 0 ? ":" : "") + (param ? (key + "=" + param) : "");
paramCount += 1;
}
});
}
return cacheKey;
}
},
/**
* Get a menu html, synchronous
*/
getMenu: function(req, res, calipso){
return function(menu) {
// Render menu
if(res.menu[menu]) {
var output = res.menu[menu].render(req);
return output;
} else {
return 'Menu ' + menu + ' does not exist!';
}
};
},
/**
* Directly call an exposed module function (e.g. over ride routing rules and inject it anywhere)
*/
getModuleFn: function(req, res, calipso){
return function(req,moduleFunction,options,next) {
// Call an exposed module function
// e.g. user.loginForm(req, res, template, block, next)
// First see if function exists
var moduleName = moduleFunction.split(".")[0];
var functionName = moduleFunction.split(".")[1];
if(calipso.modules[moduleName] && calipso.modules[moduleName].enabled && calipso.modules[moduleName].fn[functionName]) {
var fn = calipso.modules[moduleName].fn[functionName];
// Get the template
var template;
if(options.template && calipso.modules[moduleName].templates[options.template]) {
template = calipso.modules[moduleName].templates[options.template];
}
// Call the fn
try {
fn(req,res,template,null,next);
} catch(ex) {
next(ex);
}
} else {
next(null,"<div class='error'>Function " + moduleFunction + " requested via getModuleFn does not exist or module is not enabled.</div>");
}
};
},
/**
* Constructs individual classes based on the url request
*/
getPageClasses: function(req, res, calipso) {
var url = stripUrlToConvert(req.url);
return url.split('/').join(' ');
},
/**
* Constructs a single id based on the url request
*/
getPageId: function(req, res, calipso) {
var url = stripUrlToConvert(req.url),
urlFrags = url.split('/');
for (var i = 0, len = urlFrags.length; i < len; i++) {
var frag = urlFrags[i];
if (frag === '') {
urlFrags.splice(i, 1);
}
}
return urlFrags.join('-');
},
/**
* TODO Include a script in the response.
* Thoughts ... What this will actually do is add the file to a internal list.
* It will check if any of the files are different to the last request, if so,
* it will minify the contents of all the files, write to a temp js file, and reference
* that in the response (e.g. in the header of the template via another helper function).
* If nothing has changed it will just include the link to the previously generated file?
*/
includeScript: function(req, res, calipso){
return function(block) {
// TODO - not yet implemented
};
},
/**
* TODO Retrieve the concatenated / minified js scripts to add to template.
*/
getScripts: function(req, res, calipso){
return function() {
// TODO - not yet implemented
};
},
/**
* Flash message helpers
*/
flashMessages: function(req, res, calipso){
return function() {
return req.flash();
};
},
/**
* HTML helpers - form (formApi), table, link (for now)
*/
formApi: function(req, res, calipso){
return function(form) {
return calipso.form.render(form);
};
},
table: function(req, res, calipso){
return function(table) {
return calipso.table.render(table);
};
},
link: function(req, res, calipso){
return function(link) {
return calipso.link.render(link);
};
},
};