Security update to Drupal 8.4.6
[yaffs-website] / web / core / misc / machine-name.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, drupalSettings) {
9   Drupal.behaviors.machineName = {
10     attach: function attach(context, settings) {
11       var self = this;
12       var $context = $(context);
13       var timeout = null;
14       var xhr = null;
15
16       function clickEditHandler(e) {
17         var data = e.data;
18         data.$wrapper.removeClass('visually-hidden');
19         data.$target.trigger('focus');
20         data.$suffix.hide();
21         data.$source.off('.machineName');
22       }
23
24       function machineNameHandler(e) {
25         var data = e.data;
26         var options = data.options;
27         var baseValue = $(e.target).val();
28
29         var rx = new RegExp(options.replace_pattern, 'g');
30         var expected = baseValue.toLowerCase().replace(rx, options.replace).substr(0, options.maxlength);
31
32         if (xhr && xhr.readystate !== 4) {
33           xhr.abort();
34           xhr = null;
35         }
36
37         if (timeout) {
38           clearTimeout(timeout);
39           timeout = null;
40         }
41         if (baseValue.toLowerCase() !== expected) {
42           timeout = setTimeout(function () {
43             xhr = self.transliterate(baseValue, options).done(function (machine) {
44               self.showMachineName(machine.substr(0, options.maxlength), data);
45             });
46           }, 300);
47         } else {
48           self.showMachineName(expected, data);
49         }
50       }
51
52       Object.keys(settings.machineName).forEach(function (source_id) {
53         var machine = '';
54         var eventData = void 0;
55         var options = settings.machineName[source_id];
56
57         var $source = $context.find(source_id).addClass('machine-name-source').once('machine-name');
58         var $target = $context.find(options.target).addClass('machine-name-target');
59         var $suffix = $context.find(options.suffix);
60         var $wrapper = $target.closest('.js-form-item');
61
62         if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) {
63           return;
64         }
65
66         if ($target.hasClass('error')) {
67           return;
68         }
69
70         options.maxlength = $target.attr('maxlength');
71
72         $wrapper.addClass('visually-hidden');
73
74         if ($target.is(':disabled') || $target.val() !== '') {
75           machine = $target.val();
76         } else if ($source.val() !== '') {
77           machine = self.transliterate($source.val(), options);
78         }
79
80         var $preview = $('<span class="machine-name-value">' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + '</span>');
81         $suffix.empty();
82         if (options.label) {
83           $suffix.append('<span class="machine-name-label">' + options.label + ': </span>');
84         }
85         $suffix.append($preview);
86
87         if ($target.is(':disabled')) {
88           return;
89         }
90
91         eventData = {
92           $source: $source,
93           $target: $target,
94           $suffix: $suffix,
95           $wrapper: $wrapper,
96           $preview: $preview,
97           options: options
98         };
99
100         var $link = $('<span class="admin-link"><button type="button" class="link">' + Drupal.t('Edit') + '</button></span>').on('click', eventData, clickEditHandler);
101         $suffix.append($link);
102
103         if ($target.val() === '') {
104           $source.on('formUpdated.machineName', eventData, machineNameHandler).trigger('formUpdated.machineName');
105         }
106
107         $target.on('invalid', eventData, clickEditHandler);
108       });
109     },
110     showMachineName: function showMachineName(machine, data) {
111       var settings = data.options;
112
113       if (machine !== '') {
114         if (machine !== settings.replace) {
115           data.$target.val(machine);
116           data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix);
117         }
118         data.$suffix.show();
119       } else {
120         data.$suffix.hide();
121         data.$target.val(machine);
122         data.$preview.empty();
123       }
124     },
125     transliterate: function transliterate(source, settings) {
126       return $.get(Drupal.url('machine_name/transliterate'), {
127         text: source,
128         langcode: drupalSettings.langcode,
129         replace_pattern: settings.replace_pattern,
130         replace_token: settings.replace_token,
131         replace: settings.replace,
132         lowercase: true
133       });
134     }
135   };
136 })(jQuery, Drupal, drupalSettings);