<commit_before>/* global URL jsyaml cre reslackedDb slackArchive reslackPlan */

var elDaysList = document.getElementById('days');
var topTagsBar = document.getElementById('tags');

var currentSlackChannel;
var currentSlackDate;

var currentDayReslacked;

var reslackStatusesByChannelDate;
var slackDump;

var teDayListItem = cre('li.day-item');
var dayListItemsByChannelDate = new Map();

var teDingrollMessage = cre('.dingroll-message', {wall: true}, [
  cre('.tag-line',[
    cre('select', {part: 'group-select'}),
    cre('button', {type: 'button', part: 'delete-message'}, '(Delete)'),
    cre('button', {type: 'button', part: 'grab-tags'}, '^-Grab-'),
    cre('input', {type: 'text', part: 'message-tags',
      pattern: "[ a-zA-Z0-9_-]*"}),
    cre('button', {type: 'button', part: 'apply-tags'}, "<-Apply-'")
  ]),
  cre('textarea', {part: 'message-body'})
]);

var teSlackMessage = cre('.slack-message', {wall: true}, [
  cre('div', {part: 'source-area'}, [
    cre('div', {part: 'description-line'}, [
      cre('span', {part: 'username'}),
      cre('span', {part: 'timestamp'}),
      cre('button', {type: 'button', part: 'show-source'}, 'Show original'),
    ]),
    cre('div', {part: 'source-details', hidden: true}, [
      cre('p', {part: 'original-message'}),
      // This textarea holds extra JSON to accompany this Slack message's
      // migrated DingRoll stuff, to address long-tail tweaks like "make the
      // second message an hour later" or "change this to come from another
      // user". These tweaks will be invented ad-hoc as necessary, then
      // baked into the import process that handles these migrations.
      cre('textarea', {part: 'source-override'})
    ])
  ]),
  cre('div', {part: 'dingroll-messages'}, [

    // new messages get inserted here

    cre('div.new-dingroll-message', {part: 'new-dingroll-message'}, [
      cre('select', {part: 'new-dingroll-message-group'}),
      cre('button', {type: 'button', part: 'add-dingroll-message'},
        'Add DingRoll message')
    ])
  ])
]);

var migrationProfile; // aka reslackPlan

function createDayListItem(channel, date) {
  var channelDate = channel + '/' + date;
  var li = cre(teDayListItem, {textContent: channel + ' ' + date});
  var dayStatus = reslackStatusesByChannelDate &&
    reslackStatusesByChannelDate.get(channelDate);
  if (dayStatus) {
    if (dayStatus.ready) li.classList.add('ready');
    else li.classList.add('partial');
  }
  li.addEventListener('click', openDay.bind(null, channel, date));
  dayListItemsByChannelDate.set(channelDate, li);
  return li;
}

function populateSelect(select, initial) {
  var groupNames = migrationProfile.groupsForChannel(currentSlackChannel);
  for (var i = 0; i < groupNames.length; i++) {
    select.appendChild(cre('option', groupNames[i]));
  }
  if (initial) {
    select.selectedIndex = groupNames.indexOf(initial);
  }
  return select;
}

function createDingrollMessageElement(dingrollMessage) {
  var root = cre(teDingrollMessage);
  var groupSelect = root.getPart('group-select');
  populateSelect(groupSelect, dingrollMessage.group);
  root.getPart('delete-message').addEventListener('click', function() {
    root.remove();
  });
  var messageTagsBar = root.getPart('message-tags');
  root.getPart('grab-tags').addEventListener('click', function() {
    topTagsBar.value = messageTagsBar.value;
  });
  root.getPart('apply-tags').addEventListener('click', function() {
    messageTagsBar.value = topTagsBar.value;
  });
  root.getPart('message-body').value = dingrollMessage.body;
  messageTagsBar.value = dingrollMessage.tags.join(' ');
  return root;
}

