diff --git a/core/.eslintrc.json b/core/.eslintrc.json index 218d84f937..81ed0cccb5 100644 --- a/core/.eslintrc.json +++ b/core/.eslintrc.json @@ -12,6 +12,7 @@ "domready": true, "jQuery": true, "_": true, + "loadjs": true, "matchMedia": true, "Backbone": true, "Modernizr": true, diff --git a/core/core.libraries.yml b/core/core.libraries.yml index 5da51e8ae5..eaa50970bb 100644 --- a/core/core.libraries.yml +++ b/core/core.libraries.yml @@ -95,6 +95,7 @@ drupal.ajax: - core/drupalSettings - core/drupal.progress - core/jquery.once + - core/loadjs drupal.announce: version: VERSION @@ -803,6 +804,16 @@ jquery.ui.widget: dependencies: - core/jquery.ui +loadjs: + remote: https://github.com/muicss/loadjs + version: 3.5.0 + license: + name: MIT + url: https://github.com/muicss/loadjs/blob/master/LICENSE.txt + gpl-compatible: true + js: + assets/vendor/loadjs/loadjs.min.js: { minified: true } + matchmedia: remote: https://github.com/paulirish/matchMedia.js version: &matchmedia_version 0.2.0 diff --git a/core/lib/Drupal/Core/Ajax/AddJsCommand.php b/core/lib/Drupal/Core/Ajax/AddJsCommand.php new file mode 100644 index 0000000000..f7c0a8abf3 --- /dev/null +++ b/core/lib/Drupal/Core/Ajax/AddJsCommand.php @@ -0,0 +1,44 @@ +styles = $scripts; + } + + /** + * {@inheritdoc} + */ + public function render() { + + return [ + 'command' => 'add_js', + 'data' => $this->styles, + ]; + } + +} diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php index ee5208b078..0418527e71 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php +++ b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php @@ -174,11 +174,13 @@ protected function buildAttachmentsCommands(AjaxResponse $response, Request $req } if ($js_assets_header) { $js_header_render_array = $this->jsCollectionRenderer->render($js_assets_header); - $resource_commands[] = new PrependCommand('head', $this->renderer->renderPlain($js_header_render_array)); + $scripts_attributes = array_map(function ($render_array) { return $render_array['#attributes']; }, $js_header_render_array); + $resource_commands[] = new AddJsCommand($scripts_attributes); } if ($js_assets_footer) { $js_footer_render_array = $this->jsCollectionRenderer->render($js_assets_footer); - $resource_commands[] = new AppendCommand('body', $this->renderer->renderPlain($js_footer_render_array)); + $scripts_attributes = array_map(function ($render_array) { return $render_array['#attributes']; }, $js_footer_render_array); + $resource_commands[] = new AddJsCommand($scripts_attributes); } foreach (array_reverse($resource_commands) as $resource_command) { $response->addCommand($resource_command, TRUE); diff --git a/core/misc/ajax.js b/core/misc/ajax.js index fefe9f3031..0c2c233ebb 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -11,7 +11,7 @@ * included to provide Ajax capabilities. */ -(function ($, window, Drupal, drupalSettings) { +(function ($, window, Drupal, drupalSettings, loadjs) { 'use strict'; @@ -859,14 +859,27 @@ // Track if any command is altering the focus so we can avoid changing the // focus set by the Ajax command. var focusChanged = false; - for (var i in response) { - if (response.hasOwnProperty(i) && response[i].command && this.commands[response[i].command]) { - this.commands[response[i].command](this, response[i], status); - if (response[i].command === 'invoke' && response[i].method === 'focus') { - focusChanged = true; + + var that = this; + Object.keys(response).reduce(function (deferredCommand, i) { + return deferredCommand.then(function () { + var command = response[i].command; + console.log('Excuted Command: ' + command); + + if (command && that.commands[command]) { + if (command === 'invoke' && response[i].method === 'focus') { + focusChanged = true; + } + + if (command === 'add_js') { + return that.commands[command](that, response[i], status); + } + else { + that.commands[command](that, response[i], status); + } } - } - } + }); + }, $.Deferred().resolve()); // If the focus hasn't be changed by the ajax commands, try to refocus the // triggering element or one of its parents if that element does not exist @@ -1019,6 +1032,7 @@ // our presets. var $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper); var method = response.method || ajax.method; + console.log(method); var effect = ajax.getEffect(response); var settings; @@ -1338,7 +1352,25 @@ document.styleSheets[0].addImport(match[1]); } while (match); } + }, + + add_js: function (ajax, response) { + var deferred = $.Deferred(); + + // @todo: simplify JSON response and remove mapping? + var scriptsSrc = response.data.map(function (script) { + return script.src; + }); + + loadjs(scriptsSrc, { + success: function () { + console.log(scriptsSrc, 'script loaded'); + deferred.resolve(); + } + }); + + return deferred.promise(); } }; -})(jQuery, window, Drupal, drupalSettings); +})(jQuery, window, Drupal, drupalSettings, loadjs);