Version 1
[yaffs-website] / web / modules / contrib / token / js / token.js
1
2 (function ($) {
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           if (typeof drupalSettings.tokenFocusedField == 'undefined') {
24             alert(Drupal.t('First click a text field to insert your tokens into.'));
25           }
26           else {
27             var myField = drupalSettings.tokenFocusedField;
28             var myValue = $(this).text();
29
30             // IE support.
31             if (document.selection) {
32               myField.focus();
33               var sel = document.selection.createRange();
34               sel.text = myValue;
35             }
36
37             // MOZILLA/NETSCAPE support.
38             else if (myField.selectionStart || myField.selectionStart === '0') {
39               var startPos = myField.selectionStart;
40               var endPos = myField.selectionEnd;
41               myField.value = myField.value.substring(0, startPos)
42                             + myValue
43                             + myField.value.substring(endPos, myField.value.length);
44             }
45             else {
46               myField.value += myValue;
47             }
48
49             $('html,body').animate({scrollTop: $(myField).offset().top}, 500);
50           }
51           return false;
52         });
53         $(this).html(newThis);
54       });
55     }
56   };
57
58 })(jQuery, drupalSettings);