mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-04-28 18:44:51 +00:00
246 lines
8.5 KiB
JavaScript
246 lines
8.5 KiB
JavaScript
// Generated by CoffeeScript 1.6.3
|
|
(function() {
|
|
var fOnLoad;
|
|
|
|
fOnLoad = function() {
|
|
var arrActionInvoker, fFetchActionParams, fFetchEventParams, obj;
|
|
document.title = 'Rule Forge!';
|
|
$('#pagetitle').text('{{{user.username}}}, forge your custom rule!');
|
|
fFetchEventParams = function(name) {
|
|
var arr, obj;
|
|
arr = name.split(' -> ');
|
|
obj = {
|
|
command: 'get_event_poller_params',
|
|
id: arr[0]
|
|
};
|
|
return $.post('/usercommand', obj).done(function(data) {
|
|
var arrParams, fAppendParam, table, _i, _len, _results;
|
|
if (data.message) {
|
|
arrParams = JSON.parse(data.message);
|
|
$('#event_poller_params table').remove();
|
|
if (arrParams.length > 0) {
|
|
$('#event_poller_params').text('Required user-specific params:');
|
|
table = $('<table>');
|
|
$('#event_poller_params').append(table);
|
|
fAppendParam = function(name) {
|
|
var inp, tr;
|
|
tr = $('<tr>');
|
|
tr.append($('<td>').css('width', '20px'));
|
|
tr.append($('<td>').text(name));
|
|
inp = $('<input>').attr('type', 'password').attr('id', "" + name);
|
|
tr.append($('<td>').text(' :').append(inp));
|
|
return table.append(tr);
|
|
};
|
|
_results = [];
|
|
for (_i = 0, _len = arrParams.length; _i < _len; _i++) {
|
|
name = arrParams[_i];
|
|
_results.push(fAppendParam(name));
|
|
}
|
|
return _results;
|
|
}
|
|
}
|
|
}).fail(function(err) {
|
|
console.log(err);
|
|
$('#info').text('Error fetching event poller params');
|
|
return $('#info').attr('class', 'error');
|
|
});
|
|
};
|
|
obj = {
|
|
command: 'get_event_pollers'
|
|
};
|
|
$.post('/usercommand', obj).done(function(data) {
|
|
var fAppendEvent, id, name, _ref;
|
|
fAppendEvent = function(id, name) {
|
|
return $('#select_event').append($('<option>').text(id + ' -> ' + name));
|
|
};
|
|
_ref = data.message;
|
|
for (id in _ref) {
|
|
name = _ref[id];
|
|
fAppendEvent(id, name);
|
|
}
|
|
return fFetchEventParams($('#select_event option:selected').text());
|
|
}).fail(function(err) {
|
|
console.log(err);
|
|
$('#info').text('Error fetching event poller');
|
|
return $('#info').attr('class', 'error');
|
|
});
|
|
$('#select_event').change(function() {
|
|
return fFetchEventParams($(this).val());
|
|
});
|
|
arrActionInvoker = [];
|
|
obj = {
|
|
command: 'get_action_invokers'
|
|
};
|
|
$.post('/usercommand', obj).done(function(data) {
|
|
var fAppendAction, i, id, name, _ref, _results;
|
|
i = 0;
|
|
fAppendAction = function(id, name) {
|
|
$('#select_actions').append($('<option>').attr('id', i++).text(id + ' -> ' + name));
|
|
return arrActionInvoker.push(id + ' -> ' + name);
|
|
};
|
|
_ref = data.message;
|
|
_results = [];
|
|
for (id in _ref) {
|
|
name = _ref[id];
|
|
_results.push(fAppendAction(id, name));
|
|
}
|
|
return _results;
|
|
}).fail(function(err) {
|
|
console.log(err);
|
|
$('#info').text('Error fetching event poller');
|
|
return $('#info').attr('class', 'error');
|
|
});
|
|
fFetchActionParams = function(div, name) {
|
|
obj = {
|
|
command: 'get_action_invoker_params',
|
|
id: name
|
|
};
|
|
return $.post('/usercommand', obj).done(function(data) {
|
|
var arrParams, fAppendActionParam, table, _i, _len, _results;
|
|
if (data.message) {
|
|
arrParams = JSON.parse(data.message);
|
|
if (arrParams.length > 0) {
|
|
table = $('<table>');
|
|
div.append(table);
|
|
fAppendActionParam = function(name) {
|
|
var tr;
|
|
tr = $('<tr>');
|
|
tr.append($('<td>').css('width', '20px'));
|
|
tr.append($('<td>').attr('class', 'key').text(name));
|
|
tr.append($('<td>').text(' :').append($('<input>').attr('type', 'password')));
|
|
return table.append(tr);
|
|
};
|
|
_results = [];
|
|
for (_i = 0, _len = arrParams.length; _i < _len; _i++) {
|
|
name = arrParams[_i];
|
|
_results.push(fAppendActionParam(name));
|
|
}
|
|
return _results;
|
|
}
|
|
}
|
|
}).fail(function(err) {
|
|
console.log(err);
|
|
$('#info').text('Error fetching action invoker params');
|
|
return $('#info').attr('class', 'error');
|
|
});
|
|
};
|
|
$('#select_actions').on('change', function() {
|
|
var arrAI, div, img, opt, table, tr;
|
|
opt = $('option:selected', this);
|
|
arrAI = opt.val().split(' -> ');
|
|
table = $('#selected_actions');
|
|
tr = $('<tr>').attr('id', 'title_' + opt.attr('id'));
|
|
img = $('<img>').attr('src', 'red_cross_small.png');
|
|
tr.append($('<td>').css('width', '20px').append(img));
|
|
tr.append($('<td>').attr('class', 'title').text(opt.val()));
|
|
table.append(tr);
|
|
if ($('#ap_' + arrAI[0]).length === 0) {
|
|
div = $('<div>').attr('id', 'ap_' + arrAI[0]).html("<i>" + arrAI[0] + "</i>");
|
|
$('#action_params').append(div);
|
|
fFetchActionParams(div, arrAI[0]);
|
|
}
|
|
return opt.remove();
|
|
});
|
|
$('#selected_actions').on('click', 'img', function() {
|
|
var arrName, id, isSelected, name, opt;
|
|
id = $(this).closest('tr').attr('id').substring(6);
|
|
name = arrActionInvoker[id];
|
|
arrName = name.split(' -> ');
|
|
$('#title_' + id).remove();
|
|
$('#params_' + id).remove();
|
|
opt = $('<option>').attr('id', id).text(name);
|
|
$('#select_actions').append(opt);
|
|
isSelected = false;
|
|
$('#selected_actions td').each(function() {
|
|
if ($(this).text().indexOf(arrName[0]) > -1) {
|
|
return isSelected = true;
|
|
}
|
|
});
|
|
if (!isSelected) {
|
|
return $('#ap_' + arrName[0]).remove();
|
|
}
|
|
});
|
|
return $('#but_submit').click(function() {
|
|
var acts, ap, arrEP, ep, err;
|
|
try {
|
|
if ($('#select_event option:selected').length === 0) {
|
|
throw new Error('Please create an Event Poller first!');
|
|
}
|
|
arrEP = $('#select_event option:selected').val().split(' -> ');
|
|
if ($('#input_id').val() === '') {
|
|
throw new Error('Please enter a rule name!');
|
|
}
|
|
ep = {};
|
|
$("#event_poller_params tr").each(function() {
|
|
var name, val;
|
|
val = $('input', this).val();
|
|
name = $('td:nth-child(2)', this).text();
|
|
if (val === '') {
|
|
throw new Error("Please enter a value for '" + name + "' in the event module!");
|
|
}
|
|
return ep[name] = val;
|
|
});
|
|
if ($('#selected_actions tr').length === 0) {
|
|
throw new Error('Please select at least one action or create one!');
|
|
}
|
|
ap = {};
|
|
$('#action_params div').each(function() {
|
|
var id, params;
|
|
id = $(this).attr('id').substring(3);
|
|
params = {};
|
|
$('tr', this).each(function() {
|
|
var key, val;
|
|
key = $('.key', this).text();
|
|
val = $('input', this).val();
|
|
if (val === '') {
|
|
throw new Error("'" + key + "' missing for '" + id + "'");
|
|
}
|
|
return params[key] = val;
|
|
});
|
|
return ap[id] = params;
|
|
});
|
|
acts = [];
|
|
$('#selected_actions .title').each(function() {
|
|
return acts.push($(this).text());
|
|
});
|
|
obj = {
|
|
command: 'forge_rule',
|
|
id: $('#input_id').val(),
|
|
event: JSON.stringify({
|
|
module: arrEP[0],
|
|
"function": arrEP[1]
|
|
}),
|
|
event_params: JSON.stringify(ep),
|
|
conditions: JSON.stringify({}),
|
|
actions: JSON.stringify(acts),
|
|
action_params: JSON.stringify(ap)
|
|
};
|
|
return $.post('/usercommand', obj).done(function(data) {
|
|
$('#info').text(data.message);
|
|
return $('#info').attr('class', 'success');
|
|
}).fail(function(err) {
|
|
var msg, oErr;
|
|
if (err.responseText === '') {
|
|
msg = 'No Response from Server!';
|
|
} else {
|
|
try {
|
|
oErr = JSON.parse(err.responseText);
|
|
msg = oErr.message;
|
|
} catch (_error) {}
|
|
}
|
|
$('#info').text('Error in upload: ' + msg);
|
|
$('#info').attr('class', 'error');
|
|
if (err.status === 401) {
|
|
return window.location.href = 'forge?page=forge_rule';
|
|
}
|
|
});
|
|
} catch (_error) {
|
|
err = _error;
|
|
return alert(err.message);
|
|
}
|
|
});
|
|
};
|
|
|
|
window.addEventListener('load', fOnLoad, true);
|
|
|
|
}).call(this);
|