diff --git a/token.js b/token.js index 98d1ac3..beb172b 100644 --- a/token.js +++ b/token.js @@ -4,7 +4,47 @@ Drupal.behaviors.tokenTree = { attach: function (context, settings) { $('table.token-tree', context).once('token-tree', function () { - $(this).treeTable(); + $('tr', this).each(function() { + var tokenType = $(this).attr('token-type'); + if (!tokenType) return; + + $('td:eq(1)', this).bind('load_node.jstree', function() { Drupal.attachBehaviors(this) }).jstree({ + json_data : { + data : function(n, callback) { + this._get_settings().json_data.data = false; + return callback([{data: tokenType, state: "closed", metadata: {root: true}}]) + }, + + ajax : { + url : function(n) { + var url = Drupal.settings.basePath + 'token/autocomplete/' + tokenType + "/"; + + if (n.data().root) { + return url + '[' + tokenType + ':'; + } + else { + return url + $('> a', n).text().trim() + } + }, + success : function(data) { + var nodes=[]; + for(var k in data) { + if (data.hasOwnProperty(k)) { + if (k[k.length-1] == ':') { + nodes.push({data: {title: k, attr: {href: "javascript:false;"}}, state: "closed"}) + } + else { + nodes.push({data: k, attr: {class: 'token-key'}}) + } + } + } + return nodes; + } + } + }, + plugins : [ "themes", "json_data" ] + }); + }); }); } }; @@ -47,9 +87,9 @@ Drupal.behaviors.tokenInsert = { $('textarea, input[type="text"]', context).focus(function() { Drupal.settings.tokenFocusedField = this; }); - - $('.token-click-insert .token-key', context).once('token-click-insert', function() { - var newThis = $('' + $(this).html() + '').click(function(){ + + $('.token-key a', context).once('token-click-insert', function() { + $(this).click(function() { if (typeof Drupal.settings.tokenFocusedField == 'undefined') { alert(Drupal.t('First click a text field to insert your tokens into.')); } @@ -79,7 +119,6 @@ Drupal.behaviors.tokenInsert = { } return false; }); - $(this).html(newThis); }); } }; diff --git a/token.module b/token.module index 269f376..78be909 100644 --- a/token.module +++ b/token.module @@ -170,7 +170,6 @@ function token_theme() { 'global_types' => TRUE, 'click_insert' => TRUE, 'show_restricted' => FALSE, - 'recursion_limit' => 3, 'dialog' => FALSE, ), 'file' => 'token.pages.inc', @@ -191,16 +190,16 @@ function token_theme() { * Implements hook_library(). */ function token_library() { - // jQuery treeTable plugin. - $libraries['treeTable'] = array( - 'title' => 'jQuery treeTable', - 'website' => 'http://plugins.jquery.com/project/treetable', - 'version' => '2.3.0', + // jQuery jsTree plugin. + $libraries['jsTree'] = array( + 'title' => 'jQuery jsTree', + 'website' => 'http://www.jstree.com/', + 'version' => '1.0-rc3', 'js' => array( - drupal_get_path('module', 'token') . '/jquery.treeTable.js' => array(), + drupal_get_path('module', 'token') . '/jquery.jstree.js' => array(), ), 'css' => array( - drupal_get_path('module', 'token') . '/jquery.treeTable.css' => array(), + drupal_get_path('module', 'token') . '/themes/jstree/style.css' => array(), ), ); @@ -875,8 +874,6 @@ function token_form_user_admin_settings_alter(&$form, &$form_state) { * @param $show_restricted * A boolean if TRUE will show restricted tokens. Otherwise they will be * hidden. Default is FALSE. - * @param $recursion_limit - * An integer with the maximum number of token levels to recurse. * @param $parents * An optional array with the current parents of the tokens. */ diff --git a/token.pages.inc b/token.pages.inc index f761547..72823bb 100644 --- a/token.pages.inc +++ b/token.pages.inc @@ -82,7 +82,7 @@ function theme_tree_table($variables) { } if (!empty($variables['rows'])) { - drupal_add_library('token', 'treeTable'); + drupal_add_library('token', 'jsTree'); } return theme('table', $variables); @@ -108,20 +108,10 @@ function theme_token_tree($variables) { $token_types = array_merge($token_types, token_get_global_token_types()); } - $element = array( - '#cache' => array( - 'cid' => 'tree-rendered:' . hash('sha256', serialize(array('token_types' => $token_types, 'global_types' => NULL) + $variables)), - 'bin' => 'cache_token', - ), - ); - if ($cached_output = token_render_cache_get($element)) { - return $cached_output; - } - $options = array( 'flat' => TRUE, 'restricted' => $variables['show_restricted'], - 'depth' => $variables['recursion_limit'], + 'depth' => 0 ); $multiple_token_types = (count($token_types) > 1); $rows = array(); @@ -133,26 +123,14 @@ function theme_token_tree($variables) { if ($multiple_token_types) { $row = _token_token_tree_format_row($type, $type_info, TRUE); - unset($row['data']['value']); - $rows[] = $row; - } - - $tree = token_build_tree($type, $options); - foreach ($tree as $token => $token_info) { - if (!empty($token_info['restricted']) && empty($variables['show_restricted'])) { - continue; - } - if ($multiple_token_types && !isset($token_info['parent'])) { - $token_info['parent'] = $type; - } - $row = _token_token_tree_format_row($token, $token_info); + $row['token-type'] = $type; unset($row['data']['value']); $rows[] = $row; } } - $element += array( - '#theme' => 'tree_table', + $element = array( + '#theme' => 'table', '#header' => array( t('Name'), t('Token'), @@ -164,7 +142,7 @@ function theme_token_tree($variables) { '#attached' => array( 'js' => array(drupal_get_path('module', 'token') . '/token.js'), 'css' => array(drupal_get_path('module', 'token') . '/token.css'), - 'library' => array(array('token', 'treeTable')), + 'library' => array(array('token', 'jsTree')), ), ); @@ -173,9 +151,7 @@ function theme_token_tree($variables) { $element['#attributes']['class'][] = 'token-click-insert'; } - $output = drupal_render($element); - token_render_cache_set($output, $element); - return $output; + return drupal_render($element); } /**