Advertisement
Guest User

ref_popup.js v.0.3 final

a guest
Jan 15th, 2019
87
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. ///////////////////////////////////////////////////////////////////////////////
  13. //  FixedTimer
  14. var FixedTimer = function (timeout) {
  15.  
  16.    var TIMER_TIMEOUT = timeout;
  17.    var CALLBACK = null;
  18.    var ARGUMENT = null;
  19.    var TIMER_ID = 0;
  20.  
  21.    ////////////////////////////////////////////////////////////////////////////
  22.    //  _callback
  23.    function _callback () {
  24.       CALLBACK (ARGUMENT);
  25.       _reset ();
  26.    };
  27.  
  28.    ////////////////////////////////////////////////////////////////////////////
  29.    //  _reset
  30.    function _reset () {
  31.       CALLBACK = null;
  32.       ARGUMENT = null;
  33.       TIMER_ID = 0;
  34.    };
  35.  
  36.    ////////////////////////////////////////////////////////////////////////////
  37.    //  _clear
  38.    function _clear () {
  39.       if (TIMER_ID) {
  40.          clearTimeout (TIMER_ID);
  41.       }
  42.       _reset ();
  43.    };
  44.  
  45.    ////////////////////////////////////////////////////////////////////////////
  46.    //  _isActive
  47.    function _isActive () {
  48.       if (TIMER_ID) {
  49.          return true;
  50.       }
  51.       return false;
  52.    };
  53.  
  54.    ////////////////////////////////////////////////////////////////////////////
  55.    //  _set
  56.    function _set (callback, argument) {
  57.       _clear ();
  58.  
  59.       CALLBACK = callback;
  60.       ARGUMENT = argument;
  61.       TIMER_ID = setTimeout (_callback, TIMER_TIMEOUT);
  62.    };
  63.  
  64.    ////////////////////////////////////////////////////////////////////////////
  65.    //  _setCallback
  66.    function _setCallback (callback) {
  67.       CALLBACK = callback;
  68.    };
  69.  
  70.    ////////////////////////////////////////////////////////////////////////////
  71.    //  _setArgument
  72.    function _setArgument (argument) {
  73.       ARGUMENT = argument;
  74.    };
  75.  
  76.    ////////////////////////////////////////////////////////////////////////////
  77.    //  _fire
  78.    function _fire () {
  79.       if (TIMER_ID) {
  80.          clearTimeout (TIMER_ID);
  81.          _callback ();
  82.       }
  83.       else {
  84.          console.log ("FixedTimer.fire () on inactive timer!");
  85.       }
  86.    };
  87.  
  88.    ////////////////////////////////////////////////////////////////////////////
  89.    //  bless
  90.    return {
  91.       isActive    : _isActive,
  92.       set         : _set,
  93.       setCallback : _setCallback,
  94.       setArgument : _setArgument,
  95.       clear       : _clear,
  96.       fire        : _fire
  97.    };
  98. };
  99.  
  100. var RefPopup = function (props) {
  101.    var POPUP_VERTICAL_OFFSET = 5;
  102.  
  103.    var timer = new FixedTimer (250);
  104.  
  105.    ////////////////////////////////////////////////////////////////////////////
  106.    //  init
  107.    function init () {
  108.       document.addEventListener ("mouseover", onMouseOver);
  109.       document.addEventListener ("mouseout", onMouseOut);
  110.    }
  111.  
  112.    ////////////////////////////////////////////////////////////////////////////
  113.    //  destroy
  114.    function destroy () {
  115.       document.removeEventListener ("mouseover", onMouseOver);
  116.       document.removeEventListener ("mouseout", onMouseOut);
  117.    }
  118.  
  119.    ////////////////////////////////////////////////////////////////////////////
  120.    //  onMouseOver
  121.    function onMouseOver (ev) {
  122.       if (ev.target && ev.target.parentNode) {
  123.          return dispatchMouseOverEvent (ev);
  124.       }
  125.       return false;
  126.    }
  127.  
  128.    ////////////////////////////////////////////////////////////////////////////
  129.    //  onMouseOut
  130.    function onMouseOut (ev) {
  131.       if (ev.target && ev.target.parentNode) {
  132.          return dispatchMouseOutEvent (ev);
  133.       }
  134.       return false;
  135.    }
  136.  
  137.    ////////////////////////////////////////////////////////////////////////////
  138.    //  dispatchMouseOverEvent
  139.    function dispatchMouseOverEvent (ev) {
  140.       if (ev.target.classList.contains ('js-ref')) {
  141.          if (timer.isActive ()) {
  142.             timer.fire ();
  143.          }
  144.  
  145.          var hid = ev.target.innerHTML.replace ('>>', '');
  146.          var url = ev.target.getAttribute ('href').replace ('#', '');
  147.          var post = document.querySelector
  148.            ('.js-post[data-hid="' + hid + '"]');
  149.  
  150.          if (post) {
  151.             showPopup (ev.target, hid, post.cloneNode (true));
  152.          }
  153.          else {
  154.             $.get(url)
  155.               .done(function (res) {
  156.                  showPopup (ev.target, hid, res);
  157.               })
  158.               .fail(function (err) {
  159.                  console.error (err);
  160.               });
  161.          }
  162.          return true;
  163.       }
  164.  
  165.       if (timer.isActive ()) {
  166.          timer.setArgument (getPopup (ev.target));
  167.          return true;
  168.       }
  169.  
  170.       return false;
  171.    }
  172.  
  173.    ////////////////////////////////////////////////////////////////////////////
  174.    //  dispatchMouseOutEvent
  175.    function dispatchMouseOutEvent (ev) {
  176.       var oldPopup = getPopup (ev.target);
  177.       var newPopup = getPopup (ev.relatedTarget);
  178.  
  179.       if (ev.target.classList.contains ('js-ref')) {
  180.          if (!timer.isActive ()) {
  181.             timer.set (removeLastPopup, newPopup);
  182.          }
  183.          return true;
  184.       }
  185.  
  186.       if (newPopup == oldPopup) {
  187.          return false;
  188.       }
  189.  
  190.       if (oldPopup) {
  191.          if (!timer.isActive ()) {
  192.             timer.set (removeLastPopup, newPopup);
  193.          }
  194.          return true;
  195.       }
  196.  
  197.       return false;
  198.    }
  199.  
  200.    ////////////////////////////////////////////////////////////////////////////
  201.    //  showPopup
  202.    function showPopup (target, hid, content) {
  203.       var targetBox = target.getBoundingClientRect ();
  204.  
  205.       var popup = $('<div>')
  206.         .html (content)
  207.         .addClass ('ref-popup js-ref-popup')
  208.         .attr ('data-hid', hid)
  209.         .css ({
  210.            top: targetBox.top +
  211.                 targetBox.height +
  212.                 POPUP_VERTICAL_OFFSET +
  213.                 window.pageYOffset,
  214.            left: 0
  215.         });
  216.  
  217.       popup.find ('.js-toggle-thread, .js-toggle-post').remove ();
  218.  
  219.       $('.js-popup-container').append (popup);
  220.    }
  221.  
  222.    ////////////////////////////////////////////////////////////////////////////
  223.    //  removeLastPopup
  224.    function removeLastPopup (terminator) {
  225.       var popupList = $('.js-popup-container')[0];
  226.       var termPopup = getPopup (terminator);
  227.  
  228.       while (popupList.hasChildNodes ()
  229.         && termPopup != popupList.lastChild)
  230.       {
  231.          popupList.removeChild (popupList.lastChild);
  232.       }
  233.    }
  234.  
  235.    ////////////////////////////////////////////////////////////////////////////
  236.    //  getPopup
  237.    function getPopup (elem) {
  238.       var popupList = $('.js-popup-container')[0];
  239.  
  240.       if (popupList && elem && popupList.hasChildNodes ()) {
  241.          var iter = elem;
  242.  
  243.          while (iter.parentNode) {
  244.             if (iter.parentNode == popupList) {
  245.                return iter;
  246.             }
  247.             iter = iter.parentNode;
  248.          }
  249.       }
  250.       return null;
  251.    }
  252.  
  253.    ////////////////////////////////////////////////////////////////////////////
  254.    //  isBelongs
  255.    function isBelongs (obj, sample) {
  256.       if (sample) {
  257.          var iter = obj;
  258.  
  259.          while (iter) {
  260.             if (iter == sample) {
  261.                return true;
  262.             }
  263.             iter = iter.parentNode;
  264.          }
  265.       }
  266.       return false;
  267.    }
  268.  
  269.    init ();
  270.  
  271.    return {
  272.       destroy: destroy,
  273.    };
  274. };
  275.  
  276. var refPopup = new RefPopup ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement