Updated all the contrib modules to their latest versions.
[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           // 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);
42             editor.focus();
43           }
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)
47           }
48           // CKeditor module support.
49           else if (typeof(CKEDITOR) != 'undefined' && typeof(Drupal.ckeditorActiveId) != 'undefined') {
50             CKEDITOR.instances[Drupal.ckeditorActiveId].insertHtml(content);
51           }
52           else if (drupalSettings.tokenFocusedField) {
53             insertAtCursor(drupalSettings.tokenFocusedField, content);
54           }
55           else {
56             alert(Drupal.t('First click a text field to insert your tokens into.'));
57           }
58
59           return false;
60         });
61         $(this).html(newThis);
62       });
63
64       function insertAtCursor(editor, content) {
65         // Record the current scroll position.
66         var scroll = editor.scrollTop;
67
68         // IE support.
69         if (document.selection) {
70           editor.focus();
71           var sel = document.selection.createRange();
72           sel.text = content;
73         }
74
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);
80         }
81
82         // Fallback, just add to the end of the content.
83         else {
84           editor.value += content;
85         }
86
87         // Ensure the textarea does not unexpectedly scroll.
88         editor.scrollTop = scroll;
89       }
90     }
91   };
92
93 })(jQuery, Drupal, drupalSettings);