/** * This software is governed by the CeCILL-B license. If a copy of this license * is not distributed with this file, you can obtain one at * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt * * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft) * * ============================= * * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt * * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ $(document).ready(function () { $(document.formulaire).on('submit', function (e) { if (!isSubmitChoicesAvalaible()) { e.preventDefault(); e.stopPropagation(); } }); var $next = $('button[name="fin_sondage_autre"]'); var $removeAChoice = $('#remove-a-choice'); var $addAChoice = $('#add-a-choice'); var updateButtonState = function() { var $choiceFields = $('.choice-field'); $removeAChoice.prop('disabled', function() { return $choiceFields.length <= 2; }); $next.prop('disabled', !isSubmitChoicesAvalaible()); } var isSubmitChoicesAvalaible = function () { return (countFilledChoices() >= 2); }; var countFilledChoices = function() { var nb_filled_choices = 0; $('.choice-field input').each(function () { if ($(this).val() != '') { nb_filled_choices++; } }); return nb_filled_choices; }; $removeAChoice.on('click', function () { $('.choice-field:last').remove(); updateButtonState(); }); $addAChoice.on('click', function () { var $choiceFields = $('.choice-field'); var nb_choices = $choiceFields.length; var last_choice = $choiceFields.last(); var new_choice = last_choice.html(); // label var last_choice_label = last_choice.children('label').text(); var choice_text = last_choice_label.substring(0, last_choice_label.indexOf(' ')); // for and id var re_id_choice = new RegExp('"choice' + (nb_choices - 1) + '"', 'g'); var new_choice_html = new_choice.replace(re_id_choice, '"choice' + nb_choices + '"') .replace(last_choice_label, choice_text + ' ' + (nb_choices + 1)) .replace(/value="(.*?)"/g, 'value=""'); last_choice.after('