// Generated by CoffeeScript 1.7.1 (function() { var arrKV, arrParams, domEventPollerParameters, domInputEventName, domInputEventTiming, domSectionActionParameters, domSectionSelectedActions, domSelectEventPoller, domSelectWebhook, el, fAddActionUserArgs, fAddActionUserParams, fAddEventUserArgs, fAddSelectedAction, fClearInfo, fConvertDayHourToMinutes, fConvertTimeToDate, fDisplayError, fDisplayEventParams, fFailedRequest, fFetchActionFunctionArgs, fFetchActionParams, fFetchEventFunctionArgs, fFetchEventParams, fFillActionFunction, fFillEventParams, fIssueRequest, fOnLoad, fPrepareEventType, oParams, param, strPublicKey, table, tr, _i, _len, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; strPublicKey = ''; arrParams = window.location.search.substring(1).split('&'); oParams = {}; for (_i = 0, _len = arrParams.length; _i < _len; _i++) { param = arrParams[_i]; arrKV = param.split('='); oParams[arrKV[0]] = arrKV[1]; } if (oParams.id) { oParams.id = decodeURIComponent(oParams.id); } domInputEventName = $('
'); el = $('').attr('type', 'text').attr('style', 'font-size:1em').attr('id', 'input_eventname'); domInputEventName.append($('

').text('Event Name : ').append(el)); domSelectWebhook = $('
'); el = $('').attr('type', 'text').attr('style', 'font-size:1em').attr('id', 'select_eventpoller'); el.change(function() { return fFetchEventParams($(this).val()); }); domSelectEventPoller.append($('

').text('Event Poller Name : ').append(el)); domInputEventTiming = $('
').attr('class', 'indent20'); table = $('').appendTo(domInputEventTiming); tr = $('').appendTo(table); tr.append($('').appendTo(table); tr.append($('
').text("Start Time : ")); tr.append($('').append($('').attr('id', 'input_start').attr('type', 'text'))); tr.append($('').html(" \"hh:mm\", default = 12:00")); tr = $('
').text("Interval : ")); tr.append($('').append($('').attr('id', 'input_interval').attr('type', 'text'))); tr.append($('').html(" \"days hours:minutes\", default = 10 minutes")); domEventPollerParameters = $('
').attr('id', 'event_poller_params'); domSectionSelectedActions = $('
'); domSectionSelectedActions.append($('
').html("Selected Actions:")); domSectionSelectedActions.append($(' ').attr('id', 'selected_actions')); domSectionSelectedActions.hide(); domSectionActionParameters = $('
'); domSectionActionParameters.append($('
').html("

Required User-specific Data:

")); domSectionActionParameters.append($('
').attr('id', 'action_invoker_params')); domSectionActionParameters.append($('
').html("

