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 // Direct CodeMirror support.
39 else if (typeof(CodeMirror) != 'undefined' && drupalSettings.tokenFocusedField && $(drupalSettings.tokenFocusedField).parents('.CodeMirror').length) {
40 var editor = $(drupalSettings.tokenFocusedField).parents('.CodeMirror')[0].CodeMirror;
41 editor.replaceSelection(content);
44 // WYSIWYG support, should work in all editors if available.
45 else if (Drupal.wysiwyg && Drupal.wysiwyg.activeId) {
46 Drupal.wysiwyg.instances[Drupal.wysiwyg.activeId].insert(content)
48 // CKeditor module support.
49 else if (typeof(CKEDITOR) != 'undefined' && typeof(Drupal.ckeditorActiveId) != 'undefined') {
50 CKEDITOR.instances[Drupal.ckeditorActiveId].insertHtml(content);
52 else if (drupalSettings.tokenFocusedField) {
53 insertAtCursor(drupalSettings.tokenFocusedField, content);
56 alert(Drupal.t('First click a text field to insert your tokens into.'));
61 $(this).html(newThis);
64 function insertAtCursor(editor, content) {
65 // Record the current scroll position.
66 var scroll = editor.scrollTop;
69 if (document.selection) {
71 var sel = document.selection.createRange();
75 // Mozilla/Firefox/Netscape 7+ support.
76 else if (editor.selectionStart || editor.selectionStart == '0') {
77 var startPos = editor.selectionStart;
78 var endPos = editor.selectionEnd;
79 editor.value = editor.value.substring(0, startPos) + content + editor.value.substring(endPos, editor.value.length);
82 // Fallback, just add to the end of the content.
84 editor.value += content;
87 // Ensure the textarea does not unexpectedly scroll.
88 editor.scrollTop = scroll;
93 })(jQuery, Drupal, drupalSettings);