From df39fe204301d7b2ca3639fe620dd01fd7498abb Mon Sep 17 00:00:00 2001 From: Dominic Bosch Date: Sun, 27 Apr 2014 13:20:08 +0200 Subject: [PATCH] Clean state again. With user friendly features --- coffee/components-manager.coffee | 1 + coffee/engine.coffee | 2 +- coffee/event-poller.coffee | 2 +- js/components-manager.js | 1 + js/engine.js | 2 +- js/event-poller.js | 2 +- webpages/handlers/coffee/forge_rule.coffee | 222 ++++++++------- webpages/handlers/coffee/forge_webhook.coffee | 7 +- webpages/handlers/js/forge_rule.js | 257 ++++++++++-------- webpages/handlers/js/forge_webhook.js | 11 +- webpages/handlers/templates/forge_event.html | 4 +- webpages/handlers/templates/forge_rule.html | 6 +- webpages/public/style.css | 14 + 13 files changed, 316 insertions(+), 215 deletions(-) diff --git a/coffee/components-manager.coffee b/coffee/components-manager.coffee index 903f6e8..b6733bf 100644 --- a/coffee/components-manager.coffee +++ b/coffee/components-manager.coffee @@ -229,6 +229,7 @@ storeRule = ( user, oBody, callback ) => # This is how a rule is stored in the database rule = id: oBody.id + eventtype: oBody.eventtype eventname: oBody.eventname eventstart: oBody.eventstart eventinterval: oBody.eventinterval diff --git a/coffee/engine.coffee b/coffee/engine.coffee index 20b3698..441940f 100644 --- a/coffee/engine.coffee +++ b/coffee/engine.coffee @@ -273,7 +273,7 @@ processEvent = ( evt ) => fSearchAndInvokeAction node[arrPath[depth]], arrPath, funcName, evt, depth + 1 @log.info 'EN | Processing event: ' + evt.eventname - fCheckEventForUser = ( userName, oUser ) -> + fCheckEventForUser = ( userName, oUser ) => for ruleName, oMyRule of oUser ruleEvent = oMyRule.rule.eventname diff --git a/coffee/event-poller.coffee b/coffee/event-poller.coffee index a93663b..99b4ee5 100644 --- a/coffee/event-poller.coffee +++ b/coffee/event-poller.coffee @@ -81,7 +81,7 @@ fLoadModule = ( msg ) -> fAnonymous = () -> db.eventPollers.getModule msg.user, arrName[ 0 ], ( err, obj ) -> if not obj - log.warn "EP | Strange... no module retrieved: #{ arrName[0] }" + log.info "EP | No module retrieved for #{ arrName[0] }, must be a custom event or Webhook" else # we compile the module and pass: dynmod.compileString obj.data, # code diff --git a/js/components-manager.js b/js/components-manager.js index 06e53bd..c80ba04 100644 --- a/js/components-manager.js +++ b/js/components-manager.js @@ -321,6 +321,7 @@ Components Manager var args, arr, epModId, eventInfo, id, oFuncArgs, oParams, params, rule, strRule; rule = { id: oBody.id, + eventtype: oBody.eventtype, eventname: oBody.eventname, eventstart: oBody.eventstart, eventinterval: oBody.eventinterval, diff --git a/js/engine.js b/js/engine.js index 1b73ce2..390c65b 100644 --- a/js/engine.js +++ b/js/engine.js @@ -357,7 +357,7 @@ Engine ruleEvent += '_created:' + oMyRule.rule.timestamp; } if (evt.eventname === ruleEvent && validConditions(evt, oMyRule.rule, userName, ruleName)) { - this.log.info('EN | EVENT FIRED: ' + evt.eventname + ' for rule ' + ruleName); + _this.log.info('EN | EVENT FIRED: ' + evt.eventname + ' for rule ' + ruleName); _results.push((function() { var _i, _len, _ref, _results1; _ref = oMyRule.rule.actions; diff --git a/js/event-poller.js b/js/event-poller.js index ea44492..fdab541 100644 --- a/js/event-poller.js +++ b/js/event-poller.js @@ -88,7 +88,7 @@ Dynamic Modules fAnonymous = function() { return db.eventPollers.getModule(msg.user, arrName[0], function(err, obj) { if (!obj) { - return log.warn("EP | Strange... no module retrieved: " + arrName[0]); + return log.info("EP | No module retrieved for " + arrName[0] + ", must be a custom event or Webhook"); } else { return dynmod.compileString(obj.data, msg.user, msg.rule, arrName[0], obj.lang, "eventpoller", db.eventPollers, function(result) { var nd, now, oUser, start; diff --git a/webpages/handlers/coffee/forge_rule.coffee b/webpages/handlers/coffee/forge_rule.coffee index 64a0097..3fc686f 100644 --- a/webpages/handlers/coffee/forge_rule.coffee +++ b/webpages/handlers/coffee/forge_rule.coffee @@ -34,22 +34,34 @@ el = $( ' \"hh:mm\", default = 12:00" +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" -domInputInterval = $( '
' ).attr( 'class', 'indent20' ).html "Interval : - \"days hours:minutes\", default = 10 minutes" +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' -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 "

" +domSectionSelectedActions = $( '
' ) +domSectionSelectedActions.append $( '
' ).html "Selected Actions:" +domSectionSelectedActions.append $( '
' ).attr( 'id', 'selected_actions' ) +domSectionSelectedActions.hide() +domSectionActionParameters = $( '
' ) +domSectionActionParameters.append $( '
' ).html "

Required Parameters:

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

" +domSectionActionParameters.hide() + +fClearInfo = () -> + $( '#info' ).text '' + $( '#info' ).attr 'class', 'neutral' fDisplayError = ( msg ) -> window.scrollTo 0, 0 @@ -64,23 +76,22 @@ fFailedRequest = ( msg ) -> fDisplayError msg fIssueRequest = ( args ) -> - $( '#info' ).text '' + fClearInfo() $.post( '/usercommand', args.data ) .done args.done .fail args.fail # Convert a time string ( d h:m ) to a date fConvertTimeToDate = ( str ) -> - txtStart = $( '#input_start' ).val() dateConv = new Date() - if not txtStart + if not str dateConv.setHours 12 dateConv.setMinutes 0 else - arrInp = txtStart.split ':' + arrInp = str.split ':' # There's only one string entered: hour if arrInp.length is 1 - txtHr = txtStart + txtHr = str dateConv.setMinutes 0 else txtHr = arrInp[ 0 ] @@ -189,8 +200,7 @@ fPrepareEventType = ( eventtype ) -> else $( '#event_parameters' ).append domSelectEventPoller - $( '#event_parameters' ).append domInputStartTime - $( '#event_parameters' ).append domInputInterval + $( '#event_parameters' ).append domInputEventTiming.show() $( '#select_eventpoller option' ).remove() for id, events of oEps @@ -206,8 +216,6 @@ fPrepareEventType = ( eventtype ) -> # Fetch the required Event Poller parameters fFetchEventParams = ( name ) -> - console.log 'fetching event params' - console.log name $( '#event_poller_params *' ).remove() if name $( '#event_parameters' ).append domEventPollerParameters @@ -223,7 +231,6 @@ fFetchEventParams = ( name ) -> fDisplayEventParams = ( id ) -> ( data ) -> - console.log 'displaying event parameters' if data.message oParams = JSON.parse data.message table = $ '
' @@ -250,8 +257,6 @@ fFillEventParams = ( moduleId ) -> 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 () -> @@ -269,7 +274,6 @@ fFetchEventFunctionArgs = ( arrName ) -> 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 ] ] @@ -286,21 +290,18 @@ fFetchEventFunctionArgs = ( arrName ) -> td = $( '
' ).appendTo tr td.append $( '' ).attr 'type', 'text' tr.append td - fIssueRequest data: command: 'get_event_poller_user_arguments' body: JSON.stringify ruleId: $( '#input_id' ).val() - moduleId: moduleId - done: fAddEventUserArgs moduleId + moduleId: arrName[ 0 ] + done: fAddEventUserArgs arrName[ 1 ] 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 @@ -327,11 +328,7 @@ fAddSelectedAction = ( name ) -> td = $( '' ).attr( 'class', 'funcMappings').appendTo tr fFetchActionFunctionArgs td, arrName if arrName[ 0 ] not in arrEls - div = $( '
' ).appendTo $( '#action_invoker_params' ) - subdiv = $( '
').appendTo div - subdiv.append $( '
' ) - .attr( 'class', 'modName underlined' ).text arrName[ 0 ] - fFetchActionParams div, arrName[ 0 ] + fFetchActionParams arrName[ 0 ] $( "#select_actions option" ).each () -> if $( this ).text() is name $( this ).remove() @@ -339,7 +336,7 @@ fAddSelectedAction = ( name ) -> fFillActionFunction arrName[ 0 ] setTimeout fDelayed, 300 -fFetchActionParams = ( div, modName ) -> +fFetchActionParams = ( modName ) -> fIssueRequest data: command: 'get_action_invoker_params' @@ -348,20 +345,26 @@ fFetchActionParams = ( div, modName ) -> done: ( data ) -> if data.message oParams = JSON.parse data.message - table = $ '' - div.append table - fAppendActionParam = ( name, shielded ) -> - 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 - table.append tr - fAppendActionParam name, sh for name, sh of oParams + if JSON.stringify( oParams ) isnt '{}' + domSectionActionParameters.show() + div = $( '
' ).appendTo $( '#action_invoker_params' ) + subdiv = $( '
').appendTo div + subdiv.append $( '
' ) + .attr( 'class', 'modName underlined' ).text modName + table = $ '' + div.append table + for name, shielded of oParams + 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 + table.append tr + fail: fFailedRequest 'Error fetching action invoker params' fFetchActionFunctionArgs = ( tag, arrName ) -> @@ -450,6 +453,21 @@ fOnLoad = () -> editor.getSession().setMode "ace/mode/json" editor.setShowPrintMargin false + $( '#fill_example' ).click () -> + editor.setValue """ + + [ + { + "selector": ".nested_property", + "type": "string", + "operator": "<=", + "compare": "has this value" + } + ] + """ + + $( '#action_parameters' ).append domSectionSelectedActions + $( '#action_parameters' ).append domSectionActionParameters $( '#input_id' ).focus() @@ -503,32 +521,32 @@ fOnLoad = () -> $( '#select_actions' ).on 'change', () -> - $( '#action_parameters' ).append domTableSelectedActions + domSectionSelectedActions.show() opt = $ 'option:selected', this fAddSelectedAction opt.text() $( '#selected_actions' ).on 'click', 'img', () -> - console.log 'click' act = $( this ).closest( 'td' ).siblings( '.title' ).text() arrName = act.split ' -> ' - i = 0 nMods = 0 # Check whether we're the only function left that was selected from this module $( "#selected_actions td.title" ).each () -> - i++ arrNm = $( this ).text().split ' -> ' nMods++ if arrNm[ 0 ] is arrName[ 0 ] - # If we are the last selected action we detach the whole action parameter section - if i is 1 - $( '#action_parameters > *' ).detach() - if nMods is 1 $('#action_invoker_params > div').each () -> if $( this ).children( 'div.modName' ).text() is arrName[ 0 ] $( this ).remove() + # Hide if nothing to show + if $('#selected_actions td.title').length is 0 + domSectionSelectedActions.hide() + + if $('#action_invoker_params > div').length is 0 + domSectionActionParameters.hide() + opt = $( '