2 (function ($, Drupal, drupalSettings) {
6 Drupal.behaviors.tokenTree = {
7 attach: function (context, settings) {
8 $('table.token-tree', context).once('token-tree').each(function () {
9 $(this).treetable({ expandable: true });
14 Drupal.behaviors.tokenInsert = {
15 attach: function (context, settings) {
16 // Keep track of which textfield was last selected/focused.
17 $('textarea, input[type="text"]', context).focus(function () {
18 drupalSettings.tokenFocusedField = this;
21 $('.token-click-insert .token-key', context).once('token-click-insert').each(function () {
22 var newThis = $('<a href="javascript:void(0);" title="' + Drupal.t('Insert this token into your form') + '">' + $(this).html() + '</a>').click(function () {
23 var content = this.text;
25 // Always work in normal text areas that currently have focus.
26 if (drupalSettings.tokenFocusedField && (drupalSettings.tokenFocusedField.tokenDialogFocus || drupalSettings.tokenFocusedField.tokenHasFocus)) {
27 insertAtCursor(drupalSettings.tokenFocusedField, content);
29 // Direct tinyMCE support.
30 else if (typeof(tinyMCE) != 'undefined' && tinyMCE.activeEditor) {
31 tinyMCE.activeEditor.execCommand('mceInsertContent', false, content);
33 // Direct CKEditor support. Only works if the field currently has focus,
34 // which is unusual since the dialog is open.
35 else if (typeof(CKEDITOR) != 'undefined' && CKEDITOR.currentInstance) {
36 CKEDITOR.currentInstance.insertHtml(content);
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)
42 // CKeditor module support.
43 else if (typeof(CKEDITOR) != 'undefined' && typeof(Drupal.ckeditorActiveId) != 'undefined') {
44 CKEDITOR.instances[Drupal.ckeditorActiveId].insertHtml(content);
46 else if (drupalSettings.tokenFocusedField) {
47 insertAtCursor(drupalSettings.tokenFocusedField, content);
50 alert(Drupal.t('First click a text field to insert your tokens into.'));
55 $(this).html(newThis);
58 function insertAtCursor(editor, content) {
59 // Record the current scroll position.
60 var scroll = editor.scrollTop;
63 if (document.selection) {
65 var sel = document.selection.createRange();
69 // Mozilla/Firefox/Netscape 7+ support.
70 else if (editor.selectionStart || editor.selectionStart == '0') {
71 var startPos = editor.selectionStart;
72 var endPos = editor.selectionEnd;
73 editor.value = editor.value.substring(0, startPos) + content + editor.value.substring(endPos, editor.value.length);
76 // Fallback, just add to the end of the content.
78 editor.value += content;
81 // Ensure the textarea does not unexpectedly scroll.
82 editor.scrollTop = scroll;
87 })(jQuery, Drupal, drupalSettings);