This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
forked from NodeBB-Community/nodebb-plugin-custom-pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
203 lines (170 loc) · 4.8 KB
/
library.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
"use strict";
/* globals module, require, __dirname */
var plugin = {},
db = module.parent.require('./database'),
hotswap = module.parent.require('./hotswap');
var nconf = module.parent.require('nconf'),
async = module.parent.require('async'),
mkdirp = module.parent.require('mkdirp'),
winston = module.parent.require('winston'),
express = module.parent.require('express'),
middleware;
var fs = require('fs'),
path = require('path');
function renderCustomPage(req, res) {
var path = req.path.replace(/\/(api\/)?/, '').replace(/\/$/, '');
res.render(path, {
title: plugin.pagesHash[path].name
});
}
function renderAdmin(req, res) {
getCustomPages(function(err, data) {
res.render('admin/custom-pages', {
pages: data
});
});
}
function getCustomPages(callback) {
if (plugin.pagesCache) {
return callback(null, plugin.pagesCache);
} else {
db.get('plugins:custom-pages', function(err, data) {
try {
var pages = JSON.parse(data);
if (pages == null) {
pages = [];
}
// Eliminate errors in route definition
plugin.pagesCache = pages.map(function(pageObj) {
pageObj.route = pageObj.route.replace(/^\/*/g, ''); // trim leading slashes from route
return pageObj;
});
plugin.pagesHash = plugin.pagesCache.reduce(function(memo, cur) {
memo[cur.route] = cur;
return memo;
}, {});
callback(null, plugin.pagesCache);
} catch (err) {
callback(err);
}
});
}
}
plugin.prepare = function(hotswapIds, callback) {
hotswapIds.push('custom-pages');
callback(null, hotswapIds);
};
plugin.setAvailableTemplates = function(templates, callback) {
getCustomPages(function(err, data) {
for (var d in data) {
if (data.hasOwnProperty(d)) {
templates.push(data[d].route + '.tpl');
}
}
callback(err, templates);
});
};
plugin.setWidgetAreas = function(areas, callback) {
getCustomPages(function(err, data) {
for (var d in data) {
if (data.hasOwnProperty(d)) {
areas = areas.concat([
{
'name': data[d].name + ' Header',
'template': data[d].route + '.tpl',
'location': 'header'
},
{
'name': data[d].name + ' Footer',
'template': data[d].route + '.tpl',
'location': 'footer'
},
{
'name': data[d].name + ' Sidebar',
'template': data[d].route + '.tpl',
'location': 'sidebar'
},
{
'name': data[d].name + ' Content',
'template': data[d].route + '.tpl',
'location': 'content'
}
]);
}
}
callback(err, areas);
});
};
plugin.addAdminNavigation = function(header, callback) {
header.plugins.push({
route: '/custom-pages',
icon: 'fa-mobile',
name: 'Custom Static Pages'
});
callback(null, header);
};
plugin.addNavigation = function(header, callback) {
getCustomPages(function(err, data) {
for (var d in data) {
if (data.hasOwnProperty(d)) {
header.navigation.push({
"class": data[d].class,
"route": "/" + data[d].route,
"text": data[d].name,
"title": data[d].name
});
}
}
callback(null, header);
});
};
plugin.init = function(params, callback) {
var app = params.router;
middleware = params.middleware;
app.get('/admin/custom-pages', middleware.admin.buildHeader, renderAdmin);
app.get('/api/admin/custom-pages', renderAdmin);
var SocketAdmin = module.parent.require('./socket.io/admin');
SocketAdmin.settings.saveCustomPages = function(socket, data, callback) {
delete plugin.pagesCache;
delete plugin.pagesHash;
async.series([
async.apply(db.set, 'plugins:custom-pages', JSON.stringify(data)),
async.apply(plugin.reloadRoutes)
], callback);
};
plugin.reloadRoutes(callback);
};
plugin.reloadRoutes = function(callback) {
var pagesRouter = express.Router(),
helpers = module.parent.require('./routes/helpers');
pagesRouter.hotswapId = 'custom-pages';
fs.readFile(path.join(__dirname, 'templates/custom-page.tpl'), function(err, customTPL) {
customTPL = customTPL.toString();
getCustomPages(function(err, pages) {
async.each(pages, function(pageObj, next) {
var route = pageObj.route;
helpers.setupPageRoute(pagesRouter, '/' + route, middleware, [], renderCustomPage);
if (path.dirname(route) !== '.') {
// Subdirectories specified
mkdirp(path.join(nconf.get('views_dir'), path.dirname(route)), function(err) {
if (err) {
return next(err);
}
fs.writeFile(path.join(nconf.get('views_dir'), route + '.tpl'), customTPL, next);
});
} else {
fs.writeFile(path.join(nconf.get('views_dir'), route + '.tpl'), customTPL, next);
}
}, function(err) {
if (err) {
winston.error('[plugin/custom-pages] Could not re-initialise routes!');
winston.error(' ' + err.message);
return callback(err);
}
hotswap.replace('custom-pages', pagesRouter);
callback();
});
});
});
};
module.exports = plugin;