diff --git a/token.css b/token.css index 5c3b3ba..5432f0f 100644 --- a/token.css +++ b/token.css @@ -4,6 +4,10 @@ margin-left: 19px; } +.ui-dialog-content .token-tree { + margin-left: 0; +} + .token-tree td, .token-tree th { padding-top: 0; padding-bottom: 0; diff --git a/token.js b/token.js index 59715f4..ef5d36a 100644 --- a/token.js +++ b/token.js @@ -9,6 +9,32 @@ Drupal.behaviors.tokenTree = { } }; +Drupal.behaviors.tokenDialog = { + attach: function (context, settings) { + $('a.token-dialog', context).once('token-dialog').click(function() { + var url = $(this).attr('href'); + var dialog = $('').appendTo('body'); + dialog.dialog({ + title: $(this).attr('title') || Drupal.t('Available tokens'), + width: 700, + close: function(event, ui) { + dialog.remove(); + } + }); + // Load the token tree using AJAX. + dialog.load( + url, + {}, + function (responseText, textStatus, XMLHttpRequest) { + dialog.removeClass('loading'); + } + ); + // Prevent browser from following the link. + return false; + }); + } +} + Drupal.behaviors.tokenInsert = { attach: function (context, settings) { // Keep track of which textfield was last selected/focused. diff --git a/token.module b/token.module index 56e3ceb..7a7fda1 100644 --- a/token.module +++ b/token.module @@ -73,6 +73,13 @@ function token_menu() { 'file' => 'token.pages.inc', );*/ + $items['token/tree'] = array( + 'page callback' => 'token_page_output_tree', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + 'file' => 'token.pages.inc', + ); + // Devel token pages. if (module_exists('devel')) { $items['node/%node/devel/token'] = array( @@ -146,16 +153,20 @@ function token_type_load($token_type) { * Implements hook_theme(). */ function token_theme() { - return array( - 'tree_table' => array( - 'variables' => array('header' => array(), 'rows' => array(), 'attributes' => array(), 'empty' => '', 'caption' => ''), - 'file' => 'token.pages.inc', - ), - 'token_tree' => array( - 'variables' => array('token_types' => array(), 'global_types' => TRUE, 'click_insert' => TRUE, 'show_restricted' => FALSE, 'recursion_limit' => 3), - 'file' => 'token.pages.inc', - ), + $info['tree_table'] = array( + 'variables' => array('header' => array(), 'rows' => array(), 'attributes' => array(), 'empty' => '', 'caption' => ''), + 'file' => 'token.pages.inc', ); + $info['token_tree'] = array( + 'variables' => array('token_types' => array(), 'global_types' => TRUE, 'click_insert' => TRUE, 'show_restricted' => FALSE, 'recursion_limit' => 3), + 'file' => 'token.pages.inc', + ); + $info['token_tree_link'] = array( + 'variables' => array('text' => NULL, 'options' => array(), 'dialog' => TRUE) + $info['token_tree']['variables'], + 'file' => 'token.pages.inc', + ); + + return $info; } /** @@ -175,6 +186,17 @@ function token_library() { ), ); + $libraries['dialog'] = array( + 'title' => 'Token dialog', + 'version' => '1.0', + 'js' => array( + drupal_get_path('module', 'token') . '/token.js' => array(), + ), + 'dependencies' => array( + array('system', 'ui.dialog'), + ), + ); + return $libraries; } diff --git a/token.pages.inc b/token.pages.inc index d2f06e9..a2ce21f 100644 --- a/token.pages.inc +++ b/token.pages.inc @@ -6,6 +6,35 @@ */ /** + * Theme a link to a token tree either as a regular link or a dialog. + */ +function theme_token_tree_link($variables) { + if (empty($variables['text'])) { + $variables['text'] = t('Browse available tokens.'); + } + + if (!empty($variables['dialog'])) { + drupal_add_library('token', 'dialog'); + $variables['options']['attributes']['class'][] = 'token-dialog'; + $info = token_theme(); + $variables['options']['query']['token_tree'] = array_intersect_key($variables, $info['token_tree']['variables']); + } + + return l($variables['text'], 'token/tree', $variables['options']); +} + +/** + * Page callback to output a token tree as an empty page. + */ +function token_page_output_tree() { + $options = isset($_GET['token_tree']) ? $_GET['token_tree'] : array(); + $output = theme('token_tree', $options); + print '' . drupal_get_css() . drupal_get_js() . ''; + print '' . $output . ''; + drupal_exit(); +} + +/** * Theme a tree table. * * @ingroup themeable