@ -29,7 +29,9 @@ define(function(require) {
default : 7 ,
max : 31
} ,
faxboxes : { }
faxboxes : { } ,
allnumbers : [ ] , // list of all numbers in the account
faxboxnumbers : { } // { "somefaxboxid": [array of DIDs that go to this faxbox] }
} ,
initApp : function ( callback ) {
@ -46,7 +48,9 @@ define(function(require) {
self . getFaxData ( function ( results ) {
self . appFlags . faxboxes = _ . keyBy ( results . faxboxes , 'id' ) ;
console . log ( _ . size ( self . appFlags . faxboxes ) ) ;
//console.log(_.size(self.appFlags.faxboxes));
self . appFlags . faxboxnumbers = results . faxboxnumbers ;
var menus = [
{
tabs : [
@ -90,6 +94,11 @@ console.log(_.size(self.appFlags.faxboxes));
callback ( null , faxboxes ) ;
} ) ;
} ,
faxboxnumbers : function ( callback ) {
self . getFaxboxNumbers ( function ( faxboxnumbers ) {
callback ( null , faxboxnumbers ) ;
} ) ;
} ,
storage : function ( callback ) {
self . getStorage ( function ( storage ) {
callback ( null , storage ) ;
@ -182,6 +191,9 @@ console.log(_.size(self.appFlags.faxboxes));
template . find ( '.select-faxbox' ) . val ( faxboxId ) . trigger ( 'chosen:updated' ) ;
//load send from phone numbers belonging to this faxbox
self . loadNumberChoices ( template , self . appFlags . faxboxnumbers [ faxboxId ] ) ;
self . maybeShowAllNumbersOption ( template ) ;
self . displayListFaxes ( type , template , faxboxId ) ;
} ) ;
@ -315,6 +327,39 @@ console.log(_.size(self.appFlags.faxboxes));
cb . prop ( 'checked' , ! cb . prop ( 'checked' ) ) ;
afterSelect ( ) ;
} ) ;
template . on ( 'change' , '#sendfax_uploaded_file' , function ( ) {
$ ( '.outbound-hidden' ) . show ( ) ;
$ ( '.outbound-expand' ) . hide ( ) ;
if ( $ ( this ) . val ( ) ) { // Check if a file is selected
$ ( '.send-fax-button' ) . prop ( 'disabled' , false ) ;
} else {
$ ( '.send-fax-button' ) . prop ( 'disabled' , true ) ;
}
} ) ;
template . on ( 'click' , '.outbound-expand' , function ( ) {
$ ( '.outbound-hidden' ) . slideDown ( ) ;
$ ( '.outbound-expand' ) . hide ( ) ;
} ) ;
template . on ( 'click' , '.outbound-contract' , function ( ) {
$ ( '.outbound-hidden' ) . slideUp ( ) ;
$ ( '.outbound-contract' ) . hide ( ) ;
$ ( '.outbound-expand' ) . show ( ) ;
} ) ;
template . on ( 'click' , '.show-all-numbers' , function ( ) {
self . loadNumberChoices ( template , self . appFlags . allnumbers ) ; //load all numbers
$ ( '.show-all-numbers' ) . fadeOut ( 2000 ) ;
monster . ui . toast ( { type : "info" , message : self . i18n . active ( ) . fax . outbound . allNumbers } ) ;
$ ( '#from_number_header' ) . text ( self . i18n . active ( ) . fax . outbound . fromNumberAll ) ;
} ) ;
template . on ( 'click' , '.send-fax-button' , function ( e ) {
e . preventDefault ( ) ;
self . sendFaxUpload ( template ) ;
} ) ;
} ,
displayListFaxes : function ( type , container , faxboxId ) {
@ -734,8 +779,173 @@ console.log(_.size(self.appFlags.faxboxes));
callback && callback ( data . data ) ;
}
} ) ;
}
} ;
} ,
loadNumberChoices : function ( template , numbers ) {
var self = this ;
var $phoneNumberSelect = template . find ( '#sendfax_from_number' ) ;
if ( $phoneNumberSelect ) {
$phoneNumberSelect . empty ( ) ;
if ( numbers && numbers . length > 1 ) {
$phoneNumberSelect . append ( $ ( '<option>' , { value : "none" , text : self . i18n . active ( ) . fax . outbound . selectFromNumber } ) ) ;
}
_ . each ( numbers , function ( number ) {
$phoneNumberSelect . append ( $ ( '<option>' , { value : number , text : number } ) ) ;
} ) ;
}
} ,
maybeShowAllNumbersOption : function ( template ) {
var self = this ;
$ ( '#from_number_header' ) . text ( self . i18n . active ( ) . fax . outbound . fromNumber ) ;
if ( monster . util . isAdmin ( ) ) {
$ ( '.show-all-numbers' ) . show ( ) ;
}
} ,
sendFaxUpload : function ( template ) {
var self = this ;
var toNumber = template . find ( '#sendfax_to_number' ) . val ( ) ;
var fromNumber = template . find ( '#sendfax_from_number' ) . val ( ) ;
var file = template . find ( '#sendfax_uploaded_file' ) [ 0 ] . files [ 0 ] ;
if ( ! file ) return monster . ui . alert ( self . i18n . active ( ) . fax . outbound . missingFile ) ;
if ( ! toNumber ) return monster . ui . alert ( self . i18n . active ( ) . fax . outbound . missingTo ) ;
if ( ! fromNumber || fromNumber == 'none' ) return monster . ui . alert ( self . i18n . active ( ) . fax . outbound . missingFrom ) ;
var selected_faxbox = template . find ( '.select-faxbox' ) . val ( ) ;
//create json blob to use in form data
var jsonData = JSON . stringify ( { data : { from_number : fromNumber , to_number : toNumber , faxbox_id : selected_faxbox } } ) ;
var jsonBlob = new Blob ( [ jsonData ] , { type : 'application/json' } ) ;
var form = new FormData ( ) ;
form . set ( "json" , jsonBlob ) ;
form . set ( 'file' , file , file . name ) ;
$ . ajax ( {
url : monster . config . api . default + 'accounts/' + self . accountId + '/faxes' ,
method : 'put' ,
processData : false ,
contentType : false ,
headers : { 'X-Auth-Token' : monster . util . getAuthToken ( ) } ,
data : form ,
success : function ( res ) {
monster . ui . toast ( { type : "success" , message : self . i18n . active ( ) . fax . outbound . success } ) ;
console . log ( res . data ) ;
template . find ( '#sendfax_uploaded_file' ) . val ( null ) ;
} ,
error : function ( err ) {
console . log ( err ) ;
monster . ui . alert ( '<h3>Error</h3><p><pre>' + err . responseText + '</pre></p>' ) ;
}
} ) ;
} ,
getCallflows : function ( callback ) {
var self = this ;
self . callApi ( {
resource : 'callflow.list' ,
data : {
accountId : self . accountId ,
filters : { paginate : false }
} ,
error : function ( err ) {
console . log ( err ) ;
} ,
success : function ( res ) {
callback && callback ( res . data ) ;
}
} ) ;
} ,
getCallflow : function ( id , callback ) {
var self = this ;
self . callApi ( {
resource : 'callflow.get' ,
data : {
accountId : self . accountId ,
callflowId : id
} ,
error : function ( err ) {
console . log ( err ) ;
} ,
success : function ( res ) {
callback && callback ( res . data ) ;
}
} ) ;
} ,
getNumbers : function ( callback ) {
var self = this ;
if ( self . appFlags . allnumbers . length > 0 ) {
return callback && callback ( self . appFlags . allnumbers ) ; //sortof cache :)
}
self . callApi ( {
resource : 'numbers.list' ,
data : {
accountId : self . accountId ,
filters : { paginate : false }
} ,
error : function ( err ) {
console . log ( err ) ;
} ,
success : function ( res ) {
var numbers = _ . keys ( res . data . numbers ) ;
self . appFlags . allnumbers = numbers ;
callback && callback ( numbers ) ;
}
} ) ;
} ,
getFaxboxNumbers : function ( callback ) {
var self = this ,
faxboxNumbers = { } ;
var findFaxboxId = function ( flow ) {
if ( flow . module == 'faxbox' ) {
return flow . data . id ;
} else if ( flow . children . length > 0 ) {
findFaxBoxId ( flow . children [ "_" ] ) ;
}
} ;
self . getNumbers ( function ( numbers ) {
self . getCallflows ( function ( callflows ) { //get all callflows
let done = new Promise ( ( resolve , reject ) => { //let these finish before proceeding
if ( callflows . length == 0 ) resolve ( ) ;
var faxCallflows = _ . filter ( callflows , function ( cf ) { //ignore non-faxbox callflows
return cf . modules . includes ( 'faxbox' ) ;
} ) ;
if ( faxCallflows . length == 0 ) resolve ( ) ;
_ . each ( faxCallflows , function ( cf , idx ) {
self . getCallflow ( cf . id , function ( callflow ) { //this callflow has faxbox, so get it for details
var fbid = findFaxboxId ( callflow . flow ) ; //search for the faxbox ID
if ( fbid ) faxboxNumbers [ fbid ] = [ ] ;
_ . each ( cf . numbers , function ( cfnum ) { //check each callflow number to see if it's a real DID
if ( numbers . includes ( cfnum ) ) {
faxboxNumbers [ fbid ] . push ( cfnum ) ; //add the number (if it's a DID) to the list for this faxbox ID
}
} ) ;
if ( faxboxNumbers [ fbid ] && faxboxNumbers [ fbid ] . length == 0 ) { //this faxbox has no DIDs routing to it
console . log ( 'Warning: faxbox ' + fbid + ' has no DIDs routing to it!' ) ;
}
if ( ( idx + 1 ) == faxCallflows . length ) resolve ( ) ; //processed all callflows so we're done
} ) ;
} ) ;
} ) ;
done . then ( ( ) => {
callback && callback ( faxboxNumbers ) ;
} ) ;
} ) ;
} ) ;
} //getFaxboxNumbers()
} ; //app
return app ;
} ) ;