/
index.js
140 lines (117 loc) · 3.28 KB
/
index.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
/**
* Module dependencies.
*/
var url = require('url')
, qs = require('querystring')
/**
* Helpers method
*
* @param {String} name
* @return {Function}
* @api public
*/
function helpers (name, app) {
return function (req, res, next) {
res.locals.appName = name || 'App'
res.locals.title = name || 'App'
res.locals.req = req
res.locals.isActive = function (link) {
return req.url.indexOf(link) !== -1 ? 'active' : ''
}
res.locals.formatDate = formatDate
res.locals.formatDatetime = formatDatetime
res.locals.stripScript = stripScript
res.locals.createPagination = createPagination(req)
if (typeof req.flash !== 'undefined') {
res.locals.info = req.flash('info')
res.locals.errors = req.flash('errors')
res.locals.success = req.flash('success')
res.locals.warning = req.flash('warning')
}
/**
* Render mobile views
*
* If the request is coming from a mobile/tablet device, it will check if
* there is a .mobile.ext file and it that exists it tries to render it.
*
* Refer https://github.com/madhums/nodejs-express-mongoose-demo/issues/39
* For the implementation refer the above app
*/
var ua = req.header('user-agent')
var fs = require('fs')
res._render = res.render
req.isMobile = /mobile/i.test(ua)
res.render = function (template, locals, cb) {
var view = template + '.mobile.' + app.get('view engine')
var file = app.get('views') + '/' + view
if (/mobile/i.test(ua) && fs.existsSync(file)) {
res._render(view, locals, cb)
} else {
res._render(template, locals, cb)
}
}
next()
}
}
module.exports = helpers
/**
* Pagination helper
*
* @param {Number} pages
* @param {Number} page
* @return {String}
* @api private
*/
function createPagination (req) {
return function createPagination (pages, page) {
var params = qs.parse(url.parse(req.url).query)
var str = ''
params.page = 0
var clas = page == 0 ? "active" : "no"
str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">First</a></li>'
for (var p = 1; p < pages; p++) {
params.page = p
clas = page == p ? "active" : "no"
str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">'+ p +'</a></li>'
}
params.page = --p
clas = page == params.page ? "active" : "no"
str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">Last</a></li>'
return str
}
}
/**
* Format date helper
*
* @param {Date} date
* @return {String}
* @api private
*/
function formatDate (date) {
var monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
return monthNames[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear()
}
/**
* Format date time helper
*
* @param {Date} date
* @return {String}
* @api private
*/
function formatDatetime (date) {
var hour = date.getHours();
var minutes = date.getMinutes() < 10
? '0' + date.getMinutes().toString()
: date.getMinutes();
return formatDate(date) + ' ' + hour + ':' + minutes;
}
/**
* Strip script tags
*
* @param {String} str
* @return {String}
* @api private
*/
function stripScript (str) {
return str.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
}