Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/cwaf/js/cwaf_catalog.js

/* REQUIRE:
 * jquery.js
 * plugins:
 *  jquery-ui
 *  jquery.md5.js
 */

//GLOBAL VARS 
// handler url:
var url_action = 'cwaf_catalog.cgi';
// exclude picture
var exclude_pic_url = 'cwaf/img/exclude.png';
// cache object
var cwaf_cache = {};
// updates storage
var cwaf_update = [];
// max height of table content (px)
var cwaf_max_content_length = 300;
// flag fo show_*_msg() 
var cwaf_msgbusy = false;
// queue for messages [{msg: 'ERROR 1', error: true}, {msg: 'info', error: false}...] 
var cwaf_msg_queue = [];

// catalog content ID
var contentid = '#cwaf_catalog_action_info';
// catalog short description ID
var shortid = '#cwaf_catalog_short_description';
// navigation container ID
var navid = 'span[id=cwaf_nav]';//'#cwaf_nav';
// update management ID
var updateid = 'span[id=cwaf_update]';//'#cwaf_update';
// domain selector form ID
var dselectid = '#cwaf_domain_selector';
// search field selector ID
var searchid = '#cwaf_search_input';
// content header ID
var headerid = '#cwaf_catalog_content_header';
// overlay ID
var overlayid = '#cwaf_overlay';
// message box ID
var msgid = '#cwaf_msg_wrapper';
// menu element ID
var menuid = '#cwaf_catalog_menu';

// class description
var dclass = {
  'item_content': 'item_content',
  'item_status': 'item_status',
  'item_enabled': 'item_enabled',
  'item_disabled': 'item_disabled',
  'item_ex_nav': 'item_ex_nav',
  'nav_category': 'nav_category',
  'nav_group': 'nav_group',
  'nav_rule': 'nav_rule',
  'upd_implement': 'upd_implement',
  'upd_flush': 'upd_flush',
  'msg_icon': 'cwaf_msg_icon',
  'msg_content': 'cwaf_msg_content',
  'msg_error': 'cwaf_msg_error',
  'msg_info': 'cwaf_msg_info',
  'rule_global': 'global_rule_search'
  };

// confirmation dialog
$.extend({ confirm: function (title, message, yesText, yesCallback, width) {
  $("<div></div>").dialog( {
    buttons: [{
              text: yesText,
              click: function() {
                       yesCallback();
                       $(this).remove();
                       }
            },
              {
              text: "Cancel",
              click: function() {
                        $(this).remove();
                        }
            }],
    close: function (event, ui) { $(this).remove(); },
    resizable: true,
    title: title,
    width: width ? width : 300,
    show: {effect: 'drop', duration: 1000},
    modal: true }).html(message).parent().addClass("alert");
    }
  });

// extended confirmation dialog (yes/no)
$.extend({ confirm_yesno: function (title, message, yesText, yesCallback, noText, noCallback, width) {
  $("<div></div>").dialog( {
    buttons: [{
              text: yesText,
              click: function() {
                       yesCallback();
                       $(this).remove();
                       }
            },
              {
              text: noText,
              click: function() {
                        noCallback();
                        $(this).remove();
                        }
            }],
    close: function (event, ui) { $(this).remove(); },
    resizable: true,
    title: title,
    width: width ? width : 300,
    show: {effect: 'drop', duration: 1000},
    modal: true }).html(message).parent().addClass("alert");
    }
  });

