Generate the checks list in JS

This commit is contained in:
Joas Schilling 2016-07-26 17:34:05 +02:00
parent 01ec62e3d2
commit 155e4ced3f
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
3 changed files with 50 additions and 36 deletions

View File

@ -28,11 +28,27 @@
}); });
Handlebars.registerHelper('getOperators', function(classname) { Handlebars.registerHelper('getOperators', function(classname) {
return OCA.WorkflowEngine.availableChecks var check = OCA.WorkflowEngine.getCheckByClass(classname);
.getOperatorsByClassName(classname); if (!_.isUndefined(check)) {
return check['operators'];
}
return [];
}); });
OCA.WorkflowEngine = OCA.WorkflowEngine || {}; OCA.WorkflowEngine = OCA.WorkflowEngine || {
availablePlugins: [],
availableChecks: [],
getCheckByClass: function(className) {
var length = OCA.WorkflowEngine.availableChecks.length;
for (var i = 0; i < length; i++) {
if (OCA.WorkflowEngine.availableChecks[i]['class'] === className) {
return OCA.WorkflowEngine.availableChecks[i];
}
}
return undefined;
}
};
/** /**
* 888b d888 888 888 * 888b d888 888 888
@ -86,22 +102,6 @@
url: OC.generateUrl('apps/workflowengine/operations') url: OC.generateUrl('apps/workflowengine/operations')
}); });
/**
* @class OCA.WorkflowEngine.AvailableChecksCollection
*
* collection for all available checks
*/
OCA.WorkflowEngine.AvailableChecksCollection =
OC.Backbone.Collection.extend({
model: OCA.WorkflowEngine.AvailableCheck,
url: OC.generateUrl('apps/workflowengine/checks'),
getOperatorsByClassName: function(classname) {
return OCA.WorkflowEngine.availableChecks
.findWhere({'class': classname})
.get('operators');
}
});
/** /**
* 888 888 d8b * 888 888 d8b
* 888 888 Y8P * 888 888 Y8P
@ -154,7 +154,6 @@
message: '', message: '',
errorMessage: '', errorMessage: '',
saving: false, saving: false,
plugins: [],
initialize: function() { initialize: function() {
// this creates a new copy of the object to definitely have a new reference and being able to reset the model // this creates a new copy of the object to definitely have a new reference and being able to reset the model
this.originalModel = JSON.parse(JSON.stringify(this.model)); this.originalModel = JSON.parse(JSON.stringify(this.model));
@ -167,13 +166,6 @@
if (this.model.get('id') === undefined) { if (this.model.get('id') === undefined) {
this.hasChanged = true; this.hasChanged = true;
} }
this.plugins = OC.Plugins.getPlugins('OCA.WorkflowEngine.CheckPlugins');
_.each(this.plugins, function(plugin) {
if (_.isFunction(plugin.initialize)) {
plugin.initialize();
}
});
}, },
delete: function() { delete: function() {
this.model.destroy(); this.model.destroy();
@ -209,9 +201,8 @@
}, },
add: function() { add: function() {
var checks = _.clone(this.model.get('checks')), var checks = _.clone(this.model.get('checks')),
classname = OCA.WorkflowEngine.availableChecks.at(0).get('class'), classname = OCA.WorkflowEngine.availableChecks[0]['class'],
operators = OCA.WorkflowEngine.availableChecks operators = OCA.WorkflowEngine.availableChecks[0]['operators'];
.getOperatorsByClassName(classname);
checks.push({ checks.push({
'class': classname, 'class': classname,
@ -249,9 +240,10 @@
// if the class is changed most likely also the operators have changed // if the class is changed most likely also the operators have changed
// with this we set the operator to the first possible operator // with this we set the operator to the first possible operator
if (key === 'class') { if (key === 'class') {
var operators = OCA.WorkflowEngine.availableChecks var check = OCA.WorkflowEngine.getCheckByClass(value);
.getOperatorsByClassName(value); if (!_.isUndefined(check)) {
checks[id]['operator'] = operators[0]; checks[id]['operator'] = check['operators'][0];
}
} }
// model change will trigger render // model change will trigger render
this.model.set({'checks': checks}); this.model.set({'checks': checks});
@ -294,7 +286,7 @@
render: function() { render: function() {
this.$el.html(this.template({ this.$el.html(this.template({
operation: this.model.toJSON(), operation: this.model.toJSON(),
classes: OCA.WorkflowEngine.availableChecks.toJSON(), classes: OCA.WorkflowEngine.availableChecks,
hasChanged: this.hasChanged, hasChanged: this.hasChanged,
message: this.message, message: this.message,
errorMessage: this.errorMessage, errorMessage: this.errorMessage,
@ -308,7 +300,7 @@
check = checks[id], check = checks[id],
valueElement = $element.find('.check-value').first(); valueElement = $element.find('.check-value').first();
_.each(this.plugins, function(plugin) { _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
if (_.isFunction(plugin.render)) { if (_.isFunction(plugin.render)) {
plugin.render(valueElement, check['class'], check['value']); plugin.render(valueElement, check['class'], check['value']);
} }
@ -334,6 +326,8 @@
OCA.WorkflowEngine.OperationsView = OCA.WorkflowEngine.OperationsView =
OCA.WorkflowEngine.TemplateView.extend({ OCA.WorkflowEngine.TemplateView.extend({
templateId: '#operations-template', templateId: '#operations-template',
collection: null,
$el: null,
events: { events: {
'click .button-add-operation': 'add' 'click .button-add-operation': 'add'
}, },
@ -341,6 +335,16 @@
this._initialize('OCA\\WorkflowEngine\\Operation'); this._initialize('OCA\\WorkflowEngine\\Operation');
}, },
_initialize: function(classname) { _initialize: function(classname) {
OCA.WorkflowEngine.availablePlugins = OC.Plugins.getPlugins('OCA.WorkflowEngine.CheckPlugins');
_.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
if (_.isFunction(plugin.initialize)) {
plugin.initialize();
}
if (_.isFunction(plugin.getCheck)) {
OCA.WorkflowEngine.availableChecks.push(plugin.getCheck());
}
});
this.collection.fetch({data: { this.collection.fetch({data: {
'class': classname 'class': classname
}}); }});

View File

@ -24,6 +24,16 @@
OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {}; OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
OCA.WorkflowEngine.Plugins.UserGroupMembershipPlugin = { OCA.WorkflowEngine.Plugins.UserGroupMembershipPlugin = {
getCheck: function() {
return {
'class': 'OCA\\WorkflowEngine\\Check\\UserGroupMembership',
'name': t('workflowengine', 'User group membership'),
'operators': [
{'operator': 'is', 'name': t('workflowengine', 'is member of')},
{'operator': '!is', 'name': t('workflowengine', 'is not member of')}
]
};
},
render: function(element, classname, value) { render: function(element, classname, value) {
if (classname !== 'OCA\\WorkflowEngine\\Check\\UserGroupMembership') { if (classname !== 'OCA\\WorkflowEngine\\Check\\UserGroupMembership') {
return; return;

View File

@ -48,7 +48,7 @@
</select> </select>
<select class="check-operator"> <select class="check-operator">
{{#each (getOperators class)}} {{#each (getOperators class)}}
<option value="{{this}}" {{selectItem this ../operator}}>{{this}}</option> <option value="{{operator}}" {{selectItem this.operator ../operator}}>{{name}}</option>
{{/each}} {{/each}}
</select> </select>
<input type="text" class="check-value" value="{{value}}"> <input type="text" class="check-value" value="{{value}}">