token.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (function ($, Drupal, drupalSettings) {
  2. 'use strict';
  3. Drupal.behaviors.tokenTree = {
  4. attach: function (context, settings) {
  5. $('table.token-tree', context).once('token-tree').each(function () {
  6. $(this).treetable({ expandable: true });
  7. });
  8. }
  9. };
  10. Drupal.behaviors.tokenInsert = {
  11. attach: function (context, settings) {
  12. // Keep track of which textfield was last selected/focused.
  13. $('textarea, input[type="text"]', context).focus(function () {
  14. drupalSettings.tokenFocusedField = this;
  15. });
  16. $('.token-click-insert .token-key', context).once('token-click-insert').each(function () {
  17. var newThis = $('<a href="javascript:void(0);" title="' + Drupal.t('Insert this token into your form') + '">' + $(this).html() + '</a>').click(function () {
  18. var content = this.text;
  19. // Always work in normal text areas that currently have focus.
  20. if (drupalSettings.tokenFocusedField && (drupalSettings.tokenFocusedField.tokenDialogFocus || drupalSettings.tokenFocusedField.tokenHasFocus)) {
  21. insertAtCursor(drupalSettings.tokenFocusedField, content);
  22. }
  23. // Direct tinyMCE support.
  24. else if (typeof(tinyMCE) != 'undefined' && tinyMCE.activeEditor) {
  25. tinyMCE.activeEditor.execCommand('mceInsertContent', false, content);
  26. }
  27. // Direct CKEditor support. Only works if the field currently has focus,
  28. // which is unusual since the dialog is open.
  29. else if (typeof(CKEDITOR) != 'undefined' && CKEDITOR.currentInstance) {
  30. CKEDITOR.currentInstance.insertHtml(content);
  31. }
  32. // WYSIWYG support, should work in all editors if available.
  33. else if (Drupal.wysiwyg && Drupal.wysiwyg.activeId) {
  34. Drupal.wysiwyg.instances[Drupal.wysiwyg.activeId].insert(content)
  35. }
  36. // CKeditor module support.
  37. else if (typeof(CKEDITOR) != 'undefined' && typeof(Drupal.ckeditorActiveId) != 'undefined') {
  38. CKEDITOR.instances[Drupal.ckeditorActiveId].insertHtml(content);
  39. }
  40. else if (drupalSettings.tokenFocusedField) {
  41. insertAtCursor(drupalSettings.tokenFocusedField, content);
  42. }
  43. else {
  44. alert(Drupal.t('First click a text field to insert your tokens into.'));
  45. }
  46. return false;
  47. });
  48. $(this).html(newThis);
  49. });
  50. function insertAtCursor(editor, content) {
  51. // Record the current scroll position.
  52. var scroll = editor.scrollTop;
  53. // IE support.
  54. if (document.selection) {
  55. editor.focus();
  56. var sel = document.selection.createRange();
  57. sel.text = content;
  58. }
  59. // Mozilla/Firefox/Netscape 7+ support.
  60. else if (editor.selectionStart || editor.selectionStart == '0') {
  61. var startPos = editor.selectionStart;
  62. var endPos = editor.selectionEnd;
  63. editor.value = editor.value.substring(0, startPos) + content + editor.value.substring(endPos, editor.value.length);
  64. }
  65. // Fallback, just add to the end of the content.
  66. else {
  67. editor.value += content;
  68. }
  69. // Ensure the textarea does not unexpectedly scroll.
  70. editor.scrollTop = scroll;
  71. }
  72. }
  73. };
  74. })(jQuery, Drupal, drupalSettings);