jam-cloud/web/app/assets/javascripts/rsvpSubmitDialog.js

124 lines
3.5 KiB
JavaScript

(function(context,$) {
"use strict";
context.JK = context.JK || {};
context.JK.RsvpSubmitDialog = function(app, sessionId) {
var logger = context.JK.logger;
var rest = context.JK.Rest();
var $screen = null;
var dialogId = 'rsvp-submit-dialog';
var $btnSubmit = $("#btnSubmitRsvp");
function beforeShow(data) {
$('.error', $screen).hide();
}
function afterShow(data) {
$('.rsvp-instruments', $screen).empty();
rest.getSessionHistory(sessionId)
.done(function(response) {
if (response) {
$('.session-name', $screen).html(response.name);
$('.scheduled-start', $screen).html(response.scheduled_start);
if (response.recurring_mode !== null) {
$('.schedule-recurrence', $screen).html("Recurs " + response.recurring_mode + " on this day at this time");
}
}
})
.fail(function(xhr) {
});
rest.getOpenSessionSlots(sessionId, true)
.done(function(response) {
if (response && response.length > 0) {
$.each(response, function(index, val) {
var instrument = val.instrument_id;
var instrumentTitleCase = context.JK.toTitleCase(instrument);
$('.rsvp-instruments', $screen).append('<input type="checkbox" value="' + val.id + '"/>' + instrumentTitleCase + "<br/>");
});
}
else {
$('.slot-instructions', $screen).hide();
$('.rsvp-instruments', $screen).hide();
}
})
.fail(function(xhr) {
});
}
function afterHide() {
}
function showDialog() {
app.layout.showDialog(dialogId);
}
function events() {
$btnSubmit.unbind('click');
$btnSubmit.click(function(e) {
e.preventDefault();
var slotIds = [];
$("input:checked", '.rsvp-instruments').each(function(index) {
slotIds.push($(this).val());
});
if (slotIds.length === 0) {
$('.error', $screen).show();
return;
}
var error = false;
rest.submitRsvpRequest(sessionId, slotIds)
.done(function(response) {
var comment = $.trim($('#txtComment', $screen).val());
if (comment.length > 0) {
rest.addSessionInfoComment(sessionId, comment)
.done(function(response) {
})
.fail(function(xhr, textStatus, errorMessage) {
error = true;
$('.error', $screen).html("Unexpected error occurred while saving message (" + xhr.status + ")");
$('.error', $screen).show();
});
}
if (!error) {
app.layout.closeDialog(dialogId);
$btnSubmit.trigger("rsvpSubmitEvent");
}
})
.fail(function(xhr, textStatus, errorMessage) {
var error = JSON.parse(xhr.responseText);
$('.error', $screen).html(error.message);
$('.error', $screen).show();
});
});
}
function initialize() {
var dialogBindings = {
'beforeShow' : beforeShow,
'afterShow' : afterShow,
'afterHide': afterHide
};
app.bindDialog(dialogId, dialogBindings);
$screen = $('[layout-id="' + dialogId + '"]');
events();
}
this.initialize = initialize;
this.showDialog = showDialog;
}
return this;
})(window,jQuery);