-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
242 lines (217 loc) · 7.14 KB
/
plugin.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
/**
* Plugin.js file, set configs, routes, hooks and events here
*
* see http://wejs.org/docs/we/extend.plugin
*/
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
const Op = plugin.we.Op;
// set plugin configs
plugin.setConfigs({
permissions: {
'find_term': {
'group': 'term',
'title': 'Find terms',
'description': 'Find and find all terms'
},
'create_term': {
'group': 'term',
'title': 'Create one term',
'description': 'Create one new term'
},
'update_term': {
'group': 'term',
'title': 'Update one term',
'description': 'Update one new term'
},
'delete_term': {
'group': 'term',
'title': 'Delete one term',
'description': 'Delete one term record'
},
'find_vocabulary': {
'group': 'vocabulary',
'title': 'Find vocabularys',
'description': 'Find and find all vocabularys'
},
'create_vocabulary': {
'group': 'vocabulary',
'title': 'Create one vocabulary',
'description': 'Create one new vocabulary'
},
'update_vocabulary': {
'group': 'vocabulary',
'title': 'Update one vocabulary',
'description': 'Update one new vocabulary'
},
'delete_vocabulary': {
'group': 'vocabulary',
'title': 'Delete one vocabulary',
'description': 'Delete one vocabulary record'
}
}
});
plugin.setResource({
name: 'vocabulary',
routeId: ':vocabularyId'
});
plugin.setResource({
parent: 'vocabulary',
name: 'term',
routeId: ':termId',
findOne: {
metatagHandler: 'termFindOne'
},
findAll: {
metatagHandler: 'termFindAll'
}
});
// set plugin routes
plugin.setRoutes({
// Term
'get /api/v1/term-texts': {
controller : 'term',
action : 'findTermTexts',
model : 'term',
responseType : 'json'
},
'get /vocabulary/:vocabularyId/tag-clound': {
controller : 'vocabulary',
action : 'getTagUsageCount',
model : 'term',
responseType : 'json'
},
'get /vocabulary/:vocabularyId/term/:termId/:modelName': {
controller : 'term',
action : 'findOneTermContent',
model : 'term',
template : 'term/findOne',
titleHandler : 'recordField',
itemTitleHandler : 'recordField',
titleField : 'text',
loadCurrentRecord: true,
paramIdName: 'termId',
metatagHandler: 'termFindOne'
}
});
plugin.hooks.on('we-plugin-menu:after:set:core:menus', function (data, done) {
const we = data.req.we;
// set admin menu
if (data.res.locals.isAdmin) {
data.res.locals.adminMenu.addLink({
id: 'admin.vocabulary',
text: '<i class="fa fa-tags"></i> '+
data.req.__('vocabulary.find'),
href: we.router.urlTo( 'admin.vocabulary.find', [], we),
weight: 10
});
}
done();
});
plugin.paramVocabularyIdMD = function paramVocabularyIdMD (req, res, next, id) {
let where = {};
// need to check if is id to skip postgreql error if search for texts in number
if (Number(id) ) {
where = {
[Op.or]: { id: id, name: id }
};
} else {
where = { name: id };
}
plugin.we.db.models.vocabulary
.findOne({
where: where
})
.then(function afterLoadVocabulary(v) {
if (!v) {
res.notFound();
return null;
}
res.locals.currentVocabulary = v;
req.params.vocabularyName = v.name;
next();
return null;
})
.catch(next);
};
plugin.events.on('we:express:set:params', function(data) {
// load vocabulary related to term
data.express.param('vocabularyId', plugin.paramVocabularyIdMD);
});
// use before instance to set sequelize virtual fields for term fields
plugin.hooks.on('we:models:before:instance', function (we, done) {
let f, cfgs;
const models = we.db.modelsConfigs;
for (let modelName in models) {
if (models[modelName].options && models[modelName].options.termFields) {
for (f in models[modelName].options.termFields) {
// set the default vocabularyName
if (!models[modelName].options.termFields[f].vocabularyName)
models[modelName].options.termFields[f].vocabularyName = 'Tags';
// set field configs
cfgs = we.utils._.clone(models[modelName].options.termFields[f]);
cfgs.type = we.db.Sequelize.VIRTUAL;
// set virtual setter
cfgs.set = we.term.getSetTermTag(f, cfgs.onlyLowercase);
// set form field html
cfgs.formFieldType = cfgs.formFieldType || (cfgs.canCreate? 'vocabulary/tag' : 'vocabulary/category');
// set virtual fields for term fields if now exists
if (!models[modelName].definition[f]) {
models[modelName].definition[f] = cfgs;
} else {
we.log.verbose('Cant set virtual field for field tag:', f);
}
}
}
}
done();
});
// after define all models add term field hooks in models how have terms
plugin.hooks.on('we:models:set:joins', function (we, done) {
const models = we.db.models;
for (let modelName in models) {
if (!we.db.modelsConfigs[modelName]) continue;
const termFields = we.term.getModelTermFields(we.db.modelsConfigs[modelName]);
if ( we.utils._.isEmpty(termFields) ) continue;
models[modelName]
.addHook('afterFind', 'loadTerms', (r, opts)=> {
if (!r) return r;
return new Promise( (resolve, reject)=> {
const Model = we.db.models[modelName];
if ( we.utils._.isArray(r) ) {
we.utils.async.eachSeries(r, function (r1, next) {
we.term.afterFindRecord.bind(Model)(r1, opts)
.then( ()=> { next(); })
.catch(next);
}, (err)=> {
if (err) return reject(err);
resolve();
});
} else {
we.term.afterFindRecord
.bind(Model)(r, opts)
.then(resolve)
.catch(reject);
}
});
});
models[modelName].addHook('afterCreate', 'createTerms', we.term.afterCreatedRecord);
models[modelName].addHook('afterUpdate', 'updateTerms', we.term.afterUpdatedRecord);
models[modelName].addHook('afterDestroy', 'destroyTerms', we.term.afterDeleteRecord);
}
done();
});
plugin.addTermContentContext = function addTermContentContext (we) {
we.router.singleRecordActions.push('findOneTermContent');
};
plugin.setMetatagHandlers = function setMetatagHandlers(we) {
if (we.router.metatag) {
we.router.metatag.add('termFindAll', require('./lib/metatags/termFindAll.js'));
we.router.metatag.add('termFindOne', require('./lib/metatags/termFindOne.js'));
}
};
plugin.events.on('we:after:load:plugins', plugin.setMetatagHandlers);
plugin.events.on('we:after:load:plugins', require('./lib/setModelsHooks.js'));
plugin.events.on('we:after:load:plugins', plugin.addTermContentContext);
return plugin;
};