token.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // Emulate the AJAX data sent normally so that we get the same theme.
  15. var data = {};
  16. data['ajax_page_state[theme]'] = Drupal.settings.ajaxPageState.theme;
  17. data['ajax_page_state[theme_token]'] = Drupal.settings.ajaxPageState.theme_token;
  18. dialog.dialog({
  19. title: $(this).attr('title') || Drupal.t('Available tokens'),
  20. width: 700,
  21. close: function(event, ui) {
  22. dialog.remove();
  23. }
  24. });
  25. // Load the token tree using AJAX.
  26. dialog.load(
  27. url,
  28. data,
  29. function (responseText, textStatus, XMLHttpRequest) {
  30. dialog.removeClass('loading');
  31. }
  32. );
  33. // Prevent browser from following the link.
  34. return false;
  35. });
  36. }
  37. }
  38. Drupal.behaviors.tokenInsert = {
  39. attach: function (context, settings) {
  40. // Keep track of which textfield was last selected/focused.
  41. $('textarea, input[type="text"]', context).focus(function() {
  42. Drupal.settings.tokenFocusedField = this;
  43. });
  44. $('.token-click-insert .token-key', context).once('token-click-insert', function() {
  45. var newThis = $('<a href="javascript:void(0);" title="' + Drupal.t('Insert this token into your form') + '">' + $(this).html() + '</a>').click(function(){
  46. if (typeof Drupal.settings.tokenFocusedField == 'undefined') {
  47. alert(Drupal.t('First click a text field to insert your tokens into.'));
  48. }
  49. else {
  50. var myField = Drupal.settings.tokenFocusedField;
  51. var myValue = $(this).text();
  52. //IE support
  53. if (document.selection) {
  54. myField.focus();
  55. sel = document.selection.createRange();
  56. sel.text = myValue;
  57. }
  58. //MOZILLA/NETSCAPE support
  59. else if (myField.selectionStart || myField.selectionStart == '0') {
  60. var startPos = myField.selectionStart;
  61. var endPos = myField.selectionEnd;
  62. myField.value = myField.value.substring(0, startPos)
  63. + myValue
  64. + myField.value.substring(endPos, myField.value.length);
  65. } else {
  66. myField.value += myValue;
  67. }
  68. $('html,body').animate({scrollTop: $(myField).offset().top}, 500);
  69. }
  70. return false;
  71. });
  72. $(this).html(newThis);
  73. });
  74. }
  75. };
  76. })(jQuery);