// show an error message
function show_error_msg(msg) {
  remove_overlay();
  // an show in progress, push msg to queue
  if(cwaf_msgbusy) {
    cwaf_msg_queue.push({msg: msg, error: true});
    return true;
    }

  // prevent duplicate (parallel) execution an show_*_msg
  // set flag
  cwaf_msgbusy = true;
  
  // message is defined and queue is not empty, push msg to queue and get the first from the queue
  if(msg && cwaf_msg_queue.length > 0) {
    cwaf_msg_queue.push({msg: msg, error: true});
    // if the first message is info - start show info
    if(! cwaf_msg_queue[0].error) {
      cwaf_msgbusy = false;
      show_info_msg();
      return true;
      }
    // get the first message
    msg = cwaf_msg_queue.shift().msg;
    }
  // queue is not empty, get the first from the queue
  else if(cwaf_msg_queue.length > 0) {
    // if the first message is info - start show info
    if(! cwaf_msg_queue[0].error) {
      cwaf_msgbusy = false;
      show_info_msg();
      return true;
      }
    msg = cwaf_msg_queue.shift().msg;
    }
  // if message is undefined - do exit
  else if(!msg) {
    cwaf_msgbusy = false;
    return true;
    }

  // prepare html
  $(msgid).html('<div></div><div></div>');
  $(msgid + ' >div:eq(0)').addClass(dclass.msg_icon).addClass(dclass.msg_error);
  $(msgid + ' >div:eq(1)').addClass(dclass.msg_content).html('ERROR!<br/>' + msg);

  // start show
  $(msgid).show('pulsate', {}, 2000, function() {
    setTimeout(function() {
      $(msgid).hide();
      // unset flag
      cwaf_msgbusy = false;
      // after finshing, check queue and restart if need
      if(cwaf_msg_queue.length > 0) {
        show_error_msg();
        }
      return true;
      }, 4000);
    });
  }

// show an info message
// see explanation in show_erro_msg comments
function show_info_msg(msg) {
  if(cwaf_msgbusy) {
    cwaf_msg_queue.push({msg: msg, error: false});
    return true;
    }

  cwaf_msgbusy = true;
  if(msg && cwaf_msg_queue.length > 0) {
    cwaf_msg_queue.push({msg: msg, error: false});
    if(cwaf_msg_queue[0].error) {
      cwaf_msgbusy = false;
      show_error_msg();
      return true;
      }
    msg = cwaf_msg_queue.shift().msg;
    }
  else if(cwaf_msg_queue.length > 0) {
    if(cwaf_msg_queue[0].error) {
      cwaf_msgbusy = false;
      show_error_msg();
      return true;
      }
    msg = cwaf_msg_queue.shift().msg;
    }
  else if(!msg){
    cwaf_msgbusy = false;
    return true;
    }
  
  $(msgid).html('<div></div><div></div>');
  $(msgid + ' >div:eq(0)').addClass(dclass.msg_icon).addClass(dclass.msg_info);
  $(msgid + ' >div:eq(1)').addClass(dclass.msg_content).html(msg);
  $(msgid).show('clip', {}, 500, function() {
    setTimeout(function() {
      $(msgid).hide();
      cwaf_msgbusy = false;
      if(cwaf_msg_queue.length > 0) {
        show_info_msg();
        }
      return true;
      }, 5000);
    });
  }

// get needed action
function get_next_action(ckey) {
  if(typeof ckey === 'undefined' || typeof cwaf_cache[ckey] === 'undefined') {
    return 'get_category_list';
    }
  if(cwaf_cache[ckey].action_type == 'category') {
    return 'get_group_list';
    }
  return 'get_rule_list';
  }

