machine-name.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. (function ($) {
  2. /**
  3. * Attach the machine-readable name form element behavior.
  4. */
  5. Drupal.behaviors.machineName = {
  6. /**
  7. * Attaches the behavior.
  8. *
  9. * @param settings.machineName
  10. * A list of elements to process, keyed by the HTML ID of the form element
  11. * containing the human-readable value. Each element is an object defining
  12. * the following properties:
  13. * - target: The HTML ID of the machine name form element.
  14. * - suffix: The HTML ID of a container to show the machine name preview in
  15. * (usually a field suffix after the human-readable name form element).
  16. * - label: The label to show for the machine name preview.
  17. * - replace_pattern: A regular expression (without modifiers) matching
  18. * disallowed characters in the machine name; e.g., '[^a-z0-9]+'.
  19. * - replace: A character to replace disallowed characters with; e.g., '_'
  20. * or '-'.
  21. * - standalone: Whether the preview should stay in its own element rather
  22. * than the suffix of the source element.
  23. * - field_prefix: The #field_prefix of the form element.
  24. * - field_suffix: The #field_suffix of the form element.
  25. */
  26. attach: function (context, settings) {
  27. var self = this;
  28. $.each(settings.machineName, function (source_id, options) {
  29. var $source = $(source_id, context).addClass('machine-name-source');
  30. var $target = $(options.target, context).addClass('machine-name-target');
  31. var $suffix = $(options.suffix, context);
  32. var $wrapper = $target.closest('.form-item');
  33. // All elements have to exist.
  34. if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) {
  35. return;
  36. }
  37. // Skip processing upon a form validation error on the machine name.
  38. if ($target.hasClass('error')) {
  39. return;
  40. }
  41. // Figure out the maximum length for the machine name.
  42. options.maxlength = $target.attr('maxlength');
  43. // Hide the form item container of the machine name form element.
  44. $wrapper.hide();
  45. // Determine the initial machine name value. Unless the machine name form
  46. // element is disabled or not empty, the initial default value is based on
  47. // the human-readable form element value.
  48. if ($target.is(':disabled') || $target.val() != '') {
  49. var machine = $target.val();
  50. }
  51. else {
  52. var machine = self.transliterate($source.val(), options);
  53. }
  54. // Append the machine name preview to the source field.
  55. var $preview = $('<span class="machine-name-value">' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + '</span>');
  56. $suffix.empty();
  57. if (options.label) {
  58. $suffix.append(' ').append('<span class="machine-name-label">' + options.label + ':</span>');
  59. }
  60. $suffix.append(' ').append($preview);
  61. // If the machine name cannot be edited, stop further processing.
  62. if ($target.is(':disabled')) {
  63. return;
  64. }
  65. // If it is editable, append an edit link.
  66. var $link = $('<span class="admin-link"><a href="#">' + Drupal.t('Edit') + '</a></span>')
  67. .click(function () {
  68. $wrapper.show();
  69. $target.focus();
  70. $suffix.hide();
  71. $source.unbind('.machineName');
  72. return false;
  73. });
  74. $suffix.append(' ').append($link);
  75. // Preview the machine name in realtime when the human-readable name
  76. // changes, but only if there is no machine name yet; i.e., only upon
  77. // initial creation, not when editing.
  78. if ($target.val() == '') {
  79. $source.bind('keyup.machineName change.machineName input.machineName', function () {
  80. machine = self.transliterate($(this).val(), options);
  81. // Set the machine name to the transliterated value.
  82. if (machine != '') {
  83. if (machine != options.replace) {
  84. $target.val(machine);
  85. $preview.html(options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix);
  86. }
  87. $suffix.show();
  88. }
  89. else {
  90. $suffix.hide();
  91. $target.val(machine);
  92. $preview.empty();
  93. }
  94. });
  95. // Initialize machine name preview.
  96. $source.keyup();
  97. }
  98. });
  99. },
  100. /**
  101. * Transliterate a human-readable name to a machine name.
  102. *
  103. * @param source
  104. * A string to transliterate.
  105. * @param settings
  106. * The machine name settings for the corresponding field, containing:
  107. * - replace_pattern: A regular expression (without modifiers) matching
  108. * disallowed characters in the machine name; e.g., '[^a-z0-9]+'.
  109. * - replace: A character to replace disallowed characters with; e.g., '_'
  110. * or '-'.
  111. * - maxlength: The maximum length of the machine name.
  112. *
  113. * @return
  114. * The transliterated source string.
  115. */
  116. transliterate: function (source, settings) {
  117. var rx = new RegExp(settings.replace_pattern, 'g');
  118. return source.toLowerCase().replace(rx, settings.replace).substr(0, settings.maxlength);
  119. }
  120. };
  121. })(jQuery);