drupal/core/misc/machine-name.js

138 lines
4.2 KiB
JavaScript
Raw Normal View History

/**
* 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.value;
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[0].value;
const $preview = $(`<span class="machine-name-value">${options.field_prefix}${Drupal.checkPlain(machine)}${options.field_suffix}</span>`);
$suffix.empty();
if (options.label) {
$suffix.append(`<span class="machine-name-label">${options.label}: </span>`);
}
$suffix.append($preview);
if ($target.is(':disabled')) {
return;
}
const eventData = {
$source,
$target,
$suffix,
$wrapper,
$preview,
options
};
if (machine === '' && $source[0].value !== '') {
self.transliterate($source[0].value, options).done(machineName => {
self.showMachineName(machineName.substr(0, options.maxlength), eventData);
});
}
const $link = $(`<span class="admin-link"><button type="button" class="link">${Drupal.t('Edit')}</button></span>`).on('click', eventData, clickEditHandler);
$suffix.append($link);
if ($target[0].value === '') {
$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[0].value = machine;
data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix);
}
data.$suffix.show();
} else {
data.$suffix.hide();
data.$target[0].value = 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);