/** * DO NOT EDIT THIS FILE. * See the following change record for more information, * https://www.drupal.org/node/2815083 * @preserve **/ (function ($, Drupal, drupalSettings) { Drupal.behaviors.machineName = { attach(context, settings) { const self = this; const $context = $(context); let timeout = null; let xhr = null; function clickEditHandler(e) { const data = e.data; data.$wrapper.removeClass('visually-hidden'); data.$target.trigger('focus'); data.$suffix.hide(); data.$source.off('.machineName'); } function machineNameHandler(e) { const data = e.data; const options = data.options; const baseValue = $(e.target).val(); const rx = new RegExp(options.replace_pattern, 'g'); const expected = baseValue.toLowerCase().replace(rx, options.replace).substr(0, options.maxlength); if (xhr && xhr.readystate !== 4) { xhr.abort(); xhr = null; } if (timeout) { clearTimeout(timeout); timeout = null; } if (baseValue.toLowerCase() !== expected) { timeout = setTimeout(() => { xhr = self.transliterate(baseValue, options).done(machine => { self.showMachineName(machine.substr(0, options.maxlength), data); }); }, 300); } else { self.showMachineName(expected, data); } } Object.keys(settings.machineName).forEach(sourceId => { const options = settings.machineName[sourceId]; const $source = $(once('machine-name', $context.find(sourceId).addClass('machine-name-source'))); const $target = $context.find(options.target).addClass('machine-name-target'); const $suffix = $context.find(options.suffix); const $wrapper = $target.closest('.js-form-item'); if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) { return; } if ($target.hasClass('error')) { return; } options.maxlength = $target.attr('maxlength'); $wrapper.addClass('visually-hidden'); const machine = $target.val(); const $preview = $(`${options.field_prefix}${Drupal.checkPlain(machine)}${options.field_suffix}`); $suffix.empty(); if (options.label) { $suffix.append(`${options.label}: `); } $suffix.append($preview); if ($target.is(':disabled')) { return; } const eventData = { $source, $target, $suffix, $wrapper, $preview, options }; if (machine === '' && $source.val() !== '') { self.transliterate($source.val(), options).done(machineName => { self.showMachineName(machineName.substr(0, options.maxlength), eventData); }); } const $link = $(``).on('click', eventData, clickEditHandler); $suffix.append($link); if ($target.val() === '') { $source.on('formUpdated.machineName', eventData, machineNameHandler).trigger('formUpdated.machineName'); } $target.on('invalid', eventData, clickEditHandler); }); }, showMachineName(machine, data) { const settings = data.options; if (machine !== '') { if (machine !== settings.replace) { data.$target.val(machine); data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix); } data.$suffix.show(); } else { data.$suffix.hide(); data.$target.val(machine); data.$preview.empty(); } }, transliterate(source, settings) { return $.get(Drupal.url('machine_name/transliterate'), { text: source, langcode: drupalSettings.langcode, replace_pattern: settings.replace_pattern, replace_token: settings.replace_token, replace: settings.replace, lowercase: true }); } }; })(jQuery, Drupal, drupalSettings);