function setNewGroupSelection(dingrollMessageEls, newGroup) {
  var newGroupIndex = 0;
  var groupHasIndex = true;
  while (newGroupIndex < newGroup.length && groupHasIndex) {

    groupHasIndex = false;
    for (var j = 0; j < dingrollMessageEls.length && !groupHasIndex; j++) {
      var messageGroup = dingrollMessageEls[j].getPart('group-select');
      if (messageGroup.selectedIndex == newGroupIndex) groupHasIndex = true;
    }
    if (groupHasIndex) ++newGroupIndex;
  }
  if (newGroupIndex == newGroup.length) newGroupIndex = 0;
  newGroup.selectedIndex = newGroupIndex;
}

function createSlackMessageElement(slackMessage) {
  var root = cre(teSlackMessage);
  root.getPart('username').textContent = slackMessage.username;
  // TODO: hook up "Show original"
  var lastMessage = root.getPart('new-dingroll-message');
  var newGroup = root.getPart('new-dingroll-message-group');
  populateSelect(newGroup);
  // TODO: hook up additional message creator
  var dingrollMessageContainer = root.getPart('dingroll-messages');
  var dingrollMessages = slackMessage.dingrollMessages;
  var dingrollMessageElements = [];
  for (var i = 0; i < dingrollMessages.length; i++) {
    dingrollMessageElements[i] =
      createDingrollMessageElement(dingrollMessages[i]);
  }
  setNewGroupSelection(dingrollMessageElements, newGroup);
  dingrollMessageContainer.insertBefore(
    cre(dingrollMessageElements), lastMessage);
  return root;
}

function dingrollMessageFromElement(root) {
  return {
    group: root.getPart('group-select').value.textContent,
    body: root.getPart('message-body').value,
    tags: root.getPart('message-tags').value.split(/\s+/g)
  };
}

