/* REQUIRE:
* jquery.js
* cwaf_wizard_ref.js (questionnare data)
* plugins:
* jquery-ui
* jquery.bonsai.js (treeview: http://aexmachina.github.io/jquery-bonsai/)
* jquery.qubit.js (treeview checkboxes: https://github.com/aexmachina/jquery-qubit)
*/
// CWAF wizard functions
// handler URL
var wiz_handler_url = "cwaf_catalog.cgi";
// page header
var wiz_header_div = '#cwaf_wizard_header';
// questionnaire container
var wiz_questionnaire_div = '#cwaf_wizard_questionnaire';
// resuming tree container
var wiz_result_div = '#cwaf_wizard_result';
// save previous excludes
var wiz_save_excludes_choise = false;
// wizard updates storage
var wiz_updates = [];
// show first screen
function wiz_show_questionnaire(ask_save_ex) {
var ask_save_ex = typeof ask_save_ex !== 'undefined' ? ask_save_ex : true;
// prepare pages
$(wiz_header_div).html('<h1>Welcome to COMODO Protection Wizard</h1><h3>Please check categories you like to protect.</h3>');
$(wiz_questionnaire_div).html('');
$(wiz_result_div).html('');
// print questions
for (var key in wiz_reference) {
$(wiz_questionnaire_div).append( wiz_question_row(key, wiz_reference[key]['msg'], wiz_reference[key]['dscr']) );
}
// NEXT button
$(wiz_questionnaire_div).append(
$('<input>').prop({type:'button', class:'wiz_button', id:'wiz_next', value:'Next >'}).unbind('click').bind('click', function(e) {
var answers = wiz_collect_answers();
show_wizard(answers);
})
);
// do not ask for first run
if(plugin_firstrun) {
wiz_save_excludes_choise = true;
// remove first run flag to not bother user
update_firstrun();
} else {
wiz_asc_to_save_ex(ask_save_ex);
}
}
function wiz_question_row(key, question, description) {
return '<fieldset class="wiz_question"><legend>'+question+'</legend><input type="checkbox" id="'+key+'" checked> '+description+'</fieldset>';
}
function wiz_asc_to_save_ex(ask_save_ex) {
if(ask_save_ex) {
// display confirmation dialog
wiz_save_excludes_choise = false;
$.confirm(
"Please confirm", //title
"Add rules you excluded before to current choice?", //message
"Yes", //button text
wiz_save_excludes,
300
);
}
}
// save previous user excludes
function wiz_save_excludes() {
wiz_save_excludes_choise = true;
}
// get wizard data with ajax request
function show_wizard(answers) {
create_overlay();
$(wiz_header_div).html("<h1>Protection Tree</h1><h3>Please check Categories/Groups/Rules you like to protect.</h3>");
$(wiz_result_div).html('<hr>Loading...');
$.ajax({
url: wiz_handler_url,
data: {action:'wizard_scheme_list'},
type: 'POST',
statusCode: {
412: function() { show_error_msg('action is undefined'); },
400: function() { show_error_msg('unknown action'); }
}
})
.done(function(msg) {
// success
if(msg.status == 1) {
wiz_prepare_tree(msg, answers);
remove_overlay();
}
// some error during request processing
else { show_error_msg(msg.errmsg); }
})
.fail(function() {
show_error_msg("Request to server failed!");
});
return true;
}
function wiz_prepare_tree(data, unchecked) {
// parse data and send needed function
$(wiz_result_div).html(data.list);
$('#cwaf_wizard_tree').bonsai({
checkboxes: true,
createCheckboxes: true,
expandAll: false,
handleDuplicateCheckboxes: false
});
// BACK button
$(wiz_result_div).append(
$('<input>').prop({type:'button', class:'wiz_button', id:'wiz_back', value:' < Back'}).unbind('click').bind('click', function(e) {
wiz_show_questionnaire(false);
})
);
// APPLY button
$(wiz_result_div).append(
$('<input>').prop({type:'button', class:'wiz_button', id:'wiz_ok', value:'Apply changes'}).unbind('click').bind('click', function(e) {
$.confirm(
"Confirm", //title
"Apply your changes to global exclude list?", //message
"Apply", //button text
wiz_apply_changes,
300
);
})
);
// check all checkboxes in div
$('#cwaf_wizard_tree').find(':checkbox').each(function(){
$(this).attr('checked', true);
});
// uncheck user choise
var boxname = 'init';
for (var key in unchecked) {
for (var block_num in wiz_reference[key]) {
if(block_num !== 'msg') {
var refblock = wiz_reference[key][block_num];
if( refblock['id'] ) {
boxname = 'rule-'+refblock['id'];
}
else if( refblock['group'] ) {
boxname = 'group-'+refblock['group'];
}
else if( refblock['category'] ) {
boxname = 'cat-'+refblock['category'];
}
else { continue; }
$('#cwaf_wizard_tree :checkbox[name='+boxname+']').attr('checked', false).trigger('change');
}
}
}
// uncheck previously excluded
if(wiz_save_excludes_choise) {
for (var i = 0; i < data.excludes.length; i++) {
$('#cwaf_wizard_tree :checkbox[name=rule-'+data.excludes[i]+']').attr('checked', false).trigger('change');
}
}
}
// collect user answers
function wiz_collect_answers() {
// return object
var obj = {};
// loop through checkboxes and collect NOT checked so we can add excludes
$(wiz_questionnaire_div).find(':checkbox').each(function(){
if ( ! $(this).is(":checked") ) {
obj[$(this).attr('id')] = 1;
}
});
// clear form page
$(wiz_questionnaire_div).html('');
return obj;
}
// apply changes to exclude list
function wiz_apply_changes() {
// set overlay
create_overlay();
// iterate all unchecked checkboxes
wiz_prepare_exclude_data();
var udata = {};
udata.data = wiz_updates;
// ajax request
$.ajax({
url: wiz_handler_url,
data: { updates: JSON.stringify(udata), action: 'update_list' },
type: 'POST',
statusCode: {
412: function() { show_error_msg('Action is undefined'); },
400: function() { show_error_msg('Unknown action'); }
}
})
.done(function(msg) {
if(msg.status == 1) {
// clean wizard updates storage
wiz_updates = [];
remove_overlay();
show_info_msg("Update is successful.");
$(wiz_header_div).html("<h1>Your choise applied to exclude list</h1><h3>Thank you for using COMODO Protection Wizard. You can check excluded rules in 'Catalog' tab.</h3>");
$(wiz_questionnaire_div).html('');
$(wiz_result_div).html('');
}
else { show_error_msg(msg.errmsg); }
})
.fail(function() { show_error_msg("Request to server failed!"); });
return true;
}
// save all excludes from tree
function wiz_prepare_exclude_data() {
// clear update data
wiz_updates = [];
// process checked
var $sel = $('#cwaf_wizard_tree').find("input:checked");
$sel.each(function(){
wiz_process_checkbox($(this), true);
});
// process not checked
var $unsel = $('#cwaf_wizard_tree').find("input:checkbox:not(:checked)");
$unsel.each(function(){
wiz_process_checkbox($(this), false);
});
}
function wiz_process_checkbox(checkbox, test_if_checked) {
// for default check if is not checked
var test_if_checked = typeof test_if_checked !== 'undefined' ? test_if_checked : false;
// assign state according to check
var state = test_if_checked ? 1 : 0;
var unchecked_name = checkbox.attr('name');
// push undeterminate states of checkboxes (categories and groups should be ON in catalog in this case) with status 1
if ( checkbox.prop('indeterminate') ) {
state = 1;
}
//------- category -------
if ( unchecked_name.match(/^cat/i) ) {
wiz_updates.push({'category':wiz_element_name(unchecked_name), 'status':state});
return;
}
var $parent = checkbox.parent().parent().siblings('input');
var $parentname = $parent.attr('name');
var parent_prop_check = test_if_checked ? (!$parent.prop('checked')) : $parent.prop('checked');
//------- group -------
// if parent is category - it's a group checkbox
if ( $parentname.match(/^cat/i) ) {
// do not exclude children of excluded parents
if ( parent_prop_check || $parent.prop('indeterminate') ) {
wiz_updates.push({'category':wiz_element_name($parentname), 'group':wiz_element_name(unchecked_name), 'status':state});
return;
} else return;
}
//------- rule -------
// it's a rule checkbox, need to get groupname and catname
if ( $parentname.match(/^group/i) ) {
// do not exclude children of excluded parents
if ( parent_prop_check || $parent.prop('indeterminate') ) {
// save group name and re-assign parents
var group = $parentname;
$parent = $parent.parent().parent().siblings('input');
$parentname = $parent.attr('name');
// do not exclude children of excluded parents
if ( parent_prop_check || $parent.prop('indeterminate') ) {
if ( $parentname.match(/^cat/i) ) {
wiz_updates.push({'category':wiz_element_name($parentname),'group':wiz_element_name(group),'id':wiz_element_name(unchecked_name), 'status':state});
}
} else return;
} else return;
}
else {
show_error_msg('Something wrong in the tree');
}
return;
}
// get category, group, rule name form element name
// element names:
// category: cat-<category_name>
// group: group-<group_name>
// rule: rule-<rule_name>
function wiz_element_name(item_name) {
return item_name.substring(item_name.indexOf("-") + 1);
}