// get catalog data with ajax request
// or from local cache
function get_catalog_data(action, data, type) {
  // check parameters and set default if need
  var data = typeof data !== 'undefined' ? data : {};
  var type = typeof type !== 'undefined' ? type : 'post';
  // set default action if isn't defined
  data.action = typeof action !== 'undefined' ? action : get_next_action();

  // tmp variables
  var ainfo = {};
  var tmp = '';
  var display_css;

  // prepare cache key and check cache content
  $.each(data, function(k, v) { tmp += k + ':' + v; });
  var ckey = $.md5(tmp);

  // hide content while updating
  create_overlay();
  display_css = $(contentid).css('display');
  $(contentid).css('display', 'none');


  // store previous ckey
  tmp = $(contentid).data('action-cache');
  
  // clean updates if need
  if(tmp && cwaf_cache[tmp]) {
    if(data.domain != cwaf_cache[tmp].domain) {
      // we select another domain clean updates
      cwaf_update = [];
      cwaf_cache[tmp].modified = false;
      }
    }

  // cache data was found, return content
  if(typeof cwaf_cache[ckey] !== 'undefined') {
    if(cwaf_cache[ckey]['modified']) {
      // update cache key for current request
      $(contentid).data('action-cache', ckey);
      create_view_list(ckey, display_css);
      //alert("CACHE: " + ckey);
      return true;
      }
    }
    
  // remove previous cache
  if(tmp && cwaf_cache[tmp] && ! cwaf_cache[tmp].modified) {
    //alert("REMOVE OLD: " + tmp);
    delete(cwaf_cache[tmp]);
    }

  // prepare need data
  if(typeof data.domain !== 'undefined') {
    ainfo.domain = data.domain;
    }
  switch(data.action) {
    case 'get_category_list':
      ainfo.action_type = 'category';
      break;
    case 'get_group_list':
      ainfo.action_type = 'group';
      break;
    case 'get_rule_list':
    case 'get_rule_by_id':
      ainfo.action_type = 'rule';
      break;
    default:
      show_error_msg('unknown request (internal)');
      return false; 
    }

  // ajax request
  $.ajax({
      url: url_action,
      data: data,
      type: type,
      statusCode: {
                  412: function() { show_error_msg('action is undefined'); },
                  400: function() { show_error_msg('unknown action'); }
                  }
      })
      .done(function(msg) {
        // success
        if(msg.status == 1) {
          // remove request data
          delete(msg.status);
          delete(msg.errmsg);

          // store object to cache
          cwaf_cache[ckey] = msg;
          cwaf_cache[ckey]['modified'] = false;
        
          // insert action info data into the cache
          $.each(ainfo, function(key, val) { cwaf_cache[ckey][key] = val; });

          // update cache key for current request
          $(contentid).data('action-cache', ckey);

          // clean and reset search event
          set_search_event();

          // update html
          create_view_list(ckey, display_css);
          }
        // some error during request processing
        else { show_error_msg(msg.errmsg); }
        })
      .fail(function() { 
          show_error_msg("request to server failed!"); 
          });

  return true;
  }

// create html view
function create_view_list(ckey, display_css) {
  var item;
  var id;
  var output = '';

  // remove old data
  $(contentid).html('');

  update_short_description(ckey);
  update_header(ckey);

  // create view
  $.each(cwaf_cache[ckey].sort, function(index, key) {
    item = cwaf_cache[ckey].data.list[key];
    var ex = (item.exceptions == 1 ? "<a href='#' class='" + dclass.item_ex_nav + "'>"+'<img src="' + exclude_pic_url + '"></a>' : '');
    id = 'cwaf_item_' + (index + 1);
    output = '';
    output += "<p id='" + id + "'>";

    // if rule - last level
    if(cwaf_cache[ckey].action_type == 'rule') {
      output += "<span class='" + dclass.item_content + "'>" + key + '</span><span class="item_cell">' + item.description + '</span>';
      }
    // if not rule - prepare link to go to the next level
    else {
      output += "<a href='#' class='" + dclass.item_content + "'>" + key + '</a>';
      output += '<span class="item_cell">' + item.description + '</span><span class="item_cell">';
      output += (typeof item.group_count !== 'undefined' ? item.group_count : item.rule_parent_count) + '</span>';
      }
      
    // TODO: remove this hack after many categories will be added
    //if(cwaf_cache[ckey].action_type !== 'category') {
      if(item.status == 1) {
        output +=  "<a href='#' class='" + dclass.item_status + "'>ON</a>"+'<span class="item_ex">'+ex+'</span>';
        $(contentid).append(output);
        $('#' + id).attr('class', dclass.item_enabled);
        }
      else {
        output += "<a href='#' class='" + dclass.item_status + "'>OFF</a>"+'<span class="item_ex">'+ex+'</span>';
        $(contentid).append(output);
        $('#' + id).attr('class', dclass.item_disabled);
        }
      /*} else {
        output +=  "<span>ON</span>"+'<span class="item_ex">'+ex+'</span>';
        $(contentid).append(output);
        $('#' + id).attr('class', dclass.item_enabled);
      } */
    // store item name
    $('#' + id).data('item-name', key);
    });

  // check max height of table
  if($(contentid).outerHeight() > cwaf_max_content_length) {
    $(contentid).css('height', cwaf_max_content_length);
    }

  //$(contentid).css('display', display_css);
  $(contentid).show();
  if(cwaf_cache[ckey].sort.length>0) {
      update_navigation();
  }
  update_management();
  set_button_events();
  remove_overlay();
  return true;
  }

