diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-08-22 23:18:29 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-08-22 23:18:29 +0000 |
commit | a8cc3e30c374e64d1bff2fda233190fd05c9a0ee (patch) | |
tree | 900d20ce096da38fd2e2c7ca52427293d2568238 /modules/system/system.js | |
parent | 7ad9777c45a0b65cdad09c3c079d1961c447904a (diff) | |
download | brdo-a8cc3e30c374e64d1bff2fda233190fd05c9a0ee.tar.gz brdo-a8cc3e30c374e64d1bff2fda233190fd05c9a0ee.tar.bz2 |
#471018 by clemens.tolboom, sun, stBorchert, and mverbaar: Added generic pattern/js for hiding machine readable names (applied to menu and content types).
Diffstat (limited to 'modules/system/system.js')
-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); |