Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / token / js / token.js
1
2 (function ($, Drupal, drupalSettings) {
3
4   'use strict';
5
6   Drupal.behaviors.tokenTree = {
7     attach: function (context, settings) {
8       $('table.token-tree', context).once('token-tree').each(function () {
9         $(this).treetable({ expandable: true });
10       });
11     }
12   };
13
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;
19       });
20
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;
24
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);
28           }
29           // Direct tinyMCE support.
30           else if (typeof(tinyMCE) != 'undefined' && tinyMCE.activeEditor) {
31             tinyMCE.activeEditor.execCommand('mceInsertContent', false, content);
32           }
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);
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
53           return false;
54         });
55         $(this).html(newThis);
56       });
57
58       function insertAtCursor(editor, content) {
59         // Record the current scroll position.
60         var scroll = editor.scrollTop;
61
62         // IE support.
63         if (document.selection) {
64           editor.focus();
65           var sel = document.selection.createRange();
66           sel.text = content;
67         }
68
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);
74         }
75
76         // Fallback, just add to the end of the content.
77         else {
78           editor.value += content;
79         }
80
81         // Ensure the textarea does not unexpectedly scroll.
82         editor.scrollTop = scroll;
83       }
84     }
85   };
86
87 })(jQuery, Drupal, drupalSettings);