// set click events
function set_button_events() {

  // goto another level of menu
  $('.' + dclass.item_content).unbind('click').bind('click', function() {
    var name = $(this).parent().data('item-name');
    var ckey = $(contentid).data('action-cache');
    go_next_level(name, ckey);
    });

  // goto another level of menu with exclude list
  $('.' + dclass.item_ex_nav).unbind('click').bind('click', function() {
    var name = $(this).parent().parent().data('item-name');
    var ckey = $(contentid).data('action-cache');
    go_next_level(name, ckey);

    setTimeout(function(){
       filter_excludes();
    }, 1000);

    });

  // change item status
  $('.' + dclass.item_status).unbind('click').bind('click', function(event) {
    var item = $(this).parent().data('item-name');
    var ckey = $(contentid).data('action-cache');

    // set cache as modified and change item cache status
    cwaf_cache[ckey].data.list[item].status = cwaf_cache[ckey].data.list[item].status == 1 ? 0 : 1;
    cwaf_cache[ckey].modified = true;

    // modify html view
    if(cwaf_cache[ckey].data.list[item].status == 1) {
      $(this).text('ON');
      $(this).parent().attr('class', dclass.item_enabled);
      cwaf_cache[ckey].data.active_count++;
      cwaf_cache[ckey].data.inactive_count--;
      }
    else {
      $(this).text('OFF');
      $(this).parent().attr('class', dclass.item_disabled);
      cwaf_cache[ckey].data.active_count--;
      cwaf_cache[ckey].data.inactive_count++;
      }

    // disable page scrollUp
    event.preventDefault();

    collect_update(item, ckey);
    update_short_description(ckey);
    update_management();
    });

  // manage navigation menu
  $('.' + dclass.nav_category + ',.' + dclass.nav_group + ',.' + dclass.nav_rule).unbind('click').bind('click', function() {
    var data = $(navid).data('nav-data');
    var ckey = $(contentid).data('action-cache');
    var action = 'get_category_list';

    switch($(this).attr('class')) {
      case dclass.nav_category:
        delete(data.group);
        delete(data.category);
        break;
      case dclass.nav_group:
        delete(data.group);
        action = 'get_group_list';
        break;
      case dclass.nav_rule:
        action = 'get_rule_list';
      }
    if(cwaf_cache[ckey].domain) {
      data.domain = cwaf_cache[ckey].domain;
      }

    $(contentid).css('height', 'auto');
    get_catalog_data(action, data);
    });

    // global rule search
    $('.' + dclass.rule_global).unbind('click').bind('click', function() {
       var search = $(searchid).val();
       search = $.trim(search);
       if(search != "") {
         search = search.toString();
         var ckey = $(contentid).data('action-cache');
         var data = {};
         data.ruleid = search;
         if(cwaf_cache[ckey].domain) {
           data.domain = cwaf_cache[ckey].domain;
         }

        get_catalog_data('get_rule_by_id', data); 

         setTimeout(function(){
             ckey = $(contentid).data('action-cache');
             if(typeof cwaf_cache[ckey].not_found == 'undefined') {
               // show only parent rules in filter
               search = cwaf_cache[ckey].parent.toString();
               filter_element(search);
               $(searchid).val(search);
              } else {
                show_error_msg('No rule found for id ('+search+')');
              }
         }, 1000);
       }
    } );

  }

// create navigation bar ( category <- group <- rule ) 
function update_navigation() {
  var ckey = $(contentid).data('action-cache');
  var list = ['categories', 'groups', 'rules'];
  var lclass = [dclass.nav_category, dclass.nav_group, dclass.nav_rule];
  var data = {};

  switch(cwaf_cache[ckey].action_type) {
    case 'category':
      list.pop(); list.pop();
      list[0] = list[0].toUpperCase();
      break;
    case 'group':
      list.pop();
      list[1] = list[1].toUpperCase();
      data.category = cwaf_cache[ckey].data.list[cwaf_cache[ckey].sort[0]].category;
      break;
    case 'rule':
      list[2] = list[2].toUpperCase();
      data.category = cwaf_cache[ckey].data.list[cwaf_cache[ckey].sort[0]].category;
      data.group = cwaf_cache[ckey].data.list[cwaf_cache[ckey].sort[0]].group;
    }

  // remove old data
  $(navid).html('<ul></ul>');

  // store data
  $(navid).data('nav-data', data);
  
  // update html
  //$(navid).append('<ul></ul>');
  
  $.each(list, function(index, val) {
    $(navid + ">ul").append("<li><a href='#' class='" + lclass[index] + "'> " + val + " </a></li>");
    });
  }

