-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.js
165 lines (132 loc) · 5.02 KB
/
component.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
//----------------------------------------------------------------------------------------------------------------------
// The component declaration for the deluge-pages component
//
// @module component.js
//----------------------------------------------------------------------------------------------------------------------
var walk = require('walk');
var models = require('./models');
//----------------------------------------------------------------------------------------------------------------------
function socketHandler(socket)
{
var user = socket.handshake.user;
//------------------------------------------------------------------------------------------------------------------
// Page Templates
//------------------------------------------------------------------------------------------------------------------
socket.on('list page templates', function(cb)
{
var templatePath = config.get('pageTemplates', './client/components/pages/partials/');
var walker = walk.walk(templatePath, { followLinks: true });
var templates = [];
walker.on('file', function(root, stat, next)
{
var name = stat.name.replace('.tpl', '');
// We need to filter out certain files if we use the default template directory.
if(['fallback.tpl.html', 'notfound.tpl.html', 'pages.tpl.html'].indexOf(stat.name) == -1 || templatePath != './client/components/pages/partials/')
{
var templateUrlRoot = root.replace('./client', '');
templates.push({ base: name, template: templateUrlRoot + name });
} // end if
next();
});
walker.on('end', function()
{
cb(undefined, templates)
});
});
//------------------------------------------------------------------------------------------------------------------
// Pages
//------------------------------------------------------------------------------------------------------------------
socket.on('list pages', function(includeDrafts, cb)
{
filter = { draft: false };
if(includeDrafts)
{
var filter = {};
} // end if
models.Page.find(filter, function(error, pages)
{
if(error)
{
logger.error('Error retrieving pages: %s\n %s', error.message || error.toString(), error.stack || "");
} // end if
cb(error, pages);
});
});
socket.on('has pages', function(cb)
{
models.Page.find({ draft: false }, function(error, pages)
{
if(error)
{
logger.error('Error retrieving pages: %s\n %s', error.message || error.toString(), error.stack || "");
} // end if
// If we get back pages, or we return false
cb(error, !!(pages || []).length);
});
});
socket.on('add page', function(page, cb)
{
var model = new models.Page(page);
model.save(function(error)
{
if(error)
{
logger.error('Error saving page: %s\n %s', error.message || error.toString(), error.stack || "");
} // end if
cb(error, page);
});
});
socket.on('get page', function(slug, includeDrafts, cb)
{
var filter = { slug: slug, draft: false };
if(arguments.length == 2)
{
cb = includeDrafts;
includeDrafts = false;
} // end if
if(includeDrafts)
{
filter = { slug: slug };
} // end if
models.Page.findOne(filter, function(error, page)
{
if(error)
{
logger.error('Error retrieving page: %s\n %s', error.message || error.toString(), error.stack || "");
} // end if
cb(error, page);
});
});
socket.on('update page', function(page, cb)
{
models.Page.update({ slug: page.slug }, page, function(error)
{
if(error)
{
logger.error('Error retrieving page: %s\n %s', error.message || error.toString(), error.stack || "");
} // end if
cb(error);
});
});
socket.on('remove page', function(slug, cb)
{
models.Page.remove({ slug: slug }, function(error)
{
if(error)
{
logger.error('Error retrieving page: %s\n %s', error.message || error.toString(), error.stack || "");
} // end if
cb(error);
});
});
} // end socketHandler
//----------------------------------------------------------------------------------------------------------------------
module.exports = {
includes: {
js: ['pages.controllers.js']
},
socketHandler: socketHandler,
adminTemplate: '/partials/admin.html',
models: models
}; // end exports
//----------------------------------------------------------------------------------------------------------------------