2015-06-08 14:04:39 +00:00
|
|
|
/**
|
2017-05-19 22:12:53 +00:00
|
|
|
* DO NOT EDIT THIS FILE.
|
|
|
|
* See the following change record for more information,
|
2017-05-23 14:30:14 +00:00
|
|
|
* https://www.drupal.org/node/2815083
|
2017-05-19 22:12:53 +00:00
|
|
|
* @preserve
|
|
|
|
**/
|
2015-06-08 14:04:39 +00:00
|
|
|
|
2013-05-02 07:51:45 +00:00
|
|
|
(function ($, Drupal, drupalSettings) {
|
2014-01-27 21:41:32 +00:00
|
|
|
Drupal.behaviors.machineName = {
|
2021-12-18 06:12:16 +00:00
|
|
|
attach(context, settings) {
|
|
|
|
const self = this;
|
|
|
|
const $context = $(context);
|
|
|
|
let timeout = null;
|
|
|
|
let xhr = null;
|
2010-10-13 17:09:00 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function clickEditHandler(e) {
|
2021-12-18 06:12:16 +00:00
|
|
|
const data = e.data;
|
2015-05-25 03:46:41 +00:00
|
|
|
data.$wrapper.removeClass('visually-hidden');
|
2014-01-27 21:41:32 +00:00
|
|
|
data.$target.trigger('focus');
|
|
|
|
data.$suffix.hide();
|
|
|
|
data.$source.off('.machineName');
|
2012-07-15 13:02:00 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function machineNameHandler(e) {
|
2021-12-18 06:12:16 +00:00
|
|
|
const data = e.data;
|
|
|
|
const options = data.options;
|
2022-01-17 16:08:33 +00:00
|
|
|
const baseValue = e.target.value;
|
2021-12-18 06:12:16 +00:00
|
|
|
const rx = new RegExp(options.replace_pattern, 'g');
|
|
|
|
const expected = baseValue.toLowerCase().replace(rx, options.replace).substr(0, options.maxlength);
|
2014-01-27 21:41:32 +00:00
|
|
|
|
2014-11-18 22:35:32 +00:00
|
|
|
if (xhr && xhr.readystate !== 4) {
|
|
|
|
xhr.abort();
|
|
|
|
xhr = null;
|
2012-07-15 13:02:00 +00:00
|
|
|
}
|
2014-11-18 22:35:32 +00:00
|
|
|
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = null;
|
2012-07-15 13:02:00 +00:00
|
|
|
}
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2016-11-03 14:54:52 +00:00
|
|
|
if (baseValue.toLowerCase() !== expected) {
|
2021-12-18 06:12:16 +00:00
|
|
|
timeout = setTimeout(() => {
|
|
|
|
xhr = self.transliterate(baseValue, options).done(machine => {
|
2014-11-18 22:35:32 +00:00
|
|
|
self.showMachineName(machine.substr(0, options.maxlength), data);
|
|
|
|
});
|
2016-11-03 14:54:52 +00:00
|
|
|
}, 300);
|
2017-05-19 22:12:53 +00:00
|
|
|
} else {
|
2016-11-03 14:54:52 +00:00
|
|
|
self.showMachineName(expected, data);
|
|
|
|
}
|
2014-01-27 21:41:32 +00:00
|
|
|
}
|
2012-07-15 13:02:00 +00:00
|
|
|
|
2021-12-18 06:12:16 +00:00
|
|
|
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');
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2014-05-24 13:59:54 +00:00
|
|
|
if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2014-05-24 13:59:54 +00:00
|
|
|
if ($target.hasClass('error')) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2014-05-24 13:59:54 +00:00
|
|
|
options.maxlength = $target.attr('maxlength');
|
2015-05-25 03:46:41 +00:00
|
|
|
$wrapper.addClass('visually-hidden');
|
2022-01-17 16:08:33 +00:00
|
|
|
const machine = $target[0].value;
|
2021-12-18 06:12:16 +00:00
|
|
|
const $preview = $(`<span class="machine-name-value">${options.field_prefix}${Drupal.checkPlain(machine)}${options.field_suffix}</span>`);
|
2014-05-24 13:59:54 +00:00
|
|
|
$suffix.empty();
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2014-05-24 13:59:54 +00:00
|
|
|
if (options.label) {
|
2021-12-18 06:12:16 +00:00
|
|
|
$suffix.append(`<span class="machine-name-label">${options.label}: </span>`);
|
2014-05-24 13:59:54 +00:00
|
|
|
}
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2015-02-03 08:55:41 +00:00
|
|
|
$suffix.append($preview);
|
2010-11-22 00:05:58 +00:00
|
|
|
|
2014-05-24 13:59:54 +00:00
|
|
|
if ($target.is(':disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
2010-10-13 17:09:00 +00:00
|
|
|
|
2021-12-18 06:12:16 +00:00
|
|
|
const eventData = {
|
|
|
|
$source,
|
|
|
|
$target,
|
|
|
|
$suffix,
|
|
|
|
$wrapper,
|
|
|
|
$preview,
|
|
|
|
options
|
2014-05-24 13:59:54 +00:00
|
|
|
};
|
2020-02-03 09:32:45 +00:00
|
|
|
|
2022-01-17 16:08:33 +00:00
|
|
|
if (machine === '' && $source[0].value !== '') {
|
|
|
|
self.transliterate($source[0].value, options).done(machineName => {
|
2020-02-03 09:32:45 +00:00
|
|
|
self.showMachineName(machineName.substr(0, options.maxlength), eventData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-18 06:12:16 +00:00
|
|
|
const $link = $(`<span class="admin-link"><button type="button" class="link">${Drupal.t('Edit')}</button></span>`).on('click', eventData, clickEditHandler);
|
2015-02-03 08:55:41 +00:00
|
|
|
$suffix.append($link);
|
2014-01-27 21:41:32 +00:00
|
|
|
|
2022-01-17 16:08:33 +00:00
|
|
|
if ($target[0].value === '') {
|
2017-05-19 22:12:53 +00:00
|
|
|
$source.on('formUpdated.machineName', eventData, machineNameHandler).trigger('formUpdated.machineName');
|
2012-07-15 13:02:00 +00:00
|
|
|
}
|
2015-05-25 03:46:41 +00:00
|
|
|
|
|
|
|
$target.on('invalid', eventData, clickEditHandler);
|
2014-05-24 13:59:54 +00:00
|
|
|
});
|
2014-01-27 21:41:32 +00:00
|
|
|
},
|
2021-12-18 06:12:16 +00:00
|
|
|
|
|
|
|
showMachineName(machine, data) {
|
|
|
|
const settings = data.options;
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
if (machine !== '') {
|
|
|
|
if (machine !== settings.replace) {
|
2022-01-17 16:08:33 +00:00
|
|
|
data.$target[0].value = machine;
|
2014-01-27 21:41:32 +00:00
|
|
|
data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix);
|
|
|
|
}
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
data.$suffix.show();
|
2017-05-19 22:12:53 +00:00
|
|
|
} else {
|
2014-01-27 21:41:32 +00:00
|
|
|
data.$suffix.hide();
|
2022-01-17 16:08:33 +00:00
|
|
|
data.$target[0].value = machine;
|
2014-01-27 21:41:32 +00:00
|
|
|
data.$preview.empty();
|
2013-05-02 07:51:45 +00:00
|
|
|
}
|
2014-01-27 21:41:32 +00:00
|
|
|
},
|
2021-12-18 06:12:16 +00:00
|
|
|
|
|
|
|
transliterate(source, settings) {
|
2014-11-28 13:38:17 +00:00
|
|
|
return $.get(Drupal.url('machine_name/transliterate'), {
|
2014-01-27 21:41:32 +00:00
|
|
|
text: source,
|
|
|
|
langcode: drupalSettings.langcode,
|
|
|
|
replace_pattern: settings.replace_pattern,
|
SA-CORE-2016-005 by larowlan, xjm, David_Rothstein, Dave Reid, Crell, cilefen, alexpott, mlhess, catch, pwolanin, YesCT, dawehner, quicksketch, Heine, znerol, charlotte.b, jnicola, ezraw
(cherry picked from commit c5da97f9e7e8f145541c5422e3ccde6a8b5ae680)
2016-11-16 18:45:27 +00:00
|
|
|
replace_token: settings.replace_token,
|
2014-01-27 21:41:32 +00:00
|
|
|
replace: settings.replace,
|
|
|
|
lowercase: true
|
|
|
|
});
|
|
|
|
}
|
2021-12-18 06:12:16 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
};
|
2017-05-19 22:12:53 +00:00
|
|
|
})(jQuery, Drupal, drupalSettings);
|