function updateDayDocMessages() {
  var slackMessageElements =
    elMessageContainer.getElementsByClassName('slack-message');
  for (var i = 0; i < slackMessageElements.length; i++) {
    var newDingrollMessages = [];
    var dingrollMessageElements =
      slackMessageElements[i].getElementsByClassName('dingroll-message');<commit_msg>Swap "Show original" and timestamp<commit_after>/* global URL jsyaml cre reslackedDb slackArchive reslackPlan */

var elDaysList = document.getElementById('days');
var topTagsBar = document.getElementById('tags');

var currentSlackChannel;
var currentSlackDate;

var currentDayReslacked;

var reslackStatusesByChannelDate;
var slackDump;

var teDayListItem = cre('li.day-item');
var dayListItemsByChannelDate = new Map();

var teDingrollMessage = cre('.dingroll-message', {wall: true}, [
  cre('.tag-line',[
    cre('select', {part: 'group-select'}),
    cre('button', {type: 'button', part: 'delete-message'}, '(Delete)'),
    cre('button', {type: 'button', part: 'grab-tags'}, '^-Grab-'),
    cre('input', {type: 'text', part: 'message-tags',
      pattern: "[ a-zA-Z0-9_-]*"}),
    cre('button', {type: 'button', part: 'apply-tags'}, "<-Apply-'")
  ]),
  cre('textarea', {part: 'message-body'})
]);

var teSlackMessage = cre('.slack-message', {wall: true}, [
  cre('div', {part: 'source-area'}, [
    cre('div', {part: 'description-line'}, [
      cre('span', {part: 'username'}),
      cre('button', {type: 'button', part: 'show-source'}, 'Show original'),
      cre('span', {part: 'timestamp'})
    ]),
    cre('div', {part: 'source-details', hidden: true}, [
      cre('p', {part: 'original-message'}),
      // This textarea holds extra JSON to accompany this Slack message's
      // migrated DingRoll stuff, to address long-tail tweaks like "make the
      // second message an hour later" or "change this to come from another
      // user". These tweaks will be invented ad-hoc as necessary, then
      // baked into the import process that handles these migrations.
      cre('textarea', {part: 'source-override'})
    ])
  ]),
  cre('div', {part: 'dingroll-messages'}, [

    // new messages get inserted here

    cre('div.new-dingroll-message', {part: 'new-dingroll-message'}, [
      cre('select', {part: 'new-dingroll-message-group'}),
      cre('button', {type: 'button', part: 'add-dingroll-message'},
        'Add DingRoll message')
    ])
  ])
]);

var migrationProfile; // aka reslackPlan

function createDayListItem(channel, date) {
  var channelDate = channel + '/' + date;
  var li = cre(teDayListItem, {textContent: channel + ' ' + date});
  var dayStatus = reslackStatusesByChannelDate &&
    reslackStatusesByChannelDate.get(channelDate);
  if (dayStatus) {
    if (dayStatus.ready) li.classList.add('ready');
    else li.classList.add('partial');
  }
  li.addEventListener('click', openDay.bind(null, channel, date));
  dayListItemsByChannelDate.set(channelDate, li);
  return li;
}

function populateSelect(select, initial) {
  var groupNames = migrationProfile.groupsForChannel(currentSlackChannel);
  for (var i = 0; i < groupNames.length; i++) {
    select.appendChild(cre('option', groupNames[i]));
  }
  if (initial) {
    select.selectedIndex = groupNames.indexOf(initial);
  }
  return select;
}

function createDingrollMessageElement(dingrollMessage) {
  var root = cre(teDingrollMessage);
  var groupSelect = root.getPart('group-select');
  populateSelect(groupSelect, dingrollMessage.group);
  root.getPart('delete-message').addEventListener('click', function() {
    root.remove();
  });
  var messageTagsBar = root.getPart('message-tags');
  root.getPart('grab-tags').addEventListener('click', function() {
    topTagsBar.value = messageTagsBar.value;
  });
  root.getPart('apply-tags').addEventListener('click', function() {
    messageTagsBar.value = topTagsBar.value;
  });
  root.getPart('message-body').value = dingrollMessage.body;
  messageTagsBar.value = dingrollMessage.tags.join(' ');
  return root;
}

function setNewGroupSelection(dingrollMessageEls, newGroup) {
  var newGroupIndex = 0;
  var groupHasIndex = true;
  while (newGroupIndex < newGroup.length && groupHasIndex) {

    groupHasIndex = false;
    for (var j = 0; j < dingrollMessageEls.length && !groupHasIndex; j++) {
      var messageGroup = dingrollMessageEls[j].getPart('group-select');
      if (messageGroup.selectedIndex == newGroupIndex) groupHasIndex = true;
    }
    if (groupHasIndex) ++newGroupIndex;
  }
  if (newGroupIndex == newGroup.length) newGroupIndex = 0;
  newGroup.selectedIndex = newGroupIndex;
}

function createSlackMessageElement(slackMessage) {
  var root = cre(teSlackMessage);
  root.getPart('username').textContent = slackMessage.username;
  // TODO: hook up "Show original"
  var lastMessage = root.getPart('new-dingroll-message');
  var newGroup = root.getPart('new-dingroll-message-group');
  populateSelect(newGroup);
  var dingrollMessageContainer = root.getPart('dingroll-messages');
  var dingrollMessages = slackMessage.dingrollMessages;
  var dingrollMessageElements = [];
  for (var i = 0; i < dingrollMessages.length; i++) {
    dingrollMessageElements[i] =
      createDingrollMessageElement(dingrollMessages[i]);
  }
  setNewGroupSelection(dingrollMessageElements, newGroup);
  // TODO: hook up additional message creator
  dingrollMessageContainer.insertBefore(
    cre(dingrollMessageElements), lastMessage);
  return root;
}

function dingrollMessageFromElement(root) {
  return {
    group: root.getPart('group-select').value.textContent,
    body: root.getPart('message-body').value,
    tags: root.getPart('message-tags').value.split(/\s+/g)
  };
}

function updateDayDocMessages() {
  var slackMessageElements =
    elMessageContainer.getElementsByClassName('slack-message');
  for (var i = 0; i < slackMessageElements.length; i++) {
    var newDingrollMessages = [];
    var dingrollMessageElements =
      slackMessageElements[i].getElementsByClassName('dingroll-message');
{'fraction_non_alphanumeric': 0.15154474431818182, 'fraction_numerical': 0.0014204545454545455, 'mean_word_length': 4.513950073421439, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, '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': '858652', 'n_tokens_mistral': 3544, 'n_tokens_neox': 3459, 'n_words': 761}