jam-cloud/app/assets/javascripts/findSession.js

329 lines
11 KiB
JavaScript
Raw Normal View History

(function(context,$) {
"use strict";
context.JK = context.JK || {};
context.JK.FindSessionScreen = function(app) {
2013-01-21 05:52:28 +00:00
var CATEGORY = {
2013-02-07 04:24:35 +00:00
INVITATION : {index: 0, id: "table#sessions-invitations"},
2013-02-07 07:14:45 +00:00
FRIEND : {index: 1, id: "table#sessions-friends"},
2013-02-07 04:24:35 +00:00
OTHER : {index: 2, id: "table#sessions-other"}
2013-01-30 00:36:54 +00:00
};
2013-01-21 05:52:28 +00:00
var logger = context.JK.logger;
2012-11-28 02:29:46 +00:00
var sessionLatency;
2013-01-30 00:36:54 +00:00
var genreSelector = new context.JK.GenreSelector(app);
var sessions = {};
2013-01-27 20:17:46 +00:00
var invitationSessionGroup = {};
var friendSessionGroup = {};
var otherSessionGroup = {};
2013-02-07 07:14:45 +00:00
var sessionCounts = [0, 0, 0];
2013-01-25 08:00:53 +00:00
var sessionList;
2013-01-30 00:36:54 +00:00
// for unit tests
function getCategoryEnum() {
return CATEGORY;
}
// for unit tests
function getGenreSelector() {
return genreSelector;
}
function loadSessions() {
$.ajax({
type: "GET",
2012-11-28 02:29:46 +00:00
url: "/api/sessions",
2013-02-07 07:14:45 +00:00
async: false,
success: startSessionLatencyChecks
2012-11-28 02:29:46 +00:00
});
}
function search() {
clearResults();
var queryString = '';
// musician filter
var musicians = getSelectedMusicians();
if (musicians != null && musicians.length > 0) {
queryString += "participants=" + musicians.join(',');
}
// genre filter
var genres = genreSelector.getSelectedGenres();
if (genres != null && genres.length > 0) {
if (queryString.length > 0) {
queryString += "&";
}
queryString += "genres=" + genres.join(',');
}
2013-02-02 23:53:57 +00:00
// keyword filter
var keyword = $('#session-keyword-srch').val();
if (keyword != null && keyword.length > 0) {
if (queryString.length > 0) {
queryString += "&";
}
queryString += "keyword=" + $('#session-keyword-srch').val();
}
if (queryString.length > 0) {
$.ajax({
type: "GET",
url: "/api/sessions?" + queryString,
success: startSessionLatencyChecks
});
}
else {
loadSessions();
}
}
2013-02-07 07:14:45 +00:00
function initializeDisplay() {
var priorVisible;
var INVITATION = 'div#sessions-invitations';
var FRIEND = 'div#sessions-friends';
var OTHER = 'div#sessions-other';
// INVITATION
//alert(sessionCounts[CATEGORY.INVITATION.index]);
if (sessionCounts[CATEGORY.INVITATION.index] == 0) {
priorVisible = false;
$(INVITATION).hide();
}
else {
priorVisible = true;
$(INVITATION).show();
}
// FRIEND
if (!priorVisible) {
$(FRIEND).removeClass('mt35');
}
//alert(sessionCounts[CATEGORY.FRIEND.index]);
if (sessionCounts[CATEGORY.FRIEND.index] == 0) {
priorVisible = false;
$(FRIEND).hide();
}
else {
priorVisible = true;
$(FRIEND).show();
}
// OTHER
if (!priorVisible) {
$(OTHER).removeClass('mt35');
}
//alert(sessionCounts[CATEGORY.OTHER.index]);
if (sessionCounts[CATEGORY.OTHER.index] == 0) {
$(OTHER).hide();
}
else {
$(OTHER).show();
}
}
function getSelectedMusicians() {
var selectedMusicians = [];
$('#musician-list-items :checked').each(function() {
selectedMusicians.push($(this).val());
});
return selectedMusicians;
}
function startSessionLatencyChecks(response) {
sessionLatency.subscribe(app.clientId, latencyResponse);
$.each(response, function(index, session) {
sessions[session.id] = session;
2013-01-25 08:00:53 +00:00
// store session in the appropriate bucket
2013-01-27 20:17:46 +00:00
if (containsInvitation(session)) {
invitationSessionGroup[session.id] = session;
2013-02-07 07:14:45 +00:00
sessionCounts[CATEGORY.INVITATION.index]++;
2013-01-25 08:00:53 +00:00
}
else if (containsFriend(session)) {
2013-01-27 20:17:46 +00:00
friendSessionGroup[session.id] = session;
2013-02-07 07:14:45 +00:00
sessionCounts[CATEGORY.FRIEND.index]++;
2013-01-25 08:00:53 +00:00
}
else {
2013-01-27 20:17:46 +00:00
otherSessionGroup[session.id] = session;
2013-02-07 07:14:45 +00:00
sessionCounts[CATEGORY.OTHER.index]++;
2013-01-27 20:17:46 +00:00
}
sessionLatency.sessionPings(session);
});
}
2013-01-25 08:00:53 +00:00
function containsInvitation(session) {
2013-02-01 06:05:57 +00:00
var i, invitation = null;
2013-01-25 08:00:53 +00:00
// user has invitations for this session
2013-02-01 06:05:57 +00:00
for (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) {
return true;
}
2013-01-25 08:00:53 +00:00
}
2013-02-01 06:05:57 +00:00
return false;
2013-01-25 08:00:53 +00:00
}
function containsFriend(session) {
var i, participant = null;
2013-01-25 08:00:53 +00:00
for (i=0; i < session.participants.length; i++) {
2013-01-25 08:00:53 +00:00
participant = session.participants[i];
// this session participant is a friend
2013-01-28 03:05:00 +00:00
if (participant.user.is_friend) {
2013-01-25 08:00:53 +00:00
return true;
}
}
return false;
}
function latencyResponse(sessionId) {
renderSession(sessionId);
}
/**
* Not used normally. Allows modular unit testing
* of the renderSession method without having to do
* as much heavy setup.
*/
2013-01-30 00:36:54 +00:00
function setSession(session) {
invitationSessionGroup[session.id] = session;
}
/**
* Render a single session line into the table.
* It will be inserted at the appropriate place according to the
* sortScore in sessionLatency.
*/
function renderSession(sessionId) {
2013-01-25 08:00:53 +00:00
var session = null;
var $tbGroup;
2013-01-27 20:17:46 +00:00
if (invitationSessionGroup[sessionId] != null) {
session = invitationSessionGroup[sessionId];
2013-01-25 08:00:53 +00:00
$tbGroup = $(CATEGORY.INVITATION.id);
}
2013-01-27 20:17:46 +00:00
else if (friendSessionGroup[sessionId] != null) {
session = friendSessionGroup[sessionId];
2013-02-07 07:14:45 +00:00
$tbGroup = $(CATEGORY.FRIEND.id);
2013-01-25 08:00:53 +00:00
}
2013-01-27 20:17:46 +00:00
else if (otherSessionGroup[sessionId] != null) {
session = otherSessionGroup[sessionId];
2013-01-25 08:00:53 +00:00
$tbGroup = $(CATEGORY.OTHER.id);
}
else {
2013-01-30 00:36:54 +00:00
//alert('ERROR: No session with ID = ' + sessionId + ' found.');
2013-01-25 08:00:53 +00:00
return;
}
2013-01-25 08:00:53 +00:00
var row = sessionList.renderSession(session, sessionLatency, $tbGroup, $('#template-session-row').html(), $('#template-musician-info').html(),
// populate the musician filter with musicians that haven't already been added
function(musicianArray) {
var template = $('#template-musician-filter').html();
$.each(musicianArray, function(index, val) {
// check if this musician is already in the filter
if ( $('#musician-list-items input[value=' + val.id + ']').length == 0 ) {
var musicianOptionHtml = context.JK.fillTemplate(template, {value: val.id, label: val.name});
$('#musician-list-items').append(musicianOptionHtml);
}
});
});
}
// TODO: refactor this and GenreSelector into common code
function toggleMusicianBox() {
var boxHeight = $('#musician-list').css("height");
// TODO: clean this up (check class name of arrow to determine current state)
if (boxHeight == "20px") {
$('#musician-list').css({height: "auto"});
$('#musician-list-arrow').removeClass("arrow-down").addClass("arrow-up");
}
else {
$('#musician-list').css({height: "20px"});
$('#musician-list-arrow').removeClass("arrow-up").addClass("arrow-down");
}
}
function afterShow(data) {
2013-01-30 00:36:54 +00:00
clearResults();
loadSessions();
2013-02-07 07:14:45 +00:00
initializeDisplay();
}
2013-01-25 08:00:53 +00:00
function clearResults() {
2013-02-07 04:24:35 +00:00
$(CATEGORY.INVITATION).children(':not(:first-child)').remove();
2013-02-07 07:14:45 +00:00
$(CATEGORY.FRIEND).children(':not(:first-child)').remove();
2013-02-07 04:24:35 +00:00
$(CATEGORY.OTHER).children(':not(:first-child)').remove();
2013-01-25 08:00:53 +00:00
}
function deleteSession(evt) {
var sessionId = $(evt.currentTarget).attr("action-id");
if (sessionId) {
$.ajax({
type: "DELETE",
url: "/api/sessions/" + sessionId
}).done(loadSessions);
}
2013-02-07 07:14:45 +00:00
}renderSession
function events() {
2013-01-25 08:00:53 +00:00
//$('#findSession-tableBody').on("click", '[action="delete"]', deleteSession);
$('#musician-list-header').on("click", toggleMusicianBox);
$('#musician-list-arrow').on("click", toggleMusicianBox);
2013-02-02 23:53:57 +00:00
$('#session-keyword-srch').focus(function() {
$(this).val('');
});
// $('#session-keyword-srch').blur(function() {
// $(this).val('Search by Keyword');
// });
$('#btn-refresh').on("click", search);
}
/**
* Initialize, providing an instance of the SessionLatency class.
*/
2013-01-25 08:00:53 +00:00
function initialize(latency) {
2013-01-21 05:52:28 +00:00
genreSelector.initialize('Any genre', 0, $('#find-session-form'));
2013-01-25 08:00:53 +00:00
if (latency) {
sessionLatency = latency;
}
else {
logger.warn("No sessionLatency provided.");
2012-11-28 02:29:46 +00:00
}
2013-01-25 08:00:53 +00:00
sessionList = new context.JK.SessionList(app);
var screenBindings = {
'afterShow': afterShow
};
app.bindScreen('findSession', screenBindings);
events();
}
this.initialize = initialize;
this.renderSession = renderSession;
2013-01-30 00:36:54 +00:00
this.afterShow = afterShow;
// Following exposed for easier testing.
2013-01-25 08:00:53 +00:00
this.setSession = setSession;
2013-01-30 00:36:54 +00:00
this.clearResults = clearResults;
this.getCategoryEnum = getCategoryEnum;
this.getGenreSelector = getGenreSelector;
2013-01-21 05:52:28 +00:00
return this;
};
})(window,jQuery);