From 493a87045e959c0ff7293e39f55da2cc4872c619 Mon Sep 17 00:00:00 2001 From: "Dmitriy.trt" Date: Mon, 16 Mar 2015 21:13:26 +0600 Subject: [PATCH] Issue #2023423 by Dmitriy.trt: Use Drupal AJAX system to load dialog content. This way proper theme is selected, additional CSS & JS files loaded, settings merged, etc. --- token.js | 30 ++++++++++++++++-------------- token.module | 6 +++++- token.pages.inc | 13 +++++++++---- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/token.js b/token.js index 98d1ac3..1fd16fa 100644 --- a/token.js +++ b/token.js @@ -15,11 +15,6 @@ Drupal.behaviors.tokenDialog = { var url = $(this).attr('href'); var dialog = $('').appendTo('body'); - // Emulate the AJAX data sent normally so that we get the same theme. - var data = {}; - data['ajax_page_state[theme]'] = Drupal.settings.ajaxPageState.theme; - data['ajax_page_state[theme_token]'] = Drupal.settings.ajaxPageState.theme_token; - dialog.dialog({ title: $(this).attr('title') || Drupal.t('Available tokens'), width: 700, @@ -27,19 +22,26 @@ Drupal.behaviors.tokenDialog = { dialog.remove(); } }); - // Load the token tree using AJAX. - dialog.load( - url, - data, - function (responseText, textStatus, XMLHttpRequest) { - dialog.removeClass('loading'); - } - ); + + // Load the token tree using Drupal AJAX. + var ajax = new Drupal.ajax(false, false, { + url: url, + tokenDialog: dialog + }); + ajax.eventResponse(this, {}); + // Prevent browser from following the link. return false; }); } -} +}; + +Drupal.ajax.prototype.commands.tokenDialog = function(ajax, response, status) { + ajax.element_settings.tokenDialog + .html(response.tree) + .removeClass('loading'); + Drupal.attachBehaviors(ajax.element_settings.tokenDialog); +}; Drupal.behaviors.tokenInsert = { attach: function (context, settings) { diff --git a/token.module b/token.module index b683c70..e950726 100644 --- a/token.module +++ b/token.module @@ -79,6 +79,7 @@ function token_menu() { 'type' => MENU_CALLBACK, 'file' => 'token.pages.inc', 'theme callback' => 'ajax_base_page_theme', + 'delivery callback' => 'ajax_deliver', ); // Devel token pages. @@ -208,10 +209,13 @@ function token_library() { 'title' => 'Token dialog', 'version' => '1.0', 'js' => array( - drupal_get_path('module', 'token') . '/token.js' => array(), + drupal_get_path('module', 'token') . '/token.js' => array( + 'weight' => 3, + ), ), 'dependencies' => array( array('system', 'ui.dialog'), + array('system', 'drupal.ajax'), ), ); diff --git a/token.pages.inc b/token.pages.inc index f761547..b92317f 100644 --- a/token.pages.inc +++ b/token.pages.inc @@ -61,10 +61,15 @@ function token_page_output_tree() { // a dialog. $options['dialog'] = FALSE; - $output = theme('token_tree', $options); - print '' . drupal_get_css() . drupal_get_js() . ''; - print '' . $output . ''; - drupal_exit(); + $commands = array(); + $commands[] = array( + 'command' => 'tokenDialog', + 'tree' => theme('token_tree', $options), + ); + return array( + '#type' => 'ajax', + '#commands' => $commands, + ); } /** -- 2.1.4