{"size": 4147, "ext": "js", "max_stars_repo_path": "src/script-loader.js", "max_stars_repo_name": "maksim-chekrishov/script-loader", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/script-loader.js", "max_issues_repo_name": "maksim-chekrishov/script-loader", "max_issues_repo_issues_event_min_datetime": "2020-12-28T20:09:58.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-28T21:43:36.000Z", "max_forks_repo_path": "src/script-loader.js", "max_forks_repo_name": "admdev8/script-loader", "max_forks_repo_forks_event_min_datetime": "2020-12-24T19:32:59.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-24T19:32:59.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 28.7986111111, "max_line_length": 101, "alphanum_fraction": 0.6281649385}
'use strict';
/**
 * Created by m.chekryshov on 25.08.15.
 */

  // Use this util to load third party script asynchronously
(function(window) {
  if (!window || window.ScriptLoader) {
    return;
  }

  var scriptsQueue = {};

  /**
   * Append script to the head section
   * When page already contains script with equivalent src it will be replaced without duplication
   *
   * @param {string} src - script src
   * @param {string} srcName - mnemonic name of script
   */
  function appendToHead(src, srcName) {
    var script = window.document.createElement('script');
    script.type = 'text/javascript';
    script.src = src;

    if (script.readyState) { // IE
      script.onreadystatechange = function() {
        if (script.readyState === 'loaded' ||
            script.readyState === 'complete') {
          script.onreadystatechange = null;
          setTimeout(function() {
            triggerCallbacks(srcName, 'success');
          }, 0);
        }
      };
    } else { // others
      script.onload = function() {
        triggerCallbacks(srcName, 'success');
      };
      script.onerror = function() {
        triggerCallbacks(srcName, 'error');
      };
    }
    addOrReloadScript(script);
  }

  function addOrReloadScript(script) {
    var scripts = document.getElementsByTagName('script');
    var scriptToReplace;

    // try remove script if it already loaded
    for (var i = 0; i < scripts.length; i++) {
      var currentScriptSrc = scripts[i].src;
      // check script src ending
      if (currentScriptSrc.indexOf(script.src, currentScriptSrc.length - script.src.length) !== -1) {
        scriptToReplace = scripts[i];
        break;
      }
    }

    if (scriptToReplace) {
      var scriptParentNode = scriptToReplace.parentNode;

      scriptParentNode.insertBefore(script, scriptToReplace);
      scriptParentNode.removeChild(scriptToReplace);
    } else {
      var firstScript = document.getElementsByTagName('script')[0];
      firstScript.parentNode.insertBefore(script, firstScript);
    }
  }

  function triggerCallbacks(srcName, callbackName) {
    if (isArray(scriptsQueue[srcName])) {
      for (var i = 0; i < scriptsQueue[srcName].length; i++) {
        var callback = scriptsQueue[srcName][i][callbackName];
        callback && callback();
      }
    }

    scriptsQueue[srcName] = callbackName;
  }

  function isArray(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  }

  function addCallbacks(srcName, successCallback, errorCallback) {
    if (!successCallback && !errorCallback) {
      return;
    }

    if (!isArray(scriptsQueue[srcName])) {
      scriptsQueue[srcName] = [];
    }

    scriptsQueue[srcName].push({
      success: successCallback,
      error: errorCallback
    });
  }

  window.ScriptLoader = {

    /**
     * Add or reload third party script asynchronously
     * When page already contains script with equivalent src it will be replaced without duplication
     *
     * @param {string} src - script src
     * @param {string} srcName - mnemonic name of script
     * @param {Function} [successCallback]
     * @param {Function} [errorCallback]
     */
    load: function(src, srcName, successCallback, errorCallback) {
      if (!src || !srcName) {
        return;
      }

      addCallbacks(srcName, successCallback, errorCallback);
      appendToHead(src, srcName);
    },

    /**
     *  Wait loading the specified script and call callback function on ready
     *
     * @param {string} srcName - mnemonic name of the script
     * @param {Function} successCallback
     * @param {Function} [errorCallback]
     */
    wait: function(srcName, successCallback, errorCallback) {
      if (!srcName || !successCallback) {
        return;
      }

      if (scriptsQueue[srcName] === 'success') { // script was successfully loaded
        successCallback();
      } else if (scriptsQueue[srcName] === 'error') { // an error occurred
        errorCallback && errorCallback();
      } else { // script loading in progress or was newer called
        addCallbacks(srcName, successCallback, errorCallback);
      }
    }
  };
})(window);
