= simple_form_for(model_object, :url => url, :html => {:class => 'carnival-form container-fluid'}) do |f| = render "carnival/shared/form/inner_form", button: button, model_object: model_object, model_presenter: model_presenter, :url => url, :action => action, :f => f .submit_button = f.submit button, disable_with: t('please_wait'), class: 'submit' :javascript var nestedForms = {}; $(document).ready(function(){ nestedForm = $(".nested-form-list .form-new-association").each(function(index, element){ nestedForms[getFormName(element)] = element; element.remove(); }); $(".existing-options").hide(); $(".nested-form-subtitle").hide(); }); function createNewForm(selector, form){ $(".nested-form-subtitle").show(); newForm = $(nestedForms[form]).clone().wrap("
").parent(); $(newForm).find("select").show(); $(newForm).find(".chosen-container").remove(); newForm = $(newForm).html() var last_index = $(selector + " li").length newForm = newForm.replace(/([_a-zA-Z]+_attributes_)\d+(_[a-zA-Z]+)/g, "$1" + last_index + "$2"); newForm = newForm.replace(/([_a-zA-Z]+\[[_a-zA-Z]+_attributes\]\[)\d+(\]\[[_a-zA-Z]+\])/g, "$1" + last_index + "$2"); $(selector).append(newForm); $(selector).find(".carnival-select").select2({width: '100%'}); $(selector + " li:last")[0].scrollIntoView(true) setupDateFields(); var lastFormAdded = $(selector).find(".nested-form-list-item").last(); lastFormAdded.find('.select2-remote').each(function(){ addSelect2ToField(this); }); } function getFormName(element){ var classes = $(element).parent().attr("class").split(" "); classes.splice(classes.indexOf("nested-form-list"), 1); return classes[0]; } function removeNestedFormElement(element){ $(element).parent().parent().hide(); $(element).parents("li").find(".destroy").val("1"); } function toggleNestedForm(element){ $(element).parent().parent().find("ul.nested-form-list").toggle(); if($(element).text() == "#{t('nested_form.close')}") $(element).text("#{t('nested_form.open')}"); else $(element).text("#{t('nested_form.close')}"); $(".existing-options").toggle(); } function renderSelectOptions(selector){ $(selector).show(); } function removeRelationOption(id, selectId){ $('#'+id+'_options').hide(); $('#'+id).attr('checked', false); $('#'+id+'_select_option').prop('disabled', false); //$('#'+selectId).trigger("chosen:updated"); } function getNestedFormOptions(params){ $('#nested_form_options_'+params['field_name']).html(); var elemId = params['scope_html_id']; var scopeValue = $('#'+elemId).val(); var key = params['scope_name']; params['carnival_list_scope'] = {}; params['carnival_list_scope'][key] = scopeValue; $.ajax({ url: params['url'], data: params, success: function(data){ $('#nested_form_options_'+params['field_name']).html(data.template); var $select = $('#nested_form_options_'+params['field_name']).find('select'); updateNestedFormSelectHtml($select, params['has_new_action']); } }); } function updateDependentFormSelectHtml(load_dependent_path, presenter, value, field, dependentField){ if(value != ""){ $.ajax(load_dependent_path + "/" + presenter + "/" + dependentField + "/" + field + "/" + value) .done(function(data){ var selector = "select.field-" + dependentField.replace("_id", ""); $(selector).html(data); //$(selector).trigger("chosen:updated"); }) .error(function(err, data){ console.log("updateDependentFormSelectHtml ajax error " + err); }); } } function updateNestedFormSelectHtml($select, hasNewAction){ $select.css({width: $select.parent().css("width")}) if(!hasNewAction) return; $select.chosen( { create_option_text: 'Criar', create_option: function(){createNewForm('.nested-form-list.'+params['field_name'], params['field_name'])} }); }