Advertisement
Guest User

ref_popup.js v.0.2

a guest
Jan 15th, 2019
96
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 removalTimerID = 0;
  17.  
  18.    ////////////////////////////////////////////////////////////////////////////
  19.    //  init
  20.    function init () {
  21.       document.addEventListener ("mouseover", onMouseOver);
  22.       document.addEventListener ("mouseout", onMouseOut);
  23.    }
  24.  
  25.    ////////////////////////////////////////////////////////////////////////////
  26.    //  destroy
  27.    function destroy () {
  28.       document.removeEventListener ("mouseover", onMouseOver);
  29.       document.removeEventListener ("mouseout", onMouseOut);
  30.    }
  31.  
  32.    ////////////////////////////////////////////////////////////////////////////
  33.    //  onMouseOver
  34.    function onMouseOver (ev) {
  35.       if (ev.target && ev.target.parentNode) {
  36.          return dispatchMouseOverEvent (ev);
  37.       }
  38.       return false;
  39.    }
  40.  
  41.    ////////////////////////////////////////////////////////////////////////////
  42.    //  onMouseOut
  43.    function onMouseOut (ev) {
  44.       if (ev.target && ev.target.parentNode) {
  45.          return dispatchMouseOutEvent (ev);
  46.       }
  47.       return false;
  48.    }
  49.  
  50.    ////////////////////////////////////////////////////////////////////////////
  51.    //  dispatchMouseOverEvent
  52.    function dispatchMouseOverEvent (ev) {
  53.       if (ev.target.classList.contains ('js-ref')) {
  54.          var hid = ev.target.innerHTML.replace ('>>', '');
  55.          var url = ev.target.getAttribute ('href').replace ('#', '');
  56.          var post = document.querySelector
  57.            ('.js-post[data-hid="' + hid + '"]');
  58.  
  59.          if (post) {
  60.             showPopup (ev.target, hid, post.cloneNode (true));
  61.          }
  62.          else {
  63.             $.get(url)
  64.               .done(function (res) {
  65.                  showPopup (ev.target, hid, res);
  66.               })
  67.               .fail(function (err) {
  68.                  console.error (err);
  69.               });
  70.          }
  71.          return true;
  72.       }
  73.  
  74.       var oldPopup = getPopup (ev.relatedTarget);
  75.       var newPopup = getPopup (ev.target);
  76.  
  77.       if (oldPopup && removalTimerID) {
  78.          setRemovalTimer (newPopup);
  79.  
  80.          return true;
  81.       }
  82.  
  83.       return false;
  84.    }
  85.  
  86.    ////////////////////////////////////////////////////////////////////////////
  87.    //  dispatchMouseOutEvent
  88.    function dispatchMouseOutEvent (ev) {
  89.       var oldPopup = getPopup (ev.target);
  90.       var newPopup = getPopup (ev.relatedTarget);
  91.  
  92.       if (ev.target.classList.contains ('js-ref')) {
  93.          setRemovalTimer (newPopup);
  94.  
  95.          return true;
  96.       }
  97.  
  98.       if (newPopup == oldPopup) {
  99.          return false;
  100.       }
  101.  
  102.       if (oldPopup) {
  103.          setRemovalTimer (newPopup);
  104.          return true;
  105.       }
  106.  
  107.       return false;
  108.    }
  109.  
  110.    ////////////////////////////////////////////////////////////////////////////
  111.    //  showPopup
  112.    function showPopup (target, hid, content) {
  113.       var targetBox = target.getBoundingClientRect ();
  114.  
  115.       var popup = $('<div>')
  116.         .html (content)
  117.         .addClass ('ref-popup js-ref-popup')
  118.         .attr ('data-hid', hid)
  119.         .css ({
  120.            top: targetBox.top +
  121.                 targetBox.height +
  122.                 POPUP_VERTICAL_OFFSET +
  123.                 window.pageYOffset,
  124.            left: 0
  125.         });
  126.  
  127.       popup.find ('.js-toggle-thread, .js-toggle-post').remove ();
  128.  
  129.       $('.js-popup-container').append (popup);
  130.    }
  131.  
  132.    ////////////////////////////////////////////////////////////////////////////
  133.    //  removeLastPopup
  134.    function removeLastPopup (terminator) {
  135.       var popupList = $('.js-popup-container')[0];
  136.       var termPopup = getPopup (terminator);
  137.  
  138.       while (popupList.hasChildNodes ()
  139.         && termPopup != popupList.lastChild)
  140.       {
  141.          popupList.removeChild (popupList.lastChild);
  142.       }
  143.    }
  144.  
  145.    ////////////////////////////////////////////////////////////////////////////
  146.    //  getPopup
  147.    function getPopup (elem) {
  148.       var popupList = $('.js-popup-container')[0];
  149.  
  150.       if (popupList && elem && popupList.hasChildNodes ()) {
  151.          var iter = elem;
  152.  
  153.          while (iter.parentNode) {
  154.             if (iter.parentNode == popupList) {
  155.                return iter;
  156.             }
  157.             iter = iter.parentNode;
  158.          }
  159.       }
  160.       return null;
  161.    }
  162.  
  163.    ////////////////////////////////////////////////////////////////////////////
  164.    //  isBelongs
  165.    function isBelongs (obj, sample) {
  166.       if (sample) {
  167.          var iter = obj;
  168.  
  169.          while (iter) {
  170.             if (iter == sample) {
  171.                return true;
  172.             }
  173.             iter = iter.parentNode;
  174.          }
  175.       }
  176.       return false;
  177.    }
  178.  
  179.    ////////////////////////////////////////////////////////////////////////////
  180.    //  setRemovalTimer
  181.    function setRemovalTimer (terminator) {
  182.       clearRemovalTimer ();
  183.  
  184.       removalTimerID = setTimeout (function () {
  185.          removeLastPopup (terminator);
  186.          removalTimerID = 0;
  187.       }, REMOVE_TIMEOUT);
  188.    }
  189.  
  190.    ////////////////////////////////////////////////////////////////////////////
  191.    //  clearRemovalTimer
  192.    function clearRemovalTimer () {
  193.       if (removalTimerID) {
  194.          clearTimeout (removalTimerID);
  195.       }
  196.       removalTimerID = 0;
  197.    }
  198.  
  199.    init ();
  200.  
  201.    return {
  202.       destroy: destroy,
  203.    };
  204. };
  205.  
  206. var refPopup = new RefPopup ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement