// Check Questions Length function checkQuestionsLength() { var questionsLength = $('.questions > .attributes').length; if(questionsLength == 1) { $('.questions .delete').addClass('hide'); } else { $('.questions .delete').removeClass('hide'); }; }; // Role Attribute Template Data function setData(l, length, optionsI, className) { if(!optionsI) { optionsI = 0; } var id = "survey_survey_questions_attributes_", name = "survey[survey_questions_attributes][", optionID = l+"_survey_question_options_attributes_"+optionsI+"_name_translations_", optionName = l+"][survey_question_options_attributes]["+optionsI+"][name_translations]", radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_", radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]"; data = { _l: l, _length: length, _className: className, _destroy: [id+l+"_destroy", name+l+"][_destroy]"], _sequence : [id+l+"_sequence",name+l+"][sequence]"], _title_translations: [id+l+"_title_translations", name+l+"][title_translations]", "questions_title_"+l], _description_translations: [id+l+"_description_translations", name+l+"][description_translations]", "questions_depiction_"+l], _type: [id+l+"_type", name+l+"][type]"], _is_required: [id+l+"_is_required", name+l+"][is_required]"], _option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+optionsI+"_"], _radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+optionsI+"_"], _custom_option: [id+l+"_custom_option", name+l+"][custom_option]"] }; return data; }; function inputAppendLength(element, length) { if(length == 2) { element.closest('.add-target').find('.remove-input').addClass('hide').prev().addClass('last'); } element.closest('.input-append').remove(); } $(function() { var l = $('.questions > .attributes').length || 0; if($('#survey_redirect_mode').prop('checked')) { $('.questions').fadeOut(300); $('.redirect-url').fadeIn(300); } else { $('.questions').fadeIn(300); $('.redirect-url').fadeOut(300); }; $('#survey_redirect_mode').change(function(event) { if($(this).prop('checked')) { $('.questions').fadeOut(300); $('.redirect-url').fadeIn(300); } else { $('.questions').fadeIn(300); $('.redirect-url').fadeOut(300); } }); $('.field-type').each(function() { $(this).children('.control-group').each(function() { $(this).data().index = $(this).find('.input-append').length }); }); $('.add-target').each(function() { if($(this).children('.input-append').length == 1) { $(this).find('.remove-input').addClass('hide').prev().addClass('last'); }; }); $('.add-question').on(clickEvent, function(event) { $('#template-survery').tmpl(setData(l, $('.questions > .attributes').length + 1)).appendTo($('.questions')); checkQuestionsLength(); textareaResizable(); // from textarea-lang-btn.js l += 1; }); $('.questions').on('change', '.type-selector', function(event) { $(this).children("option:selected").each(function () { var $fieldType = $(this).closest('.control-group').siblings('.field-type'), _val = $(this).val(), _index = $(this).closest('.attributes').data().index; if(_val < 2) { $fieldType.fadeOut(300); } else { $fieldType.fadeIn(300); if(_val < 5) { $fieldType.find('.rgl, hr').addClass('hide').siblings('.allow').removeClass('hide'); } else { $fieldType.find('.rgl, hr').removeClass('hide').siblings('.allow').addClass('hide'); } }; }); }); $('.questions').on('keyup', '.q-title', function(event) { var $questionsTitle = $(this).closest('.attributes-body').siblings('.attributes-header').find('.questions-title'), $tabPane = $(this).closest('.tab-content'); $questionsTitle.text($tabPane.find('.active input[type="text"]').val()) event.preventDefault(); /* Act on the event */ }); $('.questions').on(clickEvent, '.add-btn a, .remove-input, .delete, .attributes-header h4 i, .btn-group .btn', function(event) { if($(this).hasClass('remove-input')){ inputAppendLength($(this), $(this).closest('.add-target').children('.input-append').length); } else if($(this).hasClass('delete')) { $(this).closest('.attributes').fadeOut(300, function() { $(this).remove(); checkQuestionsLength(); $('.questions > .attributes').each(function(index, el) { $(this).find('h4').find('span').text(index + 1); }); }); } else if($(this).hasClass('icon-caret-up') || $(this).hasClass('icon-caret-down')) { if($(this).hasClass('icon-caret-up')) { $(this).closest('.attributes-header').siblings('.attributes-body').slideUp(300) $(this).addClass('icon-caret-down').removeClass('icon-caret-up'); } else { $(this).closest('.attributes-header').siblings('.attributes-body').slideDown(300) $(this).addClass('icon-caret-up').removeClass('icon-caret-down'); } } else if($(this).parent().hasClass('btn-group')){ var $questionsTitle = $(this).closest('.attributes-body').siblings('.attributes-header').find('.questions-title'), $tabPane = $(this).closest('.btn-group').siblings('.tab-content'); $questionsTitle.text($tabPane.find('div').not('.active').children('input[type="text"]').val()) } else { var _i = $('.questions .attributes').length, _className = $(this).closest('.control-group').attr('class').match(/options|rgl/g)[0], _index = $(this).closest('.attributes').data().index, _length = $(this).closest('.add-btn').siblings('.input-append').length; console.log(_i) $('#template-survery-option').tmpl(setData(_index, "", _i, _className)).insertBefore($(this).closest('.add-btn')); $(this).closest('.add-target').find('.input-append').find('.remove-input').removeClass('hide').prev().removeClass('last'); } event.preventDefault(); }); checkQuestionsLength(); });