token.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // Direct CodeMirror support.
  33. else if (typeof(CodeMirror) != 'undefined' && drupalSettings.tokenFocusedField && $(drupalSettings.tokenFocusedField).parents('.CodeMirror').length) {
  34. var editor = $(drupalSettings.tokenFocusedField).parents('.CodeMirror')[0].CodeMirror;
  35. editor.replaceSelection(content);
  36. editor.focus();
  37. }
  38. // WYSIWYG support, should work in all editors if available.
  39. else if (Drupal.wysiwyg && Drupal.wysiwyg.activeId) {
  40. Drupal.wysiwyg.instances[Drupal.wysiwyg.activeId].insert(content)
  41. }
  42. // CKeditor module support.
  43. else if (typeof(CKEDITOR) != 'undefined' && typeof(Drupal.ckeditorActiveId) != 'undefined') {
  44. CKEDITOR.instances[Drupal.ckeditorActiveId].insertHtml(content);
  45. }
  46. else if (drupalSettings.tokenFocusedField) {
  47. insertAtCursor(drupalSettings.tokenFocusedField, content);
  48. }
  49. else {
  50. alert(Drupal.t('First click a text field to insert your tokens into.'));
  51. }
  52. return false;
  53. });
  54. $(this).html(newThis);
  55. });
  56. function insertAtCursor(editor, content) {
  57. // Record the current scroll position.
  58. var scroll = editor.scrollTop;
  59. // IE support.
  60. if (document.selection) {
  61. editor.focus();
  62. var sel = document.selection.createRange();
  63. sel.text = content;
  64. }
  65. // Mozilla/Firefox/Netscape 7+ support.
  66. else if (editor.selectionStart || editor.selectionStart == '0') {
  67. var startPos = editor.selectionStart;
  68. var endPos = editor.selectionEnd;
  69. editor.value = editor.value.substring(0, startPos) + content + editor.value.substring(endPos, editor.value.length);
  70. }
  71. // Fallback, just add to the end of the content.
  72. else {
  73. editor.value += content;
  74. }
  75. // Ensure the textarea does not unexpectedly scroll.
  76. editor.scrollTop = scroll;
  77. }
  78. }
  79. };
  80. })(jQuery, Drupal, drupalSettings);