2013-05-22 05:03:34 +00:00
( function ( context , $ ) {
"use strict" ;
context . JK = context . JK || { } ;
context . JK . AddTrackDialog = function ( app , myTracks , sessionId , sessionModel ) {
var logger = context . JK . logger ;
var ASSIGNMENT = {
CHAT : - 2 ,
OUTPUT : - 1 ,
UNASSIGNED : 0 ,
TRACK1 : 1 ,
TRACK2 : 2
} ;
var instrument _array = [ ] ;
// dialog variables
2013-06-26 04:00:40 +00:00
// dialog variables
var inputUnassignedList = [ ] ;
2013-05-22 05:03:34 +00:00
var track2AudioInputChannels = [ ] ;
function events ( ) {
// Track 2 Add
2013-09-07 05:13:26 +00:00
$ ( '#img-add-track2-input-add' ) . unbind ( "click" ) ;
2013-05-22 05:03:34 +00:00
$ ( '#img-add-track2-input-add' ) . click ( function ( ) {
$ ( '#add-track2-unused > option:selected' ) . remove ( ) . appendTo ( '#add-track2-input' ) ;
} ) ;
// Track 2 Remove
2013-09-07 05:13:26 +00:00
$ ( '#img-add-track2-input-remove' ) . unbind ( "click" ) ;
2013-05-22 05:03:34 +00:00
$ ( '#img-add-track2-input-remove' ) . click ( function ( ) {
$ ( '#add-track2-input > option:selected' ) . remove ( ) . appendTo ( '#add-track2-unused' ) ;
} ) ;
2013-05-23 12:46:40 +00:00
$ ( '#btn-cancel-new-audio' ) . click ( context . JK . showOverlay ) ;
$ ( '#btn-error-ok' ) . click ( context . JK . showOverlay ) ;
2013-08-15 05:12:59 +00:00
// $('#btn-cancel-new-audio').click(function() {
// app.layout.closeDialog('add-new-audio-gear');
// });
// $('#btn-error-ok').click(function() {
// app.layout.closeDialog('error-dialog');
// });
2013-09-07 05:13:26 +00:00
$ ( '#btn-add-track' ) . unbind ( "click" ) ;
2013-05-22 05:03:34 +00:00
$ ( '#btn-add-track' ) . click ( saveSettings ) ;
}
function showDialog ( ) {
2013-05-23 12:46:40 +00:00
$ ( '#add-track2-unused' ) . empty ( ) ;
2013-05-22 05:03:34 +00:00
$ ( '#add-track2-input' ) . empty ( ) ;
$ ( '#add-track2-instrument' ) . empty ( ) ;
initDialogData ( ) ;
// load Unused Inputs
2013-06-26 04:15:00 +00:00
context . JK . loadOptions ( $ ( '#template-option' ) . html ( ) , $ ( '#add-track2-unused' ) , inputUnassignedList , "id" , "name" , - 1 ) ;
2013-05-22 05:03:34 +00:00
// load Track 2 Input(s)
2013-05-26 20:45:13 +00:00
context . JK . loadOptions ( $ ( '#template-option' ) . html ( ) , $ ( '#add-track2-input' ) , track2AudioInputChannels , "id" , "name" , - 1 ) ;
2013-05-22 05:03:34 +00:00
// load Track 2 Instrument
2013-05-22 11:48:37 +00:00
context . JK . loadOptions ( $ ( '#template-option' ) . html ( ) , $ ( '#add-track2-instrument' ) , instrument _array , "id" , "description" , - 1 ) ;
2013-05-22 05:03:34 +00:00
}
function initDialogData ( ) {
2013-06-26 04:00:40 +00:00
// set arrays
inputUnassignedList = _loadList ( ASSIGNMENT . UNASSIGNED , true , false ) ;
track2AudioInputChannels = _loadList ( ASSIGNMENT . TRACK2 , true , false ) ;
}
function _loadList ( assignment , input , chat ) {
var list = [ ] ;
2013-05-22 05:03:34 +00:00
// get data needed for listboxes
var channels = context . jamClient . TrackGetChannels ( ) ;
2013-06-26 04:00:40 +00:00
var musicDevices = context . jamClient . TrackGetMusicDeviceNames ( input ) ;
// SEE loadList function in TrackAssignGui.cpp of client code
2013-05-22 05:03:34 +00:00
$ . each ( channels , function ( index , val ) {
2013-06-26 04:00:40 +00:00
if ( input !== val . input ) {
return ;
}
var currAssignment = context . jamClient . TrackGetAssignment ( val . id , val . input ) ;
if ( assignment !== currAssignment ) {
return ;
}
logger . debug ( "channel id=" + val . id + ", channel input=" + val . input + ", channel assignment=" + currAssignment +
", channel name=" + val . name + ", channel type=" + val . device _type + ", chat=" + val . chat ) ;
var os = context . jamClient . GetOSAsString ( ) ;
2013-08-13 14:15:38 +00:00
if ( os === context . JK . OS . WIN32 ) {
2013-06-26 04:00:40 +00:00
if ( chat && ( $ . inArray ( val . device _id , musicDevices ) > - 1 || context . jamClient . TrackIsMusicDeviceType ( val . device _type ) ) ) {
return ;
}
}
else {
if ( chat && ( $ . inArray ( val . device _id , musicDevices ) > - 1 || ! context . jamClient . TrackIsMusicDeviceType ( val . device _type ) ) ) {
return ;
2013-05-22 05:03:34 +00:00
}
}
2013-06-26 04:00:40 +00:00
if ( ! chat && $ . inArray ( val . device _id , musicDevices ) === - 1 ) {
return ;
}
if ( ( chat && ! val . chat ) || ( ! chat && val . chat ) ) {
return ;
}
list . push ( val ) ;
2013-05-22 05:03:34 +00:00
} ) ;
2013-06-26 04:00:40 +00:00
return list ;
2013-05-22 05:03:34 +00:00
}
function saveSettings ( ) {
2013-11-03 20:55:55 +00:00
if ( ! context . JK . verifyNotRecordingForTrackChange ( app ) ) {
return ;
}
2013-05-22 11:48:37 +00:00
if ( ! validateSettings ( ) ) {
2013-05-22 05:03:34 +00:00
return ;
}
2013-05-22 11:48:37 +00:00
saveTrack ( ) ;
2013-11-16 04:35:40 +00:00
2013-08-15 05:12:59 +00:00
app . layout . closeDialog ( 'add-track' ) ;
2013-05-22 05:03:34 +00:00
}
2013-05-22 11:48:37 +00:00
function saveTrack ( ) {
2013-05-22 05:03:34 +00:00
// TRACK 2 INPUTS
2013-11-16 04:35:40 +00:00
var trackId = null ;
2013-05-22 05:03:34 +00:00
$ ( "#add-track2-input > option" ) . each ( function ( ) {
logger . debug ( "Saving track 2 input = " + this . value ) ;
2013-11-16 04:35:40 +00:00
trackId = this . value ;
2013-05-22 05:03:34 +00:00
context . jamClient . TrackSetAssignment ( this . value , true , ASSIGNMENT . TRACK2 ) ;
} ) ;
// TRACK 2 INSTRUMENT
2013-05-22 11:48:37 +00:00
var instrumentVal = $ ( '#add-track2-instrument' ) . val ( ) ;
var instrumentText = $ ( '#add-track2-instrument > option:selected' ) . text ( ) . toLowerCase ( ) ;
2013-05-22 05:03:34 +00:00
logger . debug ( "Saving track 2 instrument = " + instrumentVal ) ;
context . jamClient . TrackSetInstrument ( ASSIGNMENT . TRACK2 , instrumentVal ) ;
// UPDATE SERVER
2013-05-22 11:48:37 +00:00
logger . debug ( "Adding track with instrument " + instrumentText ) ;
var data = { } ;
2013-11-16 04:35:40 +00:00
context . jamClient . TrackSaveAssignments ( ) ;
/ * *
setTimeout ( function ( ) {
2015-07-15 15:04:45 +00:00
var inputTracks = context . JK . TrackHelpers . getTracks ( context . jamClient , 4 ) ;
2013-11-16 04:35:40 +00:00
// this is some ugly logic coming up, here's why:
// we need the id (guid) that the backend generated for the new track we just added
// to get it, we need to make sure 2 tracks come back, and then grab the track that
// is not the one we just added.
if ( inputTracks . length != 2 ) {
var msg = "because we just added a track, there should be 2 available, but we found: " + inputTracks . length ;
logger . error ( msg ) ;
alert ( msg ) ;
throw new Error ( msg ) ;
}
var client _track _id = null ;
$ . each ( inputTracks , function ( index , track ) {
console . log ( "track: %o, myTrack: %o" , track , myTracks [ 0 ] ) ;
if ( track . id != myTracks [ 0 ] . id ) {
client _track _id = track . id ;
return false ;
}
} ) ;
if ( client _track _id == null )
{
var msg = "unable to find matching backend track for id: " + this . value ;
logger . error ( msg ) ;
alert ( msg ) ;
throw new Error ( msg ) ;
}
// use the first track's connection_id (not sure why we need this on the track data model)
data . connection _id = myTracks [ 0 ] . connection _id ;
data . instrument _id = instrumentText ;
data . sound = "stereo" ;
data . client _track _id = client _track _id ;
sessionModel . addTrack ( sessionId , data ) ;
} , 1000 ) ;
* /
2013-05-22 05:03:34 +00:00
}
2013-05-22 11:48:37 +00:00
function validateSettings ( ) {
2013-05-22 05:03:34 +00:00
var isValid = true ;
var noTrackErrMsg = 'You must assign at least one input port to each of your tracks. Please update your settings to correct this. If you want to delete a track, please return to the session screen and delete the track by clicking the "x" box in the upper right-hand corner of the track.' ;
2013-05-26 19:07:33 +00:00
var noInstrumentErrMsg = 'You must specify what instrument is being played for this new track. Please update your settings to correct this.' ;
2013-05-22 05:03:34 +00:00
var errMsg ;
// verify Input and Instrument exist
if ( $ ( '#add-track2-input > option' ) . size ( ) === 0 || $ ( '#add-track2-input > option' ) . size ( ) > 2 ) {
errMsg = noTrackErrMsg ;
isValid = false ;
}
2013-05-22 11:48:37 +00:00
if ( isValid && $ ( '#add-track2-instrument > option:selected' ) . length === 0 ) {
2013-05-22 05:03:34 +00:00
errMsg = noInstrumentErrMsg ;
isValid = false ;
}
if ( ! isValid ) {
2013-08-15 05:12:59 +00:00
context . JK . showErrorDialog ( app , errMsg , "invalid settings" ) ;
2013-05-22 05:03:34 +00:00
}
return isValid ;
}
// TODO: repeated in configureTrack.js
function _init ( ) {
// load instrument array for populating listboxes, using client_id in instrument_map as ID
2014-03-03 22:13:23 +00:00
instrument _array = context . JK . listInstruments ( ) ;
2013-05-22 05:03:34 +00:00
}
this . initialize = function ( ) {
events ( ) ;
_init ( ) ;
} ;
this . showDialog = showDialog ;
return this ;
} ;
} ) ( window , jQuery ) ;