diff --git a/coffee/components-manager.coffee b/coffee/components-manager.coffee index 7bc8076..903f6e8 100644 --- a/coffee/components-manager.coffee +++ b/coffee/components-manager.coffee @@ -454,21 +454,22 @@ commandFunctions = if answ.code isnt 200 callback answ else - db.getUserWebhookIDs user.username, ( err, hooks ) => - if hooks.indexOf( oBody.hookname ) > -1 - answ.code = 409 - answ.message = 'Webhook already existing: ' + oBody.hookname - callback answ + db.getAllUserWebhookNames user.username, ( err, arrHooks ) => + hookExists = false + hookExists = true for hookid, hookname of arrHooks when hookname is oBody.hookname + if hookExists + callback + code: 409 + message: 'Webhook already existing: ' + oBody.hookname else - db.getAllWebhookIDs ( arrHooks ) -> + db.getAllWebhookIDs ( err, arrHooks ) -> genHookID = ( arrHooks ) -> hookid = '' - for i in [0..1] + for i in [ 0..1 ] hookid += Math.random().toString( 36 ).substring 2 if arrHooks and arrHooks.indexOf( hookid ) > -1 - genHookID arrHooks - else - hookid + hookid = genHookID arrHooks + hookid hookid = genHookID arrHooks db.createWebhook user.username, hookid, oBody.hookname rh.activateWebhook user.username, hookid, oBody.hookname @@ -479,13 +480,13 @@ commandFunctions = hookname: oBody.hookname get_all_webhooks: ( user, oBody, callback ) -> - db.getAllUserWebhooks user.username, ( err, data ) -> + db.getAllUserWebhookNames user.username, ( err, data ) -> if err callback code: 400 message: "We didn't like your request!" else - data = JSON.stringify data || '' + data = JSON.stringify( data ) || null callback code: 200 message: data diff --git a/coffee/persistence.coffee b/coffee/persistence.coffee index 71ef805..9db798f 100644 --- a/coffee/persistence.coffee +++ b/coffee/persistence.coffee @@ -838,10 +838,10 @@ exports.getUserWebhookIDs = ( username, cb ) => ### Gets all the user's webhooks with names. -@public getAllUserWebhooks( *username* ) +@public getAllUserWebhookNames( *username* ) @param {String} username ### -exports.getAllUserWebhooks = ( username, cb ) => +exports.getAllUserWebhookNames = ( username, cb ) => getSetRecords "user:#{ username }:webhooks", exports.getWebhookName, cb ### diff --git a/coffee/request-handler.coffee b/coffee/request-handler.coffee index 2941fb1..58acceb 100644 --- a/coffee/request-handler.coffee +++ b/coffee/request-handler.coffee @@ -92,7 +92,6 @@ exports = module.exports = ( args ) => @allowedHooks = {} db.getAllWebhooks ( err, oHooks ) => if oHooks - console @log.info "RH | Initializing #{ Object.keys( oHooks ).length } Webhooks" @allowedHooks = oHooks module.exports @@ -416,6 +415,7 @@ exports.activateWebhook = ( user, hookid, name ) => hookname: name username: user + # Deactivate a webhook exports.deactivateWebhook = ( hookid ) => @log.info "HL | Webhook '#{ hookid }' deactivated" diff --git a/js/components-manager.js b/js/components-manager.js index 0cbb7c2..06e53bd 100644 --- a/js/components-manager.js +++ b/js/components-manager.js @@ -570,26 +570,34 @@ Components Manager if (answ.code !== 200) { return callback(answ); } else { - return db.getUserWebhookIDs(user.username, (function(_this) { - return function(err, hooks) { - if (hooks.indexOf(oBody.hookname) > -1) { - answ.code = 409; - answ.message = 'Webhook already existing: ' + oBody.hookname; - return callback(answ); + return db.getAllUserWebhookNames(user.username, (function(_this) { + return function(err, arrHooks) { + var hookExists, hookid, hookname; + hookExists = false; + for (hookid in arrHooks) { + hookname = arrHooks[hookid]; + if (hookname === oBody.hookname) { + hookExists = true; + } + } + if (hookExists) { + return callback({ + code: 409, + message: 'Webhook already existing: ' + oBody.hookname + }); } else { - return db.getAllWebhookIDs(function(arrHooks) { - var genHookID, hookid; + return db.getAllWebhookIDs(function(err, arrHooks) { + var genHookID; genHookID = function(arrHooks) { - var hookid, i, _i; + var i, _i; hookid = ''; for (i = _i = 0; _i <= 1; i = ++_i) { hookid += Math.random().toString(36).substring(2); } if (arrHooks && arrHooks.indexOf(hookid) > -1) { - return genHookID(arrHooks); - } else { - return hookid; + hookid = genHookID(arrHooks); } + return hookid; }; hookid = genHookID(arrHooks); db.createWebhook(user.username, hookid, oBody.hookname); @@ -608,14 +616,14 @@ Components Manager } }, get_all_webhooks: function(user, oBody, callback) { - return db.getAllUserWebhooks(user.username, function(err, data) { + return db.getAllUserWebhookNames(user.username, function(err, data) { if (err) { return callback({ code: 400, message: "We didn't like your request!" }); } else { - data = JSON.stringify(data || ''); + data = JSON.stringify(data) || null; return callback({ code: 200, message: data diff --git a/js/persistence.js b/js/persistence.js index e836284..2799d52 100644 --- a/js/persistence.js +++ b/js/persistence.js @@ -1097,11 +1097,11 @@ Persistence /* Gets all the user's webhooks with names. - @public getAllUserWebhooks( *username* ) + @public getAllUserWebhookNames( *username* ) @param {String} username */ - exports.getAllUserWebhooks = (function(_this) { + exports.getAllUserWebhookNames = (function(_this) { return function(username, cb) { return getSetRecords("user:" + username + ":webhooks", exports.getWebhookName, cb); }; diff --git a/js/request-handler.js b/js/request-handler.js index 013c3f4..90775f8 100644 --- a/js/request-handler.js +++ b/js/request-handler.js @@ -104,7 +104,6 @@ Request Handler _this.allowedHooks = {}; db.getAllWebhooks(function(err, oHooks) { if (oHooks) { - console; _this.log.info("RH | Initializing " + (Object.keys(oHooks).length) + " Webhooks"); return _this.allowedHooks = oHooks; } diff --git a/webpages/handlers/coffee/forge_rule.coffee b/webpages/handlers/coffee/forge_rule.coffee index 571ac9f..64a0097 100644 --- a/webpages/handlers/coffee/forge_rule.coffee +++ b/webpages/handlers/coffee/forge_rule.coffee @@ -14,6 +14,43 @@ for param in arrParams if oParams.id oParams.id = decodeURIComponent oParams.id +# Webpage elements. Registered here for easier access +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 () -> fFetchEventParams $( this ).val() +domSelectEventPoller.append $( '

