2015-11-09 17:15:51 +01:00
|
|
|
var CHAMPS = 0,
|
|
|
|
PJ = 1,
|
|
|
|
ERROR = -1;
|
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
var ready = function () {
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#add_type_de_champ_procedure").on('click', function (e) {
|
|
|
|
add_new_type_de('champ');
|
2015-10-27 17:31:08 +01:00
|
|
|
});
|
2015-10-28 15:55:31 +01:00
|
|
|
|
|
|
|
$("#add_type_de_piece_justificative_procedure").on('click', function (e) {
|
2015-10-29 17:22:57 +01:00
|
|
|
add_new_type_de('piece_justificative');
|
2015-10-28 15:55:31 +01:00
|
|
|
});
|
2015-10-29 17:22:57 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
add_delete_listener_on_click_for_type_de("champ", "#liste_champ .btn-danger");
|
|
|
|
add_delete_listener_on_click_for_type_de("champ", "#new_type_de_champ .btn-danger");
|
2015-10-29 17:22:57 +01:00
|
|
|
|
2015-10-29 17:39:22 +01:00
|
|
|
add_delete_listener_on_click_for_type_de("piece_justificative", "#liste_piece_justificative .btn-danger");
|
|
|
|
add_delete_listener_on_click_for_type_de("piece_justificative", "#new_type_de_piece_justificative .btn-danger");
|
2015-11-02 10:16:53 +01:00
|
|
|
|
|
|
|
config_up_and_down_button();
|
|
|
|
|
|
|
|
add_action_listener_on_click_for_button_up(".button_up");
|
|
|
|
add_action_listener_on_click_for_button_down(".button_down");
|
2015-11-16 16:16:08 +01:00
|
|
|
|
|
|
|
$("#liste_champ").on("ajax:success", "div", function(event, data, status, xhr) {
|
|
|
|
$(event.target).parents('.form-inline').fadeOut("slow", function() {
|
|
|
|
return $(this).remove();
|
|
|
|
});
|
|
|
|
});
|
2015-10-27 17:31:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$(document).ready(ready);
|
|
|
|
$(document).on('page:load', ready);
|
|
|
|
|
2015-10-29 17:39:22 +01:00
|
|
|
function add_delete_listener_on_click_for_type_de(type_libelle, node_id) {
|
2015-10-29 17:22:57 +01:00
|
|
|
$(node_id).on('click', function (e) {
|
2015-10-29 17:39:22 +01:00
|
|
|
var index = (e.target.id).replace('delete_type_de_' + type_libelle + '_', '').replace('_procedure', '');
|
2015-10-27 17:31:08 +01:00
|
|
|
|
2015-10-29 17:39:22 +01:00
|
|
|
delete_type_de(type_libelle, index);
|
2015-10-29 17:22:57 +01:00
|
|
|
});
|
|
|
|
}
|
2015-10-27 17:31:08 +01:00
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
function add_new_type_de(type_libelle) {
|
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
if (is_champ_or_pj() == ERROR) return false;
|
2015-10-29 17:22:57 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
function is_champ_or_pj() {
|
|
|
|
if (type_libelle == 'champ') return CHAMPS;
|
2015-10-29 17:22:57 +01:00
|
|
|
else if (type_libelle == 'piece_justificative') return PJ;
|
|
|
|
else return ERROR;
|
|
|
|
}
|
2015-10-27 17:31:08 +01:00
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
function which_index() {
|
2015-11-05 11:21:44 +01:00
|
|
|
return (is_champ_or_pj() == CHAMPS ? types_de_champ_index : types_de_piece_justificative_index)
|
2015-10-29 17:22:57 +01:00
|
|
|
}
|
2015-10-28 15:08:23 +01:00
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
$("#liste_" + type_libelle).append($("#type_de_" + type_libelle + "_" + which_index()));
|
|
|
|
$("#new_type_de_" + type_libelle).append($("#type_de_" + type_libelle + "_" + which_index()).clone());
|
2015-10-28 15:08:23 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
if (is_champ_or_pj() == CHAMPS) {
|
|
|
|
types_de_champ_index++;
|
|
|
|
add_new_type_de_champ_params(which_index());
|
2015-10-29 17:22:57 +01:00
|
|
|
}
|
2015-11-05 11:21:44 +01:00
|
|
|
else if (is_champ_or_pj() == PJ) {
|
2015-10-29 17:22:57 +01:00
|
|
|
types_de_piece_justificative_index++;
|
|
|
|
add_new_type_de_piece_justificative_params(which_index());
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#new_type_de_" + type_libelle + " .form-inline").attr('id', 'type_de_' + type_libelle + '_' + which_index());
|
|
|
|
|
2015-11-09 17:15:51 +01:00
|
|
|
config_delete_button(type_libelle, which_index(), is_champ_or_pj())
|
2015-10-29 17:22:57 +01:00
|
|
|
|
|
|
|
$("#new_type_de_" + type_libelle + " #add_type_de_" + type_libelle + "_button").remove();
|
|
|
|
$("#new_type_de_" + type_libelle + " .form-inline").append($("#add_type_de_" + type_libelle + "_button"))
|
2015-11-02 10:16:53 +01:00
|
|
|
|
|
|
|
add_action_listener_on_click_for_button_up("#new_type_de_" + type_libelle + " .button_up")
|
|
|
|
add_action_listener_on_click_for_button_down("#new_type_de_" + type_libelle + " .button_down")
|
|
|
|
|
|
|
|
config_up_and_down_button();
|
2015-10-27 17:31:08 +01:00
|
|
|
}
|
2015-10-28 15:55:31 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
function add_new_type_de_champ_params() {
|
2015-11-09 17:15:51 +01:00
|
|
|
$("#new_type_de_champ .libelle").attr('name', 'procedure[new_type_de_champ[' + types_de_champ_index + ']][libelle]');
|
|
|
|
$("#new_type_de_champ .libelle").attr('id', 'procedure_new_type_de_champ_'+types_de_champ_index+'__libelle');
|
|
|
|
$("#new_type_de_champ .libelle").val('');
|
2015-10-28 15:55:31 +01:00
|
|
|
|
2015-11-09 17:15:51 +01:00
|
|
|
$("#new_type_de_champ .description").attr('name', 'procedure[new_type_de_champ[' + types_de_champ_index + ']][description]');
|
|
|
|
$("#new_type_de_champ .description").attr('id', 'procedure_new_type_de_champ_'+types_de_champ_index+'__description');
|
|
|
|
$("#new_type_de_champ .description").val('');
|
2015-10-29 17:22:57 +01:00
|
|
|
|
2015-11-09 17:15:51 +01:00
|
|
|
$("#new_type_de_champ .type_champs").attr('name', 'procedure[new_type_de_champ[' + types_de_champ_index + ']][type_champs]');
|
|
|
|
$("#new_type_de_champ .type_champs").attr('id', 'procedure_new_type_de_champ_'+types_de_champ_index+'__type_champs');
|
2015-10-28 15:55:31 +01:00
|
|
|
|
2015-11-09 17:15:51 +01:00
|
|
|
$("#new_type_de_champ .order_place").attr('name', 'procedure[new_type_de_champ[' + types_de_champ_index + ']][order_place]');
|
|
|
|
$("#new_type_de_champ .order_place").attr('id', 'procedure_new_type_de_champ_'+types_de_champ_index+'__order_place');
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#new_type_de_champ .order_place").val(parseInt($("#liste_champ .order_place").last().val()) + 1);
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#new_type_de_champ .order_type_de_champ_button").attr('id', 'order_type_de_champ_' + types_de_champ_index + '_button')
|
|
|
|
$("#new_type_de_champ .order_type_de_champ_button .button_up").attr('id', 'order_type_de_champ_' + types_de_champ_index + '_up_procedure')
|
|
|
|
$("#new_type_de_champ .order_type_de_champ_button .button_down").attr('id', 'order_type_de_champ_' + types_de_champ_index + '_down_procedure')
|
2015-10-29 17:22:57 +01:00
|
|
|
}
|
2015-10-28 15:55:31 +01:00
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
function add_new_type_de_piece_justificative_params() {
|
2015-11-09 17:15:51 +01:00
|
|
|
$("#new_type_de_piece_justificative .libelle").attr('name', 'procedure[new_type_de_piece_justificative[' + types_de_piece_justificative_index + ']][libelle]');
|
|
|
|
$("#new_type_de_piece_justificative .libelle").attr('id', 'procedure_new_type_de_piece_justificative_'+types_de_piece_justificative_index+'__libelle');
|
|
|
|
$("#new_type_de_piece_justificative .libelle").val('');
|
2015-10-28 15:55:31 +01:00
|
|
|
|
2015-11-09 17:15:51 +01:00
|
|
|
$("#new_type_de_piece_justificative .description").attr('name', 'procedure[new_type_de_piece_justificative[' + types_de_piece_justificative_index + ']][description]');
|
|
|
|
$("#new_type_de_piece_justificative .description").attr('id', 'procedure_new_type_de_piece_justificative_'+types_de_piece_justificative_index+'__description');
|
|
|
|
$("#new_type_de_piece_justificative .description").val('');
|
2015-10-29 17:22:57 +01:00
|
|
|
}
|
2015-10-28 15:55:31 +01:00
|
|
|
|
2015-10-29 17:39:22 +01:00
|
|
|
function delete_type_de(type_libelle, index) {
|
2015-11-02 10:16:53 +01:00
|
|
|
var delete_node = $("#type_de_" + type_libelle + "_" + index).hide();
|
|
|
|
|
|
|
|
$("#liste_delete_" + type_libelle).append(delete_node);
|
2015-11-09 17:15:51 +01:00
|
|
|
$("#type_de_" + type_libelle + "_" + index + " .destroy").val('true');
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
if (type_libelle == 'champ') {
|
2015-11-02 10:16:53 +01:00
|
|
|
var next_order_place = parseInt($("#type_de_" + type_libelle + "_" + index + " .order_place").val());
|
2015-11-05 11:21:44 +01:00
|
|
|
var type_de_champ_to_change_order_place = $("#liste_champ .order_place");
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
type_de_champ_to_change_order_place.each(function () {
|
2015-11-02 10:16:53 +01:00
|
|
|
if ($(this).val() > next_order_place) {
|
|
|
|
$(this).val(next_order_place++);
|
|
|
|
}
|
|
|
|
});
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#new_type_de_champ .order_place").val(next_order_place);
|
2015-11-02 10:16:53 +01:00
|
|
|
|
|
|
|
config_up_and_down_button();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function config_up_and_down_button() {
|
2015-11-05 11:21:44 +01:00
|
|
|
if ($("#liste_champ .order_place").size() > 0) {
|
2015-11-09 17:15:51 +01:00
|
|
|
var first_index = $("#liste_champ .type_de_champ").first()
|
|
|
|
.attr('id')
|
|
|
|
.replace('type_de_champ_', '');
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-09 17:15:51 +01:00
|
|
|
var last_index = $("#liste_champ .type_de_champ").last()
|
|
|
|
.attr('id')
|
|
|
|
.replace('type_de_champ_', '');
|
2015-11-02 10:16:53 +01:00
|
|
|
|
|
|
|
$(".button_up").show();
|
|
|
|
$(".button_down").show();
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#liste_champ .order_type_de_champ_button").show();
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#order_type_de_champ_" + first_index + "_up_procedure").hide();
|
|
|
|
$("#order_type_de_champ_" + last_index + "_down_procedure").hide();
|
2015-11-02 10:16:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 17:15:51 +01:00
|
|
|
function config_delete_button (type_libelle, index, champ_or_pj){
|
|
|
|
$("#new_type_de_" + type_libelle + " .destroy").attr('name', 'procedure[new_type_de_' + type_libelle + '[' + index + ']][_destroy]');
|
|
|
|
$("#new_type_de_" + type_libelle + " .destroy").attr('id', 'procedure_new_type_de_' + type_libelle + '_' + index + '___destroy');
|
|
|
|
$("#new_type_de_" + type_libelle + " .destroy").val('false');
|
|
|
|
|
|
|
|
$("#new_type_de_" + type_libelle + " #delete_type_de_" + type_libelle + "_" + (index - 1) + "_button").attr('id', "delete_type_de_" + type_libelle + "_" + index + "_button");
|
|
|
|
$("#new_type_de_" + type_libelle + " #delete_type_de_" + type_libelle + "_" + (index - 1) + "_procedure").attr('id', "delete_type_de_" + type_libelle + "_" + index + "_procedure");
|
|
|
|
|
|
|
|
if (champ_or_pj == CHAMPS)
|
|
|
|
add_delete_listener_on_click_for_type_de("champ", "#delete_type_de_champ_" + index + "_procedure");
|
|
|
|
else if (champ_or_pj == PJ)
|
|
|
|
add_delete_listener_on_click_for_type_de("piece_justificative", "#delete_type_de_piece_justificative_" + index + "_procedure");
|
|
|
|
|
|
|
|
$("#delete_type_de_" + type_libelle + "_" + (index - 1) + "_button").show();
|
|
|
|
}
|
|
|
|
|
2015-11-02 10:16:53 +01:00
|
|
|
function add_action_listener_on_click_for_button_up(node_id) {
|
|
|
|
$(node_id).on('click', function (e) {
|
2015-11-05 11:21:44 +01:00
|
|
|
var index = (e.target.id).replace('order_type_de_champ_', '').replace('_up_procedure', '');
|
|
|
|
var order_place = parseInt($("#type_de_champ_" + index + " .order_place").val());
|
2015-11-02 10:16:53 +01:00
|
|
|
var order_place_before = order_place - 1;
|
|
|
|
|
|
|
|
var node_before = $("input[class='order_place'][value='" + order_place_before + "']").parent();
|
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
var index_before = (node_before.attr('id')).replace('type_de_champ_', '');
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#type_de_champ_" + index).insertBefore("#type_de_champ_" + index_before);
|
|
|
|
$("#type_de_champ_" + index_before);
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#type_de_champ_" + index_before + " .order_place").val(order_place);
|
|
|
|
$("#type_de_champ_" + index + " .order_place").val(order_place_before);
|
2015-11-02 10:16:53 +01:00
|
|
|
|
|
|
|
config_up_and_down_button();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_action_listener_on_click_for_button_down(node_id) {
|
|
|
|
$(node_id).on('click', function (e) {
|
2015-11-05 11:21:44 +01:00
|
|
|
var index = (e.target.id).replace('order_type_de_champ_', '').replace('_down_procedure', '');
|
|
|
|
var order_place = parseInt($("#type_de_champ_" + index + " .order_place").val());
|
2015-11-02 10:16:53 +01:00
|
|
|
var order_place_after = order_place + 1;
|
|
|
|
|
|
|
|
var node_after = $("input[class='order_place'][value='" + order_place_after + "']").parent();
|
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
var index_after = (node_after.attr('id')).replace('type_de_champ_', '');
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#type_de_champ_" + index).insertAfter("#type_de_champ_" + index_after);
|
|
|
|
$("#type_de_champ_" + index_after);
|
2015-11-02 10:16:53 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
$("#type_de_champ_" + index_after + " .order_place").val(order_place);
|
|
|
|
$("#type_de_champ_" + index + " .order_place").val(order_place_after);
|
2015-11-02 10:16:53 +01:00
|
|
|
|
|
|
|
config_up_and_down_button();
|
|
|
|
});
|
|
|
|
}
|