2014-04-24 15:34:00 +00:00
// Generated by CoffeeScript 1.7.1
( function ( ) {
2014-04-26 00:16:06 +00:00
var arrKV , arrParams , fDisplayError , fFailedRequest , fIssueRequest , fOnLoad , fProcessWebhookList , fUpdateWebhookList , hostUrl , oParams , param , _i , _len ;
2014-04-24 15:34:00 +00:00
2014-04-25 13:49:55 +00:00
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 ) ;
}
2014-04-26 00:16:06 +00:00
hostUrl = [ location . protocol , '//' , location . host ] . join ( '' ) ;
2014-04-25 13:49:55 +00:00
fDisplayError = function ( msg ) {
window . scrollTo ( 0 , 0 ) ;
$ ( '#info' ) . text ( "Error: " + msg ) ;
return $ ( '#info' ) . attr ( 'class' , 'error' ) ;
} ;
fIssueRequest = function ( args ) {
$ ( '#info' ) . text ( '' ) ;
return $ . post ( '/usercommand' , args . body ) . done ( args . done ) . fail ( args . fail ) ;
} ;
fFailedRequest = function ( msg ) {
return function ( err ) {
if ( err . status === 401 ) {
return window . location . href = 'forge?page=forge_rule' ;
} else {
return fDisplayError ( msg ) ;
}
} ;
} ;
2014-04-26 00:16:06 +00:00
fUpdateWebhookList = function ( ) {
return fIssueRequest ( {
body : {
command : 'get_all_webhooks'
} ,
done : fProcessWebhookList ,
fail : fFailedRequest ( 'Unable to get Webhook list' )
} ) ;
} ;
fProcessWebhookList = function ( data ) {
var hookid , hookname , img , oHooks , tdName , tdUrl , tr , _results ;
2014-04-26 14:01:33 +00:00
$ ( '#table_webhooks *' ) . remove ( ) ;
if ( data . message ) {
oHooks = JSON . parse ( data . message ) ;
$ ( '#table_webhooks' ) . append ( $ ( '<h3>' ) . text ( 'Your existing Webhooks:' ) ) ;
_results = [ ] ;
for ( hookid in oHooks ) {
hookname = oHooks [ hookid ] ;
tr = $ ( '<tr>' ) ;
tdName = $ ( '<div>' ) . text ( hookname ) ;
tdUrl = $ ( '<input>' ) . attr ( 'style' , 'width:600px' ) . val ( "" + hostUrl + "/webhooks/" + hookid ) ;
img = $ ( '<img>' ) . attr ( 'class' , 'del' ) . attr ( 'title' , 'Delete Module' ) . attr ( 'src' , 'red_cross_small.png' ) ;
tr . append ( $ ( '<td>' ) . append ( img ) ) ;
tr . append ( $ ( '<td>' ) . attr ( 'style' , 'padding-left:10px' ) . append ( tdName ) ) ;
tr . append ( $ ( '<td>' ) . attr ( 'style' , 'padding-left:10px' ) . append ( tdUrl ) ) ;
_results . push ( $ ( '#table_webhooks' ) . append ( tr ) ) ;
}
return _results ;
2014-04-25 13:49:55 +00:00
}
} ;
fOnLoad = function ( ) {
document . title = 'Create Webhooks!' ;
2014-04-26 00:16:06 +00:00
fUpdateWebhookList ( ) ;
$ ( '#but_submit' ) . click ( function ( ) {
2014-04-25 13:49:55 +00:00
var hookname ;
$ ( '#info' ) . text ( '' ) ;
hookname = $ ( '#inp_hookname' ) . val ( ) ;
if ( hookname === '' ) {
2014-04-26 00:16:06 +00:00
return fDisplayError ( 'Please provide an Event Name for your new Webhook!' ) ;
2014-04-25 13:49:55 +00:00
} else {
return fIssueRequest ( {
body : {
command : 'create_webhook' ,
body : JSON . stringify ( {
hookname : hookname
} )
} ,
done : function ( data ) {
2014-04-26 00:16:06 +00:00
var b , div , inp , oAnsw ;
2014-04-25 13:49:55 +00:00
oAnsw = JSON . parse ( data . message ) ;
2014-04-26 14:01:33 +00:00
b = $ ( '<b>' ) . text ( "This is the Webhook Url you can use for your Event '" + oAnsw . hookname + "' : " ) ;
2014-04-26 00:16:06 +00:00
$ ( '#display_hookurl' ) . append ( b ) ;
$ ( '#display_hookurl' ) . append ( $ ( '<br>' ) ) ;
inp = $ ( '<input>' ) . attr ( 'type' , 'text' ) . attr ( 'style' , 'width:600px' ) . val ( "" + hostUrl + "/webhooks/" + oAnsw . hookid ) ;
$ ( '#display_hookurl' ) . append ( inp ) ;
$ ( '#display_hookurl' ) . append ( $ ( '<br>' ) ) ;
div = $ ( '<div>' ) ;
div . append ( $ ( '<br>' ) ) ;
div . append ( $ ( '<div>' ) . html ( "1. Try it out and push your location to your new webhook via <a target=\"_blank\" href=\"" + hostUrl + "/mobile.html?hookid=" + oAnsw . hookid + "\">this page</a>." ) ) ;
div . append ( $ ( '<br>' ) ) ;
div . append ( $ ( '<div>' ) . html ( "2. Then you should setup <a target=\"_blank\" href=\"forge?page=forge_rule&eventtype=webhook&hookname=" + hookname + "\">a Rule for this Event!</a>" ) ) ;
2014-04-26 14:01:33 +00:00
$ ( '#display_hookurl' ) . append ( div ) ;
return fUpdateWebhookList ( ) ;
2014-04-25 13:49:55 +00:00
} ,
fail : function ( err ) {
if ( err . status === 409 ) {
2014-04-26 14:01:33 +00:00
fFailedRequest ( 'Webhook Event Name already existing!' ) ( err ) ;
2014-04-25 13:49:55 +00:00
} else {
2014-04-26 14:01:33 +00:00
fFailedRequest ( 'Unable to create Webhook! ' + err . message ) ( err ) ;
2014-04-25 13:49:55 +00:00
}
2014-04-26 14:01:33 +00:00
return fUpdateWebhookList ( ) ;
2014-04-25 13:49:55 +00:00
}
} ) ;
}
} ) ;
2014-04-26 00:16:06 +00:00
return $ ( '#table_webhooks' ) . on ( 'click' , 'img' , function ( ) {
var arrUrl , url ;
if ( confirm ( "Do you really want to delete this webhook?" ) ) {
url = $ ( 'input' , $ ( this ) . closest ( 'tr' ) ) . val ( ) ;
arrUrl = url . split ( '/' ) ;
return fIssueRequest ( {
body : {
command : 'delete_webhook' ,
body : JSON . stringify ( {
hookid : arrUrl [ arrUrl . length - 1 ]
} )
} ,
done : function ( data ) {
$ ( '#info' ) . text ( data . message ) ;
$ ( '#info' ) . attr ( 'class' , 'success' ) ;
return fUpdateWebhookList ( ) ;
} ,
fail : function ( err ) {
fFailedRequest ( 'Unable to delete Webhook!' ) ( err ) ;
return fUpdateWebhookList ( ) ;
}
} ) ;
}
} ) ;
2014-04-25 13:49:55 +00:00
} ;
window . addEventListener ( 'load' , fOnLoad , true ) ;
2014-04-24 15:34:00 +00:00
} ) . call ( this ) ;