2014-01-25 15:37:15 +00:00
( function ( context , $ ) {
"use strict" ;
context . JK = context . JK || { } ;
2014-02-02 23:14:47 +00:00
context . JK . ShareDialog = function ( app , entityId , entityType ) {
2014-01-25 15:37:15 +00:00
var logger = context . JK . logger ;
var rest = context . JK . Rest ( ) ;
2014-02-05 03:17:19 +00:00
var facebookRest = context . JK . FacebookRest ( ) ;
var facebookHelper = null ;
2014-02-06 18:59:19 +00:00
var dialogId = '#share-dialog' ;
2014-02-07 14:07:08 +00:00
var userDetail = null ;
var entity = null ;
2014-02-07 17:18:57 +00:00
var remainingCap = 140 - 22 - 1 ; // 140 tweet max, minus 22 for link size, minus 1 for space
2014-02-05 03:17:19 +00:00
2014-02-05 06:51:26 +00:00
var textMap = {
LIVE _SESSION : "LIVE SESSION" ,
SESSION : "SESSION" ,
RECORDING : "RECORDING" ,
RECORDED : "RECORDED"
} ;
2014-01-25 15:37:15 +00:00
2014-02-06 16:31:52 +00:00
function showSpinner ( ) {
2014-02-06 18:59:19 +00:00
$ ( dialogId + ' .dialog-inner' ) . hide ( ) ;
2014-02-06 16:31:52 +00:00
var spinner = $ ( '<div class="spinner spinner-large"></div>' )
2014-02-06 18:59:19 +00:00
$ ( dialogId + ' .content-head' ) . after ( spinner ) ;
2014-02-06 16:31:52 +00:00
}
function hideSpinner ( ) {
2014-02-06 18:59:19 +00:00
$ ( dialogId + ' .spinner' ) . remove ( ) ;
$ ( dialogId + ' .dialog-inner' ) . show ( ) ;
2014-02-06 16:31:52 +00:00
}
2014-02-07 21:28:47 +00:00
function checkShareCheckbox ( provider , checked ) {
var checkbox = $ ( dialogId + ' .share-with-' + provider + ' input' ) ;
if ( checked ) {
checkbox . attr ( 'checked' , 'checked' ) ;
}
else {
checkbox . removeAttr ( 'checked' ) ;
}
}
2014-02-06 16:31:52 +00:00
function handleRecordingShareWithGoogle ( message ) {
var defer = $ . Deferred ( ) ;
defer . resolve ( ) ; // remove when implemented
return defer ;
}
2014-02-07 21:28:47 +00:00
function handleShareWithTwitter ( message ) {
2014-02-06 16:31:52 +00:00
var defer = $ . Deferred ( ) ;
2014-02-07 17:18:57 +00:00
rest . tweet ( { message : message + ' ' + entity . share _url } )
. done ( function ( ) {
2014-02-07 21:28:47 +00:00
// uncheck facebook, because we don't want the user to re-post to their timeline by accident
checkShareCheckbox ( 'twitter' , false ) ;
2014-02-07 17:18:57 +00:00
defer . resolve ( ) ;
} )
. fail ( function ( jqXHR ) {
if ( jqXHR . status == 422 ) {
// implies twitter token error.
2014-02-07 21:28:47 +00:00
var response = JSON . parse ( jqXHR . responseText ) ;
if ( response . errors . token ) {
app . notify ( {
title : "Failed to Tweet" ,
text : "You need to re-authorize JamKazam to access your Twitter account. Click (sign in) in the Share Dialog." ,
"icon_url" : "/assets/content/icon_alert_big.png"
} ) ;
disableTwitter ( ) ;
}
else if ( response . errors . twitter ) {
app . notify ( {
title : "Failed to Tweet" ,
text : "Twitter rejected the tweet because '" + response . errors . twitter [ 0 ] + "'" ,
"icon_url" : "/assets/content/icon_alert_big.png"
} ) ;
}
else {
app . notifyServerError ( jqXHR , "Unable to Share with Twitter" ) ;
}
2014-02-07 17:18:57 +00:00
}
else {
app . notifyServerError ( jqXHR , "Unable to Share with Twitter" ) ;
}
defer . reject ( ) ;
2014-02-07 21:28:47 +00:00
} ) ;
2014-02-06 16:31:52 +00:00
return defer ;
}
2014-02-07 21:28:47 +00:00
function handleRecordingShareWithTwitter ( message ) {
return handleShareWithTwitter ( message ) ;
}
2014-02-06 16:31:52 +00:00
function handleRecordingShareWithFacebook ( message ) {
var defer = $ . Deferred ( ) ;
rest . getShareRecording ( { provider : 'facebook' , claimed _recording : entityId } )
. done ( function ( data ) {
facebookHelper . promptLogin ( )
. done ( function ( response ) {
2014-02-09 22:39:18 +00:00
handleFbStateChange ( response ) ;
2014-02-06 16:31:52 +00:00
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 ) {
2014-02-07 21:28:47 +00:00
checkShareCheckbox ( 'facebook' , false ) ;
2014-02-06 16:31:52 +00:00
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 ) {
2014-02-07 21:28:47 +00:00
return handleShareWithTwitter ( message ) ;
2014-02-06 16:31:52 +00:00
}
function handleSessionShareWithFacebook ( message ) {
var defer = $ . Deferred ( ) ;
rest . getShareSession ( { provider : 'facebook' , music _session : entityId } )
. done ( function ( data ) {
facebookHelper . promptLogin ( )
. done ( function ( response ) {
2014-02-09 22:39:18 +00:00
handleFbStateChange ( response ) ;
2014-02-06 16:31:52 +00:00
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 ) {
2014-02-07 21:28:47 +00:00
checkShareCheckbox ( 'facebook' , false ) ;
2014-02-06 16:31:52 +00:00
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 ( ) ;
2014-02-05 03:17:19 +00:00
} )
2014-02-06 16:31:52 +00:00
return defer ;
2014-02-05 03:17:19 +00:00
}
2014-02-06 16:31:52 +00:00
2014-02-07 14:07:08 +00:00
function messageTooLongForTwitter ( message ) {
2014-02-07 17:18:57 +00:00
return message && message . length > remainingCap ;
2014-02-07 14:07:08 +00:00
}
2014-02-05 01:55:51 +00:00
function socialShare ( ) {
2014-02-06 18:59:19 +00:00
var facebookCheckbox = $ ( dialogId + ' .share-with-facebook input' ) ;
var shareWithFacebook = facebookCheckbox . is ( ':checked' ) && ! facebookCheckbox . is ( ':disabled' ) ;
var googleCheckbox = $ ( dialogId + ' .share-with-google input' ) ;
var shareWithGoogle = googleCheckbox . is ( ':checked' ) && ! googleCheckbox . is ( ':disabled' ) ;
var twitterCheckbox = $ ( dialogId + ' .share-with-twitter input' ) ;
var shareWithTwitter = twitterCheckbox . is ( ':checked' ) && ! twitterCheckbox . is ( ':disabled' ) ;
2014-02-05 01:55:51 +00:00
if ( ! shareWithFacebook && ! shareWithGoogle && ! shareWithTwitter ) {
2014-02-06 18:59:19 +00:00
$ ( dialogId + ' .share-options' ) . addClass ( 'error' )
2014-02-05 03:17:19 +00:00
return ;
}
else {
2014-02-06 18:59:19 +00:00
$ ( dialogId + ' .share-options' ) . removeClass ( 'error' )
2014-02-05 03:17:19 +00:00
}
2014-02-07 14:07:08 +00:00
2014-02-06 18:59:19 +00:00
var message = $ ( dialogId + ' .share-message' ) . val ( ) ;
2014-02-06 16:31:52 +00:00
if ( ! message ) { message = undefined ; }
2014-02-07 17:18:57 +00:00
if ( shareWithTwitter && ! message ) {
$ ( dialogId + ' .share-message-holder' ) . addClass ( 'error' )
$ ( dialogId + ' .share-message-holder .error-msg' ) . text ( "You must specify a message for Twitter." ) ;
return ;
}
else
{
$ ( dialogId + ' .share-message-holder' ) . removeClass ( 'error' )
$ ( dialogId + ' .share-message-holder .error-msg' ) . text ( '' ) ;
}
// validate twitter message length
if ( shareWithTwitter && messageTooLongForTwitter ( message ) ) {
$ ( dialogId + ' .share-message-holder' ) . addClass ( 'error' )
$ ( dialogId + ' .share-message-holder .error-msg' ) . text ( "Your message must be less than " + ( remainingCap + 1 ) + " characters in length for Twitter (currently " + message . length + ")." ) ;
return ;
}
else
{
$ ( dialogId + ' .share-message-holder' ) . removeClass ( 'error' )
$ ( dialogId + ' .share-message-holder .error-msg' ) . text ( '' ) ;
}
2014-02-06 16:31:52 +00:00
showSpinner ( ) ;
var chain = [ ] ;
if ( entityType == 'session' ) {
if ( shareWithFacebook ) {
chain . push ( handleSessionShareWithFacebook ( message ) )
}
if ( shareWithTwitter ) {
chain . push ( handleSessionShareWithTwitter ( message ) )
}
if ( shareWithGoogle ) {
chain . push ( handleSessionShareWithGoogle ( message ) )
}
2014-02-05 01:55:51 +00:00
}
2014-02-06 16:31:52 +00:00
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 ( ) ;
} ) ;
2014-02-05 01:55:51 +00:00
}
2014-02-06 18:59:19 +00:00
function enableFacebook ( ) {
$ ( dialogId + ' .share-with-facebook input' ) . removeAttr ( 'disabled' )
$ ( dialogId + ' .share-with-facebook a' ) . css ( 'visibility' , 'hidden' ) ;
}
function disableFacebook ( ) {
$ ( dialogId + ' .share-with-facebook input' ) . attr ( 'disabled' , 'disabled' )
$ ( dialogId + ' .share-with-facebook a' ) . css ( 'visibility' , 'visible' ) ;
}
2014-02-07 14:07:08 +00:00
function enableTwitter ( ) {
$ ( dialogId + ' .share-with-twitter input' ) . removeAttr ( 'disabled' )
$ ( dialogId + ' .share-with-twitter a' ) . css ( 'visibility' , 'hidden' ) ;
}
function disableTwitter ( ) {
$ ( dialogId + ' .share-with-twitter input' ) . attr ( 'disabled' , 'disabled' )
$ ( dialogId + ' .share-with-twitter a' ) . css ( 'visibility' , 'visible' ) ;
}
2014-02-06 18:59:19 +00:00
function handleFbStateChange ( response ) {
2014-02-07 07:14:34 +00:00
if ( response && response . status == "connected" ) {
2014-02-06 18:59:19 +00:00
enableFacebook ( ) ;
}
2014-02-07 07:14:34 +00:00
else {
2014-02-06 18:59:19 +00:00
disableFacebook ( ) ;
}
}
function registerEvents ( onOff ) {
$ ( dialogId + ' .dialog-share-button' ) . unbind ( 'click' ) . click ( function ( e ) {
2014-02-05 01:55:51 +00:00
socialShare ( ) ;
return false ;
} ) ;
2014-02-06 18:59:19 +00:00
$ ( dialogId + ' .share-with-facebook a' ) . unbind ( 'click' ) . click ( function ( e ) {
2014-02-09 22:39:18 +00:00
facebookHelper . promptLogin ( ) . done ( function ( response ) {
handleFbStateChange ( response ) ;
} ) ;
2014-02-06 18:59:19 +00:00
return false ;
2014-02-07 14:07:08 +00:00
} ) ;
$ ( dialogId + ' .share-with-twitter a' ) . unbind ( 'click' ) . click ( function ( e ) {
app . layout . queueDialog ( 'share-dialog' )
window . location = '/auth/twitter' ;
return false ;
2014-02-07 14:11:24 +00:00
} )
2014-02-04 18:04:12 +00:00
}
2014-01-30 02:37:36 +00:00
function showDialog ( ) {
app . layout . showDialog ( 'share-dialog' ) ;
}
2014-02-02 23:14:47 +00:00
function initDialog ( ) {
2014-02-05 06:51:26 +00:00
var sessionText = textMap . SESSION ;
var liveSessionText = textMap . LIVE _SESSION ;
2014-02-02 23:14:47 +00:00
2014-02-05 06:51:26 +00:00
var fill = entityType === sessionText . toLowerCase ( ) ? "teal-fill" : "orange-fill" ;
2014-02-02 23:14:47 +00:00
2014-02-05 06:51:26 +00:00
$ ( "#shareType" ) . text ( entityType ) ;
2014-02-02 23:14:47 +00:00
$ ( "#divWidgetCodeHeader" ) . addClass ( fill ) ;
$ ( "#divWidgetPreviewHeader" ) . addClass ( fill ) ;
$ ( "#divWidgetPreview" ) . addClass ( entityType ) ;
// SESSION
2014-02-07 05:57:31 +00:00
if ( entityType === sessionText . toLowerCase ( ) ) {
2014-02-05 06:51:26 +00:00
$ ( "#lblWidgetCodeType" ) . html ( sessionText . toLowerCase ( ) ) ;
$ ( "#lblWidgetPreviewType" ) . html ( sessionText . toLowerCase ( ) ) ;
$ ( "#spnWidgetCodeBranding" ) . text ( liveSessionText . toLowerCase ( ) ) ;
$ ( "#spnWidgetPreviewBranding" ) . text ( liveSessionText . toLowerCase ( ) ) ;
2014-02-02 23:14:47 +00:00
rest . getSessionHistory ( entityId )
. done ( function ( response ) {
2014-02-07 05:57:31 +00:00
// var name, photoUrl;
$ ( ".link-contents" ) . html ( response . share _url ) ;
2014-02-02 23:14:47 +00:00
} ) ;
}
// RECORDING
2014-02-07 05:57:31 +00:00
else if ( entityType === "recording" ) {
2014-02-05 06:51:26 +00:00
var recordedText = textMap . RECORDED . toLowerCase ( ) ;
$ ( "#lblWidgetCodeType" ) . text ( textMap . RECORDING ) ;
$ ( "#lblWidgetPreviewType" ) . text ( textMap . RECORDING ) ;
$ ( "#spnWidgetCodeBranding" ) . text ( recordedText ) ;
$ ( "#spnWidgetPreviewBranding" ) . text ( recordedText ) ;
2014-02-02 23:14:47 +00:00
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 ) ;
}
2014-02-07 05:57:31 +00:00
$ ( ".link-contents" ) . html ( response . share _url ) ;
2014-02-02 23:14:47 +00:00
$ ( "#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 ) {
2014-02-05 06:51:26 +00:00
$ ( ".widget-members" ) . append ( '<div class="widget-avatar-small">' + '<img src="' + context . JK . resolveAvatarUrl ( val . user . photo _url ) + '" alt="" />' + '</div>' ) ;
2014-02-02 23:14:47 +00:00
} ) ;
} ) ;
}
}
2014-01-25 15:37:15 +00:00
function clearTextFields ( ) {
}
function beforeShow ( ) {
2014-02-07 14:07:08 +00:00
disableTwitter ( ) ;
// no disableFacebook on purpose
if ( entityType == 'recording' ) {
rest . getClaimedRecording ( entityId )
. done ( function ( data ) {
entity = data ;
$ ( dialogId + ' .link-contents' ) . text ( entity . share _url )
} )
. fail ( function ( jqXHR ) {
app . notifyServerError ( jqXHR , "Unable to Fetch Session Data" ) ;
} )
}
else {
rest . getSession ( entityId )
. done ( function ( data ) {
entity = data ;
$ ( dialogId + ' .link-contents' ) . text ( entity . share _url )
} )
. fail ( function ( jqXHR ) {
app . notifyServerError ( jqXHR , "Unable to Fetch Session Data" ) ;
} ) ;
}
rest . getUserDetail ( )
. done ( function ( data ) {
userDetail = data ;
if ( data . auth _twitter ) {
enableTwitter ( ) ;
}
else {
disableTwitter ( ) ;
}
} ) ;
2014-02-07 17:18:57 +00:00
$ ( dialogId + ' .share-message-holder' ) . removeClass ( 'error' )
$ ( dialogId + ' .share-message-holder .error-msg' ) . text ( '' ) ;
2014-02-06 18:59:19 +00:00
$ ( dialogId + ' .share-options' ) . removeClass ( 'error' ) ;
2014-01-25 15:37:15 +00:00
registerEvents ( true ) ;
}
2014-02-07 17:18:57 +00:00
function afterShow ( ) {
2014-02-13 17:51:51 +00:00
if ( context . JK . hasFlash ( ) ) {
$ ( "#btn-share-copy" ) . clipboard ( {
2014-02-07 17:18:57 +00:00
path : '/assets/jquery.clipboard.swf' ,
copy : function ( ) {
2014-02-13 17:51:51 +00:00
// Return text in closest element (useful when you have multiple boxes that can be copied)
return $ ( ".link-contents" ) . text ( ) ;
2014-02-07 17:18:57 +00:00
}
2014-02-13 17:51:51 +00:00
} ) ;
}
else {
if ( context . jamClient ) {
// uses bridge call to ultimately access QClipboard
$ ( "#btn-share-copy" ) . unbind ( 'click' ) . click ( function ( ) {
context . jamClient . SaveToClipboard ( $ ( ".link-contents" ) . text ( ) ) ;
return false ;
} )
}
else {
console . log ( "no copy-to-clipboard capabilities" )
}
}
2014-02-07 17:18:57 +00:00
}
2014-01-25 15:37:15 +00:00
function afterHide ( ) {
2014-02-06 16:31:52 +00:00
hideSpinner ( ) ;
2014-01-25 15:37:15 +00:00
registerEvents ( false ) ;
}
2014-02-05 03:17:19 +00:00
function initialize ( _facebookHelper ) {
facebookHelper = _facebookHelper ;
2014-01-25 15:37:15 +00:00
var dialogBindings = {
'beforeShow' : beforeShow ,
2014-02-07 17:18:57 +00:00
'afterShow' : afterShow ,
2014-01-25 15:37:15 +00:00
'afterHide' : afterHide
} ;
2014-02-05 02:17:52 +00:00
app . bindDialog ( 'share-dialog' , dialogBindings ) ;
2014-02-06 18:59:19 +00:00
2014-02-05 06:51:26 +00:00
initDialog ( ) ;
2014-02-06 18:59:19 +00:00
2014-02-07 21:28:47 +00:00
facebookHelper . deferredLoginStatus ( ) . done ( function ( response ) { handleFbStateChange ( response ) ; } ) ;
2014-01-25 15:37:15 +00:00
} ;
this . initialize = initialize ;
2014-01-30 02:37:36 +00:00
this . showDialog = showDialog ;
2014-01-25 15:37:15 +00:00
}
return this ;
2014-02-05 01:58:11 +00:00
} ) ( window , jQuery )