// create short description (catalg info: type, items count ....)
function update_short_description(ckey) {
  var domain = cwaf_cache[ckey].domain;
  var tmp;
  var ctg_label = 'Disabled';
  var grp_label = 'Disabled';

  switch(cwaf_cache[ckey].action_type) { 
    case 'category':
      tmp = domain ? '<p><span class="cwaf_h1">Content: categories list</span><span class="cwaf_h2">Domain: ' + domain + '</span></p>' : '<p><span class="cwaf_h1">Content: categories list (global)</span></p>';
      tmp += '<p class="cwaf_counter"><span class="cwaf_h5">Categories count: ' + (cwaf_cache[ckey].data.active_count + cwaf_cache[ckey].data.inactive_count) + '</span>';
      tmp += '<span class="cwaf_h5">Active categories: ' + cwaf_cache[ckey].data.active_count + '</span></p>';
       break;
    case 'group':
        if( typeof cwaf_cache[ckey].sort[0] !== 'undefined' ) {
          ctg_label = cwaf_cache[ckey].data.list[cwaf_cache[ckey].sort[0]].category;
        }
        tmp = domain ? '<p><span class="cwaf_h1">Content: groups list</span><span class="cwaf_h2">Domain: ' + domain + '</span>' : '<p><span class="cwaf_h1">Content: groups list (global)</span>';
        tmp += '<span class="cwaf_h3">Category: ' + ctg_label + '<span class="cwaf_h3"></p>';
        tmp += '<p class="cwaf_counter"><span class="cwaf_h5">Groups count: ' + (cwaf_cache[ckey].data.active_count + cwaf_cache[ckey].data.inactive_count) + '</span>';
        tmp += '<span class="cwaf_h5">Active groups: ' + cwaf_cache[ckey].data.active_count + '</span></p>';
      break;
    case 'rule':
        if( typeof cwaf_cache[ckey].sort[0] !== 'undefined' ) {
          ctg_label = cwaf_cache[ckey].data.list[cwaf_cache[ckey].sort[0]].category;
          grp_label = cwaf_cache[ckey].data.list[cwaf_cache[ckey].sort[0]].group;
        }
        tmp = domain ? '<p><span class="cwaf_h1">Content: rules list</span><span class="cwaf_h2">Domain: ' + domain + '</span>' : '<p><span class="cwaf_h1">Content: rules list (global)</span>';
        tmp += '<span class="cwaf_h3">Category: ' + ctg_label + '<span class="cwaf_h3">';
        tmp += '<span class="cwaf_h3">Group: ' + grp_label + '<span class="cwaf_h3"></p>';
        tmp += '<p class="cwaf_counter"><span class="cwaf_h5">Rules count: ' + (cwaf_cache[ckey].data.active_count + cwaf_cache[ckey].data.inactive_count) + '</span>';
        tmp += '<span class="cwaf_h5">Active rules: ' + cwaf_cache[ckey].data.active_count + '</span></p>';
    }

  // add update counter
  if(cwaf_update.length != 0) {
    tmp += '<p class="cwaf_counter_update"><span class="cwaf_h5">Changes count: ' + cwaf_update.length + '</span></p>';
    }

  $(shortid).html(tmp);
  }

// create content header
function update_header(ckey) {
  var tmp;
  var item = 'Groups';
  switch(cwaf_cache[ckey].action_type) {
    case 'rule':
      tmp = '<p class="item_row_header"><span class="item_cell_id">Item ID</span>';
      tmp += '<span class="item_cell_description">Description</span><span class="item_cell_status"> Status</span><span class="item_cell_exs"></span></p>';
      break;
    case 'group':
      item = 'Rules';
    default:
      tmp = '<p class="item_row_header"><span class="item_cell_id">Item ID</span>';
      tmp += '<span class="item_cell_description">Description</span><span class="item_cell_include">' + item + ' </span><span class="item_cell_status"> Status</span><span class="item_cell_exs">Excl</span></p>';
    }

  $(headerid).html(tmp);
  }