' ).text( 'Event Poller Name : ' ).append el + +domInputStartTime = $( '
' ).attr( 'class', 'indent20' ).html "Start Time : + \"hh:mm\", default = 12:00" + +domInputInterval = $( '
' ).attr( 'class', 'indent20' ).html "Interval : + \"days hours:minutes\", default = 10 minutes" + +domEventPollerParameters = $( '
' ).attr 'id', 'event_poller_params' + +domTableSelectedActions = $( ' ' ).attr( 'id', 'selected_actions' ) +domDivActionUserParams = $( '
' ).attr( 'id', 'action_invoker_params' ) +$( '#action_parameters' ).append $( '
' ).html "Selected Actions:" +$( '#action_parameters' ).append domTableSelectedActions +$( '#action_parameters' ).append $( '
' ).html "

Required Parameters:

" +$( '#action_parameters' ).append domDivActionUserParams +$( '#action_parameters' ).append $( '
' ).html "

" + + fDisplayError = ( msg ) -> window.scrollTo 0, 0 $( '#info' ).text "Error: #{ msg }" @@ -28,7 +65,7 @@ fFailedRequest = ( msg ) -> fIssueRequest = ( args ) -> $( '#info' ).text '' - $.post( '/usercommand', args.body ) + $.post( '/usercommand', args.data ) .done args.done .fail args.fail @@ -104,42 +141,32 @@ fConvertDayHourToMinutes = ( strDayHour ) -> # EVENT Related Helper Functions # -# fPlaceAndPaintInterval = () -> -# $( '#event_start' ).html 'Start Time: -# -# "hh:mm", default = 12:00' -# $( '#event_interval' ).html 'Interval: -# -# "days hours:minutes", default = 10 minutes' - # Prepare the event section when a different event type is selected fPrepareEventType = ( eventtype ) -> $( '#select_event_type' ).val eventtype - $( '#event_parameters *' ).remove() + $( '#event_parameters > div' ).detach() switch eventtype # The user wants to react to custom event when 'Custom Event' - inpEvt = $( '' ).attr( 'type', 'text' ) - .attr( 'style', 'font-size:1em' ).attr 'id', 'input_eventname' - $( '#event_parameters' ).append $( '

' ).text( 'Event Name : ' ).append inpEvt + $( '#event_parameters' ).append domInputEventName # The user wants a webhook as event producer when 'Webhook' fIssueRequest - body: command: 'get_all_webhooks' + data: command: 'get_all_webhooks' done: ( data ) -> try oHooks = JSON.parse data.message - selHook = $( '' ).attr( 'type', 'text' ) - .attr( 'style', 'font-size:1em' ).attr 'id', 'select_eventpoller' - $( '#event_parameters' ).append $( '

' ).text( 'Event Poller Name : ' ).append selPoller fIssueRequest - body: command: 'get_event_pollers' + data: command: 'get_event_pollers' done: ( data ) -> try - oEps = JSON.parse data.message - fAppendEvents = ( id, events ) -> - fAppendEvent = ( evt ) -> - $( '#select_eventpoller' ).append $( '

' i = 0 - fAppendParam = ( name, shielded ) -> + for name, shielded of oParams i++ tr = $( '' ) tr.append $( '' ) + tdName = $( '
' ).text hookname + tdUrl = $( '' ).attr( 'style', 'width:600px' ).val "#{ hostUrl }/webhooks/#{ hookid }" + img = $( '' ).attr( 'class', 'del' ) + .attr( 'title', 'Delete Module' ).attr 'src', 'red_cross_small.png' + tr.append( $( '
' ) - tdName = $( '
' ).text hookname - tdUrl = $( '' ).attr( 'style', 'width:600px' ).val "#{ hostUrl }/webhooks/#{ hookid }" - img = $( '' ).attr( 'class', 'del' ) - .attr( 'title', 'Delete Module' ).attr 'src', 'red_cross_small.png' - tr.append( $( '
' ).css 'width', '20px' @@ -213,23 +238,20 @@ fAddEventParams = ( id ) -> inp.attr( 'type', 'password' ) tr.append $( '' ).text( ' : ' ).append inp table.append tr - fAppendParam name, shielded for name, shielded of oParams if i > 0 $( '#event_poller_params' ).html 'Required Global Parameters:' $( '#event_poller_params' ).append table - - fDelayed = () -> fFillEventParams id - setTimeout fDelayed, 200 fFillEventParams = ( moduleId ) -> - obj = - command: 'get_event_poller_user_params' - body: JSON.stringify - id: moduleId fIssueRequest - body: obj + data: + command: 'get_event_poller_user_params' + body: JSON.stringify + id: moduleId done: ( data ) -> + console.log 'filling event params: ' + console.log data oParams = JSON.parse data.message for param, oParam of oParams par = $( "#event_poller_params tr" ).filter () -> @@ -239,23 +261,15 @@ fFillEventParams = ( moduleId ) -> $( 'input', par ).change () -> $( this ).attr 'unchanged', 'false' - obj.command = 'get_event_poller_user_arguments' - obj.body = JSON.stringify - ruleId: $( '#input_id' ).val() - moduleId: moduleId - fIssueRequest - body: obj - done: fAddEventUserArgs moduleId # FIXME this is wrong here - # Fetch function arguments required for an event polling function fFetchEventFunctionArgs = ( arrName ) -> - # FIXME this data gets not populated sometimes! fIssueRequest - body: + data: command: 'get_event_poller_function_arguments' body: JSON.stringify id: arrName[ 0 ] done: ( data ) -> + console.log 'fetching event function arguments: ' if data.message oParams = JSON.parse data.message if oParams[ arrName[ 1 ] ] @@ -272,10 +286,21 @@ fFetchEventFunctionArgs = ( arrName ) -> td = $( '' ).appendTo tr td.append $( '' ).attr 'type', 'text' tr.append td - fail: fFailedRequest 'Error fetching action invoker function params' + + fIssueRequest + data: + command: 'get_event_poller_user_arguments' + body: JSON.stringify + ruleId: $( '#input_id' ).val() + moduleId: moduleId + done: fAddEventUserArgs moduleId + + fail: fFailedRequest 'Error fetching event poller function arguments' fAddEventUserArgs = ( name ) -> ( data ) -> + console.log 'filling event funcction arguments: ' + console.log data for key, arrFuncs of data.message par = $ "#event_poller_params" for oFunc in JSON.parse arrFuncs @@ -291,7 +316,7 @@ fAddEventUserArgs = ( name ) -> fAddSelectedAction = ( name ) -> arrName = name.split ' -> ' - arrEls = $( "#action_params div.modName" ).map( () -> + arrEls = $( "#action_invoker_params div.modName" ).map( () -> $( this ).text() ).get() table = $( '#selected_actions' ) @@ -302,7 +327,7 @@ fAddSelectedAction = ( name ) -> td = $( '' ).attr( 'class', 'funcMappings').appendTo tr fFetchActionFunctionArgs td, arrName if arrName[ 0 ] not in arrEls - div = $( '
' ).appendTo $( '#action_params' ) + div = $( '
' ).appendTo $( '#action_invoker_params' ) subdiv = $( '
').appendTo div subdiv.append $( '
' ) .attr( 'class', 'modName underlined' ).text arrName[ 0 ] @@ -315,12 +340,11 @@ fAddSelectedAction = ( name ) -> setTimeout fDelayed, 300 fFetchActionParams = ( div, modName ) -> - obj = - command: 'get_action_invoker_params' - body: JSON.stringify - id: modName fIssueRequest - body: obj + data: + command: 'get_action_invoker_params' + body: JSON.stringify + id: modName done: ( data ) -> if data.message oParams = JSON.parse data.message @@ -342,7 +366,7 @@ fFetchActionParams = ( div, modName ) -> fFetchActionFunctionArgs = ( tag, arrName ) -> fIssueRequest - body: + data: command: 'get_action_invoker_function_arguments' body: JSON.stringify id: arrName[ 0 ] @@ -363,14 +387,14 @@ fFetchActionFunctionArgs = ( tag, arrName ) -> fFillActionFunction = ( name ) -> fIssueRequest - body: + data: command: 'get_action_invoker_user_params' body: JSON.stringify id: name done: fAddActionUserParams name fIssueRequest - body: + data: command: 'get_action_invoker_user_arguments' body: JSON.stringify ruleId: $( '#input_id' ).val() @@ -380,7 +404,7 @@ fFillActionFunction = ( name ) -> fAddActionUserParams = ( name ) -> ( data ) -> oParams = JSON.parse data.message - domMod = $( "#action_params div" ).filter () -> + domMod = $( "#action_invoker_params div" ).filter () -> $( 'div.modName', this ).text() is name for param, oParam of oParams par = $( "tr", domMod ).filter () -> @@ -409,7 +433,7 @@ fAddActionUserArgs = ( name ) -> fOnLoad = () -> # Fetch the public key from the engine fIssueRequest - body: command: 'get_public_key' + data: command: 'get_public_key' done: ( data ) -> strPublicKey = data.message fail: ( err ) -> @@ -448,35 +472,17 @@ fOnLoad = () -> when 'webhook' $( '#input_id' ).val "My '#{ oParams.hookname }' Rule" fPrepareEventType 'Webhook' - $( 'select_eventhook' ).val oParams.hookname + domSelectWebhook.val oParams.hookname when 'poller' $( '#input_id' ).val "My '#{ oParams.eventpoller }' Rule" fPrepareEventType 'Event Poller' - $( '#input_event' ).change () -> - $( '#select_event' ).val '' - $( '#select_event' ).val $( this ).val() - fFetchEventParams $( '#select_event' ).val() - if $( '#select_event' ).val() is '' - $( '#event_start' ).html '' - $( '#event_interval' ).html '' - else - fPlaceAndPaintInterval() - # ACTIONS - # Selected Actions: - #
- #

