2012-10-06 16:36:05 +00:00
|
|
|
(function(context,$) {
|
|
|
|
|
|
2012-12-07 00:28:48 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
2012-10-06 16:36:05 +00:00
|
|
|
context.JK = context.JK || {};
|
|
|
|
|
context.JK.FindSessionScreen = function(app) {
|
2012-10-14 15:48:56 +00:00
|
|
|
var logger = context.JK.logger;
|
2012-11-28 02:29:46 +00:00
|
|
|
var sessionLatency;
|
2012-12-01 23:50:28 +00:00
|
|
|
var sessions = {};
|
|
|
|
|
var selectors = {
|
|
|
|
|
TABLE_BODY: '#findSession-tableBody'
|
|
|
|
|
};
|
2012-10-06 16:36:05 +00:00
|
|
|
|
|
|
|
|
function loadSessions() {
|
|
|
|
|
$.ajax({
|
|
|
|
|
type: "GET",
|
2012-11-28 02:29:46 +00:00
|
|
|
url: "/api/sessions",
|
2012-12-01 23:50:28 +00:00
|
|
|
success: startSessionLatencyChecks
|
2012-11-28 02:29:46 +00:00
|
|
|
});
|
2012-10-06 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-01 23:50:28 +00:00
|
|
|
function startSessionLatencyChecks(response) {
|
2012-12-02 21:12:25 +00:00
|
|
|
sessionLatency.subscribe(app.clientId, latencyResponse);
|
2012-12-01 23:50:28 +00:00
|
|
|
$.each(response, function(index, session) {
|
|
|
|
|
sessions[session.id] = session;
|
|
|
|
|
sessionLatency.sessionPings(session);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function latencyResponse(sessionId) {
|
|
|
|
|
renderSession(sessionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Not used normally. Allows modular unit testing
|
|
|
|
|
* of the renderSession method without having to do
|
|
|
|
|
* as much heavy setup.
|
|
|
|
|
*/
|
|
|
|
|
function setSession(session) { sessions[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) {
|
2012-12-07 00:28:48 +00:00
|
|
|
var $tb = $(selectors.TABLE_BODY);
|
2012-10-06 16:36:05 +00:00
|
|
|
var rowTemplate = $('#template-findSession-row').html();
|
2012-12-01 23:50:28 +00:00
|
|
|
var session = sessions[sessionId];
|
|
|
|
|
var latencyInfo = sessionLatency.sessionInfo(sessionId);
|
|
|
|
|
var vals = {
|
|
|
|
|
id: session.id,
|
|
|
|
|
genres: session.genres.join (','),
|
|
|
|
|
sortScore: latencyInfo.sortScore,
|
|
|
|
|
participants: session.participants.length,
|
|
|
|
|
description: session.description || "(No description)"
|
|
|
|
|
};
|
2012-12-07 00:28:48 +00:00
|
|
|
var row = context.JK.fillTemplate(rowTemplate, vals);
|
2012-12-01 23:50:28 +00:00
|
|
|
var insertedEarly = false;
|
2012-12-07 00:28:48 +00:00
|
|
|
$.each($('tr', $tb), function(index, nextRow) {
|
|
|
|
|
var $nextRow = $(nextRow);
|
|
|
|
|
var rowSortScore = parseInt($nextRow.attr('data-sortScore'), 10);
|
2012-12-01 23:50:28 +00:00
|
|
|
if (vals.sortScore > rowSortScore) {
|
2012-12-07 00:28:48 +00:00
|
|
|
$nextRow.before(row);
|
2012-12-01 23:50:28 +00:00
|
|
|
insertedEarly = true;
|
2012-12-02 21:12:25 +00:00
|
|
|
return false; // break
|
2012-12-01 23:50:28 +00:00
|
|
|
}
|
2012-10-06 16:36:05 +00:00
|
|
|
});
|
2012-12-01 23:50:28 +00:00
|
|
|
if (!(insertedEarly)) {
|
|
|
|
|
return $tb.append(row);
|
|
|
|
|
}
|
2012-10-06 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function afterShow(data) {
|
2012-12-07 00:28:48 +00:00
|
|
|
var $tb = $(selectors.TABLE_BODY);
|
2012-12-01 23:50:28 +00:00
|
|
|
$tb.empty();
|
2012-10-06 16:36:05 +00:00
|
|
|
loadSessions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteSession(evt) {
|
2012-12-07 00:28:48 +00:00
|
|
|
var sessionId = $(evt.currentTarget).attr("action-id");
|
2012-10-06 16:36:05 +00:00
|
|
|
if (sessionId) {
|
|
|
|
|
$.ajax({
|
|
|
|
|
type: "DELETE",
|
|
|
|
|
url: "/api/sessions/" + sessionId
|
|
|
|
|
}).done(loadSessions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function events() {
|
|
|
|
|
$('#findSession-tableBody').on("click", '[action="delete"]', deleteSession);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-01 23:50:28 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize, providing an instance of the SessionLatency class.
|
|
|
|
|
*/
|
|
|
|
|
function initialize(_sessionLatency) {
|
|
|
|
|
if (_sessionLatency) {
|
|
|
|
|
sessionLatency = _sessionLatency;
|
2012-11-28 02:29:46 +00:00
|
|
|
} else {
|
2012-12-01 23:50:28 +00:00
|
|
|
logger.warn("No sessionLatency provided.");
|
2012-11-28 02:29:46 +00:00
|
|
|
}
|
2012-12-07 00:28:48 +00:00
|
|
|
var screenBindings = {
|
2012-10-14 15:48:56 +00:00
|
|
|
'afterShow': afterShow
|
|
|
|
|
};
|
|
|
|
|
app.bindScreen('findSession', screenBindings);
|
|
|
|
|
events();
|
2012-12-01 23:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.initialize = initialize;
|
|
|
|
|
this.afterShow = afterShow;
|
|
|
|
|
this.renderSession = renderSession;
|
|
|
|
|
|
|
|
|
|
// Following exposed for easier testing.
|
|
|
|
|
this.setSession = setSession;
|
|
|
|
|
this.selectors = selectors;
|
|
|
|
|
|
2012-10-06 16:36:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(window,jQuery);
|