diff options
Diffstat (limited to 'modules/system')
-rw-r--r-- | modules/system/system.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/modules/system/system.js b/modules/system/system.js index 479ecc6ea..7637109e4 100644 --- a/modules/system/system.js +++ b/modules/system/system.js @@ -169,5 +169,67 @@ Drupal.behaviors.pageCache = { }, }; +/** + * Attach the auto machine readable name behavior. + * + * Settings are expected to be an object of elements to process, where the key + * defines the source element in the form and the value is an object defining + * the following properties: + * - text: The label to display before the auto-generated value. + * - target: The target form element name. + * - searchPattern: A regular expression (without modifiers) matching disallowed + * characters in the machine readable name, f.e. '[^a-z0-9]+'. + * - replaceToken: A replacement string to replace disallowed characters, f.e. + * '-' or '_'. + * + * @see menu_edit_menu() + */ +Drupal.behaviors.machineReadableValue = { + attach: function () { + for (var value in Drupal.settings.machineReadableValue) { + var settings = Drupal.settings.machineReadableValue[value]; + + var searchPattern = new RegExp(settings.searchPattern, 'g'); + // Build selector for the source name entered by a user. + var source = '#edit-' + value; + var suffix = source + '-suffix'; + // Build selector for the machine readable name. + var target = '#edit-' + settings.target; + // Build selector for the wrapper element around the target field. + var wrapper = '.' + settings.target + '-wrapper'; + + // Do not process the element if we got an error or the given name and the + // machine readable name are identical or the machine readable name is + // empty. + if (!$(target).hasClass('error') && ($(target).val() == $(source).val().toLowerCase().replace(searchPattern, settings.replaceToken) || $(target).val() == '')) { + // Hide wrapper element. + $(wrapper).hide(); + // Bind keyup event to source element. + $(source).keyup(function () { + var machine = $(this).val().toLowerCase().replace(searchPattern, settings.replaceToken); + if (machine != '_' && machine != '') { + // Set machine readable name to the user entered value. + $(target).val(machine); + // Append the machine readable name and a link to edit it to the source field. + $(suffix).empty().append(' ' + settings.text + ': ' + machine + ' [').append($('<a href="#">' + Drupal.t('Edit') + '</a>').click(function () { + $(wrapper).show(); + $(target).focus(); + $(suffix).hide(); + $(source).unbind('keyup'); + return false; + })).append(']'); + } + else { + $(target).val(machine); + $(suffix).text(''); + } + }); + // Call keyup event on source element. + $(source).keyup(); + } + } + } +}; + })(jQuery); |