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

124 lines
3.6 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;
function beforeShow(data) {
}
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) {
});
// if the session has slots, get the open ones
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);
// var instrumentTitleCase = instrument.charAt(0).toUpperCase() + instrument.substr(1).toLowerCase();
// var instTitleCase = val.instrument_id.charAt(0).toUpperCase() + context.val.instrument_id.charAt(0)
$('.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('rsvp-submit-dialog');
}
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) {
error = true;
$('.error', $screen).html("Unexpected error occurred while saving message (" + xhr.status + ")");
$('.error', $screen).show();
});
}
})
.fail(function(xhr) {
error = true;
$('.error', $screen).html("Unexpected error occurred while saving RSVP request (" + xhr.status + ")");
$('.error', $screen).show();
});
if (!error) {
app.layout.closeDialog('rsvp-submit-dialog');
$("#btnSubmit").trigger("rsvpSubmitEvent");
}
});
}
function initialize() {
var dialogBindings = {
'beforeShow' : beforeShow,
'afterShow' : afterShow,
'afterHide': afterHide
};
app.bindDialog('rsvp-submit-dialog', dialogBindings);
$screen = $('[layout-id="rsvp-submit-dialog"]');
events();
}
this.initialize = initialize;
this.showDialog = showDialog;
}
return this;
})(window,jQuery);