- # Required Parameters: - #

- #
- #

- fIssueRequest - body: + data: command: 'get_action_invokers' done: ( data ) -> try @@ -484,32 +490,42 @@ fOnLoad = () -> catch err console.error 'ERROR: non-object received from server: ' + data.message return - fAppendActions = ( module, actions ) -> + i = 0 + for module, actions of oAis for act in actions - arrEls = $( "#action_params div" ).filter () -> + i++ + arrEls = $( "#action_invoker_params div" ).filter () -> $( this ).text() is "#{ module } -> #{ act }" - # It could have been loaded async before through the rules ito the action params + # It could have been loaded async before through the rules into the action params if arrEls.length is 0 $( '#select_actions' ).append $( '
' ).append img ) + tr.append( $( '' ).attr( 'style', 'padding-left:10px' ).append tdName ) + tr.append( $( '' ).attr( 'style', 'padding-left:10px' ).append tdUrl ) + $( '#table_webhooks' ).append tr + else + fShowWebhookUsage null + cb? hookid, hookname -fProcessWebhookList = ( data ) -> - $( '#table_webhooks *' ).remove() - if data.message - oHooks = JSON.parse data.message - $( '#table_webhooks' ).append $( '

' ).text 'Your existing Webhooks:' - for hookid, hookname of oHooks - tr = $( '

