yannperceval / Comexpo - kuzzle udpate

// ==UserScript==
// @name         Comexpo - kuzzle udpate
// @version      0.1
// @description  Add human date + color indexes
// @author       Yann Roseau
// @copyright    2019, Yann Roseau (https://github.com/yroseau)
// @license      MIT
// @match        http://console.kuzzle.io/
// @grant        none
// ==/UserScript==

(function () {

  // C'est ultra sale.. fait à la va-vite pour mon usage perso

  function updateDataView() {
    if ($('[data-customcss]').length !== 0) {
      return;
    }

    $('.index-branch .tree-item:contains("catalog")')
      .attr('data-customcss', 'catalog')
      .closest('li').attr('data-order', '1000');
    $('.index-branch .tree-item:contains("backoffice"), \
.index-branch .tree-item:contains("global")')
      .attr('data-customcss', 'global')
      .closest('li').attr('data-order', '0');
    $('.index-branch .tree-item:contains("logs"), \
.index-branch .tree-item:contains("import")')
      .attr('data-customcss', 'unused')
      .attr('title', 'This index is unused')
      .closest('li').attr('data-order', '9999');
    $('.index-branch .tree-item:contains("mails"), \
.index-branch .tree-item:contains("selections")')
      .attr('data-customcss', 'other')
      .closest('li').attr('data-order', '5');

    var order = 20;
    $('.index-branch .tree-item:not([data-customcss])')
      .each(function () {
        var salon = $(this).text().trim();
        salon = salon.substring(0, salon.indexOf(' '));
        var numSession = $('.index-branch .tree-item:contains("' + salon + '_")').length;
        if (numSession !== 0) {
          $(this).attr('data-customcss', 'salon')
            .closest('li').attr('data-order', ++order);
        }
        else {
          $(this).attr('data-customcss', 'session')
            .closest('li').attr('data-order', ++order);
        }
      });

    // sort
    $(function () {
      $("Treeview-root>li").sort(sort_li).appendTo('.Treeview-root');

      function sort_li(a, b) {
        return ($(b).data('order')) < ($(a).data('order')) ? 1 : -1;
      }
    });

    // Add splitter handle
    var $sideNav = $('.side-nav');
    $('<div class="splitter-handle"></div>').insertAfter($sideNav.closest('aside'));
    var $splitterHandle = $('.splitter-handle');
    $splitterHandle.css('left', $sideNav.width());
    var $section = $splitterHandle.next();
    $section.attr('data-init-left', $sideNav.width());

    $splitterHandle.mousedown(function (e) {
      $('body').attr('style', 'cursor: col-resize !important;' + $('body').attr('style'));
      $(document).mousemove(function (e) {
          var width = Math.max(150, e.pageX);
          width = Math.min(width, $(document).width() / 2);
          $sideNav.css('width', width);
          $splitterHandle.css('left', $sideNav.width());
          $section.css('margin-left', $sideNav.width() - $section.attr('data-init-left'));
          e.preventDefault();
        })
        .mouseup(function (e) {
          $(document).unbind('mousemove');
          $('body').css('cursor', '');
          e.preventDefault();
        });
      e.preventDefault();
    });
  }

  function addHumanDate() {
    var dateKeys = ['updateUserValidityToken:', 'creationDate:', 'lastUpdate:', 'createdAt:', 'updatedAt:', 'activationDate:', 'date:']
    var all = document.querySelectorAll('.json-formatter-key:not(.human-date-treated)')
    for (let e of all) {
      e.classList.add("human-date-treated")
      if (dateKeys.includes(e.innerText)) {
        let val = parseInt(e.nextSibling.innerText)
        if (val < 10000000000) {
          val = val * 1000
        }
        let d = new Date(val);
        e.nextSibling.innerHTML = e.nextSibling.innerHTML + ' <span class="human-date">' + d.toString() + '</span>'
      }
    }
  }

  $(document).ready(function () {
    $('body').append('<style>\
        .human-date { \
            opacity: 0.6; \
            font-size: 0.9em; \
            margin-left: 0.5em; \
            font-weight: initial; \
        } \
    </style>');

    $('body').append('<style>\
        z[data-customcss="catalog"], [data-customcss="catalog"] .fa { \
          color: #8e9633 !important; \
        } \
        z[data-customcss="salon"], [data-customcss="salon"] .fa { \
          color: #92568b !important; \
        } \
        z[data-customcss="session"], [data-customcss="session"] .fa { \
          color: #226c92 !important; \
        } \
        z[data-customcss="unused"], [data-customcss="unused"] .fa { \
          color: #cc5c5c !important; \
        } \
        z[data-customcss="global"], [data-customcss="global"] .fa { \
          color: #bf9c3f !important; \
          /*font-weight: bold !important;*/ \
        } \
        .splitter-handle { \
            top: 0; \
            bottom: 0; \
            width: 10px; \
            background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAAMCAQAAADitaTfAAAAFUlEQVQIW2O++j+bkWUTAwZgJiwBAB9kCOy+u0d3AAAAAElFTkSuQmCC); \
            background-position: right center; \
            background-repeat: no-repeat; \
            position: fixed; \
            cursor: col-resize; \
        } \
    </style>');

    var currentHash = window.location.hash;
    setTimeout(addHumanDate, 500)
    //if (window.location.hash == "#/data") {
      setTimeout(updateDataView, 500);
    //}
    setInterval(function () {
      setTimeout(addHumanDate, 500);
      if (currentHash == window.location.hash) {
        return;
      }
      currentHash = window.location.hash;
      //if (window.location.hash == "#/data") {
        setTimeout(updateDataView, 500);
      //}
    }, 1000);
  });
})();