utils.js

From disqus.com, 7 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to tracking.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/586dd05f Embed
Download Paste or View Raw
  1. /** @module stance/utils */
  2.  
  3. define('stance/utils',[
  4.     'exports',
  5.  
  6.     'core/utils/lang/isElement',
  7.     'core/utils/uniqueId',
  8. ], function (
  9.     exports,
  10.  
  11.     isElement,
  12.     uniqueId
  13. ) {
  14.     'use strict';
  15.  
  16.     /**
  17.      * Extracts element from container if applicable.
  18.      *
  19.      * @param {Element-like} obj
  20.      * @returns {?HTMLElement}
  21.      */
  22.     exports.getElement = function (obj) {
  23.         if (isElement(obj))
  24.             return obj;
  25.  
  26.         return obj && obj.el;
  27.     };
  28.  
  29.     exports.EL_ID_ATTR = 'data-visibility-id';
  30.     exports.OBJ_ID_PROP = '_visibility_id';
  31.  
  32.     /**
  33.      * Get or create an id for the element or element container, or return null.
  34.      *
  35.      * @param {Element-like} obj
  36.      * @returns {?number} Id of the associated element / view
  37.      */
  38.     exports.getId = function (obj) {
  39.         // Gets the id (possibly creating it)
  40.         var id = null;
  41.  
  42.         // TODO: Refactor
  43.         if (isElement(obj)) {
  44.             // Store the id as an element attribute
  45.             id = obj.getAttribute(exports.EL_ID_ATTR) || null;
  46.  
  47.             if (!id) {
  48.                 id = uniqueId();
  49.                 obj.setAttribute(exports.EL_ID_ATTR, id);
  50.             }
  51.         } else if (obj) {
  52.             // Store it as a property
  53.             id = obj[exports.OBJ_ID_PROP] || null;
  54.  
  55.             if (!id)
  56.                 id = obj[exports.OBJ_ID_PROP] = uniqueId();
  57.         }
  58.         return id;
  59.     };
  60.  
  61.     /**
  62.      * Calculate the the percentage of an element visible in
  63.      * the viewport given the scroll position and the element's offset.
  64.      *
  65.      * @param {ViewportPos} pos - Scroll information.
  66.      * @param {FullOffset} offset - Element's full offset information.
  67.      * @returns {Integer} Percent between 0 and 100
  68.      */
  69.     exports.visiblePercent = function (pos, offset) {
  70.         var percent = 0;
  71.  
  72.         if (!offset)
  73.             return percent;
  74.  
  75.         var screenTop = pos.top;
  76.         var screenBottom = screenTop + pos.height;
  77.  
  78.         var bleedsTop = offset.visibleTop < screenTop;
  79.         var bleedsBottom = offset.visibleBottom > screenBottom;
  80.  
  81.         // If the view bleeds over the top and bottom of the viewport then it
  82.         // is filling the entire viewport, so report 100%. Otherwise, if it
  83.         // bleeds over neither, then it is completely in view, so also report 100%.
  84.         if ((!bleedsTop && !bleedsBottom) || (bleedsTop && bleedsBottom))
  85.             percent = 1;
  86.         // If the view is bleeding off the top, the calculate how much of the bottom of the view is visible.
  87.         else if (bleedsTop)
  88.             percent = (offset.height - (screenTop - offset.visibleTop)) / offset.height;
  89.         // Otherwise, if bleeding off the bottom, then calculate how much of the top of the view is visible.
  90.         else if (bleedsBottom)
  91.             percent = (screenBottom - offset.visibleTop) / offset.height;
  92.  
  93.         return Math.round(percent * 100);
  94.     };
  95.  
  96. });
  97.  
  98. // https://c.disquscdn.com/next/next-core/core/stance/utils.js
  99.   // https://a.disquscdn.com/next/next-core/core/stance/utils.js

Replies to utils.js rss

Title Name Language When
indexOf.js disqus.com javascript 7 Months ago.

Reply to "utils.js"

Here you can reply to the paste above