' ).append img ) - tr.append( $( '' ).attr( 'style', 'padding-left:10px' ).append tdName ) - tr.append( $( '' ).attr( 'style', 'padding-left:10px' ).append tdUrl ) - $( '#table_webhooks' ).append tr +fShowWebhookUsage = ( hookid, hookname ) -> + $( '#display_hookurl *' ).remove() + if hookid + b = $( '' ).text "This is the Webhook Url you can use for your Events '#{ hookname }' : " + $( '#display_hookurl' ).append b + $( '#display_hookurl' ).append $('
') + inp = $('').attr( 'type', 'text' ).attr( 'style', 'width:600px' ) + .val "#{ hostUrl }/webhooks/#{ hookid }" + $( '#display_hookurl' ).append inp + $( '#display_hookurl' ).append $('
') + div = $( '
' ) + div.append $( '
' ) + div.append $( '
' ).html "1. Try it out and push your location to your new webhook + via this page." + div.append $( '
' ) + div.append $( '
' ).html "2. Then you should setup a Rule for the '#{ hookname }' Event!" + $( '#display_hookurl' ).append div fOnLoad = () -> document.title = 'Create Webhooks!' # Load existing Webhooks - fUpdateWebhookList() + fUpdateWebhookList fShowWebhookUsage # Register button action $( '#but_submit' ).click -> @@ -80,29 +102,15 @@ fOnLoad = () -> hookname: hookname done: ( data ) -> oAnsw = JSON.parse data.message - b = $( '' ).text "This is the Webhook Url you can use for your Event '#{ oAnsw.hookname }' : " - $( '#display_hookurl' ).append b - $( '#display_hookurl' ).append $('
') - inp = $('').attr( 'type', 'text' ).attr( 'style', 'width:600px' ) - .val "#{ hostUrl }/webhooks/#{ oAnsw.hookid }" - $( '#display_hookurl' ).append inp - $( '#display_hookurl' ).append $('
') - - div = $( '
' ) - div.append $( '
' ) - div.append $( '
' ).html "1. Try it out and push your location to your new webhook - via this page." - div.append $( '
' ) - div.append $( '
' ).html "2. Then you should setup a Rule for this Event!" - $( '#display_hookurl' ).append div - fUpdateWebhookList() + fShowWebhookUsage oAnsw.hookid, oAnsw.hookname + fUpdateWebhookList ( data ) -> + $( '#info' ).text "New Webhook successfully created!" + $( '#info' ).attr 'class', 'success' fail: ( err ) -> if err.status is 409 fFailedRequest( 'Webhook Event Name already existing!' ) err else fFailedRequest( 'Unable to create Webhook! ' + err.message ) err - fUpdateWebhookList() $( '#table_webhooks' ).on 'click', 'img', () -> if confirm "Do you really want to delete this webhook?" @@ -113,12 +121,13 @@ fOnLoad = () -> command: 'delete_webhook' body: JSON.stringify hookid: arrUrl[ arrUrl.length - 1 ] + done: ( data ) -> - $( '#info' ).text data.message - $( '#info' ).attr 'class', 'success' - fUpdateWebhookList() + fUpdateWebhookList ( data ) -> + $( '#info' ).text 'Webhook deleted!' + $( '#info' ).attr 'class', 'success' + fail: ( err ) -> fFailedRequest( 'Unable to delete Webhook!' ) err - fUpdateWebhookList() window.addEventListener 'load', fOnLoad, true diff --git a/webpages/handlers/js/forge_rule.js b/webpages/handlers/js/forge_rule.js index fb025c0..f4d508d 100644 --- a/webpages/handlers/js/forge_rule.js +++ b/webpages/handlers/js/forge_rule.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.7.1 (function() { - var arrKV, arrParams, fAddActionUserArgs, fAddActionUserParams, fAddEventParams, fAddEventUserArgs, fAddSelectedAction, fConvertDayHourToMinutes, fConvertTimeToDate, fDisplayError, fFailedRequest, fFetchActionFunctionArgs, fFetchActionParams, fFetchEventFunctionArgs, fFetchEventParams, fFillActionFunction, fFillEventParams, fIssueRequest, fOnLoad, fPrepareEventType, oParams, param, strPublicKey, _i, _len, + var arrKV, arrParams, domDivActionUserParams, domEventPollerParameters, domInputEventName, domInputInterval, domInputStartTime, domSelectEventPoller, domSelectWebhook, domTableSelectedActions, el, fAddActionUserArgs, fAddActionUserParams, fAddEventUserArgs, fAddSelectedAction, fConvertDayHourToMinutes, fConvertTimeToDate, fDisplayError, fDisplayEventParams, fFailedRequest, fFetchActionFunctionArgs, fFetchActionParams, fFetchEventFunctionArgs, fFetchEventParams, fFillActionFunction, fFillEventParams, fIssueRequest, fOnLoad, fPrepareEventType, oParams, param, strPublicKey, _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 = ''; @@ -19,6 +19,48 @@ 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)); + + domInputStartTime = $('
').attr('class', 'indent20').html("Start Time : \"hh:mm\", default = 12:00"); + + domInputInterval = $('
').attr('class', 'indent20').html("Interval : \"days hours:minutes\", default = 10 minutes"); + + domEventPollerParameters = $('
').attr('id', 'event_poller_params'); + + domTableSelectedActions = $(' ').attr('id', 'selected_actions'); + + domDivActionUserParams = $('
').attr('id', 'action_invoker_params'); + + $('#action_parameters').append($('
').html("Selected Actions:")); + + $('#action_parameters').append(domTableSelectedActions); + + $('#action_parameters').append($('
').html("

Required Parameters:

")); + + $('#action_parameters').append(domDivActionUserParams); + + $('#action_parameters').append($('
').html("

