Guest User

ref_popup.js

a guest
Jan 14th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //  "THE CAKE-WARE LICENSE" (Revision 41):
  3. //
  4. //      Millhiore Firianno Biscotti wrote this file.  As long as you retain
  5. //  this notice you can do whatever you want with this stuff. If we meet
  6. //  some day, and you think this stuff is worth it, you can buy me a cake
  7. //  in return.
  8. //
  9. //      Millihi.
  10. ///////////////////////////////////////////////////////////////////////////////
  11.  
  12. var RefPopup = function (props) {
  13.    var POPUP_VERTICAL_OFFSET = 5;
  14.    var REMOVE_TIMEOUT = 250;
  15.  
  16.    var popupIsVisible = false;
  17.    var timerId = null;
  18.  
  19.    ////////////////////////////////////////////////////////////////////////////
  20.    //  init
  21.    function init () {
  22.       document.addEventListener ("mouseover", onMouseOver);
  23.       document.addEventListener ("mouseout", onMouseOut);
  24.    }
  25.  
  26.    ////////////////////////////////////////////////////////////////////////////
  27.    //  destroy
  28.    function destroy () {
  29.       document.removeEventListener ("mouseover", onMouseOver);
  30.       document.removeEventListener ("mouseout", onMouseOut);
  31.    }
  32.  
  33.    ////////////////////////////////////////////////////////////////////////////
  34.    //  onMouseOver
  35.    function onMouseOver (ev) {
  36.       if (ev.target) {
  37.          dispatchMouseOverEvent (ev);
  38.       }
  39.    }
  40.  
  41.    ////////////////////////////////////////////////////////////////////////////
  42.    //  onMouseOut
  43.    function onMouseOut (ev) {
  44.       if (ev.target && ev.target.parentNode) {
  45.          popupIsVisible = false;
  46.          dispatchMouseOutEvent (ev);
  47.       }
  48.    }
  49.  
  50.    ////////////////////////////////////////////////////////////////////////////
  51.    //  dispatchMouseOverEvent
  52.    function dispatchMouseOverEvent (ev) {
  53.       var target = ev.target;
  54.  
  55.       if (target.parentNode.classList.contains ('ref-popup')) {
  56.          if (timerId) {
  57.             clearTimeout (timerId);
  58.             timerId = null;
  59.          }
  60.  
  61.          removeLastPopup (target.parentNode);
  62.  
  63.          return;
  64.       }
  65.  
  66.       if (target.classList.contains ('js-ref')) {
  67.          var hid = target.innerHTML.replace ('>>', '');
  68.          var url = target.getAttribute ('href').replace ('#', '');
  69.          var post = document.querySelector
  70.            ('.js-post[data-hid="' + hid + '"]');
  71.  
  72.          popupIsVisible = true;
  73.  
  74.          if (post) {
  75.             showPopup (target, hid, post.cloneNode (true));
  76.          }
  77.          else {
  78.             $.get(url)
  79.               .done(function (res) {
  80.                  showPopup (target, hid, res);
  81.               })
  82.               .fail(function (err) {
  83.                  console.error (err);
  84.               });
  85.          }
  86.  
  87.          return;
  88.       }
  89.    }
  90.  
  91.    ////////////////////////////////////////////////////////////////////////////
  92.    //  dispatchMouseOutEvent
  93.    function dispatchMouseOutEvent (ev) {
  94.       var target = ev.target;
  95.  
  96.       if (!target) {
  97.          return;
  98.       }
  99.  
  100.       if (target.classList.contains ('js-ref')) {
  101.          var popupList = $('.js-popup-container')[0];
  102.  
  103.          if (isBelongs (ev.relatedTarget, popupList.lastChild)) {
  104.             return;
  105.          }
  106.  
  107.          if (timerId) {
  108.             clearTimeout (timerId);
  109.          }
  110.  
  111.          timerId = setTimeout (function () {
  112.             removeLastPopup (ev.relatedTarget);
  113.          }, REMOVE_TIMEOUT);
  114.  
  115.          return;
  116.       }
  117.  
  118.       target = target.parentNode;
  119.  
  120.       if (target.classList.contains ('ref-popup')) {
  121.          if (isBelongs (ev.relatedTarget, target)) {
  122.             return;
  123.          }
  124.  
  125.          if (timerId) {
  126.             clearTimeout (timerId);
  127.          }
  128.  
  129.          timerId = setTimeout (function () {
  130.             removeLastPopup (ev.relatedTarget);
  131.          }, REMOVE_TIMEOUT);
  132.  
  133.          return;
  134.       }
  135.    }
  136.  
  137.    ////////////////////////////////////////////////////////////////////////////
  138.    //  showPopup
  139.    function showPopup (target, hid, content) {
  140.       if (popupIsVisible) {
  141.          var targetBox = target.getBoundingClientRect ();
  142.  
  143.          var popup = $('<div>')
  144.            .html (content)
  145.            .addClass ('ref-popup js-ref-popup')
  146.            .attr ('data-hid', hid)
  147.            .css ({
  148.               top: targetBox.top +
  149.                    targetBox.height +
  150.                    POPUP_VERTICAL_OFFSET +
  151.                    window.pageYOffset,
  152.               left: 0
  153.            });
  154.  
  155.          popup.find ('.js-toggle-thread, .js-toggle-post').remove ();
  156.  
  157.          $('.js-popup-container').append (popup);
  158.       }
  159.    }
  160.  
  161.    ////////////////////////////////////////////////////////////////////////////
  162.    //  removeLastPopup
  163.    function removeLastPopup (terminator) {
  164.       if (terminator) {
  165.          var popupList = $('.js-popup-container')[0];
  166.  
  167.          while (popupList.childElementCount > 0
  168.            && !isBelongs (terminator, popupList.lastChild))
  169.          {
  170.             popupList.removeChild (popupList.lastChild);
  171.          }
  172.       }
  173.    }
  174.  
  175.    ////////////////////////////////////////////////////////////////////////////
  176.    //  isBelongs
  177.    function isBelongs (obj, sample) {
  178.       var belongs = false;
  179.  
  180.       if (sample) {
  181.          var parent = obj;
  182.  
  183.          while (parent && !belongs) {
  184.             belongs = (parent == sample);
  185.             parent = parent.parentNode;
  186.          }
  187.       }
  188.  
  189.       return belongs;
  190.    }
  191.  
  192.    init ();
  193.  
  194.    return {
  195.       destroy: destroy,
  196.    };
  197. };
  198.  
  199. var refPopup = new RefPopup ();
Add Comment
Please, Sign In to add comment