Re: Re: domUtils.js

From disqus.com, 2 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to Re: Re: ads.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/a570661a Embed
Download Paste or View Raw
  1. // Adapted from https://github.com/DeviantArt/Safeframe/blob/master/src/lib/noderect.js
  2. define('core/ads/domUtils',[
  3.     'core/ads/NodeRect',
  4. ], function (
  5.     NodeRect
  6. ) {
  7.     'use strict';
  8.  
  9.     const ROOT_DOCUMENT_NODE_TYPE = 9;
  10.  
  11.     const getParentNode = node => node && node.parentNode;
  12.  
  13.     const getNodeStyle = (node, attr) => {
  14.         if (!node)
  15.             return null;
  16.  
  17.         const style = window.document.defaultView.getComputedStyle(node);
  18.         if (attr && style.hasOwnProperty(attr))
  19.             return style[attr];
  20.  
  21.         return style;
  22.     };
  23.  
  24.     const getScroll = () => ({
  25.         'x': window.pageXOffset,
  26.         'y': window.pageYOffset,
  27.     });
  28.  
  29.     const getRect = node => {
  30.         let rect;
  31.         if (node && node.style) {
  32.             // If the node has display:none then getBoundingClientRect will return 0,0,0,0
  33.             const oldDisplay = node.style.display;
  34.             node.style.display = 'block'; // must be visible
  35.             rect = node.getBoundingClientRect();
  36.             node.style.display = oldDisplay;
  37.             rect = new NodeRect(rect);
  38.             const scroll = getScroll();
  39.  
  40.             // Convert it to absolute position
  41.             rect.left += scroll.x;
  42.             rect.right += scroll.x;
  43.             rect.top += scroll.y;
  44.             rect.bottom += scroll.y;
  45.         } else {
  46.             rect = new NodeRect(0);
  47.         }
  48.         return rect;
  49.     };
  50.  
  51.     const getRectRelativeTo = (node, ref) => {
  52.         const rect = getRect(node);
  53.  
  54.         // ref is usually node.offsetParent, which is null if the node is display:none
  55.         if (ref) {
  56.             const refRect = getRect(ref);
  57.             rect.top = refRect.top - refRect.top + ref.scrollTop;
  58.             rect.bottom = rect.top + rect.height + ref.scrollTop;
  59.             rect.left = rect.left - refRect.left + ref.scrollLeft;
  60.             rect.right = rect.left + rect.width + ref.scrollLeft;
  61.         }
  62.         return rect;
  63.     };
  64.  
  65.     const getViewportRect = () => {
  66.         const scroll = getScroll();
  67.         const top = scroll.y;
  68.         const right = scroll.x + window.innerWidth;
  69.         const bottom = scroll.y + window.innerHeight;
  70.         const left = scroll.x;
  71.         const width = window.innerWidth;
  72.         const height = window.innerHeight;
  73.  
  74.         return new NodeRect(top, right, bottom, left, width, height);
  75.     };
  76.  
  77.     const getDocument = node => {
  78.         try {
  79.             if (node.nodeType === ROOT_DOCUMENT_NODE_TYPE)
  80.                 return node;
  81.             return node.ownerDocument;
  82.         } catch (err) {
  83.             return null;
  84.         }
  85.     };
  86.  
  87.     const getWindow = node => {
  88.         const doc = getDocument(node);
  89.         let win;
  90.         try {
  91.             if (doc)
  92.                 win = doc.parentWindow || doc.defaultView || window;
  93.         } catch (err) {
  94.             win = window;
  95.         }
  96.  
  97.         return win;
  98.     };
  99.  
  100.     const getWindowRect = node => {
  101.         const win = getWindow(node);
  102.         const rect = new NodeRect(0, win.innerWidth, win.innerHeight, 0, win.innerWidth, win.innerHeight);
  103.         // Convert it to absolute position
  104.         const scroll = getScroll();
  105.         rect.left += scroll.x;
  106.         rect.right += scroll.x;
  107.         rect.top += scroll.y;
  108.         rect.bottom += scroll.y;
  109.         return rect;
  110.     };
  111.  
  112.     const getRootNode = node => {
  113.         const doc = getDocument(node);
  114.         if (doc)
  115.             return doc.documentElement || doc.body; // body for opera
  116.     };
  117.  
  118.     const getRootRect = node => {
  119.         const root = getRootNode(node) || {};
  120.         const rect = new NodeRect();
  121.         rect.right =
  122.         rect.width = root.scrollWidth || 0;
  123.         rect.bottom =
  124.         rect.height = root.scrollHeight || 0;
  125.         return rect;
  126.     };
  127.  
  128.     const isParentOf = (parent, node) => {
  129.         while (node) {
  130.             if (node === parent)
  131.                 return true;
  132.             node = node.parentNode;
  133.         }
  134.         return false;
  135.     };
  136.  
  137.     const hasLayout = node => {
  138.         const style = getNodeStyle(node);
  139.         return style.display === 'inline-block'
  140.             || style.float !== 'none'
  141.             || style.position === 'absolute'
  142.             || style.position === 'fixed'
  143.             || style.width !== 'auto'
  144.             || style.height !== 'auto';
  145.     };
  146.  
  147.     const getNodeOverflow = node => {
  148.         const style = getNodeStyle(node);
  149.         const overflow = {};
  150.  
  151.         // The scrollbar size is calculated as a difference between
  152.         // the offsetWidth and clientWidth.
  153.         //
  154.         // The HTMLElement.offsetWidth read-only property returns the
  155.         // layout width of an element. Typically, an element's
  156.         // offsetWidth is a measurement which includes the element
  157.         // borders, the element horizontal padding, the element
  158.         // vertical scrollbar (if present, if rendered) and the element
  159.         // CSS width.
  160.         //
  161.         // The Element.clientWidth property is zero for elements with
  162.         // no CSS or inline layout boxes, otherwise it's the inner
  163.         // width of an element in pixels. It includes padding but not
  164.         // the vertical scrollbar (if present, if rendered), border or
  165.         // margin.
  166.         //
  167.         if (style.overflowX === 'scroll' || style.overflowX === 'auto')
  168.             overflow.xscroll = node.offsetWidth - node.clientWidth;
  169.         else
  170.             overflow.xscroll = 0;
  171.  
  172.         if (style.overflowY === 'scroll' || style.overflowY === 'auto')
  173.             overflow.yscroll = node.offsetHeight - node.clientHeight;
  174.         else
  175.             overflow.yscroll = 0;
  176.  
  177.         overflow.xhidden = style.overflowX === 'hidden';
  178.         overflow.yhidden = style.overflowY === 'hidden';
  179.         return overflow;
  180.     };
  181.  
  182.     const isNodeClipped = node => {
  183.         const style = getNodeStyle(node);
  184.         if ((style.clip && style.clip !== 'auto') ||
  185.             (style.clipPath && style.clipPath !== 'none'))
  186.             return true;
  187.  
  188.         return false;
  189.     };
  190.  
  191.     return {
  192.         getParentNode,
  193.         getWindow,
  194.         getScroll,
  195.         getRect,
  196.         getRectRelativeTo,
  197.         getViewportRect,
  198.         getDocument,
  199.         getWindowRect,
  200.         getRootNode,
  201.         getRootRect,
  202.         getNodeStyle,
  203.         isParentOf,
  204.         hasLayout,
  205.         getNodeOverflow,
  206.         isNodeClipped,
  207.     };
  208.  
  209. });
  210.  
  211. // https://c.disquscdn.com/next/next-core/core/ads/domUtils.js

Replies to Re: Re: domUtils.js rss

Title Name Language When
Re: Re: Geom.js disqus.com javascript 2 Months ago.

Reply to "Re: Re: domUtils.js"

Here you can reply to the paste above