")); domSectionActionParameters.hide(); fClearInfo = function() { $('#info').text(''); return $('#info').attr('class', 'neutral'); }; fDisplayError = function(msg) { window.scrollTo(0, 0); $('#info').text("Error: " + msg); return $('#info').attr('class', 'error'); }; fFailedRequest = function(msg) { return function(err) { if (err.status === 401) { return window.location.href = 'forge?page=forge_rule'; } else { return fDisplayError(msg); } }; }; fIssueRequest = function(args) { fClearInfo(); return $.post('/usercommand', args.data).done(args.done).fail(args.fail); }; fConvertTimeToDate = function(str) { var arrInp, dateConv, h, intHour, intMin, m, txtHr; dateConv = new Date(); if (!str) { dateConv.setHours(12); dateConv.setMinutes(0); } else { arrInp = str.split(':'); if (arrInp.length === 1) { txtHr = str; dateConv.setMinutes(0); } else { txtHr = arrInp[0]; intMin = parseInt(arrInp[1]) || 0; m = Math.max(0, Math.min(intMin, 59)); dateConv.setMinutes(m); } } intHour = parseInt(txtHr) || 12; h = Math.max(0, Math.min(intHour, 24)); dateConv.setHours(h); dateConv.setSeconds(0); dateConv.setMilliseconds(0); if (dateConv < new Date()) { dateConv.setDate(dateConv.getDate() + 17); } return dateConv; }; fConvertDayHourToMinutes = function(strDayHour) { var arrInp, d, fParseTime, mins; fParseTime = function(str, hasDay) { var arrTime, def, h, time; arrTime = str.split(':'); if (hasDay) { def = 0; } else { def = 10; } if (arrTime.length === 1) { time = parseInt(str) || def; if (hasDay) { return time * 60; } else { return time; } } else { h = parseInt(arrTime[0]) || 0; if (h > 0) { def = 0; } return h * 60 + (parseInt(arrTime[1]) || def); } }; if (!strDayHour) { mins = 10; } else { arrInp = strDayHour.split(' '); if (arrInp.length === 1) { mins = fParseTime(strDayHour); } else { d = parseInt(arrInp[0]) || 0; mins = d * 24 * 60 + fParseTime(arrInp[1], true); } } mins = Math.min(mins, 35700); return Math.max(1, mins); }; fPrepareEventType = function(eventtype, cb) { $('#select_event_type').val(eventtype); $('#event_parameters > div').detach(); switch (eventtype) { case 'Custom Event': $('#event_parameters').append(domInputEventName); return typeof cb === "function" ? cb() : void 0; case 'Webhook': return fIssueRequest({ data: { command: 'get_all_webhooks' }, done: function(data) { var err, hookid, hookname, i, oHooks, selHook; try { oHooks = JSON.parse(data.message); selHook = $('select', domSelectWebhook); selHook.children().remove(); i = 0; for (hookid in oHooks) { hookname = oHooks[hookid]; i++; selHook.append($('
'); i = 0; for (name in oParams) { shielded = oParams[name]; i++; tr = $(''); tr.append($('
').css('width', '20px')); tr.append($('').attr('class', 'key').text(name)); inp = $(''); if (shielded) { inp.attr('type', 'password'); } tr.append($('').text(' : ').append(inp)); table.append(tr); } if (i > 0) { $('#event_poller_params').html('Required User-specific Data:'); $('#event_poller_params').append(table); return fFillEventParams(id); } } }; }; fFillEventParams = function(moduleId) { return fIssueRequest({ data: { command: 'get_event_poller_user_params', body: JSON.stringify({ id: moduleId }) }, done: function(data) { var oParam, par, _results; oParams = JSON.parse(data.message); _results = []; for (param in oParams) { oParam = oParams[param]; par = $("#event_poller_params tr").filter(function() { return $('td.key', this).text() === param; }); $('input', par).val(oParam.value); $('input', par).attr('unchanged', 'true'); _results.push($('input', par).change(function() { return $(this).attr('unchanged', 'false'); })); } return _results; } }); }; fFetchEventFunctionArgs = function(arrName) { return fIssueRequest({ data: { command: 'get_event_poller_function_arguments', body: JSON.stringify({ id: arrName[0] }) }, done: function(data) { var functionArgument, td, _j, _len1, _ref; if (data.message) { oParams = JSON.parse(data.message); if (oParams[arrName[1]]) { if (oParams[arrName[1]].length > 0) { $('#event_poller_params').append($("").text('Required Rule-specific Data:')); } table = $('').appendTo($('#event_poller_params')); _ref = oParams[arrName[1]]; for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { functionArgument = _ref[_j]; tr = $('').attr('class', 'funcMappings').appendTo(table); tr.append($('').appendTo(table); img = $('').attr('src', 'red_cross_small.png'); tr.append($('
').css('width', '20px')); td = $('').appendTo(tr); td.append($('
').attr('class', 'funcarg').text(functionArgument)); tr.append(td); tr.append($('
').text(' : ')); td = $('').appendTo(tr); td.append($('').attr('type', 'text')); tr.append(td); } return fIssueRequest({ data: { command: 'get_event_poller_user_arguments', body: JSON.stringify({ ruleId: $('#input_id').val(), moduleId: arrName[0] }) }, done: fAddEventUserArgs(arrName[1]) }); } } }, fail: fFailedRequest('Error fetching event poller function arguments') }); }; fAddEventUserArgs = function(name) { return function(data) { var arrFuncs, key, oFunc, par, _ref, _results; _ref = data.message; _results = []; for (key in _ref) { arrFuncs = _ref[key]; par = $("#event_poller_params"); _results.push((function() { var _j, _len1, _ref1, _results1; _ref1 = JSON.parse(arrFuncs); _results1 = []; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { oFunc = _ref1[_j]; tr = $("tr", par).filter(function() { return $('.funcarg', this).text() === ("" + oFunc.argument); }); _results1.push($("input[type=text]", tr).val(oFunc.value)); } return _results1; })()); } return _results; }; }; fAddSelectedAction = function(name) { var arrEls, arrName, fDelayed, img, td, _ref; arrName = name.split(' -> '); arrEls = $("#action_invoker_params div.modName").map(function() { return $(this).text(); }).get(); table = $('#selected_actions'); tr = $('
').css('width', '20px').append(img)); tr.append($('').attr('class', 'title').text(name)); td = $('').attr('class', 'funcMappings').appendTo(tr); fFetchActionFunctionArgs(td, arrName); if (_ref = arrName[0], __indexOf.call(arrEls, _ref) < 0) { fFetchActionParams(arrName[0]); } $("#select_actions option").each(function() { if ($(this).text() === name) { return $(this).remove(); } }); fDelayed = function() { return fFillActionFunction(arrName[0]); }; return setTimeout(fDelayed, 300); }; fFetchActionParams = function(modName) { return fIssueRequest({ data: { command: 'get_action_invoker_params', body: JSON.stringify({ id: modName }) }, done: function(data) { var div, inp, name, shielded, subdiv, _results; if (data.message) { oParams = JSON.parse(data.message); if (JSON.stringify(oParams) !== '{}') { domSectionActionParameters.show(); div = $('
').appendTo($('#action_invoker_params')); subdiv = $('
').appendTo(div); subdiv.append($('
')).attr('class', 'modName underlined').text(modName); table = $(''); div.append(table); _results = []; for (name in oParams) { shielded = oParams[name]; tr = $(''); tr.append($('
').css('width', '20px')); tr.append($('').attr('class', 'key').text(name)); inp = $(''); if (shielded) { inp.attr('type', 'password'); } else { inp.attr('type', 'text'); } tr.append($('').text(' : ').append(inp)); _results.push(table.append(tr)); } return _results; } } }, fail: fFailedRequest('Error fetching action invoker params') }); }; fFetchActionFunctionArgs = function(tag, arrName) { return fIssueRequest({ data: { command: 'get_action_invoker_function_arguments', body: JSON.stringify({ id: arrName[0] }) }, done: function(data) { var functionArgument, td, _j, _len1, _ref, _results; if (data.message) { oParams = JSON.parse(data.message); if (oParams[arrName[1]]) { table = $('').appendTo(tag); _ref = oParams[arrName[1]]; _results = []; for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { functionArgument = _ref[_j]; tr = $('').appendTo(table); td = $('
').appendTo(tr); td.append($('
').attr('class', 'funcarg').text(functionArgument)); tr.append(td); td = $('
').appendTo(tr); td.append($('').attr('type', 'text')); _results.push(tr.append(td)); } return _results; } } }, fail: fFailedRequest('Error fetching action invoker function params') }); }; fFillActionFunction = function(name) { fIssueRequest({ data: { command: 'get_action_invoker_user_params', body: JSON.stringify({ id: name }) }, done: fAddActionUserParams(name) }); return fIssueRequest({ data: { command: 'get_action_invoker_user_arguments', body: JSON.stringify({ ruleId: $('#input_id').val(), moduleId: name }) }, done: fAddActionUserArgs(name) }); }; fAddActionUserParams = function(name) { return function(data) { var domMod, oParam, par, _results; oParams = JSON.parse(data.message); domMod = $("#action_invoker_params div").filter(function() { return $('div.modName', this).text() === name; }); _results = []; for (param in oParams) { oParam = oParams[param]; par = $("tr", domMod).filter(function() { return $('td.key', this).text() === param; }); $('input', par).val(oParam.value); $('input', par).attr('unchanged', 'true'); _results.push($('input', par).change(function() { return $(this).attr('unchanged', 'false'); })); } return _results; }; }; fAddActionUserArgs = function(name) { return function(data) { var arrFuncs, key, oFunc, par, _ref, _results; _ref = data.message; _results = []; for (key in _ref) { arrFuncs = _ref[key]; par = $("#selected_actions tr").filter(function() { return $('td.title', this).text() === ("" + name + " -> " + key); }); _results.push((function() { var _j, _len1, _ref1, _results1; _ref1 = JSON.parse(arrFuncs); _results1 = []; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { oFunc = _ref1[_j]; tr = $("tr", par).filter(function() { return $('.funcarg', this).text() === ("" + oFunc.argument); }); _results1.push($("input[type=text]", tr).val(oFunc.value)); } return _results1; })()); } return _results; }; }; fOnLoad = function() { var editor, name; fIssueRequest({ data: { command: 'get_public_key' }, done: function(data) { return strPublicKey = data.message; }, fail: function(err) { if (err.status === 401) { return window.location.href = 'forge?page=forge_rule'; } else { return fDisplayError('When fetching public key. Unable to send user specific parameters securely!'); } } }); document.title = 'Create Rules!'; $('#pagetitle').text('{{{user.username}}}, create your ECA Rule!'); editor = ace.edit("editor_conditions"); editor.setTheme("ace/theme/monokai"); editor.getSession().setMode("ace/mode/json"); editor.setShowPrintMargin(false); $('#fill_example').click(function() { return editor.setValue("\n[\n {\n \"selector\": \".nested_property\",\n \"type\": \"string\",\n \"operator\": \"<=\",\n \"compare\": \"has this value\"\n }\n]"); }); $('#action_parameters').append(domSectionSelectedActions); $('#action_parameters').append(domSectionActionParameters); $('#input_id').focus(); $('#select_event_type').change(function() { return fPrepareEventType($(this).val()); }); switch (oParams.eventtype) { case 'custom': name = decodeURIComponent(oParams.eventname); $('#input_id').val("My '" + name + "' Rule"); fPrepareEventType('Custom Event', function() { $('input', domInputEventName).val(name); $('input', domInputEventName).focus(); return editor.setValue("[\n\n]"); }); break; case 'webhook': name = decodeURIComponent(oParams.hookname); $('#input_id').val("My '" + name + "' Rule"); fPrepareEventType('Webhook', function() { return $('select', domSelectWebhook).val(name); }); } fIssueRequest({ data: { command: 'get_action_invokers' }, done: function(data) { var act, actions, arrEls, err, i, module, oAis, _results; try { oAis = JSON.parse(data.message); } catch (_error) { err = _error; console.error('ERROR: non-object received from server: ' + data.message); return; } i = 0; _results = []; for (module in oAis) { actions = oAis[module]; _results.push((function() { var _j, _len1, _results1; _results1 = []; for (_j = 0, _len1 = actions.length; _j < _len1; _j++) { act = actions[_j]; i++; arrEls = $("#action_invoker_params div").filter(function() { return $(this).text() === ("" + module + " -> " + act); }); if (arrEls.length === 0) { _results1.push($('#select_actions').append($('