|
|
|
@ -4,107 +4,121 @@ |
|
|
|
|
* @param {Array} resources |
|
|
|
|
* @constructor |
|
|
|
|
*/ |
|
|
|
|
$.frameReady = function (callback, targetSelector, resources) { |
|
|
|
|
/** |
|
|
|
|
* @type {window} |
|
|
|
|
*/ |
|
|
|
|
var targetWindow = document.querySelector(targetSelector); |
|
|
|
|
/** |
|
|
|
|
* @type {Document} |
|
|
|
|
*/ |
|
|
|
|
var targetDocument = null; |
|
|
|
|
|
|
|
|
|
var scripts = resources.filter(function (resource) { |
|
|
|
|
return resource.type === 'script'; |
|
|
|
|
}); |
|
|
|
|
var stylesheets = resources.filter(function (resource) { |
|
|
|
|
return resource.type === 'stylesheet'; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
var scriptsCount = (function () { |
|
|
|
|
var count = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(function (factory) { |
|
|
|
|
if (typeof define === 'function' && define.amd) { |
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
|
define(['jquery'], factory); |
|
|
|
|
} else if (typeof module === 'object' && typeof module.exports === 'object') { |
|
|
|
|
factory(require('jquery')); |
|
|
|
|
} else { |
|
|
|
|
// Browser globals
|
|
|
|
|
factory(jQuery); |
|
|
|
|
} |
|
|
|
|
}(function ($) { |
|
|
|
|
$.frameReady = function (callback, targetSelector, resources) { |
|
|
|
|
/** |
|
|
|
|
* @param {Object} parentScript |
|
|
|
|
* @type {window} |
|
|
|
|
*/ |
|
|
|
|
function countScripts(parentScript) { |
|
|
|
|
count++; |
|
|
|
|
|
|
|
|
|
if (!parentScript.hasOwnProperty('deps')) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
var targetWindow = document.querySelector(targetSelector); |
|
|
|
|
/** |
|
|
|
|
* @type {Document} |
|
|
|
|
*/ |
|
|
|
|
var targetDocument = null; |
|
|
|
|
|
|
|
|
|
parentScript.deps.forEach(countScripts); |
|
|
|
|
} |
|
|
|
|
var scripts = resources.filter(function (resource) { |
|
|
|
|
return resource.type === 'script'; |
|
|
|
|
}); |
|
|
|
|
var stylesheets = resources.filter(function (resource) { |
|
|
|
|
return resource.type === 'stylesheet'; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
scripts.forEach(countScripts); |
|
|
|
|
var scriptsCount = (function () { |
|
|
|
|
var count = 0; |
|
|
|
|
|
|
|
|
|
return count; |
|
|
|
|
})(); |
|
|
|
|
var scripsLoadedCount = 0; |
|
|
|
|
/** |
|
|
|
|
* @param {Object} parentScript |
|
|
|
|
*/ |
|
|
|
|
function countScripts(parentScript) { |
|
|
|
|
count++; |
|
|
|
|
|
|
|
|
|
targetWindow.onload = function () { |
|
|
|
|
scripsLoadedCount = 0; |
|
|
|
|
if (!parentScript.hasOwnProperty('deps')) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
targetDocument = targetWindow.contentDocument; |
|
|
|
|
parentScript.deps.forEach(countScripts); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
scripts.forEach(function (script) { |
|
|
|
|
createScript(script); |
|
|
|
|
}); |
|
|
|
|
scripts.forEach(countScripts); |
|
|
|
|
|
|
|
|
|
stylesheets.forEach(function (stylesheet) { |
|
|
|
|
createStylesheet(stylesheet); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
return count; |
|
|
|
|
})(); |
|
|
|
|
var scripsLoadedCount = 0; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param {Object} script |
|
|
|
|
*/ |
|
|
|
|
function createScript(script) { |
|
|
|
|
/** |
|
|
|
|
* @type {HTMLScriptElement} |
|
|
|
|
*/ |
|
|
|
|
var elParent = targetWindow.contentDocument.createElement('script'); |
|
|
|
|
elParent.async = false; |
|
|
|
|
elParent.onload = function () { |
|
|
|
|
scripsLoadedCount++; |
|
|
|
|
targetWindow.onload = function () { |
|
|
|
|
scripsLoadedCount = 0; |
|
|
|
|
|
|
|
|
|
if (!script.hasOwnProperty('deps')) { |
|
|
|
|
tryExecuteCallback(); |
|
|
|
|
targetDocument = targetWindow.contentDocument; |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
scripts.forEach(function (script) { |
|
|
|
|
createScript(script); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
script.deps.forEach(function (scriptB) { |
|
|
|
|
createScript(scriptB); |
|
|
|
|
stylesheets.forEach(function (stylesheet) { |
|
|
|
|
createStylesheet(stylesheet); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
elParent.setAttribute('src', script.src); |
|
|
|
|
|
|
|
|
|
targetDocument.body.appendChild(elParent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param {Object} stylesheet |
|
|
|
|
*/ |
|
|
|
|
function createStylesheet(stylesheet) { |
|
|
|
|
/** |
|
|
|
|
* @type {HTMLLinkElement} |
|
|
|
|
* @param {Object} script |
|
|
|
|
*/ |
|
|
|
|
var el = targetWindow.contentDocument.createElement('link'); |
|
|
|
|
el.setAttribute('href', stylesheet.src); |
|
|
|
|
el.setAttribute('rel', "stylesheet"); |
|
|
|
|
el.setAttribute('type', "text/css"); |
|
|
|
|
if (targetDocument.head) { |
|
|
|
|
targetDocument.head.appendChild(el); |
|
|
|
|
function createScript(script) { |
|
|
|
|
/** |
|
|
|
|
* @type {HTMLScriptElement} |
|
|
|
|
*/ |
|
|
|
|
var elParent = targetWindow.contentDocument.createElement('script'); |
|
|
|
|
elParent.async = false; |
|
|
|
|
elParent.onload = function () { |
|
|
|
|
scripsLoadedCount++; |
|
|
|
|
|
|
|
|
|
if (!script.hasOwnProperty('deps')) { |
|
|
|
|
tryExecuteCallback(); |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
script.deps.forEach(function (scriptB) { |
|
|
|
|
createScript(scriptB); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
elParent.setAttribute('src', script.src); |
|
|
|
|
|
|
|
|
|
targetDocument.body.appendChild(elParent); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function tryExecuteCallback() { |
|
|
|
|
if (scripsLoadedCount < scriptsCount) { |
|
|
|
|
return; |
|
|
|
|
/** |
|
|
|
|
* @param {Object} stylesheet |
|
|
|
|
*/ |
|
|
|
|
function createStylesheet(stylesheet) { |
|
|
|
|
/** |
|
|
|
|
* @type {HTMLLinkElement} |
|
|
|
|
*/ |
|
|
|
|
var el = targetWindow.contentDocument.createElement('link'); |
|
|
|
|
el.setAttribute('href', stylesheet.src); |
|
|
|
|
el.setAttribute('rel', "stylesheet"); |
|
|
|
|
el.setAttribute('type', "text/css"); |
|
|
|
|
if (targetDocument.head) { |
|
|
|
|
targetDocument.head.appendChild(el); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
targetWindow.contentWindow.eval('(' + callback.toString() + ')();'); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
function tryExecuteCallback() { |
|
|
|
|
if (scripsLoadedCount < scriptsCount) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
targetWindow.contentWindow.eval('(' + callback.toString() + ')();'); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
})); |