<commit_before>/**
 * Shared functions
 **/

handleDetailsClick = function (evType, ev) {
  if (typeof handleDetailsClick.detailsType == 'undefined') {
    handleDetailsClick.detailsType = 'INITVALUE';
  }
  handleDetailsClick.swapKeyContent = function (index) {
    let ch = $('.changes:eq(' + index + ')');
    let dt = $('.details:eq(' + index + ')');

    if (handleDetailsClick.detailsType == 'CHANGES') {
      dt.hide();
      ch.show();
    }
    else if (handleDetailsClick.detailsType == 'KEYINFO'){
      ch.hide();
      dt.show();
    }
  }

  ev.stopImmediatePropagation();
  let acc = $('#key-display .ui.accordion');
  let thisBtn = $(ev.currentTarget);
  let clickIndex;
  if (evType == 'CHANGES') clickIndex = $('.changesBtn').index(thisBtn);
  if (evType == 'KEYINFO') clickIndex = $('.detailsBtn').index(thisBtn);

  // This is the first click after page loads
  if (typeof handleDetailsClick.activeIndex == 'undefined') {
    handleDetailsClick.detailsType = evType;
    handleDetailsClick.swapKeyContent(clickIndex);
    acc.accordion('open', clickIndex);
    handleDetailsClick.activeIndex = clickIndex;
  }
  // The same type of button as before was clicked
  else if (evType == handleDetailsClick.detailsType) {
    // Same button as before
    console.log(clickIndex + ", " + handleDetailsClick.activeIndex);
    if (clickIndex == handleDetailsClick.activeIndex) {
      acc.accordion('toggle', clickIndex);
    }
    // Corresponding button on another row
    else {
      handleDetailsClick.swapKeyContent(clickIndex);
      acc.accordion('open', clickIndex);
      handleDetailsClick.activeIndex = clickIndex;
    }
  }
  // The other type of button was clicked
  else {
    handleDetailsClick.detailsType = evType;

    // Other button on same row
    if (clickIndex == handleDetailsClick.activeIndex) {
      // Pane already open
      if ($('.details:eq(' + clickIndex + ')').parent().hasClass('active')) {
        acc.accordion('close', clickIndex);
        Meteor.setTimeout(function() {handleDetailsClick.swapKeyContent(clickIndex);}, 500);
        Meteor.setTimeout(function() {acc.accordion('open', clickIndex);}, 500);
      }
      // Pane closed
      else {
        handleDetailsClick.swapKeyContent(clickIndex);
        acc.accordion('open', clickIndex);
      }
    }
    // Other button on different row
    else {
      handleDetailsClick.swapKeyContent(clickIndex);
      acc.accordion('open', clickIndex);
      handleDetailsClick.activeIndex = clickIndex;
    }
  }
}

/**
 * Page Events
 **/

Template.keyDisplay.onRendered(function () {
  $('#key-display .ui.accordion').accordion({
    selector: {
      trigger: null
    },
    animateChildren: false
  });
})

/**
 * Helpers
 **/

Template.keyDisplay.helpers({
  keys: function () {
    return Keys.find({});
  },
  keyInfo: function (keyID) {
    return Keys.findOne({keyID: keyID});
  },
  hasChanges: function (keyID) {
    return Changes.findOne({keyID: keyID}) ? 'toggle' : 'disabled';
  },
  numChanges: function (keyID) {
    let changeCount = 0;
    let allChanges = Changes.findOne({keyID: keyID});
    if (allChanges) {
      let changeLog = allChanges.log
      changeLog.forEach(function (changesObj) {
        changesObj.changes.forEach(function () {
          changeCount++;
        });
      });
    }
    switch (changeCount) {
      case 0:
        return "No Change";
        break;
      case 1:
        return changeCount + " Change";
        break;
      default:
        return changeCount + " Changes";
    }
  },
  insertColorMarkerHlp: function (sev) {
    return insertColorMarker(sev, true);
  },
  logThis: function () {
    console.log(this);
  }
});

Template.keyDetails.helpers({
  parseCharacters: function () {
    let chars = this.characters;
    let charStr = '';
    for (let char in chars) {
      charStr += '<li>' + chars[char].characterName + '</li>\n';
    }
    return charStr;
  }
});

Template.changeDetails.helpers({
  keyChanges: function (keyID) {
    return Changes.find({keyID: keyID});
  },
  addBottomClass: function (log) {
    Meteor.setTimeout(function () {
      $( ".changeDetail:last-child" ).removeClass("attached").
      addClass("bottom attached");
    }, 1);
  },
  parseChangeHlp: function (changeType, sev, oldValStr, newValStr, oldValObj, newValObj, ctx, email) {
    return parseChange(changeType, sev, oldValStr, newValStr, oldValObj, newValObj, ctx, email);
  }
});

/**
 * Event Handlers
 **/

Template.keyDisplay.events({
  "click .validate-button": function() {
    // Fetch all keys from the database and validate them
    // TODO: Use "Async" library to process these in parallel
    Meteor.call('runChecks');
  },
  "click .toggle.changesBtn": function(ev) {
    handleDetailsClick('CHANGES', ev);
  },
  "click .toggle.detailsBtn": function(ev) {
    handleDetailsClick('KEYINFO', ev);
  },
  "click .rm-key": function() {
    Keys.remove(Keys.findOne({keyID: this.keyID})._id)
  },
});<commit_msg>Fix removing key from Key Explorer doesn't remove changes for that key<commit_after>/**
 * Shared functions
 **/

