token.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (function ($) {
  2. Drupal.behaviors.tokenTree = {
  3. attach: function (context, settings) {
  4. $('table.token-tree', context).once('token-tree', function () {
  5. $(this).treeTable();
  6. });
  7. }
  8. };
  9. Drupal.behaviors.tokenDialog = {
  10. attach: function (context, settings) {
  11. $('a.token-dialog', context).once('token-dialog').click(function() {
  12. var url = $(this).attr('href');
  13. var dialog = $('<div style="display: none" class="loading">' + Drupal.t('Loading token browser...') + '</div>').appendTo('body');
  14. dialog.dialog({
  15. title: $(this).attr('title') || Drupal.t('Available tokens'),
  16. width: 700,
  17. close: function(event, ui) {
  18. dialog.remove();
  19. }
  20. });
  21. // Load the token tree using AJAX.
  22. dialog.load(
  23. url,
  24. {},
  25. function (responseText, textStatus, XMLHttpRequest) {
  26. dialog.removeClass('loading');
  27. }
  28. );
  29. // Prevent browser from following the link.
  30. return false;
  31. });
  32. }
  33. }
  34. Drupal.behaviors.tokenInsert = {
  35. attach: function (context, settings) {
  36. // Keep track of which textfield was last selected/focused.
  37. $('textarea, input[type="text"]', context).focus(function() {
  38. Drupal.settings.tokenFocusedField = this;
  39. });
  40. $('.token-click-insert .token-key', context).once('token-click-insert', function() {
  41. var newThis = $('<a href="javascript:void(0);" title="' + Drupal.t('Insert this token into your form') + '">' + $(this).html() + '</a>').click(function(){
  42. if (typeof Drupal.settings.tokenFocusedField == 'undefined') {
  43. alert(Drupal.t('First click a text field to insert your tokens into.'));
  44. }
  45. else {
  46. var myField = Drupal.settings.tokenFocusedField;
  47. var myValue = $(this).text();
  48. //IE support
  49. if (document.selection) {
  50. myField.focus();
  51. sel = document.selection.createRange();
  52. sel.text = myValue;
  53. }
  54. //MOZILLA/NETSCAPE support
  55. else if (myField.selectionStart || myField.selectionStart == '0') {
  56. var startPos = myField.selectionStart;
  57. var endPos = myField.selectionEnd;
  58. myField.value = myField.value.substring(0, startPos)
  59. + myValue
  60. + myField.value.substring(endPos, myField.value.length);
  61. } else {
  62. myField.value += myValue;
  63. }
  64. $('html,body').animate({scrollTop: $(myField).offset().top}, 500);
  65. }
  66. return false;
  67. });
  68. $(this).html(newThis);
  69. });
  70. }
  71. };
  72. })(jQuery);