// store new item update to storage
function collect_update(name, ckey) {
  var data = {};
  $.each(['category', 'group', 'id'], function(index, val) {
    if(typeof cwaf_cache[ckey].data.list[name][val] !== 'undefined') {
      data[val] = cwaf_cache[ckey].data.list[name][val];
      }
    else { data[val] = name; return false; }
    });
  data.status = cwaf_cache[ckey].data.list[name].status;
  cwaf_update.push(data);
  }

// refresh updates data and restore events
function update_management() {
  var tmp;
  $(updateid).html('');

  if(cwaf_update.length == 0) { return false; };
  $(updateid).html('<span class="cwaf_h3">Changes: </span><ul></ul>');
  $(updateid + '>ul').append("<li><a href='#' class='" + dclass.upd_implement + "'>Implement</a></li>");
  $(updateid + '>ul').append("<li><a href='#' class='" + dclass.upd_flush + "'>Flush</a></li>");

  // update event undo updates
  $('.' + dclass.upd_flush).unbind('click').bind('click', function() {
    var flushOk = function() {
      var data = {};
      var ckey = $(contentid).data('action-cache');

      // prepare data for request
      if(cwaf_cache[ckey].domain) {
        data.domain = cwaf_cache[ckey].domain;
        }

      // clean cache and update storage
      cwaf_update = [];
      cwaf_cache = {};

      // update html
      get_catalog_data('get_category_list', data);
      };
    $.confirm(
      "CONFIRM", //title
      "Do you really want to flush changes?", //message
      "Flush", //button text
      flushOk, //"yes" callback
      400 //width
      );
    return true;
    });

  // update event implement updates
  $('.' + dclass.upd_implement ).unbind('click').bind('click', function() {
    var tmp = 'Changes list:<br/>';
    var status;

    $.each(cwaf_update, function(ind, val) {
      status = cwaf_update[ind].status == 1 ? 'Enable' : 'Disable';
      if(cwaf_update[ind].id) {
        tmp += status + ' rule <i>' + cwaf_update[ind].id + '</i> (' + cwaf_update[ind].category + ' : '  +  cwaf_update[ind].group  +')<br/>';
        }
      else if(cwaf_update[ind].group) {
        tmp += status + ' group <i>' + cwaf_update[ind].group + '</i> (' + cwaf_update[ind].category + ')<br/>';
        }
      else {
        tmp += status + ' category <i>' + cwaf_update[ind].category + '</i><br/>';
        }
      });
      
    var updateOk = function() {
      send_json_update();
      };
      
    $.confirm(
      "CONFIRM", //title
      tmp + "<br/>Implement current changes?", //message
      "Implement", //button text
      updateOk, //"yes" callback
      500 // width
      );
    return true;
    });

  return true;
  }

// send request to update an exclude config
function send_json_update() {
  var ckey = $(contentid).data('action-cache');
  var udata = {};

  // fill update data
  if(cwaf_cache[ckey].domain) {
    udata.domain = cwaf_cache[ckey].domain;
    }
  udata.data = cwaf_update;

  // set overlay
  create_overlay();

  // ajax request
  $.ajax({
      url: url_action,
      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) {
          // stay only 'domain' if exists
          delete(udata.data);

          // clean cache and update storage
          cwaf_update = [];
          cwaf_cache = {};
          
          get_catalog_data('get_category_list', udata);
          show_info_msg('Update is successful');
          }
        else { show_error_msg(msg.errmsg); }
        })
      .fail(function() { show_error_msg("request to server failed!"); });

  return true;
  }

// enable simple search by item's name
function set_search_event() {
  $(searchid).val('');
  $(searchid).on('keyup', function(){
    var term = $(this).val();
    filter_element(term);
    });
  }

