From e6e29ac1b0d6780241ced3d5ebcb0558e219e468 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 31 Aug 2009 05:51:08 +0000 Subject: =?UTF-8?q?-=20Patch=20#444344=20by=20kkaefer,=20sun,=20Rob=20Loac?= =?UTF-8?q?h:=20this=20change=20introduces=20a=20jQuery=20.once()=20method?= =?UTF-8?q?=20which=20streamlines=20the=20way=20behavior=20functions=20wor?= =?UTF-8?q?k.=20Previously,=20we=20had=20to=20manually=20ensure=20that=20a?= =?UTF-8?q?n=20element=20is=20only=20initialized=20once.=20Usually,=20this?= =?UTF-8?q?=20happens=20by=20adding=20classes=20and=20selecting=20only=20t?= =?UTF-8?q?hose=20elements=20which=20do=20not=20have=20that=20class.=20How?= =?UTF-8?q?ever,=20this=20process=20can=20be=20separated=20out=20into=20a?= =?UTF-8?q?=20jQuery=20=E2=80=98filtering=E2=80=99=20function=20which=20do?= =?UTF-8?q?es=20all=20the=20grunt=20work.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/system/system.js | 32 +++++++++++++------------------- modules/system/system.module | 10 ++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) (limited to 'modules/system') 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', -- cgit v1.2.3