")); + fDisplayError = function(msg) { window.scrollTo(0, 0); $('#info').text("Error: " + msg); @@ -37,7 +79,7 @@ fIssueRequest = function(args) { $('#info').text(''); - return $.post('/usercommand', args.body).done(args.done).fail(args.fail); + return $.post('/usercommand', args.data).done(args.done).fail(args.fail); }; fConvertTimeToDate = function(str) { @@ -111,23 +153,22 @@ }; fPrepareEventType = function(eventtype) { - var inpEvt, selPoller; $('#select_event_type').val(eventtype); - $('#event_parameters *').remove(); + $('#event_parameters > div').detach(); switch (eventtype) { case 'Custom Event': - inpEvt = $('').attr('type', 'text').attr('style', 'font-size:1em').attr('id', 'input_eventname'); - return $('#event_parameters').append($('

').text('Event Name : ').append(inpEvt)); + return $('#event_parameters').append(domInputEventName); case 'Webhook': return fIssueRequest({ - body: { + data: { command: 'get_all_webhooks' }, done: function(data) { var err, hookid, hookname, i, oHooks, selHook; try { oHooks = JSON.parse(data.message); - selHook = $('').attr('type', 'text').attr('style', 'font-size:1em').attr('id', 'select_eventpoller'); - $('#event_parameters').append($('

').text('Event Poller Name : ').append(selPoller)); - fIssueRequest({ - body: { + return fIssueRequest({ + data: { command: 'get_event_pollers' }, done: function(data) { - var err, events, fAppendEvents, id, oEps; + var err, events, evt, id, oEps, _j, _len1; try { oEps = JSON.parse(data.message); - fAppendEvents = function(id, events) { - var evt, fAppendEvent, _j, _len1, _results; - fAppendEvent = function(evt) { - return $('#select_eventpoller').append($('

