Created
November 7, 2011 18:40
-
-
Save odats/1345775 to your computer and use it in GitHub Desktop.
recorder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var recorderWidget = new RecorderWidget({closeCallback:function(){alert("close me")}}); | |
showRecordDialog(recorderWidget.render().el); | |
RecorderView = Backbone.View.extend({ | |
isRecordReady = false, | |
recordType, | |
// parent dialog holder | |
// recordDialog.dialog('close') | |
closeCallback, | |
template: hubbub.templates.recorder, // simple <div id="recorder" class="recorder"></div> | |
//components: | |
navigation, shareToList, | |
//audio | |
audioUploader, audioRecorder, | |
//attachments | |
description, noiseReductionSelector, geoLocationSelector, geoLocationTitle, attachedImage, | |
// share buttons | |
cancel, saveToDrafts, tellEveryone, tellFriend, | |
initialize: function() { | |
navigation = new Recorder.Navigation({el:"#record-type-tabs"}); | |
navigation.bind('publicSelected', this.onNavPublicSelected, this); | |
navigation.bind('onPrivateMessage', this.onNavPrivateMessageSelected, this); | |
audioUploader = new Recorder.AudioUploader(); | |
audioUploader.bind('sourceReady', this.onAudioUploaderSourceReady, this); | |
audioUploader.bind('submitted', this.onRecordSubmitted, this); | |
// init share buttons | |
cancel = new Recorder.CancelButton(); | |
cancel.bind('clicked', this.onCancel, this); | |
saveToDrafts = new Recorder.ShareButtons.SaveToDrafts({action:"save-to-drafts"}); | |
saveToDrafts.bind('clicked', this.startSubmit, this); | |
tellEveryone = new Recorder.ShareButtons.TellEveryone({action:"tell-everyone"}); | |
tellEveryone.bind('clicked', this.startSubmit, this); | |
tellFriend = new Recorder.ShareButtons.TellFriend({action:"tell-friend"}); | |
tellEveryone.bind('clicked', this.startSubmit, this); | |
shareToList = new Recorder.ShareToList(); | |
shareToList.bind('addedToList', this.onShareToListChanged, this); | |
shareToList.bind('removedFromList', this.onShareToListChanged, this); | |
// geo location | |
geoLocationSelector = new Recorder.GeoLocationSelector(); | |
geoLocationSelector.bind('selectedValue', this.onGeoLocationSelected, this); | |
geoLocationTitle = new Recorder.geoLocationTitle(); | |
geoLocationTitle.bind('closed', this.onGeoLocationCancelled, this); | |
// attached image | |
attachedImage = new Recorder.AttachedImage(); | |
attachedImage.bind('startUploading', this.onAttachedImageFileUploading, this); // disable share buttons to prevent issues | |
attachedImage.bind('finishedUploading', this.onAttachedImageFileReady, this); // enable share buttons | |
description = new Recorder.Description(); | |
noiseReductionSelector = new Recorder.NoiseReductionSelector(); | |
}, | |
render: function() { | |
// render holder | |
$(this.el).html(this.template()); | |
// render components | |
this.navigation.render(); | |
// geo location | |
this.geoLocationSelector.render(); | |
this.geoLocationSelector.show(); | |
this.geoLocationTitle.render(); // render only holder | |
// render share buttons | |
// button is hidden by default and disabled | |
$("#shareButtons").append(cancel.render()); | |
$("#shareButtons").append(saveToDrafts.render()); | |
saveToDrafts.show(); | |
$("#shareButtons").append(tellEveryone.render()); | |
tellEveryone.show(); | |
$("#shareButtons").append(tellFriend.render()); | |
return this; | |
}, | |
remove: function() { | |
$(this.el).remove(); | |
// unbind all events | |
}, | |
close: function() { | |
closeCallback(); | |
}, | |
validate: function() { | |
if(audioUploader.getValue().ext() != 'wav' && audioUploader.getValue().ext() != 'mp3') { | |
return false; | |
} | |
return true; | |
}, | |
startSubmit: function(shareType){ | |
//validate | |
if(!this.validate()){ | |
return false; | |
} | |
// block UI | |
blockScreen(); | |
$(this.el).find("[name=shareType]").val(shareType); | |
if(recordType == "record"){ | |
// flesh submit | |
audioRecorder.submit(); | |
} else if(recordType == "upload"){ | |
// upload submit | |
audioUploader.submit(); | |
} | |
}, | |
// called after audio was successfully submitted on server | |
onRecordSubmitted: function(hubbubId) { | |
// inject hubbub id hidden field | |
$(this.el).find("[name=audioId]").val(hubbubId); | |
// set geolocation | |
if(this.geoLocationSelector.hasCoordinates){ | |
$(this.el).find("[name=geoLocation]").val(this.geoLocationSelector.getCoordinates()); //TODO: post coordinates | |
} | |
// set attached image file id | |
if(this.attachedImage.hasFile){ | |
$(this.el).find("[name=attachedImage]").val(this.attachedImage.getFile()); | |
} | |
self = this. | |
// submit form | |
$.post('api/audio/update', $("#audio-record-form").serialize()) | |
.success(function() { | |
self.close(); | |
}) | |
.error(function() { | |
self.unBlockScreen(); | |
alert("error"); | |
}); | |
}, | |
// methods | |
setUploadStrategy: function(){ | |
recordType = "upload"; | |
audioRecorder.block(); | |
}, | |
setRecordStrategy: function(){ | |
recordType = "record"; | |
audioUploader.block(); | |
}, | |
// event handlers | |
// navigation | |
onNavPublicSelected: function() { | |
shareToList.hide(); // also clears seleted users | |
tellEveryone.show(); | |
tellFriend.hide(); | |
}, | |
onNavPrivateMessageSelected: function() { | |
shareToList.show(); | |
tellEveryone.hide(); | |
tellFriend.show(); | |
}, | |
onAudioUploaderSourceReady: function(){ | |
this.setUploadStrategy(); | |
}, | |
onCancelClick: function(){ | |
this.close(); | |
}, | |
onShareToListChanged: function(){ | |
if(this.shareToList.getCountOfSelected() == 0){ | |
this.tellFriend.disable(); | |
} | |
}, | |
// user has specified geo location | |
onGeoLocationSelected: function(e){ | |
this.geoLocationTitle.setTitle(e.title); //TODO: how to get the name of the street | |
this.geoLocationSelector.hide(); | |
}, | |
// user canceled selected geolocation | |
onGeoLocationCancelled: function(){ | |
this.geoLocationTitle.close(); | |
this.geoLocationSelector.refresh(); | |
} | |
}); | |
Navigation | |
events: | |
onPublicSelected | |
onPrivateMessageSelected | |
AudioUploader | |
events: | |
onSourceReady | |
onCancelled | |
onSubmitted(e) //id | |
methods: | |
getValue // return file name | |
submit(url) | |
enable | |
disable | |
AudioRecorder | |
events: | |
onInProcess | |
onSourceReady | |
onCancelled | |
onSubmitted(e) //id | |
methods: | |
submit | |
enable | |
disable | |
ShareButton | |
events: | |
onClick | |
methods: | |
enable | |
disable | |
show | |
hide | |
Description | |
methods: | |
getValue | |
NoiseReductionSelector | |
methods: | |
getValue | |
ShareToList | |
events: | |
onAdded | |
onRemoved | |
methods: | |
getCountOfSelected | |
getSelected | |
AttachedImage | |
events: | |
onFileUploading | |
onFileReady // if response come from server | |
methods: | |
hasFile | |
getFileId | |
GeoLocationTitle | |
events: | |
onClose | |
methods: | |
close | |
setTitle | |
GeoLocationSelector { | |
events: | |
onSelected(e) //longtitude, lattitude, place | |
methods: | |
refresh // clean selected value and show button | |
show | |
hide | |
hasCoordinates | |
getCoordinates //longtitude, lattitude, place | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment