jam-cloud/web/app/assets/javascripts/web/recordings.js

160 lines
5.3 KiB
JavaScript

(function(context, $) {
context.JK.ShowRecording = function(app) {
var logger = context.JK.logger;
var rest = JK.Rest();
var ui = context.JK.UIHelper();
var recordingId = null;
var claimedRecordingId = null;
var $scope = $(".landing-details");
var $controls = $('.recording-controls');
var $sliderBar = $('.recording-position');
var $statusBar = $('.recording-status');
var $currentTime = $('.recording-current');
var $status = $('.status-text');
var $playButton = $('.play-button');
var $playIcon = $('#imgPlayPause');
var playing = false;
function startPlay() {
$playIcon.attr('src', '/assets/content/icon_pausebutton.png');
$controls.trigger('play.listenRecording');
playing = true;
}
function stopPlay() {
$playIcon.attr('src', '/assets/content/icon_playbutton.png');
$controls.trigger('pause.listenRecording');
playing = false;
}
function togglePlay() {
if(playing) {
stopPlay();
}
else {
startPlay();
}
return false;
}
function stateChange(e, data) {
if(data.isEnd) stopPlay();
if(data.isError) {
$sliderBar.hide();
$playButton.hide();
$currentTime.hide();
$statusBar.show();
$status.text(data.displayText);
}
}
function like() {
rest.addRecordingLike(recordingId, claimedRecordingId, JK.currentUserId)
.done(function(response) {
$("#spnLikeCount").html(parseInt($("#spnLikeCount").text()) + 1);
$("#btnLike", $scope).unbind("click");
});
}
function play() {
// rest.addPlayablePlay(recordingId, claimedRecordingId, JK.currentUserId)
// .done(function(response) {
// $("#spnPlayCount", $scope).html(parseInt($("#spnPlayCount").text()) + 1);
// });
}
function addComment() {
var comment = $("#txtRecordingComment").val();
if ($.trim(comment).length > 0) {
rest.addRecordingComment(recordingId, JK.currentUserId, comment)
.done(function(response) {
$("#spnCommentCount", $scope).html(parseInt($("#spnCommentCount").text()) + 1);
renderComment(comment, context.JK.currentUserId, context.JK.currentUserName,
context.JK.currentUserAvatarUrl, $.timeago(Date.now()), context.JK.currentUserMusician, false);
});
}
}
function renderComment(comment, userId, userName, userAvatarUrl, timeago, musician, append) {
var template = $('#template-landing-comment').html();
var commentHtml = context.JK.fillTemplate(template, {
avatar_url: userAvatarUrl,
user_id: userId,
hoverAction: musician ? "musician" : "fan",
name: userName,
comment: comment,
timeago: timeago
});
if (append) {
$(".landing-comment-scroller").append(commentHtml);
}
else {
$(".landing-comment-scroller").prepend(commentHtml);
}
context.JK.bindHoverEvents();
}
function initialize(_claimedRecordingId, _recordingId) {
recordingId = _recordingId;
claimedRecordingId = _claimedRecordingId;
$('.timeago').timeago();
$playButton.click(togglePlay);
$controls.bind('statechange.listenRecording', stateChange);
$controls.listenRecording({recordingId: recordingId, claimedRecordingId: claimedRecordingId, sliderSelector:'.recording-slider', sliderBarSelector: '.recording-playback', currentTimeSelector:'.recording-current'});
$("#btnShare", $scope).click(function(e) {
ui.launchShareDialog(claimedRecordingId, "recording");
});
if (JK.currentUserId) {
$("#btnPostComment").click(function(e) {
if ($.trim($("#txtRecordingComment").val()).length > 0) {
addComment();
$("#txtRecordingComment").val('');
$("#txtRecordingComment").blur();
}
});
}
else {
$("#txtRecordingComment", $scope).attr("disabled", "disabled");
$("#txtRecordingComment", $scope).val("You must be logged in to add a comment.");
}
$("#btnLike").click(like);
$("#btnPlay").click(play);
$playButton.trigger('click');
pollForUpdates(claimedRecordingId);
}
function pollForUpdates(claimedRecordingId) {
$(".landing-comment-scroller").empty();
rest.getClaimedRecording(claimedRecordingId)
.done(function(response) {
if (response.recording && response.recording.comments) {
$("#spnPlayCount", $scope).html(response.recording.play_count);
$("#spnCommentCount", $scope).html(response.recording.comment_count);
$("#spnLikeCount", $scope).html(response.recording.like_count);
$.each(response.recording.comments, function(index, val) {
renderComment(val.comment, val.creator.id, val.creator.name,
context.JK.resolveAvatarUrl(val.creator.photo_url), $.timeago(val.created_at), val.creator.musician, true);
});
setTimeout(function() {
pollForUpdates(claimedRecordingId);
}, 60000);
}
})
.fail(function(xhr) {
});
}
this.initialize = initialize;
}
})(window, jQuery);