2019-06-20 04:54:20 +08:00
|
|
|
function applyClickCallbackOnProtocolCards() {
|
|
|
|
$('.protocol-card').off('click').on('click', function(e) {
|
2019-06-28 16:41:20 +08:00
|
|
|
var currProtocolCard = $(this);
|
|
|
|
|
2019-06-28 06:43:05 +08:00
|
|
|
// Check whether this card is already active and deactivate it
|
2019-06-28 16:41:20 +08:00
|
|
|
if ($(currProtocolCard).hasClass('active')) {
|
2019-06-28 06:43:05 +08:00
|
|
|
resetPreviewPanel();
|
2019-06-28 16:41:20 +08:00
|
|
|
$(currProtocolCard).removeClass('active');
|
|
|
|
} else {
|
2019-06-28 06:43:05 +08:00
|
|
|
$('.protocol-card').removeClass('active');
|
2019-06-28 16:41:20 +08:00
|
|
|
currProtocolCard.addClass('active');
|
2019-06-28 06:43:05 +08:00
|
|
|
|
|
|
|
$.ajax({
|
2019-06-28 16:41:20 +08:00
|
|
|
url: $(currProtocolCard).data('show-url'),
|
2019-06-28 06:43:05 +08:00
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
2019-06-28 16:41:20 +08:00
|
|
|
protocol_source: $(currProtocolCard).data('protocol-source'),
|
|
|
|
protocol_id: $(currProtocolCard).data('show-protocol-id')
|
2019-06-28 06:43:05 +08:00
|
|
|
},
|
|
|
|
beforeSend: animateSpinner($('.protocol-preview-panel'), true),
|
|
|
|
success: function(data) {
|
|
|
|
$('.empty-preview-panel').hide();
|
|
|
|
$('.full-preview-panel').show();
|
2019-06-28 16:41:20 +08:00
|
|
|
$('.btn-holder').html($(currProtocolCard).find('.external-import-btn').clone());
|
2019-06-28 06:43:05 +08:00
|
|
|
$('.preview-iframe').contents().find('body').html(data.html);
|
2019-06-28 07:38:33 +08:00
|
|
|
|
|
|
|
initLoadProtocolModalPreview();
|
2019-06-28 06:43:05 +08:00
|
|
|
animateSpinner($('.protocol-preview-panel'), false);
|
|
|
|
},
|
|
|
|
error: function(_error) {
|
|
|
|
// TODO: we should probably show some alert bubble
|
|
|
|
resetPreviewPanel();
|
|
|
|
animateSpinner($('.protocol-preview-panel'), false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-06-20 04:54:20 +08:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:48:56 +08:00
|
|
|
// Resets preview to the default state
|
|
|
|
function resetPreviewPanel() {
|
|
|
|
$('.empty-preview-panel').show();
|
|
|
|
$('.full-preview-panel').hide();
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:43:05 +08:00
|
|
|
// Reset whole view to the default state
|
2019-06-28 04:48:56 +08:00
|
|
|
function setDefaultViewState() {
|
|
|
|
resetPreviewPanel();
|
|
|
|
$('.empty-text').show();
|
|
|
|
$('.list-wrapper').hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply AJAX callbacks onto the search box
|
|
|
|
function applySearchCallback() {
|
2019-06-28 07:50:52 +08:00
|
|
|
var timeout;
|
2019-06-28 16:41:20 +08:00
|
|
|
|
2019-06-28 04:48:56 +08:00
|
|
|
// Submit form on every input in the search box
|
|
|
|
$('input[name="key"]').off('input').on('input', function() {
|
2019-06-28 16:41:20 +08:00
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
2019-06-28 07:50:52 +08:00
|
|
|
|
|
|
|
timeout = setTimeout(function() {
|
|
|
|
$('form.protocols-search-bar').submit();
|
|
|
|
}, 500);
|
2019-06-28 04:48:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Submit form when clicking on sort buttons
|
2019-06-28 16:41:20 +08:00
|
|
|
$('input[name="sort_by"]').off('change').on('change', function() {
|
2019-06-28 04:48:56 +08:00
|
|
|
$('form.protocols-search-bar').submit();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Bind ajax calls on the form
|
|
|
|
$('form.protocols-search-bar').off('ajax:success').off('ajax:error')
|
|
|
|
.bind('ajax:success', function(evt, data, status, xhr) {
|
|
|
|
if (data.html) {
|
|
|
|
resetPreviewPanel();
|
|
|
|
$('.empty-text').hide();
|
|
|
|
$('.list-wrapper').show();
|
|
|
|
|
2019-06-28 08:31:25 +08:00
|
|
|
$('.list-wrapper').html(data.html);
|
2019-06-28 04:48:56 +08:00
|
|
|
applyClickCallbackOnProtocolCards();
|
2019-06-28 06:43:05 +08:00
|
|
|
initLoadProtocolModalPreview();
|
2019-06-28 04:48:56 +08:00
|
|
|
} else {
|
|
|
|
setDefaultViewState();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bind("ajax:error", function(evt, xhr, status, error) {
|
|
|
|
setDefaultViewState();
|
|
|
|
|
|
|
|
console.log(xhr.responseText);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:43:05 +08:00
|
|
|
function resetFormErrors(modal) {
|
|
|
|
// Reset all errors
|
|
|
|
modal.find('form > .form-group.has-error').removeClass('has-error');
|
|
|
|
modal.find('form > .form-group>span.help-block').html('');
|
|
|
|
modal.find('.general-error > span').html('');
|
|
|
|
}
|
|
|
|
|
|
|
|
function showFormErrors(modal, errors) {
|
|
|
|
resetFormErrors(modal);
|
|
|
|
|
|
|
|
// AR valdiation errors
|
|
|
|
Object.keys(errors.protocol).forEach(function(key) {
|
|
|
|
var input = modal.find('#protocol_' + key);
|
|
|
|
var msg;
|
|
|
|
msg = key.charAt(0).toUpperCase() + key.slice(1) + ': ' + errors.protocol[key].join(', ');
|
|
|
|
if ((input.length > 0) && (errors.protocol[key].length > 0)) {
|
|
|
|
input.parent().next('span.help-block').html(msg);
|
|
|
|
input.parent().parent().addClass('has-error');
|
|
|
|
} else if (errors.protocol[key].length > 0) {
|
|
|
|
modal.find('.general-error > span').append(msg + '<br/>');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderTable(table) {
|
|
|
|
$(table).handsontable('render');
|
|
|
|
// Yet another dirty hack to solve HandsOnTable problems
|
|
|
|
if (parseInt($(table).css('height'), 10) < parseInt($(table).css('max-height'), 10) - 30) {
|
|
|
|
$(table).find('.ht_master .wtHolder').css({ height: '100%', width: '100%' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand all steps
|
|
|
|
function expandAllSteps() {
|
|
|
|
$('.step .panel-collapse').collapse('show');
|
|
|
|
$(document).find("[data-role='step-hot-table']").each(function() {
|
|
|
|
renderTable($(this));
|
|
|
|
});
|
|
|
|
$(document).find('span.collapse-step-icon').each(function() {
|
|
|
|
$(this).addClass('fa-caret-square-up');
|
|
|
|
$(this).removeClass('fa-caret-square-down');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleFormSubmit(modal) {
|
|
|
|
var form = modal.find('form');
|
|
|
|
form.on('submit', function(e) {
|
|
|
|
var url = form.attr('action');
|
|
|
|
e.preventDefault(); // avoid to execute the actual submit of the form.
|
|
|
|
e.stopPropagation();
|
|
|
|
animateSpinner(modal, true);
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: url,
|
|
|
|
data: form.serialize(), // serializes the form's elements.
|
|
|
|
success: function(data) {
|
|
|
|
animateSpinner(modal, false);
|
|
|
|
window.location.replace(data.redirect_url);
|
|
|
|
},
|
|
|
|
error: function(data) {
|
|
|
|
showFormErrors(modal, data.responseJSON.errors);
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
animateSpinner(modal, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initLoadProtocolModalPreview() {
|
2019-06-28 08:31:25 +08:00
|
|
|
$('.external-import-btn').off('click').on('click', function(e) {
|
2019-06-28 08:00:05 +08:00
|
|
|
var link = $(this).parents('.protocol-card');
|
2019-06-28 14:49:12 +08:00
|
|
|
|
|
|
|
// When clicking on the banner button, we have no protocol-card parent
|
2019-06-28 16:41:20 +08:00
|
|
|
if (link.length === 0) {
|
2019-06-28 14:49:12 +08:00
|
|
|
link = $('.protocol-card.active');
|
2019-06-28 16:41:20 +08:00
|
|
|
}
|
2019-06-28 14:49:12 +08:00
|
|
|
|
2019-06-28 06:43:05 +08:00
|
|
|
animateSpinner(null, true);
|
|
|
|
$.ajax({
|
|
|
|
url: link.data('url'),
|
|
|
|
type: 'GET',
|
|
|
|
data: {
|
|
|
|
protocol_source: link.data('protocol-source'),
|
|
|
|
protocol_client_id: link.data('id')
|
|
|
|
},
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
var modal = $('#protocol-preview-modal');
|
|
|
|
var modalTitle = modal.find('.modal-title');
|
|
|
|
var modalBody = modal.find('.modal-body');
|
|
|
|
var modalFooter = modal.find('.modal-footer');
|
|
|
|
|
|
|
|
modalTitle.html(data.title);
|
|
|
|
modalBody.html(data.html);
|
|
|
|
modalFooter.html(data.footer);
|
|
|
|
initHandsOnTable(modalBody);
|
|
|
|
modal.modal('show');
|
|
|
|
expandAllSteps();
|
|
|
|
initHandsOnTable(modalBody);
|
|
|
|
|
|
|
|
if (data.validation_errors) {
|
|
|
|
showFormErrors(modal, data.validation_errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
initFormSubmits();
|
|
|
|
handleFormSubmit(modal);
|
|
|
|
},
|
|
|
|
error: function(error) {
|
|
|
|
console.log(error.responseJSON.errors);
|
|
|
|
alert('Server error');
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
animateSpinner(null, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initFormSubmits() {
|
|
|
|
var modal = $('#protocol-preview-modal');
|
2019-06-28 08:00:05 +08:00
|
|
|
modal.find('button[data-action=import_protocol]').off('click').on('click', function() {
|
2019-06-28 06:43:05 +08:00
|
|
|
var form = modal.find('form');
|
|
|
|
var hiddenField = form.find('#protocol_protocol_type');
|
|
|
|
hiddenField.attr('value', $(this).data('import_type'));
|
|
|
|
form.submit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:48:56 +08:00
|
|
|
applySearchCallback();
|