<commit_before>// Functions used by MIQ for the dhtmlxtoolbar control

// This function is called in miqOnLoad
function miqInitToolbars() {
  if (typeof miq_toolbars == "undefined") {
    return;
  }
  $.each(miq_toolbars, function (key) {
    miqInitToolbar(miq_toolbars[key]);
  });
  miqResizeTaskbarCell();
}

// Initialize a single toolbar
function miqInitToolbar(tb_hash) {
  tb = tb_hash.obj;
  tb.setIconsPath("/images/toolbars/");
  tb.loadXMLString(tb_hash.xml);
  tb.attachEvent("onClick", miqToolbarOnClick);
  tb.attachEvent("onStateChange", miqToolbarOnClick);
  miqHideToolbarButtons();
  miqSetToolbarButtonIds(tb);
}

// Set miq_alone class for non-pulldown buttons
function miqSetToolbarButtonIds(tb) {
  tb.forEachItem(function (itemId) {
    if (tb.objPull[tb.idPrefix + itemId].type != "buttonSelect") {
      var item = tb.objPull[tb.idPrefix + itemId];
      item.obj.id = "miq_alone"; /*class for parent div around text div*/
    }
  });
}

// Hide buttons in the toolbars
function miqHideToolbarButtons() {
  if (typeof miq_toolbars.view_tb != "undefined") {
    for (var x in miq_toolbars.view_tb.buttons) {
      if (view_tb.getType(x) == "button") {
        if (view_tb.getPosition(x) > 0 && miq_toolbars.view_tb.buttons[x].hidden) {
          view_tb.hideItem(x);
          view_tb.hideItem('sep_1');
        }
      }
    }
  }
  if (typeof miq_toolbars.center_tb != "undefined") {
    var tb = miq_toolbars.center_tb.obj;
    var buttons = miq_toolbars.center_tb.buttons;
    for (var x in buttons) {
      var count = 0;
      if (tb.getType(x) == "button") {
        if (tb.getPosition(x) >= 0 && buttons[x].hidden) {
          tb.hideItem(x);
        }
      } else if (tb.getType(x) == "buttonSelect") {
        for (var y in buttons) {
          // Hide any items in the list that is hidden
          if (tb.getListOptionPosition(x, y) > 0 && buttons[y].hidden) {
            tb.hideListOption(x, y);
            count++;
          }
          // Hide buttonselect button, if all items under it are hidden
          if (count == tb.getAllListOptions(x).length - 1 && tb.getPosition(x) > 0 && buttons[x].hidden) {
            tb.hideItem(x);
          }
          if (tb.getListOptionPosition(x, y) > 0 && typeof buttons[y].title != "undefined") {
            tb.setListOptionToolTip(x, y, buttons[y].title);
          }
        }
      }
    }
  }
  if (typeof miq_toolbars.custom_tb != "undefined") {
    var tb = miq_toolbars.custom_tb.obj;
    var buttons = miq_toolbars.custom_tb.buttons;
    for (var x in buttons) {
      var count = 0;
      if (tb.getType(x) == "buttonSelect") {
        for (var y in buttons) {
          // show titles for buttons under a button group
          if (tb.getListOptionPosition(x, y) > 0 && typeof buttons[y].title != "undefined") {
            tb.setListOptionToolTip(x, y, buttons[y].title);
          }
        }
      }
    }
  }
}

// Re-Initialize a single toolbar
function miqReinitToolbar(tb_name) {
  var tb = miq_toolbars[tb_name].obj;
  // Workaround to replace clearAll method call, it's is not available in 2.0 version of dhtmlx
  tb.forEachItem(function (id) {
    tb.removeItem(id);
  });
  var tb_hash = miq_toolbars[tb_name];
  tb.loadXMLString(tb_hash.xml);
  miqHideToolbarButtons();
}

// Function to run transactions when toolbar two state button is clicked
function miqToolbarOnStateChange(id, state) {
  miqToolbarOnClick(id);
}

