2016-05-25 20:26:45 +00:00
$ = jQuery
context = window
logger = context . JK . logger
rest = context . JK . Rest ( )
AttachmentActions = @ AttachmentActions
@AttachmentStore = Reflux . createStore (
{
listenables: AttachmentActions
lessonId: null
uploading: false
init: ->
# Register with the app store to get @app
this . listenTo ( context . AppStore , this . onAppInit )
onAppInit: (@app) ->
@ui = new context . JK . UIHelper ( @ app ) ;
recordingsSelected: (recordings) ->
logger . debug ( " recording selected " , recordings )
2018-01-16 20:27:20 +00:00
if @ lessonId
options = { id: @ lessonId }
options.recordings = recordings
rest . attachRecordingToLesson ( options ) . done ( (response) => @ attachedRecordingsToLesson ( response ) ) . fail ( (jqXHR) => @ attachedRecordingsFail ( jqXHR ) )
else if @ sessionId
options = { id: @ sessionId }
options.recordings = recordings
rest . attachRecordingToSession ( options ) . done ( (response) => @ attachedRecordingsToSession ( response ) ) . fail ( (jqXHR) => @ attachedRecordingsFail ( jqXHR ) )
2016-05-26 21:25:51 +00:00
attachedRecordingsToLesson: (response) ->
context . JK . Banner . showNotice ( ' Recording Attached ' , ' Your recording has been associated with this lesson, and can be accessed from the Messages window for this lesson. ' )
2018-01-16 20:27:20 +00:00
attachedRecordingsToSession: (response) ->
context . JK . Banner . showNotice ( ' Recording Attached ' , ' Your recording has been associated with this session. ' )
2016-05-26 21:25:51 +00:00
attachedRecordingsFail: (jqXHR) ->
@ app . ajaxError ( jqXHR )
2018-01-16 20:27:20 +00:00
onStartAttachRecording: (lessonId, sessionId = null) ->
2016-05-26 21:25:51 +00:00
if @ uploading
2016-05-25 20:26:45 +00:00
logger . warn ( " rejecting startAttachRecording attempt as currently busy " )
return
@lessonId = lessonId
2018-01-16 20:27:20 +00:00
@sessionId = sessionId
2016-05-25 20:26:45 +00:00
@ ui . launchRecordingSelectorDialog ( [ ] , (recordings) =>
@ recordingsSelected ( recordings )
)
2016-05-26 21:25:51 +00:00
@ changed ( )
2016-05-25 20:26:45 +00:00
2018-01-16 20:27:20 +00:00
onStartAttachNotation: (lessonId, sessionId = null) ->
2016-05-26 21:25:51 +00:00
if @ uploading
2016-05-25 20:26:45 +00:00
logger . warn ( " rejecting onStartAttachNotation attempt as currently busy " )
return
@lessonId = lessonId
2018-01-16 20:27:20 +00:00
@sessionId = sessionId
2016-05-25 20:26:45 +00:00
2016-05-26 21:25:51 +00:00
logger . debug ( " notation upload started for lesson: " + lessonId )
2016-05-25 20:26:45 +00:00
@ triggerNotation ( )
2016-05-26 21:25:51 +00:00
@ changed ( )
2016-05-25 20:26:45 +00:00
2018-01-16 20:27:20 +00:00
onStartAttachAudio: (lessonId, sessionId = null) ->
2016-05-26 21:25:51 +00:00
if @ uploading
2016-05-25 20:26:45 +00:00
logger . warn ( " rejecting onStartAttachAudio attempt as currently busy " )
return
@lessonId = lessonId
2018-01-16 20:27:20 +00:00
@sessionId = sessionId
2016-05-25 20:26:45 +00:00
2016-05-26 21:25:51 +00:00
logger . debug ( " audio upload started for lesson: " + lessonId )
2016-05-25 20:26:45 +00:00
@ triggerAudio ( )
@ changed ( )
triggerNotation: () ->
if ! @ attachNotationBtn ?
@attachNotationBtn = $ ( ' input.attachment-notation ' ) . eq ( 0 )
2017-12-22 23:54:31 +00:00
console . log ( " @attachNotationBtn " , @ attachNotationBtn )
2016-05-25 20:26:45 +00:00
@ attachNotationBtn . trigger ( ' click ' )
triggerAudio: () ->
if ! @ attachAudioBtn ?
@attachAudioBtn = $ ( ' input.attachment-audio ' ) . eq ( 0 )
2017-12-22 23:54:31 +00:00
console . log ( " @@attachAudioBtn " , @ attachAudioBtn )
2016-05-25 20:26:45 +00:00
@ attachAudioBtn . trigger ( ' click ' )
onUploadNotations: (notations, doneCallback, failCallback) ->
2016-05-26 21:25:51 +00:00
logger . debug ( " beginning upload of notations " , notations )
2016-05-25 20:26:45 +00:00
@uploading = true
@ changed ( )
formData = new FormData ( )
maxExceeded = false ;
$ . each ( notations , (i, file) => (
max = 10 * 1024 * 1024 ;
if file . size > max
maxExceeded = true
return false
formData . append ( ' files[] ' , file )
) )
if maxExceeded
@ app . notify ( {
title: " Maximum Music Notation Size Exceeded " ,
text: " You can only upload files up to 10 megabytes in size. "
} )
failCallback ( )
@uploading = false
@ changed ( )
return
2018-01-16 20:27:20 +00:00
if @ lessonId
formData . append ( ' lesson_session_id ' , @ lessonId )
else if @ sessionId
formData . append ( ' session_id ' , @ sessionId )
2016-05-26 21:25:51 +00:00
formData . append ( ' attachment_type ' , ' notation ' )
2016-05-25 20:26:45 +00:00
2016-05-30 05:32:55 +00:00
@ app . layout . showDialog ( ' music-notation-upload-dialog ' )
2016-05-25 20:26:45 +00:00
rest . uploadMusicNotations ( formData )
2016-05-26 21:25:51 +00:00
. done ( (response) => @ doneUploadingNotatations ( notations , response , doneCallback , failCallback ) )
. fail ( (jqXHR) => @ failUploadingNotations ( jqXHR , failCallback ) )
2016-05-25 20:26:45 +00:00
2016-05-26 21:25:51 +00:00
doneUploadingNotatations: (notations, response, doneCallback, failCallback) ->
@uploading = false
@ changed ( )
2016-05-25 20:26:45 +00:00
error_files = [ ] ;
$ . each ( response , (i, music_notation) => (
if music_notation . errors
error_files . push ( notations [ i ] . name )
)
)
if error_files . length > 0
failCallback ( )
@ app . notifyAlert ( " Failed to upload notations. " , error_files . join ( ' , ' ) ) ;
else
doneCallback ( )
2016-05-26 21:25:51 +00:00
failUploadingNotations: (jqXHR, failCallback) ->
@uploading = false
@ changed ( )
2016-05-25 20:26:45 +00:00
if jqXHR . status == 413
# the file is too big. Let the user know.
# This should happen when they select the file, but a misconfiguration on the server could cause this.
@ app . notify ( {
title: " Maximum Music Notation Size Exceeded " ,
text: " You can only upload files up to 10 megabytes in size. "
} )
else
@ app . notifyServerError ( jqXHR , " Unable to upload music notations " ) ;
2016-05-30 05:32:55 +00:00
onUploadAudios: (notations, doneCallback, failCallback) ->
logger . debug ( " beginning upload of audio " , notations )
@uploading = true
@ changed ( )
formData = new FormData ( )
maxExceeded = false ;
$ . each ( notations , (i, file) => (
max = 10 * 1024 * 1024 ;
if file . size > max
maxExceeded = true
return false
formData . append ( ' files[] ' , file )
) )
if maxExceeded
@ app . notify ( {
title: " Maximum Music Audio Size Exceeded " ,
text: " You can only upload files up to 10 megabytes in size. "
} )
failCallback ( )
@uploading = false
@ changed ( )
return
2018-01-16 20:27:20 +00:00
if @ lessonId
formData . append ( ' lesson_session_id ' , @ lessonId )
else if @ sessionId
formData . append ( ' session_id ' , @ sessionId )
2016-05-30 05:32:55 +00:00
formData . append ( ' attachment_type ' , ' audio ' )
@ app . layout . showDialog ( ' music-notation-upload-dialog ' )
rest . uploadMusicNotations ( formData )
. done ( (response) => @ doneUploadingAudios ( notations , response , doneCallback , failCallback ) )
. fail ( (jqXHR) => @ failUploadingAudios ( jqXHR , failCallback ) )
doneUploadingAudios: (notations, response, doneCallback, failCallback) ->
@uploading = false
@ changed ( )
error_files = [ ] ;
$ . each ( response , (i, music_notation) => (
if music_notation . errors
error_files . push ( notations [ i ] . name )
)
)
if error_files . length > 0
failCallback ( )
@ app . notifyAlert ( " Failed to upload audio files. " , error_files . join ( ' , ' ) ) ;
else
doneCallback ( )
failUploadingAudios: (jqXHR, failCallback) ->
@uploading = false
@ changed ( )
if jqXHR . status == 413
# the file is too big. Let the user know.
# This should happen when they select the file, but a misconfiguration on the server could cause this.
@ app . notify ( {
title: " Maximum Music Audio Size Exceeded " ,
text: " You can only upload files up to 10 megabytes in size. "
} )
else
@ app . notifyServerError ( jqXHR , " Unable to upload music audio files " ) ;
2016-05-25 20:26:45 +00:00
changed: () ->
this . trigger ( { lessonId: @ lessonId , uploading: @ uploading } )
}
)