jQuery -> ### Handle adding and removing fields ### $('form').on 'click', '.remove_fields', (event) -> $(this).prev('input[type=hidden]').val('1') $(this).closest('fieldset').hide() event.preventDefault() $('form').on 'click', '.add_fields', (event) -> time = new Date().getTime() regexp = new RegExp($(this).data('id'), 'g') new_question = $($(this).data('fields').replace(regexp, time)) $(this).before(new_question) createQuestionPreview new_question toggleEditMode new_question event.preventDefault() ### Update the edit preview mode between textQuestion, ChecklistQuestion, etc. ### updateChoiceVisibility = (select) -> question = select.parents("fieldset.question") question_choices = question.find(".edit-question-choices") question_choices.find(".question-type").hide() # first hide all the question types current_question_type = question_choices.find(".question-type."+select.val()) current_question_type.show() # Then show the current question type question.find('.required-option').show() # show the required-option field, it may be hidden later by certain question types if select.val() == 'ChooseOneQuestion' current_question_type.removeClass('show-checkbox').addClass('show-radio') else if select.val() == 'ChecklistQuestion' current_question_type.removeClass('show-radio').addClass('show-checkbox') else if select.val() == 'FormSection' question.find('.required-option').hide() # Update preview type whenever the user changes the type of this question bindTypeChange = (select) -> select.live "change", -> updateChoiceVisibility $(this) # Update once when the DOM first loads selects = $(".select-question-type select") bindTypeChange selects selects.each -> updateChoiceVisibility $(this) ### Make the list sortable ### makeSortable = -> questions = $("form .ask.questions") questions.sortable axis: "y" stop: (event, ui) -> order = 1 questions.find(".position-wrapper").each -> $(this).find("input").val order order++ makeSortable() ### Toggle between 'edit' question and 'preview' question ### createQuestionPreview = (question) -> if question.children('preview-question-fields').length == 0 # if we don't have a question preview yet asker = question.serialize().match(/^\w+/)[0] # get asker name question_type = question.find('.select-question-type select').val() # get question type question_details = question.serializeObject(/^.*?\[\d+\]/) # get question details and choices preview_section = $("
") # create DOM object question.append preview_section q_label = $("") q_label.addClass("required") if question_details.required == "1" q_help_text = if (question_details.instructions != "") then $(""+question_details.instructions+"
") else false # generate preview based on question type switch question_type when "TextQuestion" preview_section.append(q_label).append("") preview_section.append(q_help_text) if q_help_text when "EssayQuestion" preview_section.append(q_label).append("") preview_section.append(q_help_text) if q_help_text when "ChooseOneQuestion" preview_section.append(q_label) preview_section.append(q_help_text) if q_help_text $.each question_details.choices_attributes, (k,v) -> unless v._destroy == "1" checkbox_item = $("") checkbox_item.append("") checkbox_item.append(""+v.name+"") preview_section.append(checkbox_item) when "ChecklistQuestion" preview_section.append(q_label) preview_section.append(q_help_text) if q_help_text $.each question_details.choices_attributes, (k,v) -> unless v._destroy == "1" checkbox_item = $("") checkbox_item.append("") checkbox_item.append(""+v.name+"") preview_section.append(checkbox_item) when "FormSection" preview_section.append("