2013-03-04 03:38:12 +00:00
( function ( context , $ ) {
"use strict" ;
context . JK = context . JK || { } ;
context . JK . Sidebar = function ( app ) {
2014-08-31 15:30:59 +00:00
var EVENTS = context . JK . EVENTS ;
2014-02-13 04:40:41 +00:00
var logger = context . JK . logger ;
var friends = [ ] ;
var rest = context . JK . Rest ( ) ;
var invitationDialog = null ;
2014-03-20 11:53:26 +00:00
var textMessageDialog = null ;
2014-03-26 17:09:48 +00:00
var notificationPanel = null ;
2014-04-23 12:27:49 +00:00
var chatPanel = null ;
2014-03-27 18:43:15 +00:00
var me = null ;
2015-03-11 21:44:22 +00:00
var $sidebar = null ;
2014-02-13 04:40:41 +00:00
function initializeSearchPanel ( ) {
$ ( '#search_text_type' ) . change ( function ( ) {
searchForInput ( ) ;
context . JK . SearchResultScreen . searchTypeSelection ( $ ( '#search_text_type' ) . val ( ) ) ;
} ) ;
}
context . JK . Sidebar . searchTypeSelection = function ( typeSelection ) {
$ ( '#search_text_type' ) . val ( typeSelection ) ;
emptySearchResults ( ) ;
}
2014-03-26 17:31:51 +00:00
function refreshFriends ( ) {
rest . getFriends ( ) .
done ( function ( response ) {
friends = response ;
updateFriendList ( response ) ;
// set friend count
$ ( '#sidebar-friend-count' ) . html ( response . length ) ;
} )
}
2014-02-13 04:40:41 +00:00
2014-03-26 17:31:51 +00:00
function initializeFriendsPanel ( ) {
2014-02-13 04:40:41 +00:00
$ ( '#sidebar-search-header' ) . hide ( ) ;
2015-03-11 21:44:22 +00:00
app . user ( ) . done ( function ( ) {
refreshFriends ( ) ;
} )
2020-04-02 14:45:01 +00:00
$ ( '#friend-help-howto' ) . click ( function ( ) {
context . JK . popExternalLink ( "https://forum.jamkazam.com/showthread.php?tid=117" )
return false ;
} )
2014-02-13 04:40:41 +00:00
return false ;
}
function updateFriendList ( response ) {
$ ( '#sidebar-friend-list li:not(.invite-friend-row)' ) . remove ( ) ;
// show online friends first (sort by first name within online/offline groups)
response . sort ( function ( a , b ) {
var a _online = a . online ;
var b _online = b . online ;
var a _firstname = a . first _name . toLowerCase ( ) ;
var b _firstname = b . first _name . toLowerCase ( ) ;
if ( b _online != a _online ) {
if ( b _online < a _online ) return - 1 ;
if ( b _online > a _online ) return 1 ;
return 0 ;
}
if ( a _firstname < b _firstname ) return - 1 ;
if ( a _firstname > b _firstname ) return 1 ;
return 0 ;
} ) ;
2020-05-06 19:42:52 +00:00
context . FriendActions . updateFriends . trigger ( response )
2014-02-13 04:40:41 +00:00
$ . each ( response , function ( index , val ) {
var css = val . online ? '' : 'offline' ;
friends [ val . id ] = val ;
// fill in template for Connect pre-click
var template = $ ( '#template-friend-panel' ) . html ( ) ;
var searchResultHtml = context . JK . fillTemplate ( template , {
userId : val . id ,
cssClass : css ,
avatar _url : context . JK . resolveAvatarUrl ( val . photo _url ) ,
2014-03-02 21:44:36 +00:00
userName : val . name . length > 20 ? val . name . substring ( 0 , 20 ) + "..." : val . name ,
2014-02-13 04:40:41 +00:00
status : val . online ? 'Available' : 'Offline' ,
extra _info : '' ,
hoverAction : val . musician ? "musician" : "fan" ,
info _image _url : ''
} ) ;
$ ( '#sidebar-friend-list li.invite-friend-row' ) . before ( searchResultHtml ) ;
} ) ;
context . JK . bindHoverEvents ( ) ;
}
function initializeNotificationsPanel ( ) {
2014-03-26 17:09:48 +00:00
notificationPanel = new context . JK . NotificationPanel ( app ) ;
2014-03-27 18:43:15 +00:00
notificationPanel . initialize ( me , textMessageDialog ) ;
2014-02-13 04:40:41 +00:00
}
2014-05-02 16:30:56 +00:00
// session events for chat
function registerSessionEvents ( ) {
2014-08-31 15:30:59 +00:00
$ ( document ) . on ( EVENTS . SESSION _STARTED , function ( e , data ) {
2014-05-02 16:30:56 +00:00
chatPanel . sessionStarted ( e , data ) ;
return false ;
} ) ;
2014-08-31 15:30:59 +00:00
$ ( document ) . on ( EVENTS . SESSION _ENDED , function ( e , data ) {
2014-05-02 16:30:56 +00:00
chatPanel . sessionStopped ( e , data ) ;
return false ;
} ) ;
}
2014-02-13 04:40:41 +00:00
function initializeChatPanel ( ) {
2014-05-02 16:30:56 +00:00
chatPanel = new context . JK . ChatPanel ( app ) ;
chatPanel . initialize ( me ) ;
registerSessionEvents ( ) ;
2014-02-13 04:40:41 +00:00
}
function search ( query ) {
logger . debug ( 'query=' + query ) ;
if ( query !== '' ) {
context . JK . search ( query , app , context . JK . SearchResultScreen . onSearchSuccess ) ;
}
}
function hideSearchResults ( ) {
emptySearchResults ( ) ;
$ ( '#search-input' ) . val ( '' ) ;
$ ( '#sidebar-search-header' ) . hide ( ) ;
}
function emptySearchResults ( ) {
$ ( '#sidebar-search-results' ) . empty ( ) ;
}
var delay = ( function ( ) {
var timer = 0 ;
return function ( callback , ms ) {
clearTimeout ( timer ) ;
timer = setTimeout ( callback , ms ) ;
} ;
} ) ( ) ;
function inviteHoverIn ( ) {
$ ( '.invitation-button-holder' ) . slideDown ( ) ;
}
function inviteHoverOut ( ) {
$ ( '.invitation-button-holder' ) . slideUp ( ) ;
}
function searchForInput ( ) {
var query = $ ( '#search-input' ) . val ( ) ;
// logger.debug("query=" + query);
if ( query === '' ) {
return hideSearchResults ( ) ;
}
if ( query . length > 2 ) {
// FIXME: this is in searchResults
$ ( '#query' ) . html ( query ) ;
query += '&search_text_type=' + $ ( '#search_text_type' ) . val ( ) ;
emptySearchResults ( ) ;
search ( query ) ;
}
}
function events ( ) {
$ ( '#search-input' ) . keyup ( function ( evt ) {
delay ( function ( ) {
// ENTER KEY
if ( evt . which === 13 ) {
return hideSearchResults ( ) ;
}
// ESCAPE KEY
if ( evt . which === 27 ) {
return hideSearchResults ( ) ;
}
searchForInput ( ) ;
} , 500 ) ;
} ) ;
$ ( '#sidebar-search-expand' ) . click ( function ( evt ) {
$ ( '#searchForm' ) . submit ( ) ;
hideSearchResults ( ) ;
} ) ;
2020-04-02 14:45:01 +00:00
//$('.sidebar .invite-friend-row').hoverIntent(inviteHoverIn, inviteHoverOut);
2014-02-13 04:40:41 +00:00
// friend notifications
registerFriendUpdate ( ) ;
// session invitations
registerSessionJoin ( ) ;
registerSessionDepart ( ) ;
// recording notifications
registerRecordingStarted ( ) ;
registerRecordingEnded ( ) ;
// broadcast notifications
registerSourceUpRequested ( ) ;
registerSourceDownRequested ( ) ;
registerSourceUp ( ) ;
registerSourceDown ( ) ;
// watch for Invite More Users events
2020-04-04 22:51:36 +00:00
$ ( '#sidebar-div .email-invitation-holder' ) . click ( function ( ) {
2014-02-13 04:40:41 +00:00
invitationDialog . showEmailDialog ( ) ;
return false ;
} ) ;
2020-04-04 22:51:36 +00:00
$ ( '#sidebar-div .gmail-invitation-holder' ) . click ( function ( ) {
2014-02-13 04:40:41 +00:00
invitationDialog . showGoogleDialog ( ) ;
return false ;
} ) ;
2020-04-04 22:51:36 +00:00
$ ( '#sidebar-div .facebook-invitation-holder' ) . click ( function ( evt ) {
2014-02-13 04:40:41 +00:00
invitationDialog . showFacebookDialog ( evt ) ;
return false ;
} ) ;
}
function registerFriendUpdate ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . FRIEND _UPDATE , function ( header , payload ) {
logger . debug ( "Handling FRIEND_UPDATE msg " + JSON . stringify ( payload ) ) ;
2014-07-08 18:34:03 +00:00
var friend = friends [ payload . user _id ] ;
if ( friend ) {
friend . online = payload . online ;
updateFriendList ( friends ) ;
var online _text = payload . online ? "online" : "offline" ;
app . notify ( {
"title" : "Friend is now " + online _text ,
"text" : payload . msg ,
"icon_url" : context . JK . resolveAvatarUrl ( payload . photo _url )
} ) ;
}
2014-02-13 04:40:41 +00:00
} ) ;
}
function registerSessionJoin ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . SESSION _JOIN , function ( header , payload ) {
logger . debug ( "Handling SESSION_JOIN msg " + JSON . stringify ( payload ) ) ;
2016-01-07 12:52:27 +00:00
if ( app . clientId != payload . client _id ) {
// display notification
app . notify ( {
"title" : "New Session Participant" ,
"text" : payload . msg ,
"icon_url" : context . JK . resolveAvatarUrl ( payload . photo _url )
} ) ;
// tell session store that this happened
context . SessionActions . sessionJoinedByOther ( payload )
}
2016-03-03 22:08:01 +00:00
if ( context . jamClient . ClientJoinedSession ) {
context . jamClient . ClientJoinedSession ( payload [ "source_user_id" ] , payload [ "client_id" ] , payload [ "session_id" ] )
}
2014-02-13 04:40:41 +00:00
} ) ;
}
function registerSessionDepart ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . SESSION _DEPART , function ( header , payload ) {
logger . debug ( "Handling SESSION_DEPART msg " + JSON . stringify ( payload ) ) ;
2016-03-03 22:08:01 +00:00
if ( app . clientId != payload . client _id ) {
var recordingId = payload . recording _id ;
if ( recordingId && context . RecordingStore . recordingModel . isRecording ( recordingId ) ) {
context . RecordingStore . recordingModel . onServerStopRecording ( recordingId ) ;
}
else {
app . notify ( {
"title" : "Musician Left Session" ,
"text" : payload . msg ,
"icon_url" : context . JK . resolveAvatarUrl ( payload . photo _url )
} ) ;
}
}
if ( context . jamClient . ClientLeftSession ) {
context . jamClient . ClientLeftSession ( payload [ "source_user_id" ] , payload [ "client_id" ] , payload [ "session_id" ] )
2014-02-13 04:40:41 +00:00
}
} ) ;
}
function registerRecordingStarted ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . RECORDING _STARTED , function ( header , payload ) {
logger . debug ( "Handling RECORDING_STARTED msg " + JSON . stringify ( payload ) ) ;
2013-10-16 07:23:43 +00:00
2014-02-13 04:40:41 +00:00
app . notify ( {
"title" : "Recording Started" ,
2013-10-16 07:23:43 +00:00
"text" : payload . msg ,
"icon_url" : context . JK . resolveAvatarUrl ( payload . photo _url )
} ) ;
2014-02-13 04:40:41 +00:00
} ) ;
}
2013-03-04 03:38:12 +00:00
2014-02-13 04:40:41 +00:00
function registerRecordingEnded ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . RECORDING _ENDED , function ( header , payload ) {
logger . debug ( "Handling RECORDING_ENDED msg " + JSON . stringify ( payload ) ) ;
2013-12-29 04:51:35 +00:00
2014-02-13 04:40:41 +00:00
app . notify ( {
"title" : "Recording Ended" ,
2014-01-02 19:57:16 +00:00
"text" : payload . msg ,
2013-12-29 04:51:35 +00:00
"icon_url" : context . JK . resolveAvatarUrl ( payload . photo _url )
} ) ;
2014-02-13 04:40:41 +00:00
} ) ;
}
2013-12-29 04:51:35 +00:00
2014-02-13 04:40:41 +00:00
function registerSourceUpRequested ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . SOURCE _UP _REQUESTED , function ( header , payload ) {
logger . debug ( "Handling SOURCE_UP_REQUESTED msg " + JSON . stringify ( payload ) ) ;
2015-07-15 15:04:45 +00:00
var current _session _id = context . SessionStore . id ( ) ;
2014-02-13 04:40:41 +00:00
if ( ! current _session _id ) {
// we are not in a session
2015-07-15 15:04:45 +00:00
var last _session = context . SessionStore . getCurrentOrLastSession ( ) ;
2014-02-13 04:40:41 +00:00
if ( last _session && last _session . id == payload . music _session ) {
// the last session we were in was responsible for this message. not that odd at all
logger . debug ( "SOURCE_UP_REQUESTED came in for session_id" + payload . music _session + ", but was dropped because we have left that session" )
}
else {
// this means we aren't in a session, and, what's worse,
// the last session we were in does not match the specified music_session id
throw "SOURCE_UP_REQUESTED came in for session_id:" + payload . music _session + ", but we are not in a session and the last session ID did not match the one specified" ;
}
}
else {
// we are in a session
if ( current _session _id == payload . music _session ) {
context . jamClient . SessionLiveBroadcastStart ( payload . host , payload . port , payload . mount ,
payload . source _user , payload . source _pass ,
'' , payload . bitrate )
}
else {
2015-07-15 15:04:45 +00:00
var last _session = context . SessionStore . getCurrentOrLastSession ( ) ;
2014-01-25 23:38:03 +00:00
if ( last _session && last _session . id == payload . music _session ) {
// the last session we were in was responsible for this message. not that odd at all
2014-02-13 04:40:41 +00:00
logger . debug ( "SOURCE_UP_REQUESTED came in for session_id" + payload . music _session + ", but was dropped because we have left that session and are in a new one" )
2014-01-25 23:38:03 +00:00
}
else {
// this means we aren't in a session, and, what's worse,
// the last session we were in does not match the specified music_session id
2014-02-13 04:40:41 +00:00
throw "SOURCE_UP_REQUESTED came in for session_id:" + payload . music _session + ", but we are in a session and the last session ID did not match the one specified" ;
2014-01-25 23:38:03 +00:00
}
}
2014-02-13 04:40:41 +00:00
}
} ) ;
}
function registerSourceDownRequested ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . SOURCE _DOWN _REQUESTED , function ( header , payload ) {
logger . debug ( "Handling SOURCE_DOWN_REQUESTED msg " + JSON . stringify ( payload ) ) ;
2015-07-15 15:04:45 +00:00
var current _session _id = context . SessionStore . id ( ) ;
2014-02-13 04:40:41 +00:00
if ( ! current _session _id ) {
// we are not in a session
2015-07-15 15:04:45 +00:00
var last _session = context . SessionStore . getCurrentOrLastSession ( ) ;
2014-02-13 04:40:41 +00:00
if ( last _session && last _session . id == payload . music _session ) {
// the last session we were in was responsible for this message. not that odd at all
logger . debug ( "SOURCE_DOWN_REQUESTED came in for session_id" + payload . music _session + ", but was dropped because we have left that session" )
}
2014-01-25 23:38:03 +00:00
else {
2014-02-13 04:40:41 +00:00
// this means we aren't in a session, and, what's worse,
// the last session we were in does not match the specified music_session id
throw "SOURCE_DOWN_REQUESTED came in for session_id:" + payload . music _session + ", but we are not in a session and the last session ID did not match the one specified" ;
2014-01-25 23:38:03 +00:00
}
2014-02-13 04:40:41 +00:00
}
else {
// we are in a session
if ( current _session _id == payload . music _session ) {
context . jamClient . SessionLiveBroadcastStop ( ) ;
}
else {
2015-07-15 15:04:45 +00:00
var last _session = context . SessionStore . getCurrentOrLastSession ( ) ;
2014-01-25 23:38:03 +00:00
if ( last _session && last _session . id == payload . music _session ) {
// the last session we were in was responsible for this message. not that odd at all
2014-02-13 04:40:41 +00:00
logger . debug ( "SOURCE_DOWN_REQUESTED came in for session_id" + payload . music _session + ", but was dropped because we have left that session and are in a new one" )
2014-01-25 23:38:03 +00:00
}
else {
// this means we aren't in a session, and, what's worse,
// the last session we were in does not match the specified music_session id
2014-02-13 04:40:41 +00:00
throw "SOURCE_DOWN_REQUESTED came in for session_id:" + payload . music _session + ", but we are in a session and the last session ID did not match the one specified" ;
2014-01-25 23:38:03 +00:00
}
}
2014-02-13 04:40:41 +00:00
}
} ) ;
}
function registerSourceUp ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . SOURCE _UP , function ( header , payload ) {
logger . debug ( "Handling SOURCE_UP msg " + JSON . stringify ( payload ) ) ;
logger . debug ( "session %o is now being broadcasted" , payload . music _session ) ;
app . notify ( {
"title" : "Now Broadcasting" ,
"text" : "This session is now being broadcasted."
} ) ;
} ) ;
}
function registerSourceDown ( ) {
context . JK . JamServer . registerMessageCallback ( context . JK . MessageType . SOURCE _DOWN , function ( header , payload ) {
logger . debug ( "Handling SOURCE_DOWN msg " + JSON . stringify ( payload ) ) ;
logger . debug ( "session %o is no longer being broadcasted" , payload . music _session ) ;
app . notify ( {
"title" : "No Longer Broadcasting" ,
"text" : "This session is no longer being broadcasted."
} ) ;
} ) ;
}
2014-03-20 11:53:26 +00:00
this . initialize = function ( invitationDialogInstance , textMessageDialogInstance ) {
2014-03-28 02:24:53 +00:00
me = this ;
2014-03-27 18:43:15 +00:00
invitationDialog = invitationDialogInstance ;
textMessageDialog = textMessageDialogInstance ;
2015-03-11 21:44:22 +00:00
$sidebar = $ ( '#sidebar-div' )
app . user ( )
. done ( function ( ) {
events ( ) ;
initializeSearchPanel ( ) ;
initializeFriendsPanel ( ) ;
initializeChatPanel ( ) ;
initializeNotificationsPanel ( ) ;
} )
. fail ( function ( arg1 ) {
if ( arg1 == "not_logged_in" ) {
$ ( '#search-input' ) . attr ( 'disabled' , 'disabled' )
$ ( '.sidebar .invite-friend-row' ) . click ( function ( ) {
app . layout . showDialog ( 'login-required-dialog' )
} ) ;
$sidebar . addClass ( 'not-logged-in' )
}
} )
2014-02-13 04:40:41 +00:00
} ;
2014-03-26 17:31:51 +00:00
this . refreshFriends = refreshFriends ;
2014-02-13 04:40:41 +00:00
}
2013-03-04 03:38:12 +00:00
} ) ( window , jQuery ) ;