2016-08-16 15:48:19 +08:00
|
|
|
(function(){
|
|
|
|
|
|
|
|
// Create ajax hook on given 'element', which should return modal with 'id' =>
|
|
|
|
// show that modal
|
|
|
|
function initializeModal(element, id){
|
2016-09-21 21:54:12 +08:00
|
|
|
|
|
|
|
// Initialize new experiment modal listener
|
2016-08-16 15:48:19 +08:00
|
|
|
$(element)
|
|
|
|
.on("ajax:beforeSend", function(){
|
|
|
|
animateSpinner();
|
|
|
|
})
|
|
|
|
.on("ajax:success", function(e, data){
|
|
|
|
$('body').append($.parseHTML(data.html));
|
|
|
|
$(id).modal('show',{
|
|
|
|
backdrop: true,
|
|
|
|
keyboard: false,
|
|
|
|
});
|
2016-09-02 22:31:17 +08:00
|
|
|
validateMoveModal(id);
|
2016-09-22 15:50:17 +08:00
|
|
|
clearModal($(id));
|
2017-04-20 23:55:28 +08:00
|
|
|
validateExperimentForm(id);
|
2016-08-16 15:48:19 +08:00
|
|
|
})
|
|
|
|
.on("ajax:error", function() {
|
|
|
|
animateSpinner(null, false);
|
|
|
|
// TODO
|
|
|
|
})
|
|
|
|
.on("ajax:complete", function(){
|
|
|
|
animateSpinner(null, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-22 15:50:17 +08:00
|
|
|
function clearModal(id) {
|
|
|
|
//Completely remove modal when it gets closed
|
|
|
|
$(id).on('hidden.bs.modal', function() {
|
|
|
|
$(id).remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:48:19 +08:00
|
|
|
// Initialize dropdown actions on experiment:
|
|
|
|
// - edit
|
|
|
|
// - clone
|
|
|
|
function initializeDropdownActions(){
|
|
|
|
// { buttonClass: modalName } mappings
|
|
|
|
// click on buttonClass summons modalName dialog
|
|
|
|
modals = {
|
|
|
|
'.edit-experiment': '#edit-experiment-modal-',
|
|
|
|
'.clone-experiment': '#clone-experiment-modal-',
|
|
|
|
'.move-experiment': '#move-experiment-modal-'
|
2016-08-23 14:52:53 +08:00
|
|
|
};
|
2016-08-16 15:48:19 +08:00
|
|
|
|
|
|
|
$.each($(".dropdown-experiment-actions"), function(){
|
|
|
|
var $dropdown = $(this);
|
|
|
|
$.each(modals, function(buttonClass, modalName) {
|
|
|
|
var id = modalName + $dropdown.data('id');
|
|
|
|
initializeModal($dropdown.find(buttonClass), id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-02 22:31:17 +08:00
|
|
|
// Validates move action
|
|
|
|
function validateMoveModal(modal){
|
|
|
|
if ( modal.match(/#move-experiment-modal-[0-9]*/) ) {
|
|
|
|
var form = $(modal).find("form");
|
|
|
|
form
|
|
|
|
.on('ajax:success', function(e, data) {
|
|
|
|
animateSpinner(form, true);
|
|
|
|
window.location.replace(data.path);
|
|
|
|
})
|
|
|
|
.on('ajax:error', function(e, error) {
|
2016-09-26 15:55:25 +08:00
|
|
|
form.clearFormErrors();
|
2016-09-02 22:31:17 +08:00
|
|
|
var msg = JSON.parse(error.responseText);
|
|
|
|
renderFormError(e,
|
|
|
|
form.find("#experiment_project_id"),
|
2016-09-16 15:04:21 +08:00
|
|
|
msg.message.toString());
|
2016-09-26 15:55:25 +08:00
|
|
|
});
|
2016-09-02 22:31:17 +08:00
|
|
|
}
|
2016-09-06 17:57:03 +08:00
|
|
|
}
|
2017-04-20 23:55:28 +08:00
|
|
|
|
2016-09-22 15:29:38 +08:00
|
|
|
// Reload after successfully updated experiment
|
2016-09-22 16:18:21 +08:00
|
|
|
function validateExperimentForm(element){
|
2017-04-20 23:55:28 +08:00
|
|
|
if ( $(element) ) {
|
|
|
|
var form = $(element).find("form");
|
2016-09-22 15:29:38 +08:00
|
|
|
form
|
2017-04-20 23:55:28 +08:00
|
|
|
.on('ajax:success' , function(e, data){
|
2016-09-22 15:29:38 +08:00
|
|
|
animateSpinner(form, true);
|
2017-04-20 23:55:28 +08:00
|
|
|
if ( element.match(/#new-experiment-modal/) ) {
|
|
|
|
window.location.replace(data.path);
|
|
|
|
} else {
|
|
|
|
location.reload();
|
|
|
|
}
|
2016-09-22 16:18:21 +08:00
|
|
|
})
|
|
|
|
.on('ajax:error', function(e, error){
|
|
|
|
var msg = JSON.parse(error.responseText);
|
|
|
|
if ( 'name' in msg ) {
|
|
|
|
renderFormError(e,
|
2017-04-20 23:55:28 +08:00
|
|
|
$(element).find("#experiment-name"),
|
2016-09-22 16:18:21 +08:00
|
|
|
msg.name.toString(),
|
|
|
|
true);
|
|
|
|
} else if ( 'description' in msg ) {
|
|
|
|
renderFormError(e,
|
2017-04-20 23:55:28 +08:00
|
|
|
$(element).find("#experiment-description"),
|
2016-09-22 16:18:21 +08:00
|
|
|
msg.description.toString(),
|
|
|
|
true);
|
|
|
|
} else {
|
|
|
|
renderFormError(e,
|
2017-04-20 23:55:28 +08:00
|
|
|
$(element).find("#experiment-name"),
|
2016-09-22 16:18:21 +08:00
|
|
|
error.statusText,
|
|
|
|
true);
|
|
|
|
}
|
2016-09-22 15:29:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 14:52:53 +08:00
|
|
|
// Initialize no description edit link
|
|
|
|
function initEditNoDescription(){
|
|
|
|
var modal = "#edit-experiment-modal-";
|
2016-09-21 21:54:12 +08:00
|
|
|
$.each($(".experiment-no-description a"), function(){
|
2016-08-23 14:52:53 +08:00
|
|
|
var id = modal + $(this).data("id");
|
|
|
|
initializeModal($(this), id);
|
|
|
|
});
|
|
|
|
}
|
2016-08-16 15:48:19 +08:00
|
|
|
// Bind modal to new-experiment action
|
|
|
|
initializeModal($("#new-experiment"), '#new-experiment-modal');
|
2016-09-02 22:31:17 +08:00
|
|
|
|
2016-08-22 21:22:05 +08:00
|
|
|
// Bind modal to big-plus new experiment actions
|
|
|
|
initializeModal('.big-plus', '#new-experiment-modal');
|
2016-08-16 15:48:19 +08:00
|
|
|
|
|
|
|
// Bind modal to all actions listed on dropdown accesible from experiment
|
|
|
|
// panel
|
|
|
|
initializeDropdownActions();
|
2016-08-23 14:52:53 +08:00
|
|
|
|
|
|
|
// init
|
|
|
|
initEditNoDescription();
|
2016-08-16 15:48:19 +08:00
|
|
|
})();
|