machine-name.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 (sourceId) {
  46. var machine = '';
  47. var options = settings.machineName[sourceId];
  48. var $source = $context.find(sourceId).addClass('machine-name-source').once('machine-name');
  49. var $target = $context.find(options.target).addClass('machine-name-target');
  50. var $suffix = $context.find(options.suffix);
  51. var $wrapper = $target.closest('.js-form-item');
  52. if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) {
  53. return;
  54. }
  55. if ($target.hasClass('error')) {
  56. return;
  57. }
  58. options.maxlength = $target.attr('maxlength');
  59. $wrapper.addClass('visually-hidden');
  60. if ($target.is(':disabled') || $target.val() !== '') {
  61. machine = $target.val();
  62. } else if ($source.val() !== '') {
  63. machine = self.transliterate($source.val(), options);
  64. }
  65. var $preview = $('<span class="machine-name-value">' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + '</span>');
  66. $suffix.empty();
  67. if (options.label) {
  68. $suffix.append('<span class="machine-name-label">' + options.label + ': </span>');
  69. }
  70. $suffix.append($preview);
  71. if ($target.is(':disabled')) {
  72. return;
  73. }
  74. var eventData = {
  75. $source: $source,
  76. $target: $target,
  77. $suffix: $suffix,
  78. $wrapper: $wrapper,
  79. $preview: $preview,
  80. options: options
  81. };
  82. var $link = $('<span class="admin-link"><button type="button" class="link">' + Drupal.t('Edit') + '</button></span>').on('click', eventData, clickEditHandler);
  83. $suffix.append($link);
  84. if ($target.val() === '') {
  85. $source.on('formUpdated.machineName', eventData, machineNameHandler).trigger('formUpdated.machineName');
  86. }
  87. $target.on('invalid', eventData, clickEditHandler);
  88. });
  89. },
  90. showMachineName: function showMachineName(machine, data) {
  91. var settings = data.options;
  92. if (machine !== '') {
  93. if (machine !== settings.replace) {
  94. data.$target.val(machine);
  95. data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix);
  96. }
  97. data.$suffix.show();
  98. } else {
  99. data.$suffix.hide();
  100. data.$target.val(machine);
  101. data.$preview.empty();
  102. }
  103. },
  104. transliterate: function transliterate(source, settings) {
  105. return $.get(Drupal.url('machine_name/transliterate'), {
  106. text: source,
  107. langcode: drupalSettings.langcode,
  108. replace_pattern: settings.replace_pattern,
  109. replace_token: settings.replace_token,
  110. replace: settings.replace,
  111. lowercase: true
  112. });
  113. }
  114. };
  115. })(jQuery, Drupal, drupalSettings);