307 lines
11 KiB
JavaScript
307 lines
11 KiB
JavaScript
(function(context,$) {
|
|
|
|
"use strict";
|
|
context.JK = context.JK || {};
|
|
context.JK.ShareDialog = function(app, entityId, entityType) {
|
|
var logger = context.JK.logger;
|
|
var rest = context.JK.Rest();
|
|
var facebookRest = context.JK.FacebookRest();
|
|
var facebookHelper = null;
|
|
|
|
var textMap = {
|
|
LIVE_SESSION: "LIVE SESSION",
|
|
SESSION: "SESSION",
|
|
RECORDING: "RECORDING",
|
|
RECORDED: "RECORDED"
|
|
};
|
|
|
|
function showSpinner() {
|
|
$('#share-dialog .dialog-inner').hide();
|
|
var spinner = $('<div class="spinner spinner-large"></div>')
|
|
$('#share-dialog .content-head').after(spinner);
|
|
|
|
}
|
|
|
|
function hideSpinner() {
|
|
$('#share-dialog .spinner').remove();
|
|
$('#share-dialog .dialog-inner').show();
|
|
}
|
|
|
|
|
|
|
|
function handleRecordingShareWithGoogle(message) {
|
|
var defer = $.Deferred();
|
|
|
|
defer.resolve(); // remove when implemented
|
|
|
|
return defer;
|
|
}
|
|
|
|
function handleRecordingShareWithTwitter(message) {
|
|
var defer = $.Deferred();
|
|
|
|
defer.resolve(); // remove when implemented
|
|
|
|
return defer;
|
|
}
|
|
|
|
function handleRecordingShareWithFacebook(message) {
|
|
var defer = $.Deferred();
|
|
|
|
rest.getShareRecording({ provider:'facebook', claimed_recording: entityId})
|
|
.done(function(data) {
|
|
facebookHelper.promptLogin()
|
|
.done(function(response) {
|
|
if(response.status == "connected") {
|
|
facebookRest.post({
|
|
access_token: response.authResponse.accessToken,
|
|
message: message,
|
|
description: data.description,
|
|
caption: data.caption,
|
|
name: data.title,
|
|
picture: data.photo_url
|
|
})
|
|
.done(function(response) {
|
|
defer.resolve();
|
|
})
|
|
.fail(function(response) {
|
|
app.notify({
|
|
title : "Unable to Share with Facebook",
|
|
text : "Error: " + response,
|
|
"icon_url": "/assets/content/icon_alert_big.png"
|
|
});
|
|
defer.reject();
|
|
})
|
|
}
|
|
else {
|
|
// user doesn't want to auth; this is a form of success
|
|
defer.resolve();
|
|
}
|
|
})
|
|
})
|
|
.fail(function(jqXHR) {
|
|
app.notifyServerError(jqXHR, "Unable to Populate Share Data");
|
|
defer.reject();
|
|
})
|
|
|
|
return defer;
|
|
|
|
}
|
|
|
|
function handleSessionShareWithGoogle(message) {
|
|
var defer = $.Deferred();
|
|
|
|
defer.resolve(); // remove when implemented
|
|
|
|
return defer;
|
|
}
|
|
|
|
function handleSessionShareWithTwitter(message) {
|
|
var defer = $.Deferred();
|
|
|
|
defer.resolve(); // remove when implemented
|
|
|
|
return defer;
|
|
}
|
|
|
|
function handleSessionShareWithFacebook(message) {
|
|
var defer = $.Deferred();
|
|
|
|
rest.getShareSession({ provider:'facebook', music_session: entityId})
|
|
.done(function(data) {
|
|
facebookHelper.promptLogin()
|
|
.done(function(response) {
|
|
if(response.status == "connected") {
|
|
facebookRest.post({
|
|
access_token: response.authResponse.accessToken,
|
|
message: message,
|
|
description: data.description,
|
|
caption: data.caption,
|
|
name: data.title,
|
|
picture: data.photo_url
|
|
})
|
|
.done(function(response) {
|
|
defer.resolve();
|
|
})
|
|
.fail(function(response) {
|
|
app.notify({
|
|
title : "Unable to Share with Facebook",
|
|
text : "Error: " + response,
|
|
"icon_url": "/assets/content/icon_alert_big.png"
|
|
});
|
|
defer.reject();
|
|
})
|
|
}
|
|
else {
|
|
// user doesn't want to auth; this is a form of success
|
|
defer.resolve();
|
|
}
|
|
})
|
|
})
|
|
.fail(function(jqXHR) {
|
|
app.notifyServerError(jqXHR, "Unable to Populate Share Data");
|
|
defer.reject();
|
|
})
|
|
|
|
return defer;
|
|
}
|
|
|
|
function socialShare() {
|
|
var shareWithFacebook = $('.share-with-facebook').is(':checked');
|
|
var shareWithGoogle = $('.share-with-google').is(':checked');
|
|
var shareWithTwitter = $('.share-with-twitter').is(':checked');
|
|
|
|
if(!shareWithFacebook && !shareWithGoogle && !shareWithTwitter) {
|
|
$('.share-options').addClass('error')
|
|
return;
|
|
}
|
|
else {
|
|
$('.share-options').removeClass('error')
|
|
}
|
|
|
|
var message = $('.share-message').val();
|
|
if(!message) { message = undefined; }
|
|
|
|
showSpinner();
|
|
|
|
var chain = [];
|
|
|
|
if(entityType == 'session') {
|
|
if(shareWithFacebook) {
|
|
chain.push(handleSessionShareWithFacebook(message))
|
|
}
|
|
if(shareWithTwitter) {
|
|
chain.push(handleSessionShareWithTwitter(message))
|
|
}
|
|
if(shareWithGoogle) {
|
|
chain.push(handleSessionShareWithGoogle(message))
|
|
}
|
|
}
|
|
else {
|
|
if(shareWithFacebook) {
|
|
chain.push(handleRecordingShareWithFacebook(message))
|
|
}
|
|
if(shareWithTwitter) {
|
|
chain.push(handleRecordingShareWithTwitter(message))
|
|
}
|
|
if(shareWithGoogle) {
|
|
chain.push(handleRecordingShareWithGoogle(message))
|
|
}
|
|
}
|
|
|
|
$.when.apply($, chain)
|
|
.done(function() {
|
|
app.layout.closeDialog('share-dialog');
|
|
})
|
|
.fail(function() {
|
|
logger.error("share failed")
|
|
})
|
|
.always(function() {
|
|
hideSpinner();
|
|
});
|
|
}
|
|
function registerEvents(onOff) {
|
|
|
|
$('.dialog-share-button').unbind('click').click(function(e){
|
|
socialShare();
|
|
return false;
|
|
});
|
|
}
|
|
|
|
function showDialog() {
|
|
app.layout.showDialog('share-dialog');
|
|
}
|
|
|
|
function initDialog() {
|
|
var sessionText = textMap.SESSION;
|
|
var liveSessionText = textMap.LIVE_SESSION;
|
|
|
|
var fill = entityType === sessionText.toLowerCase() ? "teal-fill" : "orange-fill";
|
|
|
|
$("#shareType").text(entityType);
|
|
|
|
$("#divWidgetCodeHeader").addClass(fill);
|
|
$("#divWidgetPreviewHeader").addClass(fill);
|
|
$("#divWidgetPreview").addClass(entityType);
|
|
|
|
// SESSION
|
|
if (entityType === sessionText.toLowerCase() ) {
|
|
$("#lblWidgetCodeType").html(sessionText.toLowerCase());
|
|
$("#lblWidgetPreviewType").html(sessionText.toLowerCase());
|
|
$("#spnWidgetCodeBranding").text(liveSessionText.toLowerCase());
|
|
$("#spnWidgetPreviewBranding").text(liveSessionText.toLowerCase());
|
|
|
|
rest.getSessionHistory(entityId)
|
|
.done(function(response) {
|
|
var name, photoUrl;
|
|
});
|
|
}
|
|
|
|
// RECORDING
|
|
else {
|
|
var recordedText = textMap.RECORDED.toLowerCase();
|
|
$("#lblWidgetCodeType").text(textMap.RECORDING);
|
|
$("#lblWidgetPreviewType").text(textMap.RECORDING);
|
|
$("#spnWidgetCodeBranding").text(recordedText);
|
|
$("#spnWidgetPreviewBranding").text(recordedText);
|
|
|
|
rest.getClaimedRecording(entityId)
|
|
.done(function(response) {
|
|
var name, photoUrl;
|
|
|
|
if (response.recording.band) {
|
|
name = response.recording.band.name;
|
|
photoUrl = context.JK.resolveBandAvatarUrl(response.recording.band.photo_url);
|
|
}
|
|
else {
|
|
name = response.recording.owner.name;
|
|
photoUrl = context.JK.resolveAvatarUrl(response.recording.owner.photo_url);
|
|
}
|
|
|
|
$("#imgWidgetCodeAvatar").attr('src', photoUrl);
|
|
$("#imgWidgetPreviewAvatar").attr('src', photoUrl);
|
|
|
|
$("#divWidgetPreviewTitle").html(response.recording.name);
|
|
|
|
$("#spnWidgetCodeArtistName").html(name);
|
|
$("#spnWidgetPreviewArtistName").html(name);
|
|
|
|
$.each(response.recording.recorded_tracks, function(index, val) {
|
|
$(".widget-members").append('<div class="widget-avatar-small">' + '<img src="' + context.JK.resolveAvatarUrl(val.user.photo_url) + '" alt="" />' + '</div>');
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
function clearTextFields() {
|
|
|
|
}
|
|
|
|
function beforeShow() {
|
|
$('.share-options').removeClass('error');
|
|
registerEvents(true);
|
|
}
|
|
|
|
function afterHide() {
|
|
hideSpinner();
|
|
registerEvents(false);
|
|
}
|
|
|
|
function initialize(_facebookHelper){
|
|
facebookHelper = _facebookHelper;
|
|
|
|
var dialogBindings = {
|
|
'beforeShow' : beforeShow,
|
|
'afterHide': afterHide
|
|
};
|
|
|
|
app.bindDialog('share-dialog', dialogBindings);
|
|
initDialog();
|
|
};
|
|
|
|
this.initialize = initialize;
|
|
this.showDialog = showDialog;
|
|
}
|
|
|
|
return this;
|
|
})(window,jQuery) |