survey edit completed

This commit is contained in:
Harry Bomrah 2013-09-23 20:30:09 +08:00 committed by saurabhbhatia
parent c35d42d671
commit 42dcf0a2d2
5 changed files with 172 additions and 112 deletions

View File

@ -24,14 +24,67 @@ function setData(l, length, optionsI, className) {
_length: length,
_className: className,
_destroy: [id+l+"_destroy", name+l+"][_destroy]"],
_sequence : [id+l+"_sequence",name+l+"][sequence]"],
_sequence : [id+l+"_sequence",name+l+"][sequence]",length-1],
_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]"],
_is_required: [id+l+"_is_required", name+l+"][is_required]",false],
_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]"]
_custom_option: [id+l+"_custom_option", name+l+"][custom_option]",false]
};
return data;
};
function setEditData(l, length, d, 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]"],
_id: [id+l+"_id", name+l+"][id]",d._id],
_sequence : [id+l+"_sequence",name+l+"][sequence]",d.sequence],
_title_translations: [id+l+"_title_translations", name+l+"][title_translations]", "questions_title_"+l,d.title_translations],
_description_translations: [id+l+"_description_translations", name+l+"][description_translations]", "questions_depiction_"+l,d.description_translations],
_type: [id+l+"_type", name+l+"][type]",d.type],
_is_required: [id+l+"_is_required", name+l+"][is_required]",d.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]",d.custom_option]
};
return data;
};
function setEditQuestionAttributes(l, length, d, 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]",
option_id = l+"][survey_question_options_attributes]["+optionsI+"][id]",
radiogroups_id = l+"][survey_question_radiogroups_attributes]["+optionsI+"][id]";
data = {
_l: l,
_length: length,
_className: className,
_option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+optionsI+"_", d.name_translations],
_radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+optionsI+"_",d.name_translations],
_options_id: [name+option_id,d._id],
_radiogroups_id: [name+radiogroups_id,d._id]
};
return data;
};
@ -43,6 +96,47 @@ function inputAppendLength(element, length) {
element.closest('.input-append').remove();
}
function editQuestion (dataArray){
var l = $('.questions > .attributes').length || 0;
$.each(dataArray,function ( i, d ) {
console.log(d)
$('#template-survery').tmpl(setEditData(l, $('.questions > .attributes').length + 1, d)).appendTo($('.questions'));
checkQuestionsLength();
textareaResizable(); // from textarea-lang-btn.js
if( d.type > 1 && d.type < 5 ){
onQuestionTypeChanged( $(".questions .field-type:eq("+ i +")"), d.type );
$.each(d.survey_question_options,function ( x, data ){
$('#template-survery-option').tmpl(setEditQuestionAttributes( l, "", data, x, "options" )).insertBefore($(".questions .field-type:eq("+ i +") .options .add-btn"));
$(".questions .field-type:eq("+ i +") .options").data("index",x);
})
} else if( d.type == 5 ) {
onQuestionTypeChanged( $(".questions .field-type:eq("+ i +")"), d.type );
$.each(d.survey_question_radiogroups,function ( x, data ){
$('#template-survery-option').tmpl(setEditQuestionAttributes( l, "", data, x, "rgl" )).insertBefore($(".questions .field-type:eq("+ i +") .rgl .add-btn"));
$(".questions .field-type:eq("+ i +") .options").data("index",x);
})
$.each(d.survey_question_options,function ( x, data ){
$('#template-survery-option').tmpl(setEditQuestionAttributes( l, "", data, x, "options" )).insertBefore($(".questions .field-type:eq("+ i +") .options .add-btn"));
$(".questions .field-type:eq("+ i +") .options").data("index",x);
})
}
l++;
})
}
function onQuestionTypeChanged ( $fieldType, _val ){
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');
}
};
}
$(function() {
var l = $('.questions > .attributes').length || 0;
if($('#survey_redirect_mode').prop('checked')) {
@ -79,22 +173,18 @@ $(function() {
});
$('.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');
}
};
});
onQuestionTypeChanged( $fieldType, _val );
});
$('.questions').on(clickEvent, "input[type=checkbox]", function(){
if( $( this ).is(":checked") )
$( this ).parent().find( 'input[type=hidden]' ).val("1");
else
$( this ).parent().find( 'input[type=hidden]' ).val("0");
})
$('.questions').on('keyup', '.q-title', function(event) {
var $questionsTitle = $(this).closest('.attributes-body').siblings('.attributes-header').find('.questions-title'),
@ -118,10 +208,13 @@ $(function() {
inputAppendLength($(this), $(this).closest('.add-target').children('.input-append').length);
} else if($(this).hasClass('delete')) {
$(this).closest('.attributes').fadeOut(300, function() {
$(this).remove();
$(this).hide();
$( this ).find( '.should_destroy' ).val(1);
checkQuestionsLength();
$('.questions > .attributes').each(function(index, el) {
$(this).find('h4').find('span:eq(0)').text(index + 1);
$('.questions .attributes').each(function(index, el) {
$( this ).find( "input.sequence" ).val( index );
$( this ).find( "h4 span:eq(0)" ).text( index + 1 );
$( this ).attr( "data-index", index );
});
});
} else if($(this).hasClass('icon-caret-up') || $(this).hasClass('icon-caret-down')) {
@ -132,16 +225,15 @@ $(function() {
$(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')){
} else if($(this).hasClass('change-title')){
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 = $(this).closest('.control-group').data('index') || 1,
var _i = $(this).closest('.control-group').data('index') || 0,
_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');
_i += 1

View File

@ -48,11 +48,27 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
def edit
@survey = ::Survey.find(params[:id])
@sqs = []
@survey.survey_questions.each do |sq|
sq['title_translations'] = sq.title_translations
sq['description_translations'] = sq.description_translations
temp = []
sq.survey_question_options.each do |sqo|
sqo['name_translations'] = sqo.name_translations
temp << sqo
end
temp = []
sq.survey_question_radiogroups.each do |sqr|
sqr['name_translations'] = sqr.name_translations
temp << sqr
end
sq.survey_question_radiogroups = temp
@sqs << sq
end
end
def update
@survey = ::Survey.find(params[:id])
@survey.update_user_id = current_user.id
respond_to do |format|

View File

@ -116,31 +116,6 @@
<%= render :partial => "questions" %>
<div id= "poststuff">
<div id="post-body">
<div id="post-body-content" class="clear">
<div class="main-widget">
<div id="survey_questions_wrapper">
<div id='survey_questions' class="survey_questions_block">
<% @survey.survey_questions.each_with_index do |survey_question, i| %>
<%= f.fields_for :survey_questions, survey_question do |f| %>
<%= render :partial => 'form_survey_question', :object => survey_question, :locals => {:f => f, :i => i} %>
<% end %>
<% end %>
</div>
<div id='add_survey_question' class="info_input survey_questions_block">
<%= hidden_field_tag 'survey_question_field_count', @survey.survey_questions.count %>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
editQuestion(<%= @sqs.to_json.html_safe %>);
</script>

View File

@ -3,8 +3,11 @@
<div class="attributes-header clearfix">
<a class="btn btn-mini pull-right btn-danger delete" href="#"><i class="icon-trash"></i> Delete</a>
<input class="should_destroy" id="${_destroy[0]}" name="${_destroy[1]}" type="hidden">
<input id="${_sequence[0]}" class="sequence" name="${_sequence[1]}" type="hidden" value="${_length - 1}">
<h4><b></b> Q<span>${_length}</span> <i class="icon-caret-up"></i> <span class="questions-title"></span></h4>
<input id="${_sequence[0]}" class="sequence" name="${_sequence[1]}" type="hidden" value="${_sequence[2]}">
{{if _id}}
<input id="${_id[0]}" class="sequence" name="${_id[1]}" type="hidden" value="${_id[2]}">
{{/if}}
<h4><b></b> Q<span>${_length}</span> <i class="icon-caret-up"></i> <span class="questions-title">${_title_translations[3].en}</span></h4>
</div>
<div class="attributes-body">
<div class="control-group">
@ -13,15 +16,15 @@
<div class="input-append">
<div class="tab-content">
<div class="tab-pane fade active in" id="${_title_translations[2]+'_en'}">
<input class="span4 q-title" id="${_title_translations[0]+'_en'}" name="${_title_translations[1]+'[en]'}" type="text" value="" />
<input class="span4 q-title" id="${_title_translations[0]+'_en'}" name="${_title_translations[1]+'[en]'}" type="text" value="${_title_translations[3].en}" />
</div>
<div class="tab-pane fade" id="${_title_translations[2]+'_zh_tw'}">
<input class="span4 q-title" id="${_title_translations[0]+'_zh_tw'}" name="${_title_translations[1]+'[zh_tw]'}" type="text" value="" />
<input class="span4 q-title" id="${_title_translations[0]+'_zh_tw'}" name="${_title_translations[1]+'[zh_tw]'}" type="text" value="${_title_translations[3].zh_tw}" />
</div>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<a class="btn active" href="${'#'+_title_translations[2]+'_en'}" data-toggle="tab">English</a>
<a class="btn" href="${'#'+_title_translations[2]+'_zh_tw'}" data-toggle="tab">Chinese</a>
<a class="btn change-title active" href="${'#'+_title_translations[2]+'_en'}" data-toggle="tab">English</a>
<a class="btn change-title" href="${'#'+_title_translations[2]+'_zh_tw'}" data-toggle="tab">Chinese</a>
</div>
</div>
</div>
@ -35,10 +38,10 @@
<a class="btn" href="${'#'+_description_translations[2]+'_zh_tw'}" data-toggle="tab">Chinese</a>
</div>
<div class="tab-pane fade active in" id="${_description_translations[2]+'_en'}">
<textarea class="resizable" id="${_description_translations[0]+'_en'}" name="${_description_translations[1]+'[en]'}" rows="3"></textarea>
<textarea class="resizable" id="${_description_translations[0]+'_en'}" name="${_description_translations[1]+'[en]'}" rows="3">${_description_translations[3].en}</textarea>
</div>
<div class="tab-pane fade" id="${_description_translations[2]+'_zh_tw'}">
<textarea class="resizable" id="${_description_translations[0]+'_zh_tw'}" name="${_description_translations[1]+'[zh_tw]'}" rows="3"></textarea>
<textarea class="resizable" id="${_description_translations[0]+'_zh_tw'}" name="${_description_translations[1]+'[zh_tw]'}" rows="3">${_description_translations[3].zh_tw}</textarea>
</div>
</div>
</div>
@ -46,13 +49,13 @@
<div class="control-group">
<label class="control-label muted" for="">Type </label>
<div class="controls">
<select class="type-selector" id="${_type[0]}" name="${_type[1]}">
<option value="0">Oneline</option>
<option value="1">Multi-line</option>
<option value="2">Radio</option>
<option value="3">Check</option>
<option value="4">Select</option>
<option value="5">Radio Group</option>
<select class="type-selector" id="${_type[0]}" name="${_type[1]}" >
<option value="0" {{if _type[2] == 0}}selected="selected"{{/if}}>Oneline</option>
<option value="1" {{if _type[2] == 1}}selected="selected"{{/if}}>Multi-line</option>
<option value="2" {{if _type[2] == 2}}selected="selected"{{/if}}>Radio</option>
<option value="3" {{if _type[2] == 3}}selected="selected"{{/if}}>Check</option>
<option value="4" {{if _type[2] == 4}}selected="selected"{{/if}}>Select</option>
<option value="5" {{if _type[2] == 5}}selected="selected"{{/if}}>Radio Group</option>
</select>
</div>
</div>
@ -64,23 +67,7 @@
<label for="" class="control-label muted">Radio Group Label</label>
<div class="controls add-input">
<div class="add-target">
<div class="input-append">
<div class="tab-content">
<div class="tab-pane fade active in" id="${_radiogroups[2]+'en'}">
<input type="text" class="span4" id="${_radiogroups[0]+'en'}" name="${_radiogroups[1]+'[en]'}" />
</div>
<div class="tab-pane fade" id="${_radiogroups[2]+'zh_tw'}">
<input type="text" class="span4" id="${_radiogroups[0]+'zh_tw'}" name="${_radiogroups[1]+'[zh_tw]'}" />
</div>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<a class="btn active" href="${'#'+_radiogroups[2]+'en'}" data-toggle="tab">English</a>
<a class="btn last" href="${'#'+_radiogroups[2]+'zh_tw'}" data-toggle="tab">Chinese</a>
<a href="#" class="btn remove-input hide">
<i class="icon-trash"></i>
</a>
</div>
</div>
<p class="add-btn">
<a href="#" class="btn btn-mini btn-primary"><i class="icons-plus"></i> Add</a>
@ -95,23 +82,7 @@
<label for="" class="control-label muted">Options List</label>
<div class="controls add-input">
<div class="add-target">
<div class="input-append">
<div class="tab-content">
<div class="tab-pane fade active in" id="${_option[2]+'en'}">
<input type="text" class="span4" id="${_option[0]+'en'}" name="${_option[1]+'[en]'}" />
</div>
<div class="tab-pane fade" id="${_option[2]+'zh_tw'}">
<input type="text" class="span4" id="${_option[0]+'zh_tw'}" name="${_option[1]+'[zh_tw]'}" />
</div>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<a class="btn active" href="${'#'+_option[2]+'en'}" data-toggle="tab">English</a>
<a class="btn last" href="${'#'+_option[2]+'zh_tw'}" data-toggle="tab">Chinese</a>
<a href="#" class="btn remove-input hide">
<i class="icon-trash"></i>
</a>
</div>
</div>
<p class="add-btn">
<a href="#" class="btn btn-mini btn-primary"><i class="icons-plus"></i> Add</a>
@ -123,8 +94,8 @@
<div class="control-group allow">
<div class="controls">
<label class="checkbox">
<input name="${_custom_option[1]}" type="hidden" value="0" />
<input id="${_custom_option[0]}" name="${_custom_option[1]}" type="checkbox" value="1">
<input name="${_custom_option[1]}" type="hidden" value="${_custom_option[2] << 0}" />
<input id="${_custom_option[0]}" type="checkbox" {{if _custom_option[2]}} checked="checked" {{/if}} />
Allow Custom Option
</label>
</div>
@ -137,8 +108,8 @@
<label class="control-label muted" for="redirectMode"></label>
<div class="controls">
<label class="checkbox">
<input name="${_is_required[1]}" type="hidden" value="0" />
<input id="${_is_required[0]}" name="${_is_required[1]}" type="checkbox" value="1">
<input name="${_is_required[1]}" type="hidden" value="${_is_required[2] << 0}" />
<input id="${_is_required[0]}" type="checkbox" {{if _is_required[2]}} checked="checked" {{/if}} />
Is Required
</label>
</div>
@ -153,12 +124,15 @@
<script id="template-survery-option" type="text/x-tmpl">
{{if _className == 'rgl'}}
<div class="input-append">
{{if _radiogroups_id}}
<input type="hidden" value="${_radiogroups_id[1]}" name="${_radiogroups_id[0]}" />
{{/if}}
<div class="tab-content">
<div class="tab-pane fade active in" id="${_radiogroups[2]+'en'}">
<input type="text" class="span4" id="${_radiogroups[0]+'en'}" name="${_radiogroups[1]+'[en]'}" />
<input type="text" class="span4" id="${_radiogroups[0]+'en'}" name="${_radiogroups[1]+'[en]'}" value="${_radiogroups[3].en}" />
</div>
<div class="tab-pane fade" id="${_radiogroups[2]+'zh_tw'}">
<input type="text" class="span4" id="${_radiogroups[0]+'zh_tw'}" name="${_radiogroups[1]+'[zh_tw]'}" />
<input type="text" class="span4" id="${_radiogroups[0]+'zh_tw'}" name="${_radiogroups[1]+'[zh_tw]'}" value="${_radiogroups[3].zh_tw}" />
</div>
</div>
<div class="btn-group" data-toggle="buttons-radio">
@ -171,12 +145,15 @@
</div>
{{else}}
<div class="input-append">
{{if _options_id}}
<input type="hidden" value="${_options_id[1]}" name="${_options_id[0]}" />
{{/if}}
<div class="tab-content">
<div class="tab-pane fade active in" id="${_option[2]+'en'}">
<input type="text" class="span4" id="${_option[0]+'en'}" name="${_option[1]+'[en]'}" />
<input type="text" class="span4" id="${_option[0]+'en'}" name="${_option[1]+'[en]'}" value="${_option[3].en}" />
</div>
<div class="tab-pane fade" id="${_option[2]+'zh_tw'}">
<input type="text" class="span4" id="${_option[0]+'zh_tw'}" name="${_option[1]+'[zh_tw]'}" />
<input type="text" class="span4" id="${_option[0]+'zh_tw'}" name="${_option[1]+'[zh_tw]'}" value="${_option[3].zh_tw}" />
</div>
</div>
<div class="btn-group" data-toggle="buttons-radio">

View File

@ -1,5 +1,5 @@
<h1><%= t('survey.editing_survey') %></h1>
<%= form_for @survey, :url => panel_survey_back_end_survey_path(@survey), :html => {:class => 'clear'} do |f| %>
<%= form_for @survey, :url => panel_survey_back_end_survey_path(@survey), :html => {:class => 'form-horizontal main-forms'} do |f| %>
<%= render :partial => 'form', :locals => {:f => f} %>
<% end %>