(function(context,$) { "use strict"; context.JK = context.JK || {}; context.JK.SessionList = function(app) { var logger = context.JK.logger; var rest = context.JK.Rest(); var LATENCY = { GOOD : {description: "GOOD", style: "latency-green", min: 0.0, max: 20.0}, MEDIUM : {description: "MEDIUM", style: "latency-yellow", min: 20.0, max: 40.0}, POOR : {description: "POOR", style: "latency-red", min: 40.0, max: 10000000000.0}, UNREACHABLE: {description: "UNREACHABLE", style: "latency-grey", min: -1, max: -1} }; var AUDIENCE = { OPEN_TO_FANS: "Open to Fans", MUSICIANS_ONLY: "Musicians Only" }; var instrument_logo_map = context.JK.getInstrumentIconMap24(); /** * Render a single session line into the table. * It will be inserted at the appropriate place according to the * sortScore in sessionLatency. */ function renderSession(session, sessionLatency, tbGroup, rowTemplate, musicianTemplate) { // latency var latencyInfo = sessionLatency.sessionInfo(session.id); var latencyDescription = ""; var latencyStyle = ""; var gearLatency = context.jamClient.SessionGetDeviceLatency(); var showJoinLink = true; // quick dance to make the new scoring find sessions API work if(session.max_score) { latencyInfo = {averageLatency: session.max_score} } var totalLatency = (latencyInfo.averageLatency / 2) + gearLatency; logger.debug("latencyInfo.averageLatency=" + latencyInfo.averageLatency); logger.debug("gearLatency=" + gearLatency); if (latencyInfo.averageLatency === -1) { latencyDescription = LATENCY.UNREACHABLE.description; latencyStyle = LATENCY.UNREACHABLE.style; showJoinLink = false; } else { if (totalLatency <= LATENCY.GOOD.max) { latencyDescription = LATENCY.GOOD.description; latencyStyle = LATENCY.GOOD.style; } else if (totalLatency > LATENCY.MEDIUM.min && totalLatency <= LATENCY.MEDIUM.max) { latencyDescription = LATENCY.MEDIUM.description; latencyStyle = LATENCY.MEDIUM.style; } else { latencyDescription = LATENCY.POOR.description; latencyStyle = LATENCY.POOR.style; //showJoinLink = false; } } // audience var audience = AUDIENCE.OPEN_TO_FANS; if (!(session.fan_access)) { audience = AUDIENCE.MUSICIANS_ONLY; } var i, participant = null; var musicians = ''; if ("participants" in session) { for (i=0; i < session.participants.length; i++) { participant = session.participants[i]; var instrumentLogoHtml = ''; var j; // loop through the tracks to get the instruments for (j=0; j < participant.tracks.length; j++) { var track = participant.tracks[j]; logger.debug("Find:Finding instruments. Participant tracks:"); logger.debug(participant.tracks); var inst = '../assets/content/icon_instrument_default24.png'; if (track.instrument_id in instrument_logo_map) { inst = instrument_logo_map[track.instrument_id]; } instrumentLogoHtml += ' '; } var id = participant.user.id; var name = participant.user.name; var musicianVals = { userId: id, avatar_url: context.JK.resolveAvatarUrl(participant.user.photo_url), profile_url: "/client#/profile/" + id, musician_name: name, instruments: instrumentLogoHtml }; var musician = {}; musician.id = id; musician.name = name; var musicianInfo = context.JK.fillTemplate(musicianTemplate, musicianVals); musicians += musicianInfo; } } var sessionVals = { id: session.id, genres: session.genres.join (', '), description: session.description || "(No description)", musician_template: musicians, audience: audience, latency_text: latencyDescription, latency_style: latencyStyle, sortScore: latencyInfo.sortScore, play_url: context.JK.root_url + "sessions/" + session.id, join_link_display_style: showJoinLink ? "block" : "none" }; var row = context.JK.fillTemplate(rowTemplate, sessionVals); var insertedEarly = false; $.each($('tr', tbGroup), function(index, nextRow) { var $nextRow = $(nextRow); var rowSortScore = parseInt($nextRow.attr('data-sortScore'), 10); if (sessionVals.sortScore > rowSortScore) { $nextRow.before(row); insertedEarly = true; return false; // break } }); if (!insertedEarly) { $(tbGroup).append(row); } // wire up the Join Link to the T&Cs dialog var $parentRow = $('tr[id=' + session.id + ']', tbGroup); $('.join-link', $parentRow).click(function(evt) { // If no FTUE, show that first. if (!context.JK.hasOneConfiguredDevice() || context.JK.TrackHelpers.getUserTracks(context.jamClient).length == 0) { app.afterFtue = function() { joinClick(session.id); }; app.layout.startNewFtue(); } else { joinClick(session.id); } return false; }); } function joinClick(sessionId) { var hasInvitation = false; var session = null; // we need to do a real-time check of the session in case the settings have // changed while the user was sitting on the Find Session screen rest.getSession(sessionId) .done(function(response) { session = response; if ("invitations" in session) { var invitation; // user has invitations for this session for (var i=0; i < session.invitations.length; i++) { invitation = session.invitations[i]; // session contains an invitation for this user if (invitation.receiver_id === JK.currentUserId) { hasInvitation = true; } } } if (session) { // if user has an invitation, always open terms and allow joining regardless of settings if (hasInvitation) { logger.debug("Found invitation for user " + JK.currentUserId + ", session " + sessionId); openTerms(sessionId); } else { if (session.musician_access) { if (session.approval_required) { openAlert(sessionId); } else { openTerms(sessionId); } } } } }) .fail(function(xhr, textStatus, errorMessage) { logger.debug("xhr.status = " + xhr.status); if (xhr.status === 404) { sessionNotJoinableAlert(); } else { JK.app.notify( { title: "Unable to Join Session", text: "There was an unexpected error while attempting to join the session." }, { no_cancel: true }); } }); } function openAlert(sessionId) { var alertDialog = new context.JK.AlertDialog(app, "YES", "You must be approved to join this session. Would you like to send a request to join?", sessionId, onCreateJoinRequest); alertDialog.initialize(); app.layout.showDialog('alert'); } function sessionNotJoinableAlert() { var alertDialog = new context.JK.AlertDialog(app, "OK", "This session is over or is no longer public and cannot be joined. Please click Refresh to update the session list.", null, function(evt) { app.layout.closeDialog('alert'); } ); alertDialog.initialize(); app.layout.showDialog('alert'); } function onCreateJoinRequest(sessionId) { var joinRequest = {}; joinRequest.music_session = sessionId; joinRequest.user = context.JK.currentUserId; rest.createJoinRequest(joinRequest) .done(function(response) { }).error(app.ajaxError); app.layout.closeDialog('alert'); } function openTerms(sessionId) { var termsDialog = new context.JK.TermsDialog(app, sessionId, onTermsAccepted); termsDialog.initialize(); app.layout.showDialog('terms'); } function onTermsAccepted(sessionId) { context.location = '/client#/session/' + sessionId; } function events() { } this.renderSession = renderSession; return this; }; })(window,jQuery);