Jump to content

User:GhostInTheMachine/TalkHelper2.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.loader.load( '/w/index.php?title=User:BrandonXLF/FloatSide.css&action=raw&ctype=text/css', 'text/css' );

mw.loader.using(
  [ 'ext.gadget.CommentsInLocalTime',
    'jquery.makeCollapsible'
  ],
  TalkHelper2Init,
  function(){
    alert('TalkHelper2 could not load dependencies');
  }
);

function TalkHelper2Init() {
  console.log('TalkHelper2 init');

  if (window.commentsInLocalTimeWasRun) {
    console.log('TalkHelper2 CILT loaded');
    TalkHelper2CSS();
    TalkHelper2Run();
  } else {
  	console.log('TalkHelper2 waiting');
    setTimeout(TalkHelper2Init, 10);
  }
}

function TalkHelper2CSS() {
  mw.util.addCSS(`

  .localcomments {
    border-bottom: solid 2px cornflowerblue;
  }

  .talkHelperToday {
    background-color: moccasin;
    border-color: #3366cc;
    border-style: solid;
    border-width: thin;
    padding: 5px;
  }

  .talkHelperYesterday {
    background-color: cornsilk;
    border-color: #3366cc;
    border-style: dashed;
    border-width: thin;
    padding: 5px;
  }

  .talkHelperMe {
    border-width: medium;
  }

  .talkHelperRef {
    font-size: small;
    margin-left: 1em;
  }

  `);
}

function TalkHelper2Run() {

  $('#mw-panel').wrapInner('<div id="mw-panel-wp" class="navbox" style="text-align: left;"></div>');

  $subPanel = $('#mw-panel-wp');

  $subPanel.wrapInner('<div class="mw-collapsible-content"></div>');
  $subPanel.prepend('Wikipedia');
  $subPanel.makeCollapsible();
  $subPanel.addClass('navbox');

  TalkHelper2Build('Yesterday');
  TalkHelper2Build('Today');
}

function TalkHelper2Build(when) {
  var posts = [];

  $.each(
    $(".localcomments:contains(', " + when + "')"),
    function(){

      var id = $(this).attr('timestamp');
      $(this).parent().addClass('talkHelper' + when).attr('id', id);
      posts.push(1 * id);

    }
  );

  console.log('TalkHelper2 ' + when + '=' + posts.length);
  if(posts.length < 1) {
  	return;
  }

  mw.util.addPortletLink(
    'p-namespaces',
    '#',
    posts.length + ' ' + when.toLowerCase(),
    'ca-goto-' + when,
    'Go to first post for ' + when
  );

  $('#ca-goto-' + when).click( function(e){
    e.preventDefault();
    $('html, body').animate({scrollTop: $('.talkHelper' + when).first().offset().top - 20});
  });

  posts.sort();

  $('#mw-panel').prepend('<div id="mw-panel-' + when + '" class="navbox" style="text-align: left;"></div><p></p>');
  var $subPanel = $('#mw-panel-' + when);

  posts.forEach(
    function(id){
      var ts = new Date(id);
      $($subPanel).append(
        '<div class="talkHelperRef">' +
        '<a href="#' + id + '">' +
        ts.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false }) +
        '</a></div>'
      );
    }
  );

  $subPanel.wrapInner('<div class="mw-collapsible-content"></div>');
  $subPanel.prepend(posts.length + ' ' + when);
  $subPanel.makeCollapsible();

  $('.talkHelper' + when + ':contains(' + mw.config.get('wgUserName') + ')').addClass('talkHelperMe');
}