summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-08-31 05:51:08 +0000
committerDries Buytaert <dries@buytaert.net>2009-08-31 05:51:08 +0000
commite6e29ac1b0d6780241ced3d5ebcb0558e219e468 (patch)
treea6bdbf6b69ab26e54295d8063e7d1c839cd42f3a /modules
parent41dca3c4e0ec3d355977735f74af2e49ea0eedd7 (diff)
downloadbrdo-e6e29ac1b0d6780241ced3d5ebcb0558e219e468.tar.gz
brdo-e6e29ac1b0d6780241ced3d5ebcb0558e219e468.tar.bz2
- Patch #444344 by kkaefer, sun, Rob Loach: this change introduces a jQuery .once() method which streamlines the way behavior functions work. Previously, we had to manually ensure that an element is only initialized once. Usually, this happens by adding classes and selecting only those elements which do not have that class. However, this process can be separated out into a jQuery ‘filtering’ function which does all the grunt work.
Diffstat (limited to 'modules')
-rw-r--r--modules/block/block.js3
-rw-r--r--modules/color/color.js8
-rw-r--r--modules/comment/comment.js4
-rw-r--r--modules/system/system.js32
-rw-r--r--modules/system/system.module10
-rw-r--r--modules/toolbar/toolbar.js14
-rw-r--r--modules/user/user.js4
7 files changed, 34 insertions, 41 deletions
diff --git a/modules/block/block.js b/modules/block/block.js
index 34c04d246..e83f6ea04 100644
--- a/modules/block/block.js
+++ b/modules/block/block.js
@@ -51,7 +51,7 @@ Drupal.behaviors.blockDrag = {
};
// Add the behavior to each region select list.
- $('select.block-region-select:not(.blockregionselect-processed)', context).each(function () {
+ $('select.block-region-select', context).once('block-region-select', function () {
$(this).change(function (event) {
// Make our new row and select field.
var row = $(this).parents('tr:first');
@@ -82,7 +82,6 @@ Drupal.behaviors.blockDrag = {
// Remove focus from selectbox.
select.get(0).blur();
});
- $(this).addClass('blockregionselect-processed');
});
var checkEmptyRegions = function (table, rowObject) {
diff --git a/modules/color/color.js b/modules/color/color.js
index c286c40a9..a834f11ed 100644
--- a/modules/color/color.js
+++ b/modules/color/color.js
@@ -4,10 +4,10 @@
Drupal.behaviors.color = {
attach: function (context, settings) {
// This behavior attaches by ID, so is only valid once on a page.
- if ($('#color_scheme_form .color-form.color-processed').size()) {
+ var form = $('#system-theme-settings .color-form', context).once('color');
+ if (form.length == 0) {
return;
}
- var form = $('#system-theme-settings .color-form', context);
var inputs = [];
var hooks = [];
var locks = [];
@@ -24,9 +24,7 @@ Drupal.behaviors.color = {
}
// Build a preview.
- $('#preview:not(.color-processed)')
- .append('<div id="gradient"></div>')
- .addClass('color-processed');
+ $('#preview').once('color').append('<div id="gradient"></div>');
var gradient = $('#preview #gradient');
var h = parseInt(gradient.css('height')) / 10;
for (i = 0; i < h; ++i) {
diff --git a/modules/comment/comment.js b/modules/comment/comment.js
index bdd1c3b95..58f00dc6b 100644
--- a/modules/comment/comment.js
+++ b/modules/comment/comment.js
@@ -6,9 +6,7 @@ Drupal.behaviors.comment = {
$.each(['name', 'homepage', 'mail'], function () {
var cookie = Drupal.comment.getCookie('comment_info_' + this);
if (cookie) {
- $('#comment-form input[name=' + this + ']:not(.comment-processed)', context)
- .val(cookie)
- .addClass('comment-processed');
+ $('#comment-form input[name=' + this + ']', context).once('comment').val(cookie);
}
});
}
diff --git a/modules/system/system.js b/modules/system/system.js
index 11aa8b4b8..4da80811d 100644
--- a/modules/system/system.js
+++ b/modules/system/system.js
@@ -32,7 +32,7 @@ Drupal.behaviors.cleanURLsSettingsCheck = {
// This behavior attaches by ID, so is only valid once on a page.
// Also skip if we are on an install page, as Drupal.cleanURLsInstallCheck will handle
// the processing.
- if (!($('#edit-clean-url').size()) || $('.clean-url-processed, #edit-clean-url.install').size()) {
+ if (!($('#edit-clean-url').length) || $('#edit-clean-url.install').once('clean-url').length) {
return;
}
var url = settings.basePath + 'admin/config/search/clean-urls/check';
@@ -44,7 +44,6 @@ Drupal.behaviors.cleanURLsSettingsCheck = {
location = settings.basePath +"admin/config/search/clean-urls";
}
});
- $('#clean-url').addClass('clean-url-processed');
}
};
@@ -68,7 +67,6 @@ Drupal.cleanURLsInstallCheck = function () {
$('#edit-clean-url').attr('value', 1);
}
});
- $('#edit-clean-url').addClass('clean-url-processed');
};
/**
@@ -79,21 +77,17 @@ Drupal.cleanURLsInstallCheck = function () {
Drupal.behaviors.copyFieldValue = {
attach: function (context, settings) {
for (var sourceId in settings.copyFieldValue) {
- // Get the list of target fields.
- targetIds = settings.copyFieldValue[sourceId];
- if (!$('#'+ sourceId + '.copy-field-values-processed', context).size()) {
+ $('#' + sourceId, context).once('copy-field-values').bind('blur', function () {
+ // Get the list of target fields.
+ var targetIds = settings.copyFieldValue[sourceId];
// Add the behavior to update target fields on blur of the primary field.
- sourceField = $('#' + sourceId);
- sourceField.bind('blur', function () {
- for (var delta in targetIds) {
- var targetField = $('#'+ targetIds[delta]);
- if (targetField.val() == '') {
- targetField.val(this.value);
- }
+ for (var delta in targetIds) {
+ var targetField = $('#' + targetIds[delta]);
+ if (targetField.val() == '') {
+ targetField.val(this.value);
}
- });
- sourceField.addClass('copy-field-values-processed');
- }
+ }
+ });
}
}
};
@@ -104,12 +98,12 @@ Drupal.behaviors.copyFieldValue = {
Drupal.behaviors.dateTime = {
attach: function (context, settings) {
// Show/hide custom format depending on the select's value.
- $('select.date-format:not(.date-time-processed)', context).change(function () {
- $(this).addClass('date-time-processed').parents('div.date-container').children('div.custom-container')[$(this).val() == 'custom' ? 'show' : 'hide']();
+ $('select.date-format', context).once('date-time').change(function () {
+ $(this).parents('div.date-container').children('div.custom-container')[$(this).val() == 'custom' ? 'show' : 'hide']();
});
// Attach keyup handler to custom format inputs.
- $('input.custom-format:not(.date-time-processed)', context).addClass('date-time-processed').keyup(function () {
+ $('input.custom-format', context).once('date-time').keyup(function () {
var input = $(this);
var url = settings.dateTime.lookup + (settings.dateTime.lookup.match(/\?q=/) ? '&format=' : '?format=') + encodeURIComponent(input.val());
$.getJSON(url, function (data) {
diff --git a/modules/system/system.module b/modules/system/system.module
index b975c8597..d9a29a0cb 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -933,6 +933,16 @@ function system_library() {
),
);
+ // jQuery Once.
+ $libraries['once'] = array(
+ 'title' => 'jQuery Once',
+ 'website' => 'http://plugins.jquery.com/project/once',
+ 'version' => '1.2',
+ 'js' => array(
+ 'misc/jquery.once.js' => array('weight' => JS_LIBRARY - 19),
+ ),
+ );
+
// jQuery Form Plugin.
$libraries['form'] = array(
'title' => 'jQuery Form Plugin',
diff --git a/modules/toolbar/toolbar.js b/modules/toolbar/toolbar.js
index 7b804182f..c19010039 100644
--- a/modules/toolbar/toolbar.js
+++ b/modules/toolbar/toolbar.js
@@ -8,18 +8,12 @@ Drupal.behaviors.admin = {
attach: function() {
// Set the intial state of the toolbar.
- $('#toolbar:not(.processed)').each(function() {
- Drupal.admin.toolbar.init();
- $(this).addClass('processed');
- });
+ $('#toolbar', context).once('toolbar', Drupal.admin.toolbar.init);
// Toggling of admin shortcuts visibility.
- $('#toolbar span.toggle:not(.processed)').each(function() {
- $(this).click(function() {
- Drupal.admin.toolbar.toggle();
- return false;
- });
- $(this).addClass('processed');
+ $('#toolbar span.toggle', context).once('toolbar-toggle').click(function() {
+ Drupal.admin.toolbar.toggle();
+ return false;
});
}
};
diff --git a/modules/user/user.js b/modules/user/user.js
index 9d75911fa..d382375ab 100644
--- a/modules/user/user.js
+++ b/modules/user/user.js
@@ -8,8 +8,8 @@
Drupal.behaviors.password = {
attach: function (context, settings) {
var translate = settings.password;
- $('input.password-field:not(.password-processed)', context).each(function () {
- var passwordInput = $(this).addClass('password-processed');
+ $('input.password-field', context).once('password', function () {
+ var passwordInput = $(this);
var innerWrapper = $(this).parent();
var outerWrapper = $(this).parent().parent();