-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
204 lines (175 loc) · 5.17 KB
/
plugin.js
File metadata and controls
204 lines (175 loc) · 5.17 KB
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
/**
* Plugin.js file, set configs, routes, hooks and events here
*
* see http://wejs.org/docs/we/plugin
*/
const fs = require('fs');
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
const Op = plugin.we.Op;
plugin.setConfigs({
permissions: {
'manage_widget': {
'group': 'admin',
'title': 'Manage widget',
'description': 'Create, update and delete widgets'
},
'create_context_widget': {
'group': 'admin',
'title': 'Create global widgets',
'description': 'Create, update and delete widgets in context'
}
}
});
plugin.setRoutes({
'get /api/v1/widget-form/:theme/:layout/:type': {
'controller' : 'widget',
'action' : 'getCreateForm',
'model' : 'widget',
'permission' : true
},
});
// widget type instances
plugin.widgetTypes = {};
// widgets lists with widget folder
plugin.widgets = {};
/**
* Get default widget query.
*
* @return {Object} Query object to use in sequelize.findAll
*/
plugin.getDefaultWidgetQuery = function getDefaultWidgetQuery(req, res) {
let regions = null;
const theme = res.getTheme();
if (theme) {
if (!res.locals.layoutName || !theme.layouts[res.locals.layoutName])
res.locals.layoutName = 'default';
regions = Object.keys(theme.layouts[res.locals.layoutName].regions);
}
let path = req.path;
// remove last slash from internal paths
if (path.length > 1 && path.substr(-1) == '/') {
path = path.slice(0, -1);
}
return {
theme: { [Op.or]: [ res.locals.theme || null, null, ''] },
layout: res.locals.layoutName,
regionName: regions,
context: res.locals.widgetContext || null,
// path
path: { [Op.or]: [ path, null, '' ]},
// widget visibility conditions
[Op.or]: [
// current session (model)
{
[Op.and]: [
{ modelName: res.locals.model || null },
{ modelId: null },
]
},
// global
{
[Op.and]: [
{ modelName: null },
{ modelId: null },
]
},
// current record
{
[Op.and]: [
{ modelName: res.locals.model || null },
{ modelId: res.locals.id || null },
]
},
// contents of this sesison
{
[Op.and]: [
{ modelName: res.locals.model || null },
{ modelId: null },
{ inRecord: true },
]
}
]
};
};
/**
* Load widgets from folder server/widgets
*
* @param {Object} we
* @param {Function} cb callback
*/
plugin.loadWidgets = function loadWidgets(data, cb) {
let name, file;
const we = data.we;
const pi = data.plugin;
var widgetsPath = pi.pluginPath + '/server/widgets';
fs.readdir(widgetsPath, (err, list)=> {
if (err) {
if (err.code === 'ENOENT') return cb();
return cb(err);
}
for (let i = 0; i < list.length; i++) {
name = list[i];
file = widgetsPath +'/'+name;
plugin.widgets[name] = file;
plugin.widgetTypes[name] = require(plugin.widgets[name])(
we.projectPath, plugin.Widget
);
}
cb();
});
}
plugin.hooks.on('request:view:after:resolve:layout', function (data, done) {
const theme = data.res.getTheme();
if (!theme) return done();
const req = data.req;
const res = data.res;
const we = req.we;
const regions = Object.keys(theme.layouts[res.locals.layoutName].regions);
for (let i = 0; i < regions.length; i++) {
res.locals.regions[regions[i]] = { widgets: [] };
}
let where = plugin.getDefaultWidgetQuery(req, res);
if (res.locals.action != 'findOne') {
where.inRecord = { [Op.or]: [false , null] };
}
// preload all widgets for this response
return we.db.models.widget.findAll({
where: where,
order: [
['weight', 'ASC'], ['createdAt', 'DESC']
],
})
.then(function afterFindAllWidgets(widgets) {
we.utils.async.each(widgets, (widget, nextW)=> {
// set widget
res.locals.regions[widget.regionName].widgets.push(widget);
// run view middleware for load widget view data
we.log.verbose('widget.viewMiddleware:', widget.id, widget.type);
widget.viewMiddleware(req, res, nextW);
}, done);
return null;
})
.catch(done);
});
plugin.hooks.on('we:before:load:plugin:features', function (we, done) {
plugin.Widget = require('./lib/Widget')(we);
done();
});
plugin.hooks.on('plugin:load:features', plugin.loadWidgets);
// bind update widget middleware
plugin.events.on('router:add:acl:middleware:after', function (data) {
data.middlewares.push(require('./lib/updateWidgetMiddleware'));
});
plugin.addJs('we-plugin-widget', {
weight: 11,
pluginName: 'we-plugin-widget',
path: 'files/public/we-plugin-widget.js'
});
plugin.addCss('we-plugin-widget', {
weight: 11,
pluginName: 'we-plugin-widget',
path: 'files/public/we-plugin-widget.css'
});
return plugin;
};