// Function to run transactions when toolbar button is clicked
function miqToolbarOnClick(id) {
  var tb_url;
  var tb_hash;
  var button;
  tb_hash = miq_toolbars[this.base.parentNode.id]; // Use this line for toolbar v3.0

  if (!tb_hash.buttons.hasOwnProperty(id)) {
    return false;
  }

  button = tb_hash.buttons[id];

  if (button.hasOwnProperty("confirm") && !button.hasOwnProperty("popup")) {
    if (!confirm(button.confirm)) {
      return;
    }
  } else if (button.hasOwnProperty("confirm") && button.hasOwnProperty("popup")) {
    // to open console in a new window
    if (confirm(button.confirm)) {
      if (button.popup != "undefined" && button.popup) {
        if (button.hasOwnProperty("console_url")) {
          window.open(button.console_url);
        }
      }
    }
    return;
  } else if (!button.hasOwnProperty("confirm") && button.hasOwnProperty("popup")) {
    // to open readonly report in a new window, doesnt have confirm message
    if (button.popup) {
      if (button.hasOwnProperty("console_url")) {
        window.open(button.console_url);
      }
    }
    return;
  }

  if (button.hasOwnProperty("url")) {
    // See if a url is defined
    if (button.url.indexOf("/") === 0) {
      // If url starts with / it is non-ajax
      tb_url = "/" + miq_controller + button.url;
      if (typeof miq_record_id != "undefined" && miq_record_id != null) {
        tb_url += "/" + miq_record_id;
      }
      if (button.hasOwnProperty("url_parms")) {
        tb_url += button.url_parms;
      }
      DoNav(encodeURI(tb_url));
      return;
    } else {
      // An ajax url was defined
      tb_url = "/" + miq_controller + "/" + button.url;
      if (button.url.indexOf("x_history") !== 0) {
        // If not an explorer history button
        if (typeof miq_record_id != "undefined" && miq_record_id != null) {
          tb_url += "/" + miq_record_id;
        }
      }
    }
  } else {
    // No url specified, run standard button ajax transaction
    if (typeof button.explorer != "undefined" && button.explorer) {
      // Use x_button method for explorer ajax
      tb_url = "/" + miq_controller + "/x_button";
    } else {
      tb_url = "/" + miq_controller + "/button";
    }
    if (typeof miq_record_id != "undefined" && miq_record_id != null) {
      tb_url += "/" + miq_record_id;
    }
    tb_url += "?pressed=";
    if (typeof button.pressed == "undefined") {
      tb_url += id.split("__").pop();
    } else {
      tb_url += button.pressed;
    }
  }

  collect_log_buttons = [ 'support_vmdb_choice__collect_logs',
                          'support_vmdb_choice__collect_current_logs',
                          'support_vmdb_choice__zone_collect_logs',
                          'support_vmdb_choice__zone_collect_current_logs'
  ];
  if (jQuery.inArray(button.name, collect_log_buttons) >= 0 && button.prompt) {
    tb_url = miqSupportCasePrompt(tb_url);
    if (!tb_url) {
      return false;
    }
  }

  // put url_parms into params var, if defined
  var params;
  if (button.hasOwnProperty("url_parms")) {
    if (button.url_parms.match("_div$")) {
      if (miqDomElementExists('miq_grid_checks')) {
        params = "miq_grid_checks=" + $('#miq_grid_checks').val();
      } else {
        params = miqSerializeForm(button.url_parms);
      }
    } else {
      params = button.url_parms.split("?")[1];
    }
  }

  // TODO:
  // Checking for perf_reload button to not turn off spinning Q (will be done after charts are drawn).
  // Need to design this feature into the toolbar button support at a later time.
  if ((button.name == "perf_reload") ||
      (button.name == "vm_perf_reload") ||
      (button.name.match("_console$"))) {
    if (typeof params == "undefined") {
      miqJqueryRequest(tb_url, {beforeSend: true});
    } else {
      miqJqueryRequest(tb_url, {beforeSend: true, data: params});
    }
  } else {
    if (typeof params == "undefined") {
      miqJqueryRequest(tb_url, {beforeSend: true, complete: true});
    } else {
      miqJqueryRequest(tb_url, {beforeSend: true, complete: true, data: params});
    }
  }
  return false;
}

function miqSupportCasePrompt(tb_url) {
  var support_case = prompt('Enter Support Case:', '');
  if (support_case === null) {
    return false;
  } else if (support_case.trim() == '') {
    alert('Support Case must be provided to collect logs');
    return false;
  } else {
    tb_url = tb_url + '&support_case=' + encodeURIComponent(support_case);
    return tb_url;
  }
}

// Handle chart context menu clicks
function miqWidgetToolbarClick(itemId, itemValue) {
  if (itemId == "reset") {
    if (confirm("Are you sure you want to reset this Dashboard's Widgets to the defaults?")) {
      miqAjax("/dashboard/reset_widgets");
    }
  } else if (itemId == "add_widget") {
    return;
  } else {
    miqAjax("/dashboard/widget_add?widget=" + itemId);
  }
}
<commit_msg>Custom button fix for https://github.com/ManageIQ/manageiq/pull/3365 causes js exception in non-explorer tabs<commit_after>// Functions used by MIQ for the dhtmlxtoolbar control

