diff --git a/core/misc/machine-name.js b/core/misc/machine-name.js index a7ba0f7..51a1144 100644 --- a/core/misc/machine-name.js +++ b/core/misc/machine-name.js @@ -27,103 +27,193 @@ Drupal.behaviors.machineName = { * - field_suffix: The #field_suffix of the form element. */ attach: function (context, settings) { - var self = this; - var $context = $(context); - var source_id, options, machine, eventData; + + var sourceId; + + $.extend(Drupal.MachineName, {names : []}); - function clickEditHandler(e) { - var data = e.data; - e.preventDefault(); - data.$wrapper.show(); - data.$target.focus(); - data.$suffix.hide(); - data.$source.unbind('.machineName'); + for (sourceId in settings.machineName) { + Drupal.MachineName.names.push(new Drupal.MachineName(sourceId, settings, context)); } + } +}; - function machineNameHandler(e) { - var data = e.data; - machine = self.transliterate($(e.target).val(), data.options); - // Set the machine name to the transliterated value. - if (machine !== '') { - if (machine !== data.options.replace) { - data.$target.val(machine); - data.$preview.html(data.options.field_prefix + Drupal.checkPlain(machine) + data.options.field_suffix); - } - data.$suffix.show(); +Drupal.MachineName = function(sourceId, settings, context) { + + /** + * @var self + * javascript object containing the current Drupal.machineName for later use. + */ + var self = this; + + /** + * @var options + * An object of options used for retrieving machine name options + * @var machine + * A string containing the current machine name + */ + var options, machine; + + // Get options from the settings object using the sourceId + options = settings.machineName[sourceId]; + + try { + var $context = $(context); + + if (settings.machineName.hasOwnProperty(sourceId)) { + + // Get eventData object + var eventData = this.eventDataHandler(sourceId, options, $context); + + // Figure out the maximum length for the machine name. + options.maxlength = eventData.$target.attr('maxlength'); + // Hide the form item container of the machine name form element. + eventData.$wrapper.hide(); + // Determine the initial machine name value. Unless the machine name form + // element is disabled or not empty, the initial default value is based on + // the human-readable form element value. + if (eventData.$target.is(':disabled') || eventData.$target.val() !== '') { + machine = eventData.$target.val(); } else { - data.$suffix.hide(); - data.$target.val(machine); - data.$preview.empty(); + machine = self.transliterate(eventData.$source.val(), options); } - } - - for (source_id in settings.machineName) { - if (settings.machineName.hasOwnProperty(source_id)) { - options = settings.machineName[source_id]; - - var $source = $context.find(source_id).addClass('machine-name-source'); - var $target = $context.find(options.target).addClass('machine-name-target'); - var $suffix = $context.find(options.suffix); - var $wrapper = $target.closest('.form-item'); - // All elements have to exist. - if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) { - return; - } - // Skip processing upon a form validation error on the machine name. - if ($target.hasClass('error')) { - return; - } - // Figure out the maximum length for the machine name. - options.maxlength = $target.attr('maxlength'); - // Hide the form item container of the machine name form element. - $wrapper.hide(); - // Determine the initial machine name value. Unless the machine name form - // element is disabled or not empty, the initial default value is based on - // the human-readable form element value. - if ($target.is(':disabled') || $target.val() !== '') { - machine = $target.val(); - } - else { - machine = self.transliterate($source.val(), options); - } - // Append the machine name preview to the source field. - var $preview = $('' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + ''); - $suffix.empty(); - if (options.label) { - $suffix.append(' ').append('' + options.label + ':'); - } - $suffix.append(' ').append($preview); + // Append the machine name preview to the source field. + var $preview = $('' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + ''); + eventData.$preview = $preview; + eventData.$suffix.empty(); + if (options.label) { + eventData.$suffix.append(' ').append('' + options.label + ':'); + } + eventData.$suffix.append(' ').append(eventData.$preview); - // If the machine name cannot be edited, stop further processing. - if ($target.is(':disabled')) { - return; - } + // If the machine name cannot be edited, stop further processing. + if (eventData.$target.is(':disabled')) { + return; + } + + // If it is editable, append an edit link. + var $link = $('' + Drupal.t('Edit') + '').bind('click', eventData, self.clickEditHandler); + eventData.$link = $link; + + eventData.$suffix.append(' ').append(eventData.$link); - eventData = { - $source: $source, - $target: $target, - $suffix: $suffix, - $wrapper: $wrapper, - $preview: $preview, - options: options - }; - // If it is editable, append an edit link. - var $link = $('' + Drupal.t('Edit') + '').bind('click', eventData, clickEditHandler); - $suffix.append(' ').append($link); + // Preview the machine name in realtime when the human-readable name + // changes, but only if there is no machine name yet; i.e., only upon + // initial creation, not when editing. + if (eventData.$target.val() === '') { + eventData.$source.bind('keyup.machineName change.machineName', eventData, self.machineNameHandler) + // Initialize machine name preview. + .keyup(); + } + } + } catch (e) { + // Stop processing init if there are errors + // This supresses exceptions, thus failing silently as before refactoring + return; + } +}; - // Preview the machine name in realtime when the human-readable name - // changes, but only if there is no machine name yet; i.e., only upon - // initial creation, not when editing. - if ($target.val() === '') { - $source.bind('keyup.machineName change.machineName', eventData, machineNameHandler) - // Initialize machine name preview. - .keyup(); - } +Drupal.MachineName.prototype = { + + /** + * Handler for generating default eventData object. + * + * @param sourceId + * ID of source element + * + * @param $context + * Context element for finding elements + */ + eventDataHandler : function (sourceId, options, $context) { + var $source = $context.find(sourceId).addClass('machine-name-source'); + var $target = $context.find(options.target).addClass('machine-name-target'); + var $suffix = $context.find(options.suffix); + var $wrapper = $target.closest('.form-item'); + + // All elements have to exist. + if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) { + if (!$source.length) { + throw new Error('Source element is missing.'); + } + + if (!$target.length) { + throw new Error('Target element is missing.'); + } + + if (!$suffix.length) { + throw new Error('Suffix element is missing.'); + } + + if (!$wrapper.length) { + throw new Error('Wrapper element is missing.'); } } + // Skip processing upon a form validation error on the machine name. + if ($target.hasClass('error')) { + throw new Error('Machine name could not be validated.'); + } + + var eventData = { + $source: $source, + $target: $target, + $suffix: $suffix, + $wrapper: $wrapper, + options: options + }; + + return eventData; }, - + + /** + * Edit button action for when editing a transliterated machine name. + * + * @param e + * A jQuery event object. + * - data: the event data + * - $wrapper: Wrapping form item div element. + * - $target: Target input form item element. + * - $suffix: Mandetory suffix to be added to the transliterated machine name. + * - $source: Source input form item element of the machine name. + */ + clickEditHandler : function (e) { + var data = e.data; + e.preventDefault(); + data.$wrapper.show(); + data.$target.focus(); + data.$suffix.hide(); + data.$source.unbind('.machineName'); + }, + + /** + * Machine name handler for generating the machine name from a human readable name. + * @param e + * A jQuery event object. + * - data: the event data + * - $wrapper: Wrapping form item div element. + * - $target: Target input form item element. + * - $suffix: Mandetory suffix to be added to the transliterated machine name. + * - $source: Source input form item element of the machine name. + * - $preview: Preview html element containing the machine name. + */ + machineNameHandler : function (e) { + var data = e.data; + var machine = Drupal.MachineName.prototype.transliterate($(e.target).val(), data.options); + // Set the machine name to the transliterated value. + if (machine !== '') { + if (machine !== data.options.replace) { + data.$target.val(machine); + data.$preview.html(data.options.field_prefix + Drupal.checkPlain(machine) + data.options.field_suffix); + } + data.$suffix.show(); + } + else { + data.$suffix.hide(); + data.$target.val(machine); + data.$preview.empty(); + } + }, + /** * Transliterate a human-readable name to a machine name. * @@ -140,7 +230,7 @@ Drupal.behaviors.machineName = { * @return * The transliterated source string. */ - transliterate: function (source, settings) { + transliterate : function (source, settings) { var rx = new RegExp(settings.replace_pattern, 'g'); return source.toLowerCase().replace(rx, settings.replace).substr(0, settings.maxlength); }