machine-name.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. (function ($, Drupal, drupalSettings) {
  8. Drupal.behaviors.machineName = {
  9. attach: function attach(context, settings) {
  10. var self = this;
  11. var $context = $(context);
  12. var timeout = null;
  13. var xhr = null;
  14. function clickEditHandler(e) {
  15. var data = e.data;
  16. data.$wrapper.removeClass('visually-hidden');
  17. data.$target.trigger('focus');
  18. data.$suffix.hide();
  19. data.$source.off('.machineName');
  20. }
  21. function machineNameHandler(e) {
  22. var data = e.data;
  23. var options = data.options;
  24. var baseValue = $(e.target).val();
  25. var rx = new RegExp(options.replace_pattern, 'g');
  26. var expected = baseValue.toLowerCase().replace(rx, options.replace).substr(0, options.maxlength);
  27. if (xhr && xhr.readystate !== 4) {
  28. xhr.abort();
  29. xhr = null;
  30. }
  31. if (timeout) {
  32. clearTimeout(timeout);
  33. timeout = null;
  34. }
  35. if (baseValue.toLowerCase() !== expected) {
  36. timeout = setTimeout(function () {
  37. xhr = self.transliterate(baseValue, options).done(function (machine) {
  38. self.showMachineName(machine.substr(0, options.maxlength), data);
  39. });
  40. }, 300);
  41. } else {
  42. self.showMachineName(expected, data);
  43. }
  44. }
  45. Object.keys(settings.machineName).forEach(function (source_id) {
  46. var machine = '';
  47. var eventData = void 0;
  48. var options = settings.machineName[source_id];
  49. var $source = $context.find(source_id).addClass('machine-name-source').once('machine-name');
  50. var $target = $context.find(options.target).addClass('machine-name-target');
  51. var $suffix = $context.find(options.suffix);
  52. var $wrapper = $target.closest('.js-form-item');
  53. if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) {
  54. return;
  55. }
  56. if ($target.hasClass('error')) {
  57. return;
  58. }
  59. options.maxlength = $target.attr('maxlength');
  60. $wrapper.addClass('visually-hidden');
  61. if ($target.is(':disabled') || $target.val() !== '') {
  62. machine = $target.val();
  63. } else if ($source.val() !== '') {
  64. machine = self.transliterate($source.val(), options);
  65. }
  66. var $preview = $('<span class="machine-name-value">' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + '</span>');
  67. $suffix.empty();
  68. if (options.label) {
  69. $suffix.append('<span class="machine-name-label">' + options.label + ': </span>');
  70. }
  71. $suffix.append($preview);
  72. if ($target.is(':disabled')) {
  73. return;
  74. }
  75. eventData = {
  76. $source: $source,
  77. $target: $target,
  78. $suffix: $suffix,
  79. $wrapper: $wrapper,
  80. $preview: $preview,
  81. options: options
  82. };
  83. var $link = $('<span class="admin-link"><button type="button" class="link">' + Drupal.t('Edit') + '</button></span>').on('click', eventData, clickEditHandler);
  84. $suffix.append($link);
  85. if ($target.val() === '') {
  86. $source.on('formUpdated.machineName', eventData, machineNameHandler).trigger('formUpdated.machineName');
  87. }
  88. $target.on('invalid', eventData, clickEditHandler);
  89. });
  90. },
  91. showMachineName: function showMachineName(machine, data) {
  92. var settings = data.options;
  93. if (machine !== '') {
  94. if (machine !== settings.replace) {
  95. data.$target.val(machine);
  96. data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix);
  97. }
  98. data.$suffix.show();
  99. } else {
  100. data.$suffix.hide();
  101. data.$target.val(machine);
  102. data.$preview.empty();
  103. }
  104. },
  105. transliterate: function transliterate(source, settings) {
  106. return $.get(Drupal.url('machine_name/transliterate'), {
  107. text: source,
  108. langcode: drupalSettings.langcode,
  109. replace_pattern: settings.replace_pattern,
  110. replace_token: settings.replace_token,
  111. replace: settings.replace,
  112. lowercase: true
  113. });
  114. }
  115. };
  116. })(jQuery, Drupal, drupalSettings);