// This function is called in miqOnLoad
function miqInitToolbars() {
  if (typeof miq_toolbars == "undefined") {
    return;
  }
  $.each(miq_toolbars, function (key) {
    miqInitToolbar(miq_toolbars[key]);
  });
  if (typeof miqResizeTaskbarCell == "undefined") {
     return;
  }
  miqResizeTaskbarCell();
}

// Initialize a single toolbar
function miqInitToolbar(tb_hash) {
  tb = tb_hash.obj;
  tb.setIconsPath("/images/toolbars/");
  tb.loadXMLString(tb_hash.xml);
  tb.attachEvent("onClick", miqToolbarOnClick);
  tb.attachEvent("onStateChange", miqToolbarOnClick);
  miqHideToolbarButtons();
  miqSetToolbarButtonIds(tb);
}

// Set miq_alone class for non-pulldown buttons
function miqSetToolbarButtonIds(tb) {
  tb.forEachItem(function (itemId) {
    if (tb.objPull[tb.idPrefix + itemId].type != "buttonSelect") {
      var item = tb.objPull[tb.idPrefix + itemId];
      item.obj.id = "miq_alone"; /*class for parent div around text div*/
    }
  });
}

// Hide buttons in the toolbars
function miqHideToolbarButtons() {
  if (typeof miq_toolbars.view_tb != "undefined") {
    for (var x in miq_toolbars.view_tb.buttons) {
      if (view_tb.getType(x) == "button") {
        if (view_tb.getPosition(x) > 0 && miq_toolbars.view_tb.buttons[x].hidden) {
          view_tb.hideItem(x);
          view_tb.hideItem('sep_1');
        }
      }
    }
  }
  if (typeof miq_toolbars.center_tb != "undefined") {
    var tb = miq_toolbars.center_tb.obj;
    var buttons = miq_toolbars.center_tb.buttons;
    for (var x in buttons) {
      var count = 0;
      if (tb.getType(x) == "button") {
        if (tb.getPosition(x) >= 0 && buttons[x].hidden) {
          tb.hideItem(x);
        }
      } else if (tb.getType(x) == "buttonSelect") {
        for (var y in buttons) {
          // Hide any items in the list that is hidden
          if (tb.getListOptionPosition(x, y) > 0 && buttons[y].hidden) {
            tb.hideListOption(x, y);
            count++;
          }
          // Hide buttonselect button, if all items under it are hidden
          if (count == tb.getAllListOptions(x).length - 1 && tb.getPosition(x) > 0 && buttons[x].hidden) {
            tb.hideItem(x);
          }
          if (tb.getListOptionPosition(x, y) > 0 && typeof buttons[y].title != "undefined") {
            tb.setListOptionToolTip(x, y, buttons[y].title);
          }
        }
      }
    }
  }
  if (typeof miq_toolbars.custom_tb != "undefined") {
    var tb = miq_toolbars.custom_tb.obj;
    var buttons = miq_toolbars.custom_tb.buttons;
    for (var x in buttons) {
      var count = 0;
      if (tb.getType(x) == "buttonSelect") {
        for (var y in buttons) {
          // show titles for buttons under a button group
          if (tb.getListOptionPosition(x, y) > 0 && typeof buttons[y].title != "undefined") {
            tb.setListOptionToolTip(x, y, buttons[y].title);
          }
        }
      }
    }
  }
}

// Re-Initialize a single toolbar
function miqReinitToolbar(tb_name) {
  var tb = miq_toolbars[tb_name].obj;
  // Workaround to replace clearAll method call, it's is not available in 2.0 version of dhtmlx
  tb.forEachItem(function (id) {
    tb.removeItem(id);
  });
  var tb_hash = miq_toolbars[tb_name];
  tb.loadXMLString(tb_hash.xml);
  miqHideToolbarButtons();
}

// Function to run transactions when toolbar two state button is clicked
function miqToolbarOnStateChange(id, state) {
  miqToolbarOnClick(id);
}

// Function to run transactions when toolbar button is clicked
function miqToolbarOnClick(id) {
  var tb_url;
  var tb_hash;
  var button;
  tb_hash = miq_toolbars[this.base.parentNode.id]; // Use this line for toolbar v3.0

  if (!tb_hash.buttons.hasOwnProperty(id)) {
    return false;
  }

  button = tb_hash.buttons[id];

  if (button.hasOwnProperty("confirm") && !button.hasOwnProperty("popup")) {
    if (!confirm(button.confirm)) {
      return;
    }
  } else if (button.hasOwnProperty("confirm") && button.hasOwnProperty("popup")) {
    // to open console in a new window
    if (confirm(button.confirm)) {
      if (button.popup != "undefined" && button.popup) {
        if (button.hasOwnProperty("console_url")) {
          window.open(button.console_url);
        }
      }
    }
    return;
  } else if (!button.hasOwnProperty("confirm") && button.hasOwnProperty("popup")) {
    // to open readonly report in a new window, doesnt have confirm message
    if (button.popup) {
      if (button.hasOwnProperty("console_url")) {
        window.open(button.console_url);
      }
    }
    return;
  }

  if (button.hasOwnProperty("url")) {
    // See if a url is defined
    if (button.url.indexOf("/") === 0) {
      // If url starts with / it is non-ajax
      tb_url = "/" + miq_controller + button.url;
      if (typeof miq_record_id != "undefined" && miq_record_id != null) {
        tb_url += "/" + miq_record_id;
      }
      if (button.hasOwnProperty("url_parms")) {
        tb_url += button.url_parms;
      }
      DoNav(encodeURI(tb_url));
      return;
    } else {
      // An ajax url was defined
      tb_url = "/" + miq_controller + "/" + button.url;
      if (button.url.indexOf("x_history") !== 0) {
        // If not an explorer history button
        if (typeof miq_record_id != "undefined" && miq_record_id != null) {
          tb_url += "/" + miq_record_id;
        }
      }
    }
  } else {
    // No url specified, run standard button ajax transaction
    if (typeof button.explorer != "undefined" && button.explorer) {
      // Use x_button method for explorer ajax
      tb_url = "/" + miq_controller + "/x_button";
    } else {
      tb_url = "/" + miq_controller + "/button";
    }
    if (typeof miq_record_id != "undefined" && miq_record_id != null) {
      tb_url += "/" + miq_record_id;
    }
    tb_url += "?pressed=";
    if (typeof button.pressed == "undefined") {
      tb_url += id.split("__").pop();
    } else {
      tb_url += button.pressed;
    }
  }

  collect_log_buttons = [ 'support_vmdb_choice__collect_logs',
                          'support_vmdb_choice__collect_current_logs',
                          'support_vmdb_choice__zone_collect_logs',
                          'support_vmdb_choice__zone_collect_current_logs'
  ];
  if (jQuery.inArray(button.name, collect_log_buttons) >= 0 && button.prompt) {
    tb_url = miqSupportCasePrompt(tb_url);
    if (!tb_url) {
      return false;
    }
  }

  // put url_parms into params var, if defined
  var params;
  if (button.hasOwnProperty("url_parms")) {
    if (button.url_parms.match("_div$")) {
      if (miqDomElementExists('miq_grid_checks')) {
        params = "miq_grid_checks=" + $('#miq_grid_checks').val();
      } else {
        params = miqSerializeForm(button.url_parms);
      }
    } else {
      params = button.url_parms.split("?")[1];
    }
  }

  // TODO:
  // Checking for perf_reload button to not turn off spinning Q (will be done after charts are drawn).
  // Need to design this feature into the toolbar button support at a later time.
  if ((button.name == "perf_reload") ||
      (button.name == "vm_perf_reload") ||
      (button.name.match("_console$"))) {
    if (typeof params == "undefined") {
      miqJqueryRequest(tb_url, {beforeSend: true});
    } else {
      miqJqueryRequest(tb_url, {beforeSend: true, data: params});
    }
  } else {
    if (typeof params == "undefined") {
      miqJqueryRequest(tb_url, {beforeSend: true, complete: true});
    } else {
      miqJqueryRequest(tb_url, {beforeSend: true, complete: true, data: params});
    }
  }
  return false;
}

function miqSupportCasePrompt(tb_url) {
  var support_case = prompt('Enter Support Case:', '');
  if (support_case === null) {
    return false;
  } else if (support_case.trim() == '') {
    alert('Support Case must be provided to collect logs');
    return false;
  } else {
    tb_url = tb_url + '&support_case=' + encodeURIComponent(support_case);
    return tb_url;
  }
}

// Handle chart context menu clicks
function miqWidgetToolbarClick(itemId, itemValue) {
  if (itemId == "reset") {
    if (confirm("Are you sure you want to reset this Dashboard's Widgets to the defaults?")) {
      miqAjax("/dashboard/reset_widgets");
    }
  } else if (itemId == "add_widget") {
    return;
  } else {
    miqAjax("/dashboard/widget_add?widget=" + itemId);
  }
}

{'fraction_non_alphanumeric': 0.13577085690466265, 'fraction_numerical': 0.002381944857976538, 'mean_word_length': 2.735320284697509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4881376', 'n_tokens_mistral': 5588, 'n_tokens_neox': 5575, 'n_words': 1275}