Created
March 4, 2013 20:00
-
-
Save gogogarrett/5085092 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="text/javascript"> | |
| var SubmissionForm = { | |
| edit_link: "/find_submission_option_contents", | |
| edit_option_link: "/find_submission_option_settings", | |
| save_sort_url: "/save_submission_sort", | |
| finished_content: false, | |
| finished_type: false, | |
| current_locale: $("#meth_locale").val(), | |
| init: function() { | |
| $('ol.children').hide(); | |
| $("#meth_locale").on('change', function() { | |
| this.current_locale = $(this).val(); | |
| }); | |
| $(".edit_alert").hide(); | |
| $(".edit_option_errors").hide(); | |
| $(".sub_form_preview").on('click', this.form_preview); | |
| $("#initiative_methodology_submission_options_attributes_0_item_type").on('change', this.item_type_change); | |
| $("#save_new_type").on('click', this.save_new_type); | |
| $("#save_main_option").on('click', this.save_main_option); | |
| $("#save_new_option").on('click', this.new_option); | |
| $("a.delete_option").on('click', this.delete_option); | |
| $("a.delete_child_option").on('click', this.delete_child_option); | |
| $("#submission_form").submit(this.submit_data); | |
| $('a.add_option_value').on('click', function() { | |
| $('#submission_option_id').attr('value', $(this).data("id").toString() ); | |
| $('#submission_option_id').parents( "form:first" ).dirtyForm(); | |
| }); | |
| $("i.sub_fold").on('click', this.toggle_children); | |
| $("a.edit_sub_option").on('click', this.edit_sub_option); | |
| $("#initiative_methodology_submission_options_attributes_0_translations_attributes_1_content").on('keyup', this.set_content); | |
| $("#initiative_methodology_submission_options_attributes_0_item_type").change(this.set_type); | |
| $("a.edit_main_option").on('click', this.edit_main_option); | |
| $("input[name^='option[translations_attributes]']").on('keyup', this.enable_save_button); | |
| $('#submit_sort_save').on('click', this.submit_sort_save); | |
| $('a.close_children_option').on('click', this.clear_children_option_data); | |
| }, | |
| enable_save_button: function(e) { | |
| if ( $(this).val().length > 0 ) { | |
| $("#save_new_option").attr('disabled', false) | |
| } else { | |
| $("#save_new_option").attr('disabled', true) | |
| } | |
| }, | |
| form_preview: function(e){ | |
| e.preventDefault(); | |
| var init_id = $(this).data('init-id'), | |
| meth_id = $(this).data('meth-id'), | |
| $body = $("#form_preview").find('.modal-body'), | |
| form_data = { initiative_id: init_id, initiative_methodology_id: meth_id }; | |
| $.post("/submission_form_preview", form_data, function(data) { | |
| $body.html(""); | |
| $("#sub_preview").tmpl(data.records).appendTo($body); | |
| }); | |
| }, | |
| clear_children_option_data: function(e) { | |
| $(this).parent().prev().find('input[name^="option[translations_attributes]"]').val('') | |
| }, | |
| submit_sort_save: function(e){ | |
| e.preventDefault(); | |
| info = $('ol.sortable').nestedSortable('toHierarchy', {startDepthCount: 0}) | |
| set = [] | |
| $.each(info, function(i, e){ | |
| $.each(e.children, function(index, elem){ | |
| if ( isNaN(parseInt(elem["id"], 10)) ) { | |
| e.children.splice(index, index + 1) | |
| } | |
| }) | |
| set.push(e); | |
| }) | |
| var sortData = {set: JSON.stringify(set)} | |
| $.post(SubmissionForm.save_sort_url, sortData, function(data) { | |
| if ( data.status == "ok" ) { | |
| $sort_info = $("#submission_form_info"); | |
| $sort_info.slideUp('fast'); | |
| $sort_info.html(""); | |
| $sort_info.html(data.message); | |
| $sort_info.slideDown('fast'); | |
| } | |
| }); | |
| }, | |
| save_main_option: function(e) { | |
| e.preventDefault(); | |
| //if ( SubmissionForm.validate_edit_details() ) { | |
| $(this).button('loading'); | |
| $("#edit_submission_option").submit(); | |
| $("#option_field").modal({show: false}); | |
| //} else{ | |
| // $(".edit_alert").show(); | |
| //} | |
| $(this).off('click'); | |
| }, | |
| validate_edit_details: function() { | |
| var inputCount = $('#meth_locale').prop('selectedIndex'), | |
| inputVal = $("#save_main_option").parent().prev().find('input[name="option[translations_attributes]['+(inputCount+1)+'][content]"]'), | |
| typeVal = $("#save_main_option").parent().prev().find('select') | |
| is_valid = true, | |
| errorString = ""; | |
| if ( !$(inputVal).val().length > 0 ) { | |
| errorString += " Content must be present. "; | |
| is_valid = false; | |
| } | |
| if ( !$(typeVal).val().length > 0 ) { | |
| errorString += " Item type must be present. "; | |
| is_valid = false; | |
| } | |
| $("#edit_errors").html(""); | |
| $("#edit_errors").html( errorString ); | |
| return is_valid; | |
| }, | |
| edit_main_option: function(e) { | |
| e.preventDefault(); | |
| var $lines = $('div.line'), | |
| $form = $("#edit_submission_option"); | |
| option_id = $(this).data("id"); | |
| $form.children('div').first().append($("<input name=\"_method\" type=\"hidden\" value=\"put\" />")); | |
| $form.attr('action', "/submission_options/" + option_id ); | |
| $.post(SubmissionForm.edit_option_link, { id: option_id }, function(data) { | |
| $("#submission_option_required").prop('checked', data.required ); | |
| $("#submission_option_item_type").val( data.item_type ); | |
| $("#submission_option_min_length").val( data.min_length ); | |
| $("#submission_option_max_length").val( data.max_length ); | |
| data.item_type == "Text" ? $lines.removeClass('hidden') : $lines.addClass('hidden'); | |
| $.each(data.content, function(i, e) { | |
| a = $("#edit_submission_option").find('input[name$="[locale]"][value="' + e.locale + '"]'); | |
| a.next('.mainOpt').find('input').val(e.content); | |
| }); | |
| $form.dirtyForm(); | |
| }); | |
| }, | |
| set_content: function() { | |
| self = SubmissionForm; | |
| $(this).val().length > 0 ? self.finished_content = true : self.finished_content = false; | |
| self.check_validity(); | |
| }, | |
| set_type: function() { | |
| self = SubmissionForm; | |
| $(this).val() !== "" ? self.finished_type = true : self.finished_type = false; | |
| self.check_validity(); | |
| }, | |
| check_validity: function() { | |
| if ( this.finished_content && this.finished_type ) { | |
| $("#save_new_type").attr('disabled', false); | |
| } else { | |
| $("#save_new_type").attr('disabled', true); | |
| } | |
| }, | |
| edit_sub_option: function(e) { | |
| var $form = $("#new_option_form"), | |
| option_id = $(this).data("id"); | |
| $form.children('div').first().append($("<input name=\"_method\" type=\"hidden\" value=\"put\" />")); | |
| $form.attr('action', "/options/" + option_id ); | |
| $.post(SubmissionForm.edit_link, { id: option_id }, function(data) { | |
| $.each(data, function(i, e) { | |
| a = $form.find('input[name$="[locale]"][value="' + e.locale + '"]'); | |
| a.next('.sub').find('input').val(e.content); | |
| }); | |
| }); | |
| $("#save_new_option").attr('disabled', false); | |
| e.preventDefault(); | |
| }, | |
| toggle_children: function(e) { | |
| var $children = $(this).closest('.submission_option').next(".children"); | |
| if ( $(this).hasClass('icon-chevron-up') ) { | |
| $(this).removeClass('icon-chevron-up') | |
| $(this).addClass('icon-chevron-down') | |
| } | |
| else { | |
| $(this).removeClass('icon-chevron-down') | |
| $(this).addClass('icon-chevron-up') | |
| } | |
| $children.slideToggle('fast'); | |
| }, | |
| new_option: function(e) { | |
| e.preventDefault(); | |
| if ( SubmissionForm.validate_option_edit_details() ) { | |
| $("#option_field").modal('hide'); | |
| $("#sub_option_field").modal('hide'); | |
| $("#new_option_form").submit(); | |
| } else{ | |
| $(".edit_option_errors").show(); | |
| } | |
| $(this).off('click'); | |
| }, | |
| validate_option_edit_details: function() { | |
| var inputCount = $('#meth_locale').prop('selectedIndex'), | |
| inputVal = $("#save_new_option").parent().prev().find('input[name="option[translations_attributes]['+(inputCount+1)+'][content]"]'), | |
| is_valid = true, | |
| errorString = ""; | |
| if ( inputVal.val().length < 0 ) { | |
| errorString += " Content must be present. "; | |
| is_valid = false; | |
| } | |
| $("#edit_option_errors").html(""); | |
| $("#edit_option_errors").html( errorString ); | |
| return is_valid; | |
| }, | |
| submit_data: function(e) { | |
| var $form_info = $("#submission_form_info"); | |
| $.post( $(this).attr('action'), $(this).serialize(), function( data ) { | |
| $form_info.slideUp('fast'); | |
| $form_info.html(""); | |
| $form_info.html(data.message); | |
| $form_info.slideDown('fast'); | |
| }); | |
| e.preventDefault(); | |
| }, | |
| delete_option: function() { | |
| $(this).closest('li').addClass('hidden'); | |
| }, | |
| delete_child_option: function(e) { | |
| e.preventDefault(); | |
| var child_id = $(this).data('id'); | |
| $.post(this.href, { id: child_id, _method: 'delete' }, null, "script" ); | |
| $(this).closest('li').addClass('hidden'); | |
| }, | |
| item_type_change: function() { | |
| var $lines = $('div.line'); | |
| $(this).val() == "Text" ? $lines.removeClass('hidden') : $lines.addClass('hidden'); | |
| }, | |
| save_new_type: function(e) { | |
| e.preventDefault(); | |
| var $form = $("#submission_form"); | |
| $("#add_new_submission_option").modal('hide'); | |
| if ( $(this).attr("disabled") != "disabled" ) { | |
| $.post( $form.attr('action'), $form.serialize(), function( data ) { | |
| if ( data.status == "success" ) { | |
| $("#add_new_submission_option").modal('hide'); | |
| SubmissionForm.loadNewEntries(); | |
| SubmissionForm.cleanUpForm(); | |
| } else { | |
| console.log("BROKEN!!!") | |
| } | |
| }); | |
| } | |
| $(this).off("click"); | |
| }, | |
| loadNewEntries: function() { | |
| window.location.reload() | |
| }, | |
| cleanUpForm: function() { | |
| $("#sub_locale").val($("#sub_locale option:first").val()); | |
| $("#initiative_methodology_submission_options_attributes_0_required").prop('checked', false); | |
| $("#initiative_methodology_submission_options_attributes_0_translations_attributes_1_content").val(" "); | |
| $("#initiative_methodology_submission_options_attributes_0_item_type") | |
| .val($("#initiative_methodology_submission_options_attributes_0_item_type option:first").val()); | |
| }, | |
| }; | |
| $(function() { | |
| $("#add_new_submission_option").modal({show: false}); | |
| $("#option_field").modal({show: false}); | |
| $("#add_new_option").modal({show: false}); | |
| $("#form_preview").modal({show: false}); | |
| $("ol.sortable").nestedSortable({ | |
| placeholder: "ui-state-highlight", | |
| disableNesting: 'no-nesting', | |
| forcePlaceholderSize: true, | |
| handle: 'div', | |
| helper: 'clone', | |
| items: 'li', | |
| maxLevels: 2, | |
| opacity: .6, | |
| tabSize: 25, | |
| tolerance: 'pointer', | |
| toleranceElement: '> div' | |
| }); | |
| $("ol.children").nestedSortable({ | |
| placeholder: "ui-state-highlight", | |
| disableNesting: 'no-nesting', | |
| forcePlaceholderSize: true, | |
| handle: 'div', | |
| helper: 'clone', | |
| items: 'li', | |
| maxLevels: 2, | |
| opacity: .6, | |
| tabSize: 25, | |
| tolerance: 'pointer', | |
| toleranceElement: '> div' | |
| }); | |
| SubmissionForm.init(); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment