delete and sequencing fixed for survey questions

This commit is contained in:
Harry Bomrah 2013-09-30 15:47:27 +08:00 committed by saurabhbhatia
parent dfec6f7642
commit 74ca99a581
2 changed files with 39 additions and 15 deletions

View File

@ -21,6 +21,7 @@ function setData(l, length, optionsI, className) {
radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]"; radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]";
data = { data = {
_l: l, _l: l,
_ques: "new",
_length: length, _length: length,
_className: className, _className: className,
_destroy: [id+l+"_destroy", name+l+"][_destroy]"], _destroy: [id+l+"_destroy", name+l+"][_destroy]"],
@ -48,6 +49,7 @@ function setEditData(l, length, d, optionsI, className) {
radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]"; radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]";
data = { data = {
_l: l, _l: l,
_ques: "old",
_length: length, _length: length,
_className: className, _className: className,
_destroy: [id+l+"__destroy", name+l+"][_destroy]"], _destroy: [id+l+"__destroy", name+l+"][_destroy]"],
@ -75,11 +77,14 @@ function setEditQuestionAttributes(l, length, d, optionsI, className) {
radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_", radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_",
radiogroupsName = 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]", option_id = l+"][survey_question_options_attributes]["+optionsI+"][id]",
destroy = l+"][survey_question_options_attributes]["+optionsI+"][_destroy]",
radiogroups_id = l+"][survey_question_radiogroups_attributes]["+optionsI+"][id]"; radiogroups_id = l+"][survey_question_radiogroups_attributes]["+optionsI+"][id]";
data = { data = {
_l: l, _l: l,
_length: length, _length: length,
_ques: "old",
_className: className, _className: className,
_destroy: [name+destroy],
_option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+optionsI+"_", d.name_translations], _option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+optionsI+"_", d.name_translations],
_radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+optionsI+"_",d.name_translations], _radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+optionsI+"_",d.name_translations],
_options_id: [name+option_id,d._id], _options_id: [name+option_id,d._id],
@ -93,7 +98,12 @@ function inputAppendLength(element, length) {
if(length == 2) { if(length == 2) {
element.closest('.add-target').find('.remove-input').addClass('hide').prev().addClass('last'); element.closest('.add-target').find('.remove-input').addClass('hide').prev().addClass('last');
} }
element.closest('.input-append').remove(); var option = element.closest('.input-append');
if( option.data( 'question-option' ) == "old" ){
option.hide().removeAttr( 'question-option' ).find( "input.should_destroy_option" ).val("1");
} else if( option.data( "question-option" ) == "new"){
option.remove();
}
} }
function editQuestion (dataArray){ function editQuestion (dataArray){
@ -165,7 +175,7 @@ $(function() {
}; };
}); });
$('.add-question').on(clickEvent, function(event) { $('.add-question').on(clickEvent, function(event) {
$('#template-survery').tmpl(setData(l, $('.questions > .attributes').length + 1)).appendTo($('.questions')); $('#template-survery').tmpl(setData(l, $('.questions > .attributes[data-question]').length + 1)).appendTo($('.questions'));
checkQuestionsLength(); checkQuestionsLength();
textareaResizable(); // from textarea-lang-btn.js textareaResizable(); // from textarea-lang-btn.js
l += 1; l += 1;
@ -207,14 +217,26 @@ $(function() {
inputAppendLength($(this), $(this).closest('.add-target').children('.input-append').length); inputAppendLength($(this), $(this).closest('.add-target').children('.input-append').length);
} else if($(this).hasClass('delete')) { } else if($(this).hasClass('delete')) {
$(this).closest('.attributes').fadeOut(300, function() { $(this).closest('.attributes').fadeOut(300, function() {
$(this).hide(); if( $( this ).data( 'question' ) == "old" ){
$( this ).find( '.should_destroy' ).val(1); $(this).hide().removeAttr('data-question');
checkQuestionsLength(); $( this ).find( '.should_destroy' ).val(1);
$('.questions .attributes').each(function(index, el) { l--;
$( this ).find( "input.sequence" ).val( index ); checkQuestionsLength();
$( this ).find( "h4 span:eq(0)" ).text( index + 1 ); $('.questions .attributes[data-question]').each(function(index, el) {
$( this ).attr( "data-index", index ); $( this ).find( "input.sequence" ).val( index );
}); $( this ).find( "h4 span:eq(0)" ).text( index + 1 );
$( this ).attr( "data-index", index );
});
} else if( $( this ).data( 'question' ) == "new" ){
$( this ).remove();
l--;
checkQuestionsLength();
$('.questions .attributes[data-question]').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')) { } else if($(this).hasClass('icon-caret-up') || $(this).hasClass('icon-caret-down')) {
if($(this).hasClass('icon-caret-up')) { if($(this).hasClass('icon-caret-up')) {
@ -236,7 +258,7 @@ $(function() {
$('#template-survery-option').tmpl(setData(_index, "", _i, _className)).insertBefore($(this).closest('.add-btn')); $('#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'); $(this).closest('.add-target').find('.input-append').find('.remove-input').removeClass('hide').prev().removeClass('last');
_i += 1 _i += 1
$(this).closest('.control-group').data('index', _i) $(this).closest('.control-group').data('index', _i);
} }
event.preventDefault(); event.preventDefault();
}); });

View File

@ -1,11 +1,11 @@
<script id="template-survery" type="text/x-tmpl"> <script id="template-survery" type="text/x-tmpl">
<div class="attributes" data-index="${_l}"> <div class="attributes" data-index="${_l}" data-question="${_ques}">
<div class="attributes-header clearfix"> <div class="attributes-header clearfix">
<a class="btn btn-mini pull-right btn-danger delete" href="#"><i class="icon-trash"></i> Delete</a> <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 class="should_destroy" id="${_destroy[0]}" name="${_destroy[1]}" type="hidden">
<input id="${_sequence[0]}" class="sequence" name="${_sequence[1]}" type="hidden" value="${_sequence[2]}"> <input id="${_sequence[0]}" class="sequence" name="${_sequence[1]}" type="hidden" value="${_sequence[2]}">
{{if _id}} {{if _id}}
<input id="${_id[0]}" class="sequence" name="${_id[1]}" type="hidden" value="${_id[2]}"> <input id="${_id[0]}" name="${_id[1]}" type="hidden" value="${_id[2]}">
{{/if}} {{/if}}
<h4><b></b> Q<span>${_length}</span> <i class="icon-caret-up"></i> <span class="questions-title">${_title_translations[3].en}</span></h4> <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>
@ -123,9 +123,10 @@
<script id="template-survery-option" type="text/x-tmpl"> <script id="template-survery-option" type="text/x-tmpl">
{{if _className == 'rgl'}} {{if _className == 'rgl'}}
<div class="input-append"> <div class="input-append" data-question-option="${_ques}">
{{if _radiogroups_id}} {{if _radiogroups_id}}
<input type="hidden" value="${_radiogroups_id[1]}" name="${_radiogroups_id[0]}" /> <input type="hidden" value="${_radiogroups_id[1]}" name="${_radiogroups_id[0]}" />
<input type="hidden" class="should_destroy_option" name="${_destroy[0]}" />
{{/if}} {{/if}}
<div class="tab-content"> <div class="tab-content">
<div class="tab-pane fade active in" id="${_radiogroups[2]+'en'}"> <div class="tab-pane fade active in" id="${_radiogroups[2]+'en'}">
@ -144,9 +145,10 @@
</div> </div>
</div> </div>
{{else}} {{else}}
<div class="input-append"> <div class="input-append" data-question-option="${_ques}">
{{if _options_id}} {{if _options_id}}
<input type="hidden" value="${_options_id[1]}" name="${_options_id[0]}" /> <input type="hidden" value="${_options_id[1]}" name="${_options_id[0]}" />
<input type="hidden" class="should_destroy_option" name="${_destroy[0]}" />
{{/if}} {{/if}}
<div class="tab-content"> <div class="tab-content">
<div class="tab-pane fade active in" id="${_option[2]+'en'}"> <div class="tab-pane fade active in" id="${_option[2]+'en'}">