'); i = 0; - fAppendParam = function(name, shielded) { - var inp, tr; + for (name in oParams) { + shielded = oParams[name]; i++; tr = $(''); tr.append($('
').css('width', '20px')); @@ -236,36 +265,29 @@ inp.attr('type', 'password'); } tr.append($('').text(' : ').append(inp)); - return table.append(tr); - }; - for (name in oParams) { - shielded = oParams[name]; - fAppendParam(name, shielded); + table.append(tr); } if (i > 0) { $('#event_poller_params').html('Required Global Parameters:'); $('#event_poller_params').append(table); - } - fDelayed = function() { return fFillEventParams(id); - }; - return setTimeout(fDelayed, 200); + } } }; }; fFillEventParams = function(moduleId) { - var obj; - obj = { - command: 'get_event_poller_user_params', - body: JSON.stringify({ - id: moduleId - }) - }; - fIssueRequest({ - body: obj, + return fIssueRequest({ + data: { + command: 'get_event_poller_user_params', + body: JSON.stringify({ + id: moduleId + }) + }, done: function(data) { var oParam, par, _results; + console.log('filling event params: '); + console.log(data); oParams = JSON.parse(data.message); _results = []; for (param in oParams) { @@ -282,27 +304,19 @@ return _results; } }); - obj.command = 'get_event_poller_user_arguments'; - obj.body = JSON.stringify({ - ruleId: $('#input_id').val(), - moduleId: moduleId - }); - return fIssueRequest({ - body: obj, - done: fAddEventUserArgs(moduleId) - }); }; fFetchEventFunctionArgs = function(arrName) { return fIssueRequest({ - body: { + data: { command: 'get_event_poller_function_arguments', body: JSON.stringify({ id: arrName[0] }) }, done: function(data) { - var functionArgument, table, td, tr, _j, _len1, _ref, _results; + var functionArgument, table, td, tr, _j, _len1, _ref; + console.log('fetching event function arguments: '); if (data.message) { oParams = JSON.parse(data.message); if (oParams[arrName[1]]) { @@ -311,7 +325,6 @@ } table = $('').appendTo($('#event_poller_params')); _ref = oParams[arrName[1]]; - _results = []; for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { functionArgument = _ref[_j]; tr = $('').attr('class', 'funcMappings').appendTo(table); @@ -322,19 +335,30 @@ tr.append($(''); - tdName = $('
').text(hookname); - tdUrl = $('').attr('style', 'width:600px').val("" + hostUrl + "/webhooks/" + hookid); - img = $('').attr('class', 'del').attr('title', 'Delete Module').attr('src', 'red_cross_small.png'); - tr.append($('
'); + tdName = $('
').text(hookname); + tdUrl = $('').attr('style', 'width:600px').val("" + hostUrl + "/webhooks/" + hookid); + img = $('').attr('class', 'del').attr('title', 'Delete Module').attr('src', 'red_cross_small.png'); + tr.append($('
').text(' : ')); td = $('').appendTo(tr); td.append($('').attr('type', 'text')); - _results.push(tr.append(td)); + tr.append(td); } - return _results; + return fIssueRequest({ + data: { + command: 'get_event_poller_user_arguments', + body: JSON.stringify({ + ruleId: $('#input_id').val(), + moduleId: moduleId + }) + }, + done: fAddEventUserArgs(moduleId) + }); } } }, - fail: fFailedRequest('Error fetching action invoker function params') + fail: fFailedRequest('Error fetching event poller function arguments') }); }; fAddEventUserArgs = function(name) { return function(data) { var arrFuncs, key, oFunc, par, tr, _ref, _results; + console.log('filling event funcction arguments: '); + console.log(data); _ref = data.message; _results = []; for (key in _ref) { @@ -361,7 +385,7 @@ fAddSelectedAction = function(name) { var arrEls, arrName, div, fDelayed, img, subdiv, table, td, tr, _ref; arrName = name.split(' -> '); - arrEls = $("#action_params div.modName").map(function() { + arrEls = $("#action_invoker_params div.modName").map(function() { return $(this).text(); }).get(); table = $('#selected_actions'); @@ -372,7 +396,7 @@ td = $('').attr('class', 'funcMappings').appendTo(tr); fFetchActionFunctionArgs(td, arrName); if (_ref = arrName[0], __indexOf.call(arrEls, _ref) < 0) { - div = $('
').appendTo($('#action_params')); + div = $('
').appendTo($('#action_invoker_params')); subdiv = $('
').appendTo(div); subdiv.append($('
')).attr('class', 'modName underlined').text(arrName[0]); fFetchActionParams(div, arrName[0]); @@ -389,15 +413,13 @@ }; fFetchActionParams = function(div, modName) { - var obj; - obj = { - command: 'get_action_invoker_params', - body: JSON.stringify({ - id: modName - }) - }; return fIssueRequest({ - body: obj, + data: { + command: 'get_action_invoker_params', + body: JSON.stringify({ + id: modName + }) + }, done: function(data) { var fAppendActionParam, name, sh, table, _results; if (data.message) { @@ -432,7 +454,7 @@ fFetchActionFunctionArgs = function(tag, arrName) { return fIssueRequest({ - body: { + data: { command: 'get_action_invoker_function_arguments', body: JSON.stringify({ id: arrName[0] @@ -466,7 +488,7 @@ fFillActionFunction = function(name) { fIssueRequest({ - body: { + data: { command: 'get_action_invoker_user_params', body: JSON.stringify({ id: name @@ -475,7 +497,7 @@ done: fAddActionUserParams(name) }); return fIssueRequest({ - body: { + data: { command: 'get_action_invoker_user_arguments', body: JSON.stringify({ ruleId: $('#input_id').val(), @@ -490,7 +512,7 @@ return function(data) { var domMod, oParam, par, _results; oParams = JSON.parse(data.message); - domMod = $("#action_params div").filter(function() { + domMod = $("#action_invoker_params div").filter(function() { return $('div.modName', this).text() === name; }); _results = []; @@ -540,7 +562,7 @@ fOnLoad = function() { var editor, obj; fIssueRequest({ - body: { + data: { command: 'get_public_key' }, done: function(data) { @@ -575,29 +597,18 @@ case 'webhook': $('#input_id').val("My '" + oParams.hookname + "' Rule"); fPrepareEventType('Webhook'); - $('select_eventhook').val(oParams.hookname); + domSelectWebhook.val(oParams.hookname); break; case 'poller': $('#input_id').val("My '" + oParams.eventpoller + "' Rule"); fPrepareEventType('Event Poller'); } - $('#input_event').change(function() { - $('#select_event').val(''); - $('#select_event').val($(this).val()); - fFetchEventParams($('#select_event').val()); - if ($('#select_event').val() === '') { - $('#event_start').html(''); - return $('#event_interval').html(''); - } else { - return fPlaceAndPaintInterval(); - } - }); fIssueRequest({ - body: { + data: { command: 'get_action_invokers' }, done: function(data) { - var actions, err, fAppendActions, module, oAis, _results; + var act, actions, arrEls, err, i, module, oAis, _results; try { oAis = JSON.parse(data.message); } catch (_error) { @@ -605,50 +616,58 @@ console.error('ERROR: non-object received from server: ' + data.message); return; } - fAppendActions = function(module, actions) { - var act, arrEls, _j, _len1, _results; - _results = []; - for (_j = 0, _len1 = actions.length; _j < _len1; _j++) { - act = actions[_j]; - arrEls = $("#action_params div").filter(function() { - return $(this).text() === ("" + module + " -> " + act); - }); - if (arrEls.length === 0) { - _results.push($('#select_actions').append($('
').append(img)); - tr.append($('').attr('style', 'padding-left:10px').append(tdName)); - tr.append($('').attr('style', 'padding-left:10px').append(tdUrl)); - _results.push($('#table_webhooks').append(tr)); + fProcessWebhookList = function(cb) { + return function(data) { + var hookid, hookname, img, oHooks, tdName, tdUrl, tr; + $('#table_webhooks *').remove(); + if (data.message) { + oHooks = JSON.parse(data.message); + $('#table_webhooks').append($('

').text('Your existing Webhooks:')); + for (hookid in oHooks) { + hookname = oHooks[hookid]; + tr = $('

').append(img)); + tr.append($('').attr('style', 'padding-left:10px').append(tdName)); + tr.append($('').attr('style', 'padding-left:10px').append(tdUrl)); + $('#table_webhooks').append(tr); + } + } else { + fShowWebhookUsage(null); } - return _results; + return typeof cb === "function" ? cb(hookid, hookname) : void 0; + }; + }; + + fShowWebhookUsage = function(hookid, hookname) { + var b, div, inp; + $('#display_hookurl *').remove(); + if (hookid) { + b = $('').text("This is the Webhook Url you can use for your Events '" + hookname + "' : "); + $('#display_hookurl').append(b); + $('#display_hookurl').append($('
')); + inp = $('').attr('type', 'text').attr('style', 'width:600px').val("" + hostUrl + "/webhooks/" + hookid); + $('#display_hookurl').append(inp); + $('#display_hookurl').append($('
')); + div = $('
'); + div.append($('
')); + div.append($('
').html("1. Try it out and push your location to your new webhook via this page.")); + div.append($('
')); + div.append($('
').html("2. Then you should setup a Rule for the '" + hookname + "' Event!")); + return $('#display_hookurl').append(div); } }; fOnLoad = function() { document.title = 'Create Webhooks!'; - fUpdateWebhookList(); + fUpdateWebhookList(fShowWebhookUsage); $('#but_submit').click(function() { var hookname; $('#info').text(''); @@ -89,29 +111,20 @@ }) }, done: function(data) { - var b, div, inp, oAnsw; + var oAnsw; oAnsw = JSON.parse(data.message); - b = $('').text("This is the Webhook Url you can use for your Event '" + oAnsw.hookname + "' : "); - $('#display_hookurl').append(b); - $('#display_hookurl').append($('
')); - inp = $('').attr('type', 'text').attr('style', 'width:600px').val("" + hostUrl + "/webhooks/" + oAnsw.hookid); - $('#display_hookurl').append(inp); - $('#display_hookurl').append($('
')); - div = $('
'); - div.append($('
')); - div.append($('
').html("1. Try it out and push your location to your new webhook via this page.")); - div.append($('
')); - div.append($('
').html("2. Then you should setup a Rule for this Event!")); - $('#display_hookurl').append(div); - return fUpdateWebhookList(); + fShowWebhookUsage(oAnsw.hookid, oAnsw.hookname); + return fUpdateWebhookList(function(data) { + $('#info').text("New Webhook successfully created!"); + return $('#info').attr('class', 'success'); + }); }, fail: function(err) { if (err.status === 409) { - fFailedRequest('Webhook Event Name already existing!')(err); + return fFailedRequest('Webhook Event Name already existing!')(err); } else { - fFailedRequest('Unable to create Webhook! ' + err.message)(err); + return fFailedRequest('Unable to create Webhook! ' + err.message)(err); } - return fUpdateWebhookList(); } }); } @@ -129,13 +142,13 @@ }) }, done: function(data) { - $('#info').text(data.message); - $('#info').attr('class', 'success'); - return fUpdateWebhookList(); + return fUpdateWebhookList(function(data) { + $('#info').text('Webhook deleted!'); + return $('#info').attr('class', 'success'); + }); }, fail: function(err) { - fFailedRequest('Unable to delete Webhook!')(err); - return fUpdateWebhookList(); + return fFailedRequest('Unable to delete Webhook!')(err); } }); } diff --git a/webpages/handlers/templates/forge_webhook.html b/webpages/handlers/templates/forge_webhook.html index a5e4819..69318b1 100644 --- a/webpages/handlers/templates/forge_webhook.html +++ b/webpages/handlers/templates/forge_webhook.html @@ -1,5 +1,5 @@

Create your own Webhooks

-

Choose a name for the Events that are pushed to the new Webhook : +

Label the Events received over the new Webhook with :



diff --git a/webpages/public/mobile.html b/webpages/public/mobile.html index eb1bda0..c50cdf5 100644 --- a/webpages/public/mobile.html +++ b/webpages/public/mobile.html @@ -59,7 +59,8 @@ } function displayError(positionError) { - $('#info').text('Error: ' + positionError); + console.log(positionError); + $('#info').text('Error: ' + positionError.message); } var gl = navigator.geolocation; diff --git a/webpages/public/style.css b/webpages/public/style.css index dc1c14a..c763842 100644 --- a/webpages/public/style.css +++ b/webpages/public/style.css @@ -31,6 +31,10 @@ input[type=password]:focus { border: 1px solid rgba(81, 203, 238, 1); } +.indent20 { + padding-left: 20px; +} + #mobile { margin: 10px 20px 10px 20px; } @@ -101,6 +105,10 @@ input[type=password]:focus { margin-left: 20px; } +#event_poller_params { + padding-top: 10px; +} + #input_id { font-size: 1em; }