diff --git a/core/lib/Drupal/Core/Ajax/AddJsCommand.php b/core/lib/Drupal/Core/Ajax/AddJsCommand.php new file mode 100644 index 0000000..f7c0a8a --- /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 45aa45e..6cb9f3e 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 fefe9f3..de5a418 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -859,14 +859,28 @@ // 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 +1033,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 +1353,40 @@ document.styleSheets[0].addImport(match[1]); } while (match); } + }, + + add_js: function (ajax, response, status) { + var deferred = $.Deferred(); + + // @todo: simplify JSON reponse and remove mapping? + var scriptsSrc = response.data.map(function (script) { + return script.src; + }); + + Drupal.drupalScriptLoader(scriptsSrc) + .fail(function () { + // @todo: error handler? + }).always(function () { + // @todo: simulated network delay on test purpose only, remove setTimeOut + deferred.resolve(); + }); + + return deferred.promise(); } }; + + // @todo: add into correct files. + // @todo: make it as slim conditional script loader. accept condition evelaution for futher loading. e.g. Let devleoper extend to load in IE only. + Drupal.drupalScriptLoader = function (scripts) { + return $.when.apply($, scripts.map(function (script) { + return $.ajax({ + url: script, + dataType: 'script', + cache: true + }); + })); + }; + + })(jQuery, window, Drupal, drupalSettings); diff --git a/core/misc/dialog/dialog.ajax.js b/core/misc/dialog/dialog.ajax.js index 3f1b0c2..6b8f89d 100644 --- a/core/misc/dialog/dialog.ajax.js +++ b/core/misc/dialog/dialog.ajax.js @@ -5,6 +5,8 @@ (function ($, Drupal) { + console.log('dialog.ajax.js loaded'); + 'use strict'; /** @@ -111,6 +113,7 @@ * Returns false if there was no selector property in the response object. */ Drupal.AjaxCommands.prototype.openDialog = function (ajax, response, status) { + console.log('openDialog() ajax command called'); if (!response.selector) { return false; }