Source: scripts/example/_delayed-content.js

/**
 * @file
 * Example delayed content
 */

/* global jQuery */
/* global UCASUtilities */

'use strict'

/**
 * @public Global UCASDesignFramework object.
 */
var UCASDesignFramework = UCASDesignFramework || {};
/**
 * @param {object} global - UCASDesignFramework object.
 * @param {object} $ - jQuery.
 * @param {object} context - context to limit the selectors.
 * @param {object} _u - UCASUtilities object.
 */
(function (global, $, context, _u) {
  // Create namespaced object for module
  global.exampleDelayedContent = {}

  /**
   * Initialise exampleDelayedContent plugin
   * @param {object} context
   */
  function init (context) {
    // @event loadExampleDelayedContent
    var event = context.createEvent('Event')
    event.initEvent('loadExampleDelayedContent', true, true)
    context.dispatchEvent(event)
  }

  /**
   * Add random content.
   * @param {object} parent - the element to add the content to.
   */
  function add (parent) {
    var width = getRandomInt(2, 5) * 100
    var height = getRandomInt(2, 5) * 100
    var src = '//placehold.it/' + width + 'x' + height
    setTimeout(function () {
      parent.append($('<img>', {class: 'example--delayed-content', src: src}))
    }, getRandomInt(1, 5) * 1000)
  }

  /**
   * Remove randomly-added content.
   * @param {object} parent - the element to remove the content from.
   */
  function remove (parent) {
    $('.example--delayed-content', parent).remove()
  }

  /**
   * Returns a random integer between min (inclusive) and max (inclusive)
   * Using Math.round() will give you a non-uniform distribution!
   */
  function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min
  }

  /**
   * Assign public methods.
   */
  global.exampleDelayedContent = {
    init: init,
    add: add,
    remove: remove
  }
})(UCASDesignFramework, jQuery, document, UCASUtilities)