Workflow engine to ocs api

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2018-04-25 07:47:16 +02:00
parent d53193fd8e
commit 8c845be11c
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
1 changed files with 341 additions and 337 deletions

View File

@ -19,372 +19,376 @@
*/ */
(function() { (function() {
Handlebars.registerHelper('selectItem', function(currentValue, itemValue) { Handlebars.registerHelper('selectItem', function(currentValue, itemValue) {
if(currentValue === itemValue) { if (currentValue === itemValue) {
return 'selected="selected"'; return 'selected="selected"';
} }
return ""; return "";
}); });
Handlebars.registerHelper('getOperators', function(classname) { Handlebars.registerHelper('getOperators', function(classname) {
var check = OCA.WorkflowEngine.getCheckByClass(classname); var check = OCA.WorkflowEngine.getCheckByClass(classname);
if (!_.isUndefined(check)) { if (!_.isUndefined(check)) {
return check['operators']; return check['operators'];
} }
return []; return [];
}); });
OCA.WorkflowEngine = _.extend(OCA.WorkflowEngine || {}, { OCA.WorkflowEngine = _.extend(OCA.WorkflowEngine || {}, {
availablePlugins: [], availablePlugins: [],
availableChecks: [], availableChecks: [],
getCheckByClass: function(className) { getCheckByClass: function(className) {
var length = OCA.WorkflowEngine.availableChecks.length; var length = OCA.WorkflowEngine.availableChecks.length;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (OCA.WorkflowEngine.availableChecks[i]['class'] === className) { if (OCA.WorkflowEngine.availableChecks[i]['class'] === className) {
return OCA.WorkflowEngine.availableChecks[i]; return OCA.WorkflowEngine.availableChecks[i];
} }
} }
return undefined; return undefined;
} }
}); });
/** /**
* 888b d888 888 888 * 888b d888 888 888
* 8888b d8888 888 888 * 8888b d8888 888 888
* 88888b.d88888 888 888 * 88888b.d88888 888 888
* 888Y88888P888 .d88b. .d88888 .d88b. 888 .d8888b * 888Y88888P888 .d88b. .d88888 .d88b. 888 .d8888b
* 888 Y888P 888 d88""88b d88" 888 d8P Y8b 888 88K * 888 Y888P 888 d88""88b d88" 888 d8P Y8b 888 88K
* 888 Y8P 888 888 888 888 888 88888888 888 "Y8888b. * 888 Y8P 888 888 888 888 888 88888888 888 "Y8888b.
* 888 " 888 Y88..88P Y88b 888 Y8b. 888 X88 * 888 " 888 Y88..88P Y88b 888 Y8b. 888 X88
* 888 888 "Y88P" "Y88888 "Y8888 888 88888P' * 888 888 "Y88P" "Y88888 "Y8888 888 88888P'
*/ */
/** /**
* @class OCA.WorkflowEngine.Operation * @class OCA.WorkflowEngine.Operation
*/ */
OCA.WorkflowEngine.Operation = OCA.WorkflowEngine.Operation =
OC.Backbone.Model.extend({ OC.Backbone.Model.extend({
defaults: { defaults: {
'class': 'OCA\\WorkflowEngine\\Operation', 'class': 'OCA\\WorkflowEngine\\Operation',
'name': '', 'name': '',
'checks': [], 'checks': [],
'operation': '' 'operation': ''
} }
}); });
/** /**
* .d8888b. 888 888 888 d8b * .d8888b. 888 888 888 d8b
* d88P Y88b 888 888 888 Y8P * d88P Y88b 888 888 888 Y8P
* 888 888 888 888 888 * 888 888 888 888 888
* 888 .d88b. 888 888 .d88b. .d8888b 888888 888 .d88b. 88888b. .d8888b * 888 .d88b. 888 888 .d88b. .d8888b 888888 888 .d88b. 88888b. .d8888b
* 888 d88""88b 888 888 d8P Y8b d88P" 888 888 d88""88b 888 "88b 88K * 888 d88""88b 888 888 d8P Y8b d88P" 888 888 d88""88b 888 "88b 88K
* 888 888 888 888 888 888 88888888 888 888 888 888 888 888 888 "Y8888b. * 888 888 888 888 888 888 88888888 888 888 888 888 888 888 888 "Y8888b.
* Y88b d88P Y88..88P 888 888 Y8b. Y88b. Y88b. 888 Y88..88P 888 888 X88 * Y88b d88P Y88..88P 888 888 Y8b. Y88b. Y88b. 888 Y88..88P 888 888 X88
* "Y8888P" "Y88P" 888 888 "Y8888 "Y8888P "Y888 888 "Y88P" 888 888 88888P' * "Y8888P" "Y88P" 888 888 "Y8888 "Y8888P "Y888 888 "Y88P" 888 888 88888P'
*/ */
/** /**
* @class OCA.WorkflowEngine.OperationsCollection * @class OCA.WorkflowEngine.OperationsCollection
* *
* collection for all configurated operations * collection for all configurated operations
*/ */
OCA.WorkflowEngine.OperationsCollection = OCA.WorkflowEngine.OperationsCollection =
OC.Backbone.Collection.extend({ OC.Backbone.Collection.extend({
model: OCA.WorkflowEngine.Operation, model: OCA.WorkflowEngine.Operation,
url: OC.generateUrl('apps/workflowengine/operations') url: OC.generateUrl('apps/workflowengine/operations')
}); });
/** /**
* 888 888 d8b * 888 888 d8b
* 888 888 Y8P * 888 888 Y8P
* 888 888 * 888 888
* Y88b d88P 888 .d88b. 888 888 888 .d8888b * Y88b d88P 888 .d88b. 888 888 888 .d8888b
* Y88b d88P 888 d8P Y8b 888 888 888 88K * Y88b d88P 888 d8P Y8b 888 888 888 88K
* Y88o88P 888 88888888 888 888 888 "Y8888b. * Y88o88P 888 88888888 888 888 888 "Y8888b.
* Y888P 888 Y8b. Y88b 888 d88P X88 * Y888P 888 Y8b. Y88b 888 d88P X88
* Y8P 888 "Y8888 "Y8888888P" 88888P' * Y8P 888 "Y8888 "Y8888888P" 88888P'
*/ */
/** /**
* @class OCA.WorkflowEngine.TemplateView * @class OCA.WorkflowEngine.TemplateView
* *
* a generic template that handles the Handlebars template compile step * a generic template that handles the Handlebars template compile step
* in a method called "template()" * in a method called "template()"
*/ */
OCA.WorkflowEngine.TemplateView = OCA.WorkflowEngine.TemplateView =
OC.Backbone.View.extend({ OC.Backbone.View.extend({
_template: null, _template: null,
template: function(vars) { template: function(vars) {
if (!this._template) { if (!this._template) {
this._template = Handlebars.compile($(this.templateId).html()); this._template = Handlebars.compile($(this.templateId).html());
} }
return this._template(vars); return this._template(vars);
} }
}); });
/** /**
* @class OCA.WorkflowEngine.OperationView * @class OCA.WorkflowEngine.OperationView
* *
* this creates the view for a single operation * this creates the view for a single operation
*/ */
OCA.WorkflowEngine.OperationView = OCA.WorkflowEngine.OperationView =
OCA.WorkflowEngine.TemplateView.extend({ OCA.WorkflowEngine.TemplateView.extend({
templateId: '#operation-template', templateId: '#operation-template',
events: { events: {
'change .check-class': 'checkChanged', 'change .check-class': 'checkChanged',
'change .check-operator': 'checkChanged', 'change .check-operator': 'checkChanged',
'change .check-value': 'checkChanged', 'change .check-value': 'checkChanged',
'change .operation-name': 'operationChanged', 'change .operation-name': 'operationChanged',
'change .operation-operation': 'operationChanged', 'change .operation-operation': 'operationChanged',
'click .button-reset': 'reset', 'click .button-reset': 'reset',
'click .button-save': 'save', 'click .button-save': 'save',
'click .button-add': 'add', 'click .button-add': 'add',
'click .button-delete': 'delete', 'click .button-delete': 'delete',
'click .button-delete-check': 'deleteCheck' 'click .button-delete-check': 'deleteCheck'
}, },
originalModel: null, originalModel: null,
hasChanged: false, hasChanged: false,
message: '', message: '',
errorMessage: '', errorMessage: '',
saving: false, saving: false,
groups: [], groups: [],
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));
this.model.on('change', function(){ this.model.on('change', function() {
console.log('model changed'); console.log('model changed');
this.hasChanged = true; this.hasChanged = true;
this.render(); this.render();
}, this); }, this);
if (this.model.get('id') === undefined) { if (this.model.get('id') === undefined) {
this.hasChanged = true; this.hasChanged = true;
} }
var self = this; var self = this;
$.ajax({ $.ajax({
url: OC.generateUrl('settings/users/groups'), url: OC.linkToOCS('cloud/groups', 2) + 'details',
dataType: 'json', dataType: 'json',
quietMillis: 100, quietMillis: 100,
}).success(function(response) { }).success(function(response) {
// add admin groups if (data.ocs.data.groups && data.ocs.data.groups.length > 0) {
$.each(response.data.adminGroups, function(id, group) {
self.groups.push({ id: group.id, displayname: group.name });
});
// add groups
$.each(response.data.groups, function(id, group) {
self.groups.push({ id: group.id, displayname: group.name });
});
self.render();
}).error(function(data) {
OC.Notification.error(t('workflowengine', 'Unable to retrieve the group list'), {type: 'error'});
console.log(data);
});
},
delete: function() {
if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.delete, this));
return;
}
this.model.destroy(); data.ocs.data.groups.forEach(function(group) {
this.remove(); self.groups.push({ id: group.id, displayname: group.displayname });
}, })
reset: function() { self.render();
this.hasChanged = false;
// silent is need to not trigger the change event which resets the hasChanged attribute
this.model.set(this.originalModel, {silent: true});
this.render();
},
save: function() {
if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.save, this));
return;
}
var success = function(model, response, options) { } else {
this.saving = false; OC.Notification.error(t('workflowengine', 'Group list is empty'), { type: 'error' });
this.originalModel = JSON.parse(JSON.stringify(this.model)); console.log(data);
}
}).error(function(data) {
OC.Notification.error(t('workflowengine', 'Unable to retrieve the group list'), { type: 'error' });
console.log(data);
});
},
delete: function() {
if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.delete, this));
return;
}
this.message = t('workflowengine', 'Saved'); this.model.destroy();
this.errorMessage = ''; this.remove();
this.render(); },
}; reset: function() {
var error = function(model, response, options) { this.hasChanged = false;
this.saving = false; // silent is need to not trigger the change event which resets the hasChanged attribute
this.hasChanged = true; this.model.set(this.originalModel, { silent: true });
this.render();
},
save: function() {
if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.save, this));
return;
}
this.message = t('workflowengine', 'Saving failed:'); var success = function(model, response, options) {
this.errorMessage = response.responseText; this.saving = false;
this.render(); this.originalModel = JSON.parse(JSON.stringify(this.model));
};
this.hasChanged = false;
this.saving = true;
this.render();
this.model.save(null, {success: success, error: error, context: this});
},
add: function() {
var checks = _.clone(this.model.get('checks')),
classname = OCA.WorkflowEngine.availableChecks[0]['class'],
operators = OCA.WorkflowEngine.availableChecks[0]['operators'];
checks.push({ this.message = t('workflowengine', 'Saved');
'class': classname, this.errorMessage = '';
'operator': operators[0]['operator'], this.render();
'value': '' };
}); var error = function(model, response, options) {
this.model.set({'checks': checks}); this.saving = false;
}, this.hasChanged = true;
checkChanged: function(event) {
var value = event.target.value,
id = $(event.target.parentElement).data('id'),
// this creates a new copy of the object to definitely have a new reference
checks = JSON.parse(JSON.stringify(this.model.get('checks'))),
key = null;
for (var i = 0; i < event.target.classList.length; i++) { this.message = t('workflowengine', 'Saving failed:');
var className = event.target.classList[i]; this.errorMessage = response.responseText;
if (className.substr(0, 'check-'.length) === 'check-') { this.render();
key = className.substr('check-'.length); };
break; this.hasChanged = false;
} this.saving = true;
} this.render();
this.model.save(null, { success: success, error: error, context: this });
},
add: function() {
var checks = _.clone(this.model.get('checks')),
classname = OCA.WorkflowEngine.availableChecks[0]['class'],
operators = OCA.WorkflowEngine.availableChecks[0]['operators'];
if (key === null) { checks.push({
console.warn('checkChanged triggered but element doesn\'t have any "check-" class'); 'class': classname,
return; 'operator': operators[0]['operator'],
} 'value': ''
});
this.model.set({ 'checks': checks });
},
checkChanged: function(event) {
var value = event.target.value,
id = $(event.target.parentElement).data('id'),
// this creates a new copy of the object to definitely have a new reference
checks = JSON.parse(JSON.stringify(this.model.get('checks'))),
key = null;
if (!_.has(checks[id], key)) { for (var i = 0; i < event.target.classList.length; i++) {
console.warn('key "' + key + '" is not available in check', check); var className = event.target.classList[i];
return; if (className.substr(0, 'check-'.length) === 'check-') {
} key = className.substr('check-'.length);
break;
}
}
checks[id][key] = value; if (key === null) {
// if the class is changed most likely also the operators have changed console.warn('checkChanged triggered but element doesn\'t have any "check-" class');
// with this we set the operator to the first possible operator return;
if (key === 'class') { }
var check = OCA.WorkflowEngine.getCheckByClass(value);
if (!_.isUndefined(check)) {
checks[id]['operator'] = check['operators'][0]['operator'];
}
}
// model change will trigger render
this.model.set({'checks': checks});
},
deleteCheck: function(event) {
console.log(arguments);
var id = $(event.target.parentElement).data('id'),
checks = JSON.parse(JSON.stringify(this.model.get('checks')));
// splice removes 1 element at index `id` if (!_.has(checks[id], key)) {
checks.splice(id, 1); console.warn('key "' + key + '" is not available in check', check);
// model change will trigger render return;
this.model.set({'checks': checks}); }
},
operationChanged: function(event) {
var value = event.target.value,
key = null;
for (var i = 0; i < event.target.classList.length; i++) { checks[id][key] = value;
var className = event.target.classList[i]; // if the class is changed most likely also the operators have changed
if (className.substr(0, 'operation-'.length) === 'operation-') { // with this we set the operator to the first possible operator
key = className.substr('operation-'.length); if (key === 'class') {
break; var check = OCA.WorkflowEngine.getCheckByClass(value);
} if (!_.isUndefined(check)) {
} checks[id]['operator'] = check['operators'][0]['operator'];
}
}
// model change will trigger render
this.model.set({ 'checks': checks });
},
deleteCheck: function(event) {
console.log(arguments);
var id = $(event.target.parentElement).data('id'),
checks = JSON.parse(JSON.stringify(this.model.get('checks')));
if (key === null) { // splice removes 1 element at index `id`
console.warn('operationChanged triggered but element doesn\'t have any "operation-" class'); checks.splice(id, 1);
return; // model change will trigger render
} this.model.set({ 'checks': checks });
},
operationChanged: function(event) {
var value = event.target.value,
key = null;
if (key !== 'name' && key !== 'operation') { for (var i = 0; i < event.target.classList.length; i++) {
console.warn('key "' + key + '" is no valid attribute'); var className = event.target.classList[i];
return; if (className.substr(0, 'operation-'.length) === 'operation-') {
} key = className.substr('operation-'.length);
break;
}
}
// model change will trigger render if (key === null) {
this.model.set(key, value); console.warn('operationChanged triggered but element doesn\'t have any "operation-" class');
}, return;
render: function() { }
this.$el.html(this.template({
operation: this.model.toJSON(),
classes: OCA.WorkflowEngine.availableChecks,
hasChanged: this.hasChanged,
message: this.message,
errorMessage: this.errorMessage,
saving: this.saving
}));
var checks = this.model.get('checks'); if (key !== 'name' && key !== 'operation') {
_.each(this.$el.find('.check'), function(element){ console.warn('key "' + key + '" is no valid attribute');
var $element = $(element), return;
id = $element.data('id'), }
check = checks[id],
valueElement = $element.find('.check-value').first();
var self = this;
_.each(OCA.WorkflowEngine.availablePlugins, function(plugin) { // model change will trigger render
if (_.isFunction(plugin.render)) { this.model.set(key, value);
plugin.render(valueElement, check, self.groups); },
} render: function() {
}); this.$el.html(this.template({
}, this); operation: this.model.toJSON(),
classes: OCA.WorkflowEngine.availableChecks,
hasChanged: this.hasChanged,
message: this.message,
errorMessage: this.errorMessage,
saving: this.saving
}));
if (this.message !== '') { var checks = this.model.get('checks');
// hide success messages after some time _.each(this.$el.find('.check'), function(element) {
_.delay(function(elements){ var $element = $(element),
$(elements).css('opacity', 0); id = $element.data('id'),
}, 7000, this.$el.find('.msg.success')); check = checks[id],
this.message = ''; valueElement = $element.find('.check-value').first();
} var self = this;
return this.$el; _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
} if (_.isFunction(plugin.render)) {
}); plugin.render(valueElement, check, self.groups);
}
});
}, this);
/** if (this.message !== '') {
* @class OCA.WorkflowEngine.OperationsView // hide success messages after some time
* _.delay(function(elements) {
* this creates the view for configured operations $(elements).css('opacity', 0);
*/ }, 7000, this.$el.find('.msg.success'));
OCA.WorkflowEngine.OperationsView = this.message = '';
OCA.WorkflowEngine.TemplateView.extend({ }
templateId: '#operations-template',
collection: null,
$el: null,
events: {
'click .button-add-operation': 'add'
},
initialize: function(classname) {
if (!OCA.WorkflowEngine.availablePlugins.length) {
OCA.WorkflowEngine.availablePlugins = OC.Plugins.getPlugins('OCA.WorkflowEngine.CheckPlugins');
_.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
if (_.isFunction(plugin.getCheck)) {
OCA.WorkflowEngine.availableChecks.push(plugin.getCheck(classname));
}
});
}
this.collection.fetch({data: { return this.$el;
'class': classname }
}}); });
this.collection.once('sync', this.render, this);
}, /**
add: function() { * @class OCA.WorkflowEngine.OperationsView
var operation = this.collection.create(); *
this.renderOperation(operation); * this creates the view for configured operations
}, */
renderOperation: function(subView){ OCA.WorkflowEngine.OperationsView =
var operationsElement = this.$el.find('.operations'); OCA.WorkflowEngine.TemplateView.extend({
operationsElement.append(subView.$el); templateId: '#operations-template',
subView.render(); collection: null,
}, $el: null,
render: function() { events: {
this.$el.html(this.template()); 'click .button-add-operation': 'add'
this.collection.each(this.renderOperation, this); },
} initialize: function(classname) {
}); if (!OCA.WorkflowEngine.availablePlugins.length) {
})(); OCA.WorkflowEngine.availablePlugins = OC.Plugins.getPlugins('OCA.WorkflowEngine.CheckPlugins');
_.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
if (_.isFunction(plugin.getCheck)) {
OCA.WorkflowEngine.availableChecks.push(plugin.getCheck(classname));
}
});
}
this.collection.fetch({
data: {
'class': classname
}
});
this.collection.once('sync', this.render, this);
},
add: function() {
var operation = this.collection.create();
this.renderOperation(operation);
},
renderOperation: function(subView) {
var operationsElement = this.$el.find('.operations');
operationsElement.append(subView.$el);
subView.render();
},
render: function() {
this.$el.html(this.template());
this.collection.each(this.renderOperation, this);
}
});
})();