MDL-66550 theme: Wrap JS calls in pendingJS

This commit is contained in:
Andrew Nicols
2019-09-12 20:58:10 +08:00
parent af0fd8128f
commit 484606eb08
16 changed files with 67 additions and 23 deletions
+3 -1
View File
@@ -57,8 +57,10 @@ class fragment_requirements_manager extends page_requirements_manager {
$output = '';
// First include must be to a module with no dependencies, this prevents multiple requests.
$prefix = "require(['core/first'], function() {\n";
$prefix = 'M.util.js_pending("core/first");';
$prefix .= "require(['core/first'], function() {\n";
$suffix = "\n});";
$suffix .= 'M.util.js_complete("core/first");';
$output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
return $output;
}
+12 -6
View File
@@ -1016,7 +1016,9 @@ class page_requirements_manager {
$component = clean_param($component, PARAM_COMPONENT);
$module = clean_param($module, PARAM_ALPHANUMEXT);
$modname = "{$component}/{$module}";
$functioncode = [];
if ($func !== null) {
$func = clean_param($func, PARAM_ALPHANUMEXT);
@@ -1035,12 +1037,14 @@ class page_requirements_manager {
}
}
$js = 'require(["' . $component . '/' . $module . '"], function(amd) { amd.' . $func . '(' . $strparams . '); });';
} else {
$js = 'require(["' . $component . '/' . $module . '"]);';
$functioncode[] = "amd.{$func}({$strparams});";
}
$functioncode[] = "M.util.js_complete('{$modname}');";
$initcode = implode(' ', $functioncode);
$js = "M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {{$initcode}});";
$this->js_amd_inline($js);
}
@@ -1362,8 +1366,10 @@ class page_requirements_manager {
}
// First include must be to a module with no dependencies, this prevents multiple requests.
$prefix = "require(['core/first'], function() {\n";
$suffix = "\n});";
$prefix = 'M.util.js_pending("core/first");';
$prefix .= "require(['core/first'], function() {\n";
$suffix = 'M.util.js_complete("core/first");';
$suffix .= "\n});";
$output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
return $output;
}
+8 -3
View File
@@ -125,8 +125,13 @@ class core_outputrequirementslib_testcase extends advanced_testcase {
$html = $page->requires->get_end_code();
$this->assertContains('require(["theme_foobar/lightbox"]);', $html);
$this->assertContains('require(["theme_foobar/demo_one"], function(amd) { amd.init(); });', $html);
$this->assertContains('require(["theme_foobar/demo_two"], function(amd) { amd.init("foo", "baz", [42,"xyz"]); });', $html);
$modname = 'theme_foobar/lightbox';
$this->assertContains("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {M.util.js_complete('{$modname}');});", $html);
$modname = 'theme_foobar/demo_one';
$this->assertContains("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {amd.init(); M.util.js_complete('{$modname}');});", $html);
$modname = 'theme_foobar/demo_two';
$this->assertContains("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {amd.init(\"foo\", \"baz\", [42,\"xyz\"]); M.util.js_complete('{$modname}');});", $html);
}
}
+1 -1
View File
@@ -1 +1 @@
define(["jquery","core/pending"],function(a,b){return{init:function(){var c=!1,d=function(){c=!0},e=function(){var a=c;return c=!1,a};a('[data-toggle="dropdown"]').keydown(function(b){var c,e=b.which||b.keyCode;38==e&&d(),27==e&&(c=a(b.target).attr("aria-expanded"),b.preventDefault(),"false"==c&&a(b.target).click()),32!=e&&13!=e||(b.preventDefault(),a(b.target).click())});var f=function(c){var d=function(b){a(this).focus(),b.resolve()}.bind(c);setTimeout(d,50,new b("core/aria:delayed-focus"))};a(".dropdown").on("shown.bs.dropdown",function(b){var c=a(b.target).find('[role="menu"]'),d=!1,g=!1;c&&(d=a(c).find('[role="menuitem"]')),d&&d.length>0&&(g=e()?d[d.length-1]:d[0]),g&&f(g)}),a('.dropdown [role="menu"] [role="menuitem"]').keypress(function(b){var c,d,e=String.fromCharCode(b.which||b.keyCode),g=a(b.target).closest('[role="menu"]'),h=0,i=!1;if(g&&(i=a(g).find('[role="menuitem"]')))for(e=e.toLowerCase(),h=0;h<i.length;h++)if(c=a(i[h]),d=c.text().trim().toLowerCase(),0==d.indexOf(e)){f(c);break}}),a('.dropdown [role="menu"] [role="menuitem"]').keydown(function(b){var c=b.which||b.keyCode,d=!1,e=a(b.target).closest('[role="menu"]'),g=0,h=!1;if(e&&(h=a(e).find('[role="menuitem"]'))){if(40==c){for(g=0;g<h.length-1;g++)if(h[g]==b.target){d=h[g+1];break}d||(d=h[0])}else if(38==c){for(g=1;g<h.length;g++)if(h[g]==b.target){d=h[g-1];break}d||(d=h[h.length-1])}else 36==c?d=h[0]:35==c&&(d=h[h.length-1]);d&&(b.preventDefault(),f(d))}}),a(".dropdown").on("hidden.bs.dropdown",function(b){var c=a(b.target).find('[data-toggle="dropdown"]');c&&f(c)}),a(function(){window.setTimeout(function(b){var c=a('[role="alert"][data-aria-autofocus="true"]');c.length>0&&(a(c[0]).attr("tabindex","0"),a(c[0]).focus()),b.resolve()},300,new b("core/aria:delayed-focus"))})}}});
define(["jquery","core/pending"],function(a,b){return{init:function(){var c=!1,d=function(){c=!0},e=function(){var a=c;return c=!1,a};a('[data-toggle="dropdown"]').keydown(function(b){var c,e=b.which||b.keyCode;38==e&&d(),27==e&&(c=a(b.target).attr("aria-expanded"),b.preventDefault(),"false"==c&&a(b.target).click()),32!=e&&13!=e||(b.preventDefault(),a(b.target).click())});var f=function(c){var d=function(b){a(this).focus(),b.resolve()}.bind(c);setTimeout(d,50,new b("core/aria:delayed-focus"))};a(".dropdown").on("shown.bs.dropdown",function(b){var c=a(b.target).find('[role="menu"]'),d=!1,g=!1;c&&(d=a(c).find('[role="menuitem"]')),d&&d.length>0&&(g=e()?d[d.length-1]:d[0]),g&&f(g)}),a('.dropdown [role="menu"] [role="menuitem"]').keypress(function(b){var c,d,e=String.fromCharCode(b.which||b.keyCode),g=a(b.target).closest('[role="menu"]'),h=0,i=!1;if(g&&(i=a(g).find('[role="menuitem"]')))for(e=e.toLowerCase(),h=0;h<i.length;h++)if(c=a(i[h]),d=c.text().trim().toLowerCase(),0==d.indexOf(e)){f(c);break}}),a('.dropdown [role="menu"] [role="menuitem"]').keydown(function(b){var c=b.which||b.keyCode,d=!1,e=a(b.target).closest('[role="menu"]'),g=0,h=!1;if(e&&(h=a(e).find('[role="menuitem"]'))){if(40==c){for(g=0;g<h.length-1;g++)if(h[g]==b.target){d=h[g+1];break}d||(d=h[0])}else if(38==c){for(g=1;g<h.length;g++)if(h[g]==b.target){d=h[g-1];break}d||(d=h[h.length-1])}else 36==c?d=h[0]:35==c&&(d=h[h.length-1]);d&&(b.preventDefault(),f(d))}}),a(".dropdown").on("hidden.bs.dropdown",function(b){var c=a(b.target).find('[data-toggle="dropdown"]');c&&f(c)});var g=new b("core/aria:delayed-focus");a(function(){window.setTimeout(function(b){var c=a('[role="alert"][data-aria-autofocus="true"]');c.length>0&&(a(c[0]).attr("tabindex","0"),a(c[0]).focus()),b.resolve()},300,g)})}}});
+1 -1
View File
@@ -1 +1 @@
define(["jquery","./tether","core/event"],function(a,b,c){return window.jQuery=a,window.Tether=b,require(["theme_boost/aria","theme_boost/pending","theme_boost/util","theme_boost/alert","theme_boost/button","theme_boost/carousel","theme_boost/collapse","theme_boost/dropdown","theme_boost/modal","theme_boost/scrollspy","theme_boost/tab","theme_boost/tooltip","theme_boost/popover"],function(b){a("body").popover({trigger:"focus",selector:"[data-toggle=popover][data-trigger!=hover]"}),a("html").popover({container:"body",selector:"[data-toggle=popover][data-trigger=hover]",trigger:"hover",delay:{hide:500}}),c.getLegacyEvents().done(function(b){a(document).on(b.FILTER_CONTENT_UPDATED,function(){a("body").popover({selector:'[data-toggle="popover"]',trigger:"focus"})})}),b.init()}),{}});
define(["jquery","./tether","core/event"],function(a,b,c){return window.jQuery=a,window.Tether=b,M.util.js_pending("theme_boost/loader:children"),require(["theme_boost/aria","theme_boost/pending","theme_boost/util","theme_boost/alert","theme_boost/button","theme_boost/carousel","theme_boost/collapse","theme_boost/dropdown","theme_boost/modal","theme_boost/scrollspy","theme_boost/tab","theme_boost/tooltip","theme_boost/popover"],function(b){a("body").popover({trigger:"focus",selector:"[data-toggle=popover][data-trigger!=hover]"}),a("html").popover({container:"body",selector:"[data-toggle=popover][data-trigger=hover]",trigger:"hover",delay:{hide:500}}),c.getLegacyEvents().done(function(b){a(document).on(b.FILTER_CONTENT_UPDATED,function(){a("body").popover({selector:'[data-toggle="popover"]',trigger:"focus"})})}),b.init(),M.util.js_complete("theme_boost/loader:children")}),{}});
+2 -1
View File
@@ -186,6 +186,7 @@ define(['jquery', 'core/pending'], function($, Pending) {
});
// After page load, focus on any element with special autofocus attribute.
var delayedFocusPromise = new Pending('core/aria:delayed-focus');
$(function() {
window.setTimeout(function(pendingPromise) {
var alerts = $('[role="alert"][data-aria-autofocus="true"]');
@@ -194,7 +195,7 @@ define(['jquery', 'core/pending'], function($, Pending) {
$(alerts[0]).focus();
}
pendingPromise.resolve();
}, 300, new Pending('core/aria:delayed-focus'));
}, 300, delayedFocusPromise);
});
}
};
+2
View File
@@ -27,6 +27,7 @@ define(['jquery', './tether', 'core/event'], function(jQuery, Tether, Event) {
window.jQuery = jQuery;
window.Tether = Tether;
M.util.js_pending('theme_boost/loader:children');
require(['theme_boost/aria',
'theme_boost/pending',
@@ -70,6 +71,7 @@ define(['jquery', './tether', 'core/event'], function(jQuery, Tether, Event) {
});
Aria.init();
M.util.js_complete('theme_boost/loader:children');
});
+4 -1
View File
@@ -64,5 +64,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}
+6 -1
View File
@@ -95,8 +95,13 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
M.util.js_pending('theme_boost/drawer');
require(['theme_boost/drawer'], function(mod) {
mod.init();
M.util.js_complete('theme_boost/drawer');
});
{{/js}}
+4 -1
View File
@@ -53,5 +53,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}
+4 -1
View File
@@ -67,5 +67,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}
+4 -1
View File
@@ -73,5 +73,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}
+4 -1
View File
@@ -87,5 +87,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}
+4 -2
View File
@@ -96,6 +96,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}
+4 -1
View File
@@ -68,5 +68,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}
+4 -1
View File
@@ -106,5 +106,8 @@
</body>
</html>
{{#js}}
require(['theme_boost/loader']);
M.util.js_pending('theme_boost/loader');
require(['theme_boost/loader'], function() {
M.util.js_complete('theme_boost/loader');
});
{{/js}}