Commit e588fb8f by polesye

TNL-272: Fix double loading of cms-modules.js.

parent 0130a837
......@@ -19,19 +19,28 @@ define(["jquery", "underscore", "mathjax", "codemirror", "tinymce",
'define': define
};
var urls = ${urls};
var head = $("head");
/**
* Loads all modules one-by-one in exact order.
* The module should be used until we'll use RequireJS for XModules.
* @param {Array} modules A list of urls.
* @return {jQuery Promise}
**/
var requireQueue = function(modules) {
var deferred = $.Deferred();
var numResources = urls.length;
$.each(urls, function (i, url) {
head.append($("<script/>", {src: url}));
// Wait for all the scripts to execute.
require([url], function () {
if (i === numResources - 1) {
var loadScript = function (queue) {
// Loads the next script if queue is not empty.
if (queue.length) {
require([queue.shift()], function() {
loadScript(queue);
});
} else {
deferred.resolve();
}
});
});
};
loadScript(modules.concat());
return deferred.promise();
};
return requireQueue(${urls});
});
......@@ -57,7 +57,13 @@ REQUIREJS_WAIT = {
re.compile(r'^\s*Files & Uploads'): [
'js/base', 'jquery.ui', 'coffee/src/main', 'underscore',
'js/views/assets', 'js/views/asset'
]
],
# Pages
re.compile('^Pages \|'): [
'js/models/explicit_url', 'coffee/src/views/tabs',
'xmodule', 'coffee/src/main', 'xblock/cms.runtime.v1'
],
}
......@@ -147,24 +153,7 @@ class RequireJSError(Exception):
pass
@world.absorb
def wait_for_requirejs(dependencies=None):
"""
If requirejs is loaded on the page, this function will pause
Selenium until require is finished loading the given dependencies.
If requirejs is not loaded on the page, this function will return
immediately.
:param dependencies: a list of strings that identify resources that
we should wait for requirejs to load. By default, requirejs will only
wait for jquery.
"""
if not dependencies:
dependencies = ["jquery"]
# stick jquery at the front
if dependencies[0] != "jquery":
dependencies.insert(0, "jquery")
def load_requrejs_modules(dependencies, callback="callback(true);"):
javascript = """
var callback = arguments[arguments.length - 1];
if(window.require) {{
......@@ -175,16 +164,17 @@ def wait_for_requirejs(dependencies=None):
addEventListener("beforeunload", unloadHandler);
addEventListener("unload", unloadHandler);
require({deps}, function($) {{
var modules = arguments;
setTimeout(function() {{
removeEventListener("beforeunload", unloadHandler);
removeEventListener("unload", unloadHandler);
callback(true);
{callback}
}}, 50);
}});
}} else {{
callback(false);
}}
""".format(deps=json.dumps(dependencies))
""".format(deps=json.dumps(dependencies), callback=callback)
for _ in range(5): # 5 attempts max
try:
result = world.browser.driver.execute_async_script(dedent(javascript))
......@@ -217,6 +207,46 @@ def wait_for_requirejs(dependencies=None):
return result
def wait_for_xmodules_to_load():
"""
If requirejs is loaded on the page, this function will pause
Selenium until require is finished loading all xmodules.
If requirejs is not loaded on the page, this function will return
immediately.
"""
callback = """
if (modules[0] && modules[0].done) {{
modules[0].done(function () {{callback(true)}});
}}
"""
return load_requrejs_modules(["xmodule"], callback)
@world.absorb
def wait_for_requirejs(dependencies=None):
"""
If requirejs is loaded on the page, this function will pause
Selenium until require is finished loading the given dependencies.
If requirejs is not loaded on the page, this function will return
immediately.
:param dependencies: a list of strings that identify resources that
we should wait for requirejs to load. By default, requirejs will only
wait for jquery.
"""
if not dependencies:
dependencies = ["jquery"]
# stick jquery at the front
if dependencies[0] != "jquery":
dependencies.insert(0, "jquery")
result = load_requrejs_modules(dependencies)
if result and "xmodule" in dependencies:
result = wait_for_xmodules_to_load()
return result
@world.absorb
def wait_for_ajax_complete():
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment