From 3d8a84c5525b3dccc7353c230f69cfa55b5dda8f Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sat, 1 Mar 2014 22:19:13 -0500 Subject: [PATCH] minor refactor --- web/app/assets/javascripts/sessionList.js | 508 +++++++++--------- web/app/assets/javascripts/sidebar.js | 1 - web/app/views/clients/_hoverMusician.html.erb | 18 +- 3 files changed, 258 insertions(+), 269 deletions(-) diff --git a/web/app/assets/javascripts/sessionList.js b/web/app/assets/javascripts/sessionList.js index 13c513d3f..9c34430a5 100644 --- a/web/app/assets/javascripts/sessionList.js +++ b/web/app/assets/javascripts/sessionList.js @@ -1,263 +1,259 @@ (function(context,$) { - "use strict"; + "use strict"; - context.JK = context.JK || {}; - context.JK.SessionList = function(app) { - var logger = context.JK.logger; - var rest = context.JK.Rest(); + 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; - - 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())) { - app.afterFtue = function() { joinClick(session.id); }; - app.layout.startNewFtue(); - return; - } else { - joinClick(session.id); - } - }); - } - - 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 - $.ajax({ - type: "GET", - url: "/api/sessions/" + sessionId, - async: false, - success: 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 === context.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 " + context.JK.currentUserId + ", session " + sessionId); - openTerms(sessionId); - } - else { - if (session.musician_access) { - if (session.approval_required) { - openAlert(sessionId); - } - else { - openTerms(sessionId); - } - } - } - } - }, - error: function(xhr, textStatus, errorMessage) { - logger.debug("xhr.status = " + xhr.status); - if (xhr.status === 404) { - sessionNotJoinableAlert(); - } - else { - 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; + 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; + + 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())) { + app.afterFtue = function() { joinClick(session.id); }; + app.layout.startNewFtue(); + return; + } else { + joinClick(session.id); + } + }); + } + + 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); \ No newline at end of file diff --git a/web/app/assets/javascripts/sidebar.js b/web/app/assets/javascripts/sidebar.js index 7fd37acce..79204b04e 100644 --- a/web/app/assets/javascripts/sidebar.js +++ b/web/app/assets/javascripts/sidebar.js @@ -737,7 +737,6 @@ handleNotification(payload, header.type); - // TODO: add LISTEN button linking to session app.notify({ "title": "Band Joined Session", "text": payload.msg, diff --git a/web/app/views/clients/_hoverMusician.html.erb b/web/app/views/clients/_hoverMusician.html.erb index a5ce0f6fa..d5a95312a 100644 --- a/web/app/views/clients/_hoverMusician.html.erb +++ b/web/app/views/clients/_hoverMusician.html.erb @@ -16,18 +16,15 @@ // }); // } -/*********** TODO: THE NEXT 6 FUNCTIONS ARE COPIED FROM sessionList.js. REFACTOR TO COMMON PLACE. *************/ + /*********** TODO: THE NEXT 6 FUNCTIONS ARE COPIED FROM sessionList.js. REFACTOR TO COMMON PLACE. *************/ 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 - $.ajax({ - type: "GET", - url: "/api/sessions/" + sessionId, - async: false, - success: function(response) { + rest.getSession(sessionId) + .done(function(response) { session = response; if ("invitations" in session) { var invitation; @@ -58,8 +55,8 @@ } } } - }, - error: function(xhr, textStatus, errorMessage) { + }) + .fail(function(xhr, textStatus, errorMessage) { logger.debug("xhr.status = " + xhr.status); if (xhr.status === 404) { sessionNotJoinableAlert(); @@ -71,8 +68,7 @@ }, { no_cancel: true }); } - } - }); + }); } function openAlert(sessionId) { @@ -110,9 +106,7 @@ } function openTerms(sessionId) { - console.log("sessionId=%o", sessionId); var termsDialog = new JK.TermsDialog(JK.app, sessionId, onTermsAccepted); - console.log("sessionId=%o", sessionId); termsDialog.initialize(); JK.app.layout.showDialog('terms'); }