// goto another level of menu
function go_next_level(name, ckey) {
    var item = cwaf_cache[ckey].data.list[name];
    var data = {};
    var tmp;

    // rule - the last level
    // prevent goto action
    if(cwaf_cache[ckey].action_type == 'rule') {
      // show_error_msg('something gets wrong');
      return false;
      }

    // item is enabled, go to next level
    tmp = cwaf_cache[ckey].action_type == 'category' ? ['category'] : ['category', 'group'];
    if(item.status == 1) {
      // prepare data for request
      $.each(tmp, function(index, value) {
        if(typeof item[value] !== 'undefined') { data[value] = item[value]; }
        else { data[value] = name; }
        });

      // add domain if exists
      if(typeof cwaf_cache[ckey].domain !== 'undefined') {
        data.domain = cwaf_cache[ckey].domain;
        }

      // set height to auto
      $(contentid).css("height", "auto");
      get_catalog_data(get_next_action(ckey), data);
      }
    else {
      show_info_msg('You should enable this item first!');
      return false
      }
}

function filter_element(value) {
    if(value == "null") return;
    if(value != "") {
      value = value.toLowerCase();
      $(contentid + " >[id^='cwaf_item']").hide();

      // set height to auto
      $(contentid).css("height", "auto");
      $(contentid + " [id^='cwaf_item'] >[class~='" + dclass.item_content + "']").filter(function() {
          return $(this).text().toLowerCase().indexOf(value) > -1
          }).parent("[id^='cwaf_item']").show();

      // check max height of table
      if($(contentid).outerHeight() > cwaf_max_content_length) {
        $(contentid).css('height', cwaf_max_content_length);
        }
      }
    else  {
      $(contentid + " > [id^='cwaf_item']").show();

      // check max height of table
      if($(contentid).outerHeight() > cwaf_max_content_length) {
        $(contentid).css('height', cwaf_max_content_length);
        }
      }
}

function filter_excludes() {
  $(contentid + " >[id^='cwaf_item']").hide();

  // set height to auto
  $(contentid).css("height", "auto");
  $(contentid + " >[id^='cwaf_item']").filter(function() {
      var a = $(".item_status", $(this)).text() == "OFF";
      var b = $(".item_ex", $(this)).html().length != 0;
      return a || b ;
      }).show();

  // check max height of table
  if($(contentid).outerHeight() > cwaf_max_content_length) {
    $(contentid).css('height', cwaf_max_content_length);
    }
}

// prepare html form for domains selector
function get_domain_list() {
  // ajax request
  $.ajax({
      url: url_action,
      data: { 'action': 'domain_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) {
          $(dselectid+' option').remove();
          $(dselectid).append("<option value='global'>Global config</option>");
          // prepare html form
          $.each(msg.list, function(index, value) {
             if($.inArray(value, msg.disabled) < 0) {
                $(dselectid).append("<option value='" + value + "'>" + value + "</option>");
              }
            });
          select_config_by_domain();
          // fancy search widget
          $(dselectid).chosen();
          }
        else { show_error_msg(msg.errmsg); }
        })
      .fail(function() { show_error_msg("request to server failed!"); });
  return true;
  }

// set event to show relevant config for domain
function select_config_by_domain() {
  $(dselectid).on('change', function() {
    data = {};
    if($(this).val() != 'global') {
      data.domain = $(this).val();
      }
    get_catalog_data('get_category_list', data); 
    });
  }


// create(remove) overlay to prevent user's click action
function create_overlay(addx, addy) {
  addx = addx ? addx : 0;
  addy = addy ? addy : 0;
  var menupos = $(overlayid).position();
  $(overlayid).append("<div></div>");
  $(overlayid + '>div').css('background-position', (menupos.left + 800 + addx) + 'px ' + (menupos.top + 20 + addy) + 'px');
  };

function remove_overlay() {
  $(overlayid + '>div').remove();
  }

// first init or reactivation of catalog data
function activate_catalog() {
  var data = {};
  var action = 'get_category_list';
  var ckey;

  get_domain_list();
  // first init
  if(! $(contentid) || ! $(contentid).data('action-cache')) {
    $(dselectid + " option[value *= 'global']").attr('selected', true);
    get_catalog_data();
    }
  // return to category list for some domain
  else {
    ckey = $(contentid).data('action-cache');
    if(cwaf_cache[ckey].domain) {
      data.domain = cwaf_cache[ckey].domain;
      }
    get_catalog_data(action, data);
    }

  set_search_event();
  }
Back to Directory File Manager