handleDetailsClick = function (evType, ev) {
  if (typeof handleDetailsClick.detailsType == 'undefined') {
    handleDetailsClick.detailsType = 'INITVALUE';
  }
  handleDetailsClick.swapKeyContent = function (index) {
    let ch = $('.changes:eq(' + index + ')');
    let dt = $('.details:eq(' + index + ')');

    if (handleDetailsClick.detailsType == 'CHANGES') {
      dt.hide();
      ch.show();
    }
    else if (handleDetailsClick.detailsType == 'KEYINFO'){
      ch.hide();
      dt.show();
    }
  }

  ev.stopImmediatePropagation();
  let acc = $('#key-display .ui.accordion');
  let thisBtn = $(ev.currentTarget);
  let clickIndex;
  if (evType == 'CHANGES') clickIndex = $('.changesBtn').index(thisBtn);
  if (evType == 'KEYINFO') clickIndex = $('.detailsBtn').index(thisBtn);

  // This is the first click after page loads
  if (typeof handleDetailsClick.activeIndex == 'undefined') {
    handleDetailsClick.detailsType = evType;
    handleDetailsClick.swapKeyContent(clickIndex);
    acc.accordion('open', clickIndex);
    handleDetailsClick.activeIndex = clickIndex;
  }
  // The same type of button as before was clicked
  else if (evType == handleDetailsClick.detailsType) {
    // Same button as before
    if (clickIndex == handleDetailsClick.activeIndex) {
      acc.accordion('toggle', clickIndex);
    }
    // Corresponding button on another row
    else {
      handleDetailsClick.swapKeyContent(clickIndex);
      acc.accordion('open', clickIndex);
      handleDetailsClick.activeIndex = clickIndex;
    }
  }
  // The other type of button was clicked
  else {
    handleDetailsClick.detailsType = evType;

    // Other button on same row
    if (clickIndex == handleDetailsClick.activeIndex) {
      // Pane already open
      if ($('.details:eq(' + clickIndex + ')').parent().hasClass('active')) {
        acc.accordion('close', clickIndex);
        Meteor.setTimeout(function() {handleDetailsClick.swapKeyContent(clickIndex);}, 500);
        Meteor.setTimeout(function() {acc.accordion('open', clickIndex);}, 500);
      }
      // Pane closed
      else {
        handleDetailsClick.swapKeyContent(clickIndex);
        acc.accordion('open', clickIndex);
      }
    }
    // Other button on different row
    else {
      handleDetailsClick.swapKeyContent(clickIndex);
      acc.accordion('open', clickIndex);
      handleDetailsClick.activeIndex = clickIndex;
    }
  }
}

/**
 * Page Events
 **/

Template.keyDisplay.onRendered(function () {
  $('#key-display .ui.accordion').accordion({
    selector: {
      trigger: null
    },
    animateChildren: false
  });
})

/**
 * Helpers
 **/

Template.keyDisplay.helpers({
  keys: function () {
    return Keys.find({});
  },
  keyInfo: function (keyID) {
    return Keys.findOne({keyID: keyID});
  },
  hasChanges: function (keyID) {
    return Changes.findOne({keyID: keyID}) ? 'toggle' : 'disabled';
  },
  numChanges: function (keyID) {
    let changeCount = 0;
    let allChanges = Changes.findOne({keyID: keyID});
    if (allChanges) {
      let changeLog = allChanges.log
      changeLog.forEach(function (changesObj) {
        changesObj.changes.forEach(function () {
          changeCount++;
        });
      });
    }
    switch (changeCount) {
      case 0:
        return "No Change";
        break;
      case 1:
        return changeCount + " Change";
        break;
      default:
        return changeCount + " Changes";
    }
  },
  insertColorMarkerHlp: function (sev) {
    return insertColorMarker(sev, true);
  },
  logThis: function () {
    console.log(this);
  }
});

Template.keyDetails.helpers({
  parseCharacters: function () {
    let chars = this.characters;
    let charStr = '';
    for (let char in chars) {
      charStr += '<li>' + chars[char].characterName + '</li>\n';
    }
    return charStr;
  }
});

Template.changeDetails.helpers({
  keyChanges: function (keyID) {
    return Changes.find({keyID: keyID});
  },
  addBottomClass: function (log) {
    Meteor.setTimeout(function () {
      $( ".changeDetail:last-child" ).removeClass("attached").
      addClass("bottom attached");
    }, 1);
  },
  parseChangeHlp: function (changeType, sev, oldValStr, newValStr, oldValObj, newValObj, ctx, email) {
    return parseChange(changeType, sev, oldValStr, newValStr, oldValObj, newValObj, ctx, email);
  }
});

/**
 * Event Handlers
 **/

Template.keyDisplay.events({
  "click .validate-button": function() {
    // Fetch all keys from the database and validate them
    // TODO: Use "Async" library to process these in parallel
    Meteor.call('runChecks');
  },
  "click .toggle.changesBtn": function(ev) {
    handleDetailsClick('CHANGES', ev);
  },
  "click .toggle.detailsBtn": function(ev) {
    handleDetailsClick('KEYINFO', ev);
  },
  "click .rm-key": function() {
    let foundChanges = Changes.findOne({keyID: this.keyID});

    if (foundChanges) Changes.remove(foundChanges._id);
    Keys.remove(Keys.findOne({keyID: this.keyID})._id);
  },
});
{'fraction_non_alphanumeric': 0.14633661397753256, 'fraction_numerical': 0.0019882692116512577, 'mean_word_length': 3.11788784281621, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5692445', 'n_tokens_mistral': 3130, 'n_tokens_neox': 3050, 'n_words': 676}