Commit f1d90a0c authored by catch's avatar catch

Issue #1428524 by nod_, katbailey: Replace all $.each() with filtered for loop.

parent 2afd1ed4
var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} };
// Allow other JavaScript libraries to use $.
......@@ -49,12 +48,13 @@ jQuery.noConflict();
Drupal.attachBehaviors = function (context, settings) {
context = context || document;
settings = settings || Drupal.settings;
var i, behaviors = Drupal.behaviors;
// Execute all of them.
$.each(Drupal.behaviors, function () {
if ($.isFunction(this.attach)) {
this.attach(context, settings);
for (i in behaviors) {
if (behaviors.hasOwnProperty(i) && behaviors[i].attach) {
behaviors[i].attach(context, settings);
}
});
}
};
/**
......@@ -101,12 +101,13 @@ Drupal.detachBehaviors = function (context, settings, trigger) {
context = context || document;
settings = settings || Drupal.settings;
trigger = trigger || 'unload';
var i, behaviors = Drupal.behaviors;
// Execute all of them.
$.each(Drupal.behaviors, function () {
if ($.isFunction(this.detach)) {
this.detach(context, settings, trigger);
for (i in behaviors) {
if (behaviors.hasOwnProperty(i) && behaviors[i].detach) {
behaviors[i].detach(context, settings, trigger);
}
});
}
};
/**
......
......@@ -62,15 +62,17 @@ Drupal.behaviors.formUpdated = {
*/
Drupal.behaviors.fillUserInfoFromCookie = {
attach: function (context, settings) {
var userInfo = ['name', 'mail', 'homepage'];
$('form.user-info-from-cookie').once('user-info-from-cookie', function () {
var formContext = this;
$.each(['name', 'mail', 'homepage'], function () {
var $element = $('[name=' + this + ']', formContext);
var cookie = $.cookie('Drupal.visitor.' + this);
var i, il, $element, cookie;
for (i = 0, il = userInfo.length; i < il; i += 1) {
$element = $('[name=' + userInfo[i] + ']', formContext);
cookie = $.cookie('Drupal.visitor.' + userInfo[i]);
if ($element.length && cookie) {
$element.val(cookie);
}
});
}
});
}
};
......
......@@ -26,80 +26,84 @@ Drupal.behaviors.machineName = {
*/
attach: function (context, settings) {
var self = this;
$.each(settings.machineName, function (source_id, options) {
var $source = $(source_id, context).addClass('machine-name-source');
var $target = $(options.target, context).addClass('machine-name-target');
var $suffix = $(options.suffix, context);
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() != '') {
var machine = $target.val();
}
else {
var machine = self.transliterate($source.val(), options);
}
// Append the machine name preview to the source field.
var $preview = $('<span class="machine-name-value">' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + '</span>');
$suffix.empty();
if (options.label) {
$suffix.append(' ').append('<span class="machine-name-label">' + options.label + ':</span>');
}
$suffix.append(' ').append($preview);
for (var i in settings.machineName) {
if (settings.machineName.hasOwnProperty(i)) {
(function (source_id, options) {
var $source = $(source_id, context).addClass('machine-name-source');
var $target = $(options.target, context).addClass('machine-name-target');
var $suffix = $(options.suffix, context);
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() != '') {
var machine = $target.val();
}
else {
var machine = self.transliterate($source.val(), options);
}
// Append the machine name preview to the source field.
var $preview = $('<span class="machine-name-value">' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + '</span>');
$suffix.empty();
if (options.label) {
$suffix.append(' ').append('<span class="machine-name-label">' + options.label + ':</span>');
}
$suffix.append(' ').append($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 ($target.is(':disabled')) {
return;
}
// If it is editable, append an edit link.
var $link = $('<span class="admin-link"><a href="#">' + Drupal.t('Edit') + '</a></span>')
.click(function () {
$wrapper.show();
$target.focus();
$suffix.hide();
$source.unbind('.machineName');
return false;
});
$suffix.append(' ').append($link);
// If it is editable, append an edit link.
var $link = $('<span class="admin-link"><a href="#">' + Drupal.t('Edit') + '</a></span>')
.click(function () {
$wrapper.show();
$target.focus();
$suffix.hide();
$source.unbind('.machineName');
return false;
});
$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 ($target.val() == '') {
$source.bind('keyup.machineName change.machineName', function () {
machine = self.transliterate($(this).val(), options);
// Set the machine name to the transliterated value.
if (machine != '') {
if (machine != options.replace) {
$target.val(machine);
$preview.html(options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix);
}
$suffix.show();
}
else {
$suffix.hide();
$target.val(machine);
$preview.empty();
// 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', function () {
machine = self.transliterate($(this).val(), options);
// Set the machine name to the transliterated value.
if (machine != '') {
if (machine != options.replace) {
$target.val(machine);
$preview.html(options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix);
}
$suffix.show();
}
else {
$suffix.hide();
$target.val(machine);
$preview.empty();
}
});
// Initialize machine name preview.
$source.keyup();
}
});
// Initialize machine name preview.
$source.keyup();
}(i, settings.machineName[i]));
}
});
}
},
/**
......
......@@ -211,8 +211,8 @@ Drupal.behaviors.dashboard = {
$('#dashboard div.region').each(function () {
var region = $(this).parent().attr('id').replace(/-/g, '_');
var blocks = $(this).sortable('toArray');
$.each(blocks, function() {
order.push(region + '[]=' + this);
order = $.map(blocks, function (block) {
return region + '[]=' + block;
});
});
order = order.join('&');
......
......@@ -232,10 +232,13 @@ Drupal.fieldUIOverview = {
// Separate keys and values.
var rowNames = [];
var ajaxElements = [];
$.each(rows, function (rowName, ajaxElement) {
rowNames.push(rowName);
ajaxElements.push(ajaxElement);
});
var rowName;
for (rowName in rows) {
if (rows.hasOwnProperty(rowName)) {
rowNames.push(rowName);
ajaxElements.push(rows[rowName]);
}
}
if (rowNames.length) {
// Add a throbber next each of the ajaxElements.
......
/**
* @file
* Provides JavaScript additions to the managed file field type.
......@@ -15,18 +14,27 @@
*/
Drupal.behaviors.fileValidateAutoAttach = {
attach: function (context, settings) {
var validateExtension = Drupal.file.validateExtension;
var selector, elements;
if (settings.file && settings.file.elements) {
$.each(settings.file.elements, function(selector) {
var extensions = settings.file.elements[selector];
$(selector, context).bind('change', {extensions: extensions}, Drupal.file.validateExtension);
});
elements = settings.file.elements;
for (selector in elements) {
if (elements.hasOwnProperty(selector)) {
$(selector, context).bind('change', {extensions: elements[selector]}, validateExtension);
}
}
}
},
detach: function (context, settings) {
var validateExtension = Drupal.file.validateExtension;
var selector, elements;
if (settings.file && settings.file.elements) {
$.each(settings.file.elements, function(selector) {
$(selector, context).unbind('change', Drupal.file.validateExtension);
});
elements = settings.file.elements;
for (selector in elements) {
if (elements.hasOwnProperty(selector)) {
$(selector, context).unbind('change', validateExtension);
}
}
}
}
};
......
(function ($) {
/**
......@@ -85,9 +84,12 @@ Drupal.overlayChild.prototype = {};
* Attach child related behaviors to the iframe document.
*/
Drupal.overlayChild.attachBehaviors = function (context, settings) {
$.each(this.behaviors, function () {
this(context, settings);
});
var behavior, behaviors = this.behaviors;
for (behavior in behaviors) {
if (behaviors.hasOwnProperty(behavior)) {
behaviors[behavior](context, settings);
}
}
};
/**
......
......@@ -774,19 +774,24 @@ Drupal.overlay.fragmentizeLink = function (link, parentLocation) {
* corresponding to this region.
*/
Drupal.overlay.refreshRegions = function (data) {
$.each(data, function () {
var region_info = this;
$.each(region_info, function (regionClass) {
var regionName = region_info[regionClass];
var regionSelector = '.' + regionClass;
// Allow special behaviors to detach.
Drupal.detachBehaviors($(regionSelector));
$.get(Drupal.settings.basePath + Drupal.settings.overlay.ajaxCallback + '/' + regionName, function (newElement) {
$(regionSelector).replaceWith($(newElement));
Drupal.attachBehaviors($(regionSelector), Drupal.settings);
});
});
});
var region, region_info, regionClass, regionName, regionSelector;
for (region in data) {
if (data.hasOwnProperty(region)) {
region_info = data[region];
for (regionClass in region_info) {
if (region_info.hasOwnProperty(regionClass)) {
(function (regionName, regionSelector) {
var $region = $(regionSelector);
Drupal.detachBehaviors($region);
$.get(Drupal.settings.basePath + Drupal.settings.overlay.ajaxCallback + '/' + regionName, function (newElement) {
$region.replaceWith($(newElement));
Drupal.attachBehaviors($region, Drupal.settings);
});
}(region_info[regionClass], '.' + regionClass));
}
}
}
}
};
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment