386 lines
18 KiB
JavaScript
386 lines
18 KiB
JavaScript
// Check Questions Length
|
|
function checkQuestionsLength() {
|
|
var questionsLength = $('.questions > .attributes').length;
|
|
if(questionsLength == 1) {
|
|
$('.questions .delete').addClass('hide');
|
|
} else {
|
|
$('.questions .delete').removeClass('hide');
|
|
};
|
|
};
|
|
|
|
// Role Attribute Template Data
|
|
function setData(l, length, optionsI, className) {
|
|
if(!optionsI) {
|
|
optionsI = 0;
|
|
}
|
|
var id = "questionnaire_survey_survey_questions_attributes_",
|
|
name = "questionnaire_survey[survey_questions_attributes][",
|
|
optionID = l+"_survey_question_options_attributes_"+optionsI+"_name_translations_",
|
|
optionName = l+"][survey_question_options_attributes]["+optionsI+"][name_translations]",
|
|
pointsID = l+"_survey_question_options_attributes_"+optionsI+"_points",
|
|
pointsName = l+"][survey_question_options_attributes]["+optionsI+"][points]",
|
|
radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_",
|
|
radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]",
|
|
question_type_name = name+l+"][survey_question_options_attributes]["+optionsI+"][question_type]";
|
|
data = {
|
|
_l: l,
|
|
_ques: "new",
|
|
_length: length,
|
|
_className: className,
|
|
_destroy: [id+l+"_destroy", name+l+"][_destroy]"],
|
|
_sequence : [id+l+"_sequence",name+l+"][sequence]",length-1],
|
|
_title_translations: [id+l+"_title_translations", name+l+"][title_translations]", "questions_title_"+l,{"en":"","zh_tw":""}],
|
|
_description_translations: [id+l+"_description_translations", name+l+"][description_translations]", "questions_depiction_"+l,{"en":"","zh_tw":""}],
|
|
_type: [id+l+"_type", name+l+"][type]"],
|
|
_selectable_question: [name+l+"][selectable_question]", ''],
|
|
_selectable_question_type: [name+l+"][selectable_question_type]",''],
|
|
_question_type: [question_type_name,'',String(question_type_name).replaceAll(/\[|\]/g,'')],
|
|
_option_layout_type: [name+l+"][option_layout_type]",'',String(name+l+"][option_layout_type]").replaceAll(/\[|\]/g,'')],
|
|
_dateformat : [id+l+"_datetime_type", name+l+"][datetime_type]"],
|
|
_is_required: [id+l+"_is_required", name+l+"][is_required]",false],
|
|
_option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_",{"en":"","zh_tw":""}],
|
|
_points: [id+pointsID, name+pointsName, "attributes_"+l+"_options_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_"],
|
|
_radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_",{"en":"","zh_tw":""}],
|
|
_custom_option: [id+l+"_custom_option", name+l+"][custom_option]",false],
|
|
_custom_option_type: [name+l+"][custom_option_type][]",'',id+l+"_custom_option_type"],
|
|
_custom_option_title_translations: [id+l+"_custom_option_title_translations", name+l+"][custom_option_title_translations]", "custom_option_title_"+l,{"en":"","zh_tw":""}],
|
|
|
|
};
|
|
|
|
return data;
|
|
};
|
|
|
|
function setEditData(l, length, d, optionsI, className) {
|
|
if(!optionsI) {
|
|
optionsI = 0;
|
|
}
|
|
var id = "questionnaire_survey_survey_questions_attributes_",
|
|
name = "questionnaire_survey[survey_questions_attributes][",
|
|
optionID = l+"_survey_question_options_attributes_"+optionsI+"_name_translations_",
|
|
optionName = l+"][survey_question_options_attributes]["+optionsI+"][name_translations]",
|
|
pointsID = l+"_survey_question_options_attributes_"+optionsI+"_points",
|
|
pointsName = l+"][survey_question_options_attributes]["+optionsI+"][points]",
|
|
radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_",
|
|
radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]";
|
|
data = {
|
|
_l: l,
|
|
_ques: "old",
|
|
_length: length,
|
|
_className: className,
|
|
_destroy: [id+l+"__destroy", name+l+"][_destroy]"],
|
|
_id: [id+l+"_id", name+l+"][id]",d.qid],
|
|
_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],
|
|
_selectable_question: [name+l+"][selectable_question]", d.selectable_question],
|
|
_selectable_question_type: [name+l+"][selectable_question_type]",d.selectable_question_type],
|
|
_question_type: [name+l+"][question_type]",d.question_type,String(name+l+"][question_type]").replaceAll(/\[|\]/g,'')],
|
|
_option_layout_type: [name+l+"][option_layout_type]",d.option_layout_type,String(name+l+"][option_layout_type]").replaceAll(/\[|\]/g,'')],
|
|
_dateformat : [id+l+"_datetime_type", name+l+"][datetime_type]",d.datetime_type],
|
|
_is_required: [id+l+"_is_required", name+l+"][is_required]",d.is_required],
|
|
_option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_"],
|
|
_points: [id+pointsID, name+pointsName, "attributes_"+l+"_options_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_", d.points],
|
|
_radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_"],
|
|
_custom_option: [id+l+"_custom_option", name+l+"][custom_option]",d.custom_option],
|
|
_custom_option_type: [name+l+"][custom_option_type][]",d.custom_option_type,id+l+"_custom_option_type"],
|
|
_custom_option_title_translations: [id+l+"_custom_option_title_translations", name+l+"][custom_option_title_translations]", "custom_option_title_"+l,d.custom_option_title_translations],
|
|
|
|
};
|
|
return data;
|
|
};
|
|
|
|
function setEditQuestionAttributes(l, length, d, optionsI, className) {
|
|
if(!optionsI) {
|
|
optionsI = 0;
|
|
}
|
|
var id = "questionnaire_survey_survey_questions_attributes_",
|
|
name = "questionnaire_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]",
|
|
pointsID = l+"_survey_question_options_attributes_"+optionsI+"_points",
|
|
pointsName = l+"][survey_question_options_attributes]["+optionsI+"][points]",
|
|
option_id = l+"][survey_question_options_attributes]["+optionsI+"][id]",
|
|
destroy = (className=='rgl' ? l+"][survey_question_radiogroups_attributes]["+optionsI+"][_destroy]" : l+"][survey_question_options_attributes]["+optionsI+"][_destroy]"),
|
|
radiogroups_id = l+"][survey_question_radiogroups_attributes]["+optionsI+"][id]",
|
|
question_type_name = name+l+"][survey_question_options_attributes]["+optionsI+"][question_type]";
|
|
data = {
|
|
_l: l,
|
|
_length: length,
|
|
_ques: "old",
|
|
_className: className,
|
|
_destroy: [name+destroy],
|
|
_option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_", d.name_translations],
|
|
_points: [id+pointsID, name+pointsName, "attributes_"+l+"_options_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_", d.points],
|
|
_radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+String(optionsI).replaceAll(/\[|\]/g,'')+"_",d.name_translations],
|
|
_options_id: [name+option_id,d.qid],
|
|
_radiogroups_id: [name+radiogroups_id,d.qid],
|
|
_question_type: [question_type_name,d.question_type,String(question_type_name).replaceAll(/\[|\]/g,'')],
|
|
|
|
};
|
|
return data;
|
|
};
|
|
|
|
function inputAppendLength(element, length) {
|
|
var option = element.closest('.input-append');
|
|
var $level1 = element.parents('.level1').eq(0)
|
|
var $level2 = element.parents('.level2').eq(0)
|
|
if(length == 2 && $level2.length==0) {
|
|
element.closest('.add-target').find('.remove-input').addClass('hide').prev().addClass('last');
|
|
}
|
|
if( option.data( 'question-option' ) == "old" ){
|
|
option.hide().removeAttr( 'question-option' ).find( "input.should_destroy_option" ).val("1");
|
|
if ($level1.length>0 && $level2.length==0){
|
|
$level1.hide()
|
|
}
|
|
} else if( option.data( "question-option" ) == "new"){
|
|
option.remove();
|
|
if ($level1.length>0 && $level2.length==0){
|
|
$level1.remove()
|
|
}
|
|
}
|
|
}
|
|
|
|
function editQuestion (dataArray){
|
|
var l = $('.questions > .attributes').length || 0;
|
|
$.each(dataArray,function ( i, 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);
|
|
})
|
|
}else if(d.type == 6){
|
|
onQuestionTypeChanged( $(".questions .field-type:eq("+ i +")"), d.type );
|
|
}else if(d.type == 7){
|
|
onQuestionTypeChanged( $(".questions .field-type:eq("+ i +")"), d.type );
|
|
var $add_btn = $(".questions .field-type:eq("+ i +") .options .add-btn")
|
|
$.each(d.survey_question_options,function ( x, data ){
|
|
var level1 = parse_2_level_options( l, x, data );
|
|
level1.insertBefore($add_btn);
|
|
})
|
|
}
|
|
l++;
|
|
})
|
|
}
|
|
|
|
function parse_2_level_options( l, x, data ){
|
|
var level1 = $('#template-survery-option').tmpl(setEditQuestionAttributes( l, "", data, x, "options" ))
|
|
var level2 = $('#template-survery-option-level2-wrapper').tmpl()
|
|
var wrapper = $('#template-survery-option-leve11-wrapper').tmpl(setEditQuestionAttributes( l, "", data, x, "options" ));
|
|
wrapper.attr('data-index',x)
|
|
var level2_controls = level2.find('.controls .add-target')
|
|
if (data.level2){
|
|
$.each(data.level2.reverse(),function ( level2_x, level2_data ){
|
|
var level2_child = $('#template-survery-option').tmpl(setEditQuestionAttributes( l, "", level2_data, x+"][level2]["+level2_x, "options" ))
|
|
level2_controls.prepend(level2_child)
|
|
})
|
|
}else{
|
|
var level2_child = $('#template-survery-option').tmpl(setData(l, "", x+"][level2]["+level2_x, "options" ))
|
|
level2_controls.prepend(level2_child);
|
|
}
|
|
wrapper.find('.level1_wrapper').prepend(level1)
|
|
wrapper.append(level2)
|
|
return wrapper
|
|
}
|
|
function add_2_level_options(_index, _i, _className){
|
|
var level1 = $('#template-survery-option').tmpl(setData(_index, "", _i, _className));
|
|
var level2 = $('#template-survery-option-level2-wrapper').tmpl()
|
|
var level2_child = $('#template-survery-option').tmpl(setData(_index, "", _i+"][level2][0", _className))
|
|
level2.find('.controls .add-target').prepend(level2_child);
|
|
var wrapper = $('#template-survery-option-leve11-wrapper').tmpl(setData(_index, "", _i, _className));
|
|
wrapper.attr('data-index',_i)
|
|
wrapper.find('.level1_wrapper').prepend(level1)
|
|
wrapper.append(level2)
|
|
return wrapper
|
|
}
|
|
function onQuestionTypeChanged ( $fieldType, _val ){
|
|
if(_val < 2) {
|
|
$fieldType.fadeOut(300);
|
|
$fieldType.next(".date-format").fadeOut(300);
|
|
}else if(_val == 6){
|
|
$fieldType.fadeOut(300);
|
|
$fieldType.next(".date-format").fadeIn(300);
|
|
} else {
|
|
$fieldType.fadeIn(300);
|
|
$fieldType.next(".date-format").fadeOut(300);
|
|
if (_val == 7){
|
|
var $input_append = $fieldType.find('.control-group.options .add-target').eq(0).children('.input-append')
|
|
if ($input_append.length!=0){
|
|
var children_list = $input_append.eq(0).parent().children()
|
|
var _index = $input_append.eq(0).parents('.attributes').eq(0).data('index')
|
|
$.each($input_append,function(){
|
|
var _i = children_list.index($(this))
|
|
var wrapper = $('#template-survery-option-leve11-wrapper').tmpl(setData(_index, "", _i, 'options'));
|
|
wrapper.find('.level1_wrapper').prepend($(this).clone())
|
|
wrapper.attr('data-index',_i)
|
|
var level2 = $('#template-survery-option-level2-wrapper').tmpl()
|
|
var level2_child = $('#template-survery-option').tmpl(setData(_index, "", _i+"][level2][0", 'options'))
|
|
level2.find('.controls .add-target').prepend(level2_child)
|
|
wrapper.append(level2)
|
|
$(this).replaceWith(wrapper)
|
|
})
|
|
}
|
|
$fieldType.find('.level2').removeClass('hide')
|
|
$fieldType.find('.question_type_wrapper').removeClass('hide')
|
|
}else{
|
|
$fieldType.find('.level2').addClass('hide')
|
|
$fieldType.find('.question_type_wrapper').addClass('hide')
|
|
}
|
|
if(_val < 5 || _val == 7) {
|
|
$fieldType.find('.rgl, hr').addClass('hide').siblings('.allow').removeClass('hide');
|
|
} else {
|
|
$fieldType.find('.rgl, hr').removeClass('hide').siblings('.allow').removeClass('hide');
|
|
}
|
|
};
|
|
}
|
|
|
|
$(function() {
|
|
var l = $('.questions > .attributes').length || 0;
|
|
if($('#survey_redirect_mode').prop('checked')) {
|
|
$('.questions').fadeOut(300);
|
|
$('.redirect-url').fadeIn(300);
|
|
} else {
|
|
$('.questions').fadeIn(300);
|
|
$('.redirect-url').fadeOut(300);
|
|
};
|
|
$('#survey_redirect_mode').change(function(event) {
|
|
if($(this).prop('checked')) {
|
|
$('.questions').fadeOut(300);
|
|
$('.redirect-url').fadeIn(300);
|
|
} else {
|
|
$('.questions').fadeIn(300);
|
|
$('.redirect-url').fadeOut(300);
|
|
}
|
|
});
|
|
$('.field-type').each(function() {
|
|
$(this).children('.control-group').each(function() {
|
|
$(this).data().index = $(this).find('.input-append').length
|
|
});
|
|
});
|
|
$('.add-target').each(function() {
|
|
if($(this).children('.input-append').length == 1) {
|
|
$(this).find('.remove-input').addClass('hide').prev().addClass('last');
|
|
};
|
|
});
|
|
$('.add-question').on(clickEvent, function(event) {
|
|
$('#template-survery').tmpl(setData(l, $('.questions > .attributes[data-question]').length + 1)).appendTo($('.questions'));
|
|
checkQuestionsLength();
|
|
textareaResizable(); // from textarea-lang-btn.js
|
|
l += 1;
|
|
$(document).trigger('ready')
|
|
});
|
|
|
|
$('.questions').on('change', '.type-selector', function(event) {
|
|
var $fieldType = $(this).closest('.control-group').siblings('.field-type'),
|
|
_val = $(this).val(),
|
|
_index = $(this).closest('.attributes').data().index;
|
|
|
|
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'),
|
|
$tabPane = $(this).closest('.tab-content');
|
|
$questionsTitle.text($tabPane.find('.active input[type="text"]').val())
|
|
event.preventDefault();
|
|
/* Act on the event */
|
|
});
|
|
$(".questions").sortable({
|
|
update : function ( event, ui ){
|
|
$(".questions .attributes").each( function ( i ){
|
|
$( this ).find( "input.sequence" ).val( i );
|
|
$( this ).find( "h4 span:eq(0)" ).text( i + 1 );
|
|
$( this ).attr( "data-index", i );
|
|
})
|
|
}
|
|
});
|
|
|
|
$('.questions').on(clickEvent, '.add-btn a, .remove-input, .delete, .attributes-header h4 i, .btn-group .btn', function(event) {
|
|
if($(this).hasClass('remove-input')){
|
|
inputAppendLength($(this), $(this).closest('.add-target').children('.input-append').length);
|
|
} else if($(this).hasClass('delete')) {
|
|
$(this).closest('.attributes').fadeOut(300, function() {
|
|
if( $( this ).data( 'question' ) == "old" ){
|
|
$(this).hide().removeAttr('data-question');
|
|
$( this ).find( '.should_destroy' ).val(1);
|
|
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 ).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')) {
|
|
if($(this).hasClass('icon-caret-up')) {
|
|
$(this).closest('.attributes-header').siblings('.attributes-body').slideUp(300)
|
|
$(this).addClass('icon-caret-down').removeClass('icon-caret-up');
|
|
} else {
|
|
$(this).closest('.attributes-header').siblings('.attributes-body').slideDown(300)
|
|
$(this).addClass('icon-caret-up').removeClass('icon-caret-down');
|
|
}
|
|
} 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') || 0,
|
|
_className = $(this).closest('.control-group').attr('class').match(/options|rgl/g)[0],
|
|
_index = $(this).closest('.attributes').data().index,
|
|
$add_btn = $(this).closest('.add-btn'),
|
|
_length = $add_btn.siblings('.input-append').length,
|
|
_form_type = parseInt($(this).parents('.attributes-body').eq(0).find('.type-selector').val());
|
|
var option_html;
|
|
if (_form_type == 7){
|
|
if (!$add_btn.hasClass('level2')){
|
|
_i = $(this).parents('.add-target').eq(0).find('.level1').length
|
|
option_html = add_2_level_options(_index, _i, _className)
|
|
}else{
|
|
_i = $(this).parents('.level1').data('index')
|
|
var level2_index = $add_btn.parents('.add-target').eq(0).children('.input-append').length
|
|
option_html = $('#template-survery-option').tmpl(setData(_index, "", _i+"][level2]["+level2_index, _className));
|
|
}
|
|
}else{
|
|
option_html = $('#template-survery-option').tmpl(setData(_index, "", _i, _className));
|
|
}
|
|
option_html.insertBefore($(this).closest('.add-btn'));
|
|
$(this).closest('.add-target').find('.input-append').find('.remove-input').removeClass('hide').prev().removeClass('last');
|
|
_i += 1
|
|
$(this).closest('.control-group').data('index', _i);
|
|
}
|
|
event.preventDefault();
|
|
});
|
|
checkQuestionsLength();
|
|
}); |