diff --git a/core/misc/ajax.js b/core/misc/ajax.js index c35241c..c944c06 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -17,8 +17,7 @@ Drupal.ajax = Drupal.ajax || {}; /** * Attaches the Ajax behavior to each Ajax form element. */ -Drupal.behaviors.AJAX = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { // Load all Ajax behaviors specified in the settings. for (var base in settings.ajax) { var element_settings = settings.ajax[base]; @@ -65,8 +64,7 @@ Drupal.behaviors.AJAX = { var base = $(this).attr('id'); Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); }); - } -}; +}); /** * Ajax object. @@ -282,7 +280,7 @@ Drupal.ajax.prototype.beforeSerialize = function (element, options) { // isn't called, but don't rely on that: explicitly check this.form. if (this.form) { var settings = this.settings || Drupal.settings; - Drupal.detachBehaviors(this.form, settings, 'serialize'); + $(this.form).trigger('detach', settings, 'serialize'); } // Prevent duplicate HTML ids in the returned markup. @@ -402,7 +400,7 @@ Drupal.ajax.prototype.success = function (response, status) { // to be reattached. if (this.form) { var settings = this.settings || Drupal.settings; - Drupal.attachBehaviors(this.form, settings); + $(this.form).trigger('attach', settings); } Drupal.unfreezeHeight(); @@ -458,7 +456,7 @@ Drupal.ajax.prototype.error = function (response, uri) { // Reattach behaviors, if they were detached in beforeSerialize(). if (this.form) { var settings = response.settings || this.settings || Drupal.settings; - Drupal.attachBehaviors(this.form, settings); + $(this.form).trigger('attach', settings); } }; @@ -506,7 +504,7 @@ Drupal.ajax.prototype.commands = { case 'empty': case 'remove': var settings = response.settings || ajax.settings || Drupal.settings; - Drupal.detachBehaviors(wrapper, settings); + $(wrapper).trigger('detach', settings); } // Add the new content to the page. @@ -534,7 +532,7 @@ Drupal.ajax.prototype.commands = { if (new_content.parents('html').length > 0) { // Apply any settings from the returned JSON if available. var settings = response.settings || ajax.settings || Drupal.settings; - Drupal.attachBehaviors(new_content, settings); + $(new_content).trigger('attach', settings); } }, @@ -543,7 +541,7 @@ Drupal.ajax.prototype.commands = { */ remove: function (ajax, response, status) { var settings = response.settings || ajax.settings || Drupal.settings; - Drupal.detachBehaviors($(response.selector), settings); + $(response.selector).trigger('detach', settings); $(response.selector).remove(); }, diff --git a/core/misc/authorize.js b/core/misc/authorize.js index d522a5b..5174228 100644 --- a/core/misc/authorize.js +++ b/core/misc/authorize.js @@ -7,8 +7,7 @@ (function ($) { -Drupal.behaviors.authorizeFileTransferForm = { - attach: function(context) { +$(document).on('attach', function (e) { $('#edit-connection-settings-authorize-filetransfer-default').change(function() { $('.filetransfer').hide().filter('.filetransfer-' + $(this).val()).show(); }); @@ -21,7 +20,6 @@ Drupal.behaviors.authorizeFileTransferForm = { // Hides the submit button for non-js users. $('#edit-submit-connection').hide(); $('#edit-submit-process').show(); - } -}; +}); })(jQuery); diff --git a/core/misc/autocomplete.js b/core/misc/autocomplete.js index a4f6335..3ed7f4a 100644 --- a/core/misc/autocomplete.js +++ b/core/misc/autocomplete.js @@ -3,10 +3,9 @@ /** * Attaches the autocomplete behavior to all required fields. */ -Drupal.behaviors.autocomplete = { - attach: function (context, settings) { +$(document).on('attach', function (e) { var acdb = []; - $(context).find('input.autocomplete').once('autocomplete', function () { + $(e.target).find('input.autocomplete').once('autocomplete', function () { var uri = this.value; if (!acdb[uri]) { acdb[uri] = new Drupal.ACDB(uri); @@ -22,8 +21,7 @@ Drupal.behaviors.autocomplete = { ); new Drupal.jsAC($input, acdb[uri]); }); - } -}; +}); /** * Prevents the form from submitting if the suggestions popup is open diff --git a/core/misc/batch.js b/core/misc/batch.js index 6d28b69..5338919 100644 --- a/core/misc/batch.js +++ b/core/misc/batch.js @@ -3,9 +3,8 @@ /** * Attaches the batch behavior to progress bars. */ -Drupal.behaviors.batch = { - attach: function (context, settings) { - $(context).find('#progress').once('batch', function () { +$(document).on('attach', function (e, settings) { + $(e.target).find('#progress').once('batch', function () { var holder = $(this); // Success: redirect to the summary. @@ -26,7 +25,6 @@ Drupal.behaviors.batch = { holder.append(progress.element); progress.startMonitoring(settings.batch.uri + '&op=do', 10); }); - } -}; +}); })(jQuery); diff --git a/core/misc/collapse.js b/core/misc/collapse.js index 28281a1..1c2286c 100644 --- a/core/misc/collapse.js +++ b/core/misc/collapse.js @@ -53,9 +53,8 @@ Drupal.collapseScrollIntoView = function (node) { } }; -Drupal.behaviors.collapse = { - attach: function (context, settings) { - $(context).find('fieldset.collapsible').once('collapse', function () { +$(document).on('attach', function (e) { + $(e.target).find('fieldset.collapsible').once('collapse', function () { var $fieldset = $(this); // Expand fieldset if there are errors inside, or if it contains an // element that is targeted by the uri fragment identifier. @@ -97,7 +96,6 @@ Drupal.behaviors.collapse = { $legend.append(summary); }); - } -}; +}); })(jQuery); diff --git a/core/misc/drupal.js b/core/misc/drupal.js index c7917b0..756b9d9 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -6,109 +6,13 @@ jQuery.noConflict(); (function ($) { /** - * Attach all registered behaviors to a page element. + * To initialize your scripts, bind the 'ready' and 'attach' event on the document. * - * Behaviors are event-triggered actions that attach to page elements, enhancing - * default non-JavaScript UIs. Behaviors are registered in the Drupal.behaviors - * object using the method 'attach' and optionally also 'detach' as follows: - * @code - * Drupal.behaviors.behaviorName = { - * attach: function (context, settings) { - * ... - * }, - * detach: function (context, settings, trigger) { - * ... - * } - * }; - * @endcode - * - * Drupal.attachBehaviors is added below to the jQuery ready event and so - * runs on initial page load. Developers implementing AHAH/Ajax in their - * solutions should also call this function after new page content has been - * loaded, feeding in an element to be processed, in order to attach all - * behaviors to the new content. - * - * Behaviors should use - * @code - * $(selector).once('behavior-name', function () { - * ... - * }); - * @endcode - * to ensure the behavior is attached only once to a given element. (Doing so - * enables the reprocessing of given elements, which may be needed on occasion - * despite the ability to limit behavior attachment to a particular element.) - * - * @param context - * An element to attach behaviors to. If none is given, the document element - * is used. - * @param settings - * An object containing settings for the current context. If none given, the - * global Drupal.settings object is used. */ -Drupal.attachBehaviors = function (context, settings) { - context = context || document; - settings = settings || Drupal.settings; - var i, behaviors = Drupal.behaviors; - // Execute all of them. - for (i in behaviors) { - if (behaviors.hasOwnProperty(i) && typeof behaviors[i].attach === 'function') { - behaviors[i].attach(context, settings); - } - } -}; /** - * Detach registered behaviors from a page element. - * - * Developers implementing AHAH/Ajax in their solutions should call this - * function before page content is about to be removed, feeding in an element - * to be processed, in order to allow special behaviors to detach from the - * content. - * - * Such implementations should look for the class name that was added in their - * corresponding Drupal.behaviors.behaviorName.attach implementation, i.e. - * behaviorName-processed, to ensure the behavior is detached only from - * previously processed elements. - * - * @param context - * An element to detach behaviors from. If none is given, the document element - * is used. - * @param settings - * An object containing settings for the current context. If none given, the - * global Drupal.settings object is used. - * @param trigger - * A string containing what's causing the behaviors to be detached. The - * possible triggers are: - * - unload: (default) The context element is being removed from the DOM. - * - move: The element is about to be moved within the DOM (for example, - * during a tabledrag row swap). After the move is completed, - * Drupal.attachBehaviors() is called, so that the behavior can undo - * whatever it did in response to the move. Many behaviors won't need to - * do anything simply in response to the element being moved, but because - * IFRAME elements reload their "src" when being moved within the DOM, - * behaviors bound to IFRAME elements (like WYSIWYG editors) may need to - * take some action. - * - serialize: When an Ajax form is submitted, this is called with the - * form as the context. This provides every behavior within the form an - * opportunity to ensure that the field elements have correct content - * in them before the form is serialized. The canonical use-case is so - * that WYSIWYG editors can update the hidden textarea to which they are - * bound. - * - * @see Drupal.attachBehaviors + * To clean up your scripts, bind the 'detach' event on the document. */ -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. - for (i in behaviors) { - if (behaviors.hasOwnProperty(i) && typeof behaviors[i].detach === 'function' ) { - behaviors[i].detach(context, settings, trigger); - } - } -}; /** * Encode special characters in a plain-text string for display as HTML. @@ -373,16 +277,14 @@ $('html').addClass('js'); // 'js enabled' cookie. document.cookie = 'has_js=1; path=/'; -//Attach all behaviors. $(function () { - Drupal.attachBehaviors(document, Drupal.settings); + $(document).trigger('attach', Drupal.settings); }); /** * The default themes. */ Drupal.theme.prototype = { - /** * Formats text for emphasized display in a placeholder inside a sentence. * diff --git a/core/misc/form.js b/core/misc/form.js index 4dc5b8c..4005081 100644 --- a/core/misc/form.js +++ b/core/misc/form.js @@ -41,11 +41,10 @@ $.fn.drupalSetSummary = function (callback) { /** * Sends a 'formUpdated' event each time a form element is modified. */ -Drupal.behaviors.formUpdated = { - attach: function (context) { +$(document).on('attach', function (e) { // These events are namespaced so that we can remove them later. var events = 'change.formUpdated click.formUpdated blur.formUpdated keyup.formUpdated'; - $(context) + $(e.target) // Since context could be an input element itself, it's added back to // the jQuery object and filtered again. .find(':input').andSelf().filter(':input') @@ -54,14 +53,12 @@ Drupal.behaviors.formUpdated = { .unbind(events).bind(events, function () { $(this).trigger('formUpdated'); }); - } -}; +}); /** * Prepopulate form fields with information from the visitor cookie. */ -Drupal.behaviors.fillUserInfoFromCookie = { - attach: function (context, settings) { +$(document).on('attach', function (e) { var userInfo = ['name', 'mail', 'homepage']; $('form.user-info-from-cookie').once('user-info-from-cookie', function () { var formContext = this; @@ -75,7 +72,6 @@ Drupal.behaviors.fillUserInfoFromCookie = { } } }); - } -}; +}); })(jQuery); diff --git a/core/misc/machine-name.js b/core/misc/machine-name.js index ffe774f..e611cc4 100644 --- a/core/misc/machine-name.js +++ b/core/misc/machine-name.js @@ -3,7 +3,7 @@ /** * Attach the machine-readable name form element behavior. */ -Drupal.behaviors.machineName = { +var machineName = { /** * Attaches the behavior. * @@ -24,9 +24,9 @@ Drupal.behaviors.machineName = { * - field_prefix: The #field_prefix of the form element. * - field_suffix: The #field_suffix of the form element. */ - attach: function (context, settings) { + attach: function (e, settings) { var self = this; - var $context = $(context); + var $context = $(e.target); for (var i in settings.machineName) { if (settings.machineName.hasOwnProperty(i)) { (function (source_id, options) { @@ -129,4 +129,7 @@ Drupal.behaviors.machineName = { } }; +$(document).on('attach', machineName.attach); +Drupal.behaviors.machineName = machineName; + })(jQuery); diff --git a/core/misc/states.js b/core/misc/states.js index 30b63ba..21c8243 100644 --- a/core/misc/states.js +++ b/core/misc/states.js @@ -14,8 +14,7 @@ var states = Drupal.states = { /** * Attaches the states. */ -Drupal.behaviors.states = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { for (var selector in settings.states) { for (var state in settings.states[selector]) { new states.Dependent({ @@ -30,8 +29,7 @@ Drupal.behaviors.states = { while (states.postponed.length) { (states.postponed.shift())(); } - } -}; +}); /** * Object representing an element that depends on other elements. diff --git a/core/misc/tabledrag.js b/core/misc/tabledrag.js index 16800e0..8d7d319 100644 --- a/core/misc/tabledrag.js +++ b/core/misc/tabledrag.js @@ -11,17 +11,15 @@ * overriding the .onDrag, .onDrop, .row.onSwap, and .row.onIndent methods. * See blocks.js for an example of adding additional functionality to tableDrag. */ -Drupal.behaviors.tableDrag = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { for (var base in settings.tableDrag) { - $(context).find('#' + base).once('tabledrag', function () { + $(e.target).find('#' + base).once('tabledrag', function () { // Create the new tableDrag instance. Save in the Drupal variable // to allow other scripts access to the object. Drupal.tableDrag[base] = new Drupal.tableDrag(this, settings.tableDrag[base]); }); } - } -}; +}); /** * Constructor for the tableDrag object. Provides table and field manipulation. @@ -1003,9 +1001,10 @@ Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) { * DOM element what will be swapped with the row group. */ Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) { - Drupal.detachBehaviors(this.group, Drupal.settings, 'move'); + var $group = $(this.group); + $group.trigger('detach', Drupal.settings, 'move'); $(row)[position](this.group); - Drupal.attachBehaviors(this.group, Drupal.settings); + $group.trigger('attach', Drupal.settings); this.changed = true; this.onSwap(row); }; diff --git a/core/misc/tableheader.js b/core/misc/tableheader.js index aa1c8bb..1e755e0 100644 --- a/core/misc/tableheader.js +++ b/core/misc/tableheader.js @@ -3,17 +3,15 @@ /** * Attaches sticky table headers. */ -Drupal.behaviors.tableHeader = { - attach: function (context, settings) { +$(document).on('attach', function (e) { if (!$.support.fixedPosition) { return; } - $(context).find('table.sticky-enabled').once('tableheader', function () { + $(e.target).find('table.sticky-enabled').once('tableheader', function () { $(this).data("drupal-tableheader", new Drupal.tableHeader(this)); }); - } -}; +}); /** * Constructor for the tableHeader object. Provides sticky table headers. diff --git a/core/misc/tableselect.js b/core/misc/tableselect.js index b7011e0..4448a9e 100644 --- a/core/misc/tableselect.js +++ b/core/misc/tableselect.js @@ -1,11 +1,9 @@ (function ($) { -Drupal.behaviors.tableSelect = { - attach: function (context, settings) { +$(document).on('attach', function (e) { // Select the inner-most table in case of nested tables. - $(context).find('th.select-all').closest('table').once('table-select', Drupal.tableSelect); - } -}; + $(e.target).find('th.select-all').closest('table').once('table-select', Drupal.tableSelect); +}); Drupal.tableSelect = function () { // Do not add a "Select all" checkbox if there are no rows with checkboxes in the table diff --git a/core/misc/timezone.js b/core/misc/timezone.js index 62b7d4b..14219db 100644 --- a/core/misc/timezone.js +++ b/core/misc/timezone.js @@ -3,9 +3,8 @@ /** * Set the client's system time zone as default values of form fields. */ -Drupal.behaviors.setTimezone = { - attach: function (context, settings) { - $(context).find('select.timezone-detect').once('timezone', function () { +$(document).on('attach', function (e) { + $(e.target).find('select.timezone-detect').once('timezone', function () { var dateString = Date(); // In some client environments, date strings include a time zone // abbreviation, between 3 and 5 letters enclosed in parentheses, @@ -60,7 +59,6 @@ Drupal.behaviors.setTimezone = { } }); }); - } -}; +}); })(jQuery); diff --git a/core/misc/vertical-tabs.js b/core/misc/vertical-tabs.js index 65e5683..67163db 100644 --- a/core/misc/vertical-tabs.js +++ b/core/misc/vertical-tabs.js @@ -11,9 +11,8 @@ * which is called every time the user performs an update to a form * element inside the tab pane. */ -Drupal.behaviors.verticalTabs = { - attach: function (context) { - $(context).find('.vertical-tabs-panes').once('vertical-tabs', function () { +$(document).on('attach', function (e) { + $(e.target).find('.vertical-tabs-panes').once('vertical-tabs', function () { var $this = $(this); var focusID = $this.find(':hidden.vertical-tabs-active-tab').val(); var tab_focus; @@ -63,8 +62,7 @@ Drupal.behaviors.verticalTabs = { tab_focus.data('verticalTab').focus(); } }); - } -}; +}); /** * The vertical tab object represents a single tab within a tab group. diff --git a/core/modules/block/block.js b/core/modules/block/block.js index dabb570..563641c 100644 --- a/core/modules/block/block.js +++ b/core/modules/block/block.js @@ -3,8 +3,7 @@ /** * Provide the summary information for the block settings vertical tabs. */ -Drupal.behaviors.blockSettingsSummary = { - attach: function (context) { +$(document).on('attach', function (e) { // The drupalSetSummary method required for this behavior is not available // on the Blocks administration page, so we need to make sure this // behavior is processed only if drupalSetSummary is defined. @@ -12,7 +11,7 @@ Drupal.behaviors.blockSettingsSummary = { return; } - var $context = $(context); + var $context = $(e.target); $context.find('fieldset#edit-path').drupalSetSummary(function (context) { if (!$(context).find('textarea[name="pages"]').val()) { return Drupal.t('Not restricted'); @@ -65,8 +64,7 @@ Drupal.behaviors.blockSettingsSummary = { return $radio.next('label').text(); } }); - } -}; +}); /** * Move a block in the blocks table from one region to another via select list. @@ -74,13 +72,12 @@ Drupal.behaviors.blockSettingsSummary = { * This behavior is dependent on the tableDrag behavior, since it uses the * objects initialized in that behavior to update the row. */ -Drupal.behaviors.blockDrag = { - attach: function (context, settings) { +$(document).on('attach', function (e) { // tableDrag is required and we should be on the blocks admin page. if (typeof Drupal.tableDrag == 'undefined' || typeof Drupal.tableDrag.blocks == 'undefined') { return; } - + var context = e.target; var table = $('table#blocks'); var tableDrag = Drupal.tableDrag.blocks; // Get the blocks tableDrag object. @@ -161,7 +158,6 @@ Drupal.behaviors.blockDrag = { } }); }; - } -}; +}); })(jQuery); diff --git a/core/modules/book/book.js b/core/modules/book/book.js index e6ab4fa..6d632c1 100644 --- a/core/modules/book/book.js +++ b/core/modules/book/book.js @@ -1,8 +1,7 @@ (function ($) { -Drupal.behaviors.bookFieldsetSummaries = { - attach: function (context) { - $(context).find('fieldset.book-outline-form').drupalSetSummary(function (context) { +$(document).on('attach', function (e) { + $(e.target).find('fieldset.book-outline-form').drupalSetSummary(function (context) { var $select = $('.form-item-book-bid select'); var val = $select.val(); @@ -16,7 +15,6 @@ Drupal.behaviors.bookFieldsetSummaries = { return Drupal.checkPlain($select.find(':selected').text()); } }); - } -}; +}); })(jQuery); diff --git a/core/modules/color/color.js b/core/modules/color/color.js index d833a4b..a6baff2 100644 --- a/core/modules/color/color.js +++ b/core/modules/color/color.js @@ -5,9 +5,9 @@ (function ($) { -Drupal.behaviors.color = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { var i, j, colors, field_name; + var context = e.target; // This behavior attaches by ID, so is only valid once on a page. var form = $(context).find('#system-theme-settings .color-form').once('color'); if (form.length == 0) { @@ -236,7 +236,6 @@ Drupal.behaviors.color = { // Render preview. preview(); - } -}; +}); })(jQuery); diff --git a/core/modules/comment/comment-node-form.js b/core/modules/comment/comment-node-form.js index 51ea492..a0c75b7 100644 --- a/core/modules/comment/comment-node-form.js +++ b/core/modules/comment/comment-node-form.js @@ -5,9 +5,8 @@ (function ($) { -Drupal.behaviors.commentFieldsetSummaries = { - attach: function (context) { - var $context = $(context); +$(document).on('attach', function (e) { + var $context = $(e.target); $context.find('fieldset.comment-node-settings-form').drupalSetSummary(function (context) { return Drupal.checkPlain($(context).find('.form-item-comment input:checked').next('label').text()); }); @@ -32,7 +31,6 @@ Drupal.behaviors.commentFieldsetSummaries = { return Drupal.checkPlain(vals.join(', ')); }); - } -}; +}); })(jQuery); diff --git a/core/modules/contextual/contextual.js b/core/modules/contextual/contextual.js index 1c3305f..843c9e9 100644 --- a/core/modules/contextual/contextual.js +++ b/core/modules/contextual/contextual.js @@ -10,9 +10,8 @@ Drupal.contextualLinks = Drupal.contextualLinks || {}; /** * Attaches outline behavior for regions associated with contextual links. */ -Drupal.behaviors.contextualLinks = { - attach: function (context) { - $(context).find('div.contextual-links-wrapper').once('contextual-links', function () { +$(document).on('attach', function (e) { + $(e.target).find('div.contextual-links-wrapper').once('contextual-links', function () { var $wrapper = $(this); var $region = $wrapper.closest('.contextual-region'); var $links = $wrapper.find('ul'); @@ -37,8 +36,7 @@ Drupal.behaviors.contextualLinks = { // Prepend the trigger. $wrapper.prepend($trigger); }); - } -}; +}); /** * Disables outline for the region contextual links are associated with. diff --git a/core/modules/dashboard/dashboard.js b/core/modules/dashboard/dashboard.js index 39d87c2..eac059e 100644 --- a/core/modules/dashboard/dashboard.js +++ b/core/modules/dashboard/dashboard.js @@ -8,9 +8,9 @@ /** * Implements Drupal.behaviors for the Dashboard module. */ -Drupal.behaviors.dashboard = { - attach: function (context, settings) { - $(context).find('#dashboard').once(function () { +var dashboard = { + attach: function (e) { + $(e.target).find('#dashboard').once(function () { $(this).prepend('
'); $('.customize .action-links a', this).click(Drupal.behaviors.dashboard.enterCustomizeMode); }); @@ -182,7 +182,7 @@ Drupal.behaviors.dashboard = { item.find('div.content').html(Drupal.settings.dashboard.emptyBlockText); } - Drupal.attachBehaviors(item); + $(item).trigger('attach'); }, 'html' ); @@ -221,4 +221,7 @@ Drupal.behaviors.dashboard = { } }; +$(document).on('attach', dashboard.attach); +Drupal.behaviors.dashboard = dashboard; + })(jQuery); diff --git a/core/modules/field/modules/text/text.js b/core/modules/field/modules/text/text.js index 8527355..643057c 100644 --- a/core/modules/field/modules/text/text.js +++ b/core/modules/field/modules/text/text.js @@ -3,9 +3,8 @@ /** * Auto-hide summary textarea if empty and show hide and unhide links. */ -Drupal.behaviors.textSummary = { - attach: function (context, settings) { - $(context).find('.text-summary').once('text-summary', function () { +$(document).on('attach', function (e) { + $(e.target).find('.text-summary').once('text-summary', function () { var $widget = $(this).closest('div.field-type-text-with-summary'); var $summaries = $widget.find('div.text-summary-wrapper'); @@ -42,7 +41,6 @@ Drupal.behaviors.textSummary = { return; }); }); - } -}; +}); })(jQuery); diff --git a/core/modules/field_ui/field_ui.js b/core/modules/field_ui/field_ui.js index 6de5c15..d1295b1 100644 --- a/core/modules/field_ui/field_ui.js +++ b/core/modules/field_ui/field_ui.js @@ -5,13 +5,11 @@ (function($) { -Drupal.behaviors.fieldUIFieldOverview = { - attach: function (context, settings) { - $(context).find('table#field-overview').once('field-overview', function () { - Drupal.fieldUIFieldOverview.attachUpdateSelects(this, settings); +$(document).on('attach', function (e) { + $(e.target).find('table#field-overview').once('field-overview', function () { + Drupal.fieldUIFieldOverview.attachUpdateSelects(this, Drupal.settings); + }); }); - } -}; Drupal.fieldUIFieldOverview = { /** @@ -105,13 +103,11 @@ jQuery.fn.fieldUIPopulateOptions = function (options, selected) { }); }; -Drupal.behaviors.fieldUIDisplayOverview = { - attach: function (context, settings) { - $(context).find('table#field-display-overview').once('field-display-overview', function() { - Drupal.fieldUIOverview.attach(this, settings.fieldUIRowsData, Drupal.fieldUIDisplayOverview); +$(document).on('attach', function (e) { + $(e.target).find('table#field-display-overview').once('field-display-overview', function() { + Drupal.fieldUIOverview.attach(this, Drupal.settings.fieldUIRowsData, Drupal.fieldUIDisplayOverview); + }); }); - } -}; Drupal.fieldUIOverview = { /** diff --git a/core/modules/file/file.js b/core/modules/file/file.js index a934afe..cd7f52b 100644 --- a/core/modules/file/file.js +++ b/core/modules/file/file.js @@ -12,9 +12,8 @@ /** * Attach behaviors to managed file element upload fields. */ -Drupal.behaviors.fileValidateAutoAttach = { - attach: function (context, settings) { - var $context = $(context); +$(document).on('attach', function (e, settings) { + var $context = $(e.target); var validateExtension = Drupal.file.validateExtension; var selector, elements; if (settings.file && settings.file.elements) { @@ -25,9 +24,10 @@ Drupal.behaviors.fileValidateAutoAttach = { } } } - }, - detach: function (context, settings) { - var $context = $(context); +}); + +$(document).on('detach', function (e, settings, trigger) { + var $context = $(e.target); var validateExtension = Drupal.file.validateExtension; var selector, elements; if (settings.file && settings.file.elements) { @@ -38,36 +38,31 @@ Drupal.behaviors.fileValidateAutoAttach = { } } } - } -}; +}); /** * Attach behaviors to the file upload and remove buttons. */ -Drupal.behaviors.fileButtons = { - attach: function (context) { - var $context = $(context); +$(document).on('attach', function (e) { + var $context = $(e.target); $context.find('input.form-submit').bind('mousedown', Drupal.file.disableFields); $context.find('div.form-managed-file input.form-submit').bind('mousedown', Drupal.file.progressBar); - }, - detach: function (context) { - var $context = $(context); +}); +$(document).on('detach', function (e) { + var $context = $(e.target); $context.find('input.form-submit').unbind('mousedown', Drupal.file.disableFields); $context.find('div.form-managed-file input.form-submit').unbind('mousedown', Drupal.file.progressBar); - } -}; +}); /** * Attach behaviors to links within managed file elements. */ -Drupal.behaviors.filePreviewLinks = { - attach: function (context) { - $(context).find('div.form-managed-file .file a, .file-widget .file a').bind('click',Drupal.file.openInNewWindow); - }, - detach: function (context){ - $(context).find('div.form-managed-file .file a, .file-widget .file a').unbind('click', Drupal.file.openInNewWindow); - } -}; +$(document).on('attach', function (e) { + $(e.target).find('div.form-managed-file .file a, .file-widget .file a').bind('click',Drupal.file.openInNewWindow); +}); +$(document).on('detach', function (e){ + $(e.target).find('div.form-managed-file .file a, .file-widget .file a').unbind('click', Drupal.file.openInNewWindow); +}); /** * File upload utility functions. diff --git a/core/modules/filter/filter.admin.js b/core/modules/filter/filter.admin.js index 43cda26..1958750 100644 --- a/core/modules/filter/filter.admin.js +++ b/core/modules/filter/filter.admin.js @@ -1,8 +1,7 @@ (function ($) { -Drupal.behaviors.filterStatus = { - attach: function (context, settings) { - var $context = $(context); +$(document).on('attach', function (e, settings) { + var $context = $(e.target); $context.find('#filters-status-wrapper input.form-checkbox').once('filter-status', function () { var $checkbox = $(this); // Retrieve the tabledrag row belonging to this filter. @@ -39,7 +38,6 @@ Drupal.behaviors.filterStatus = { // Trigger our bound click handler to update elements to initial state. $checkbox.triggerHandler('click.filterUpdate'); }); - } -}; +}); })(jQuery); diff --git a/core/modules/filter/filter.js b/core/modules/filter/filter.js index db5f42a..f8cc15c 100644 --- a/core/modules/filter/filter.js +++ b/core/modules/filter/filter.js @@ -3,9 +3,8 @@ /** * Automatically display the guidelines of the selected text format. */ -Drupal.behaviors.filterGuidelines = { - attach: function (context) { - $(context).find('.filter-guidelines').once('filter-guidelines') +$(document).on('attach', function (e) { + $(e.target).find('.filter-guidelines').once('filter-guidelines') .find(':header').hide() .closest('.filter-wrapper').find('select.filter-list') .bind('change', function () { @@ -14,7 +13,6 @@ Drupal.behaviors.filterGuidelines = { .siblings('.filter-guidelines-' + this.value).show(); }) .change(); - } -}; +}); })(jQuery); diff --git a/core/modules/menu/menu.admin.js b/core/modules/menu/menu.admin.js index 4e5bf07..0b3552a 100644 --- a/core/modules/menu/menu.admin.js +++ b/core/modules/menu/menu.admin.js @@ -1,15 +1,13 @@ (function ($) { -Drupal.behaviors.menuChangeParentItems = { - attach: function (context, settings) { +$(document).on('attach', function (e) { $('fieldset#edit-menu input').each(function () { $(this).change(function () { // Update list of available parent menu items. Drupal.menu_update_parent_list(); }); }); - } -}; +}); /** * Function to set the options of the menu parent item dropdown. diff --git a/core/modules/menu/menu.js b/core/modules/menu/menu.js index f315e7a..1efc807 100644 --- a/core/modules/menu/menu.js +++ b/core/modules/menu/menu.js @@ -1,8 +1,7 @@ (function ($) { -Drupal.behaviors.menuFieldsetSummaries = { - attach: function (context) { - $(context).find('fieldset.menu-link-form').drupalSetSummary(function (context) { +$(document).on('attach', function (e) { + $(e.target).find('fieldset.menu-link-form').drupalSetSummary(function (context) { var $context = $(context); if ($context.find('.form-item-menu-enabled input').is(':checked')) { return Drupal.checkPlain($context.find('.form-item-menu-link-title input').val()); @@ -11,15 +10,13 @@ Drupal.behaviors.menuFieldsetSummaries = { return Drupal.t('Not in menu'); } }); - } -}; +}); /** * Automatically fill in a menu link title, if possible. */ -Drupal.behaviors.menuLinkAutomaticTitle = { - attach: function (context) { - var $context = $(context); +$(document).on('attach', function (e) { + var $context = $(e.target); $context.find('fieldset.menu-link-form').each(function () { var $this = $(this); // Try to find menu settings widget elements as well as a 'title' field in @@ -62,7 +59,6 @@ Drupal.behaviors.menuLinkAutomaticTitle = { } }); }); - } -}; +}); })(jQuery); diff --git a/core/modules/node/content_types.js b/core/modules/node/content_types.js index 43cfa37..831a02d 100644 --- a/core/modules/node/content_types.js +++ b/core/modules/node/content_types.js @@ -1,8 +1,7 @@ (function ($) { -Drupal.behaviors.contentTypes = { - attach: function (context) { - var $context = $(context); +$(document).on('attach', function (e) { + var $context = $(e.target); // Provide the vertical tab summaries. $context.find('fieldset#edit-submission').drupalSetSummary(function(context) { var vals = []; @@ -30,7 +29,6 @@ Drupal.behaviors.contentTypes = { } return vals.join(', '); }); - } -}; +}); })(jQuery); diff --git a/core/modules/node/node.js b/core/modules/node/node.js index 8990164..f46dc82 100644 --- a/core/modules/node/node.js +++ b/core/modules/node/node.js @@ -1,8 +1,7 @@ (function ($) { -Drupal.behaviors.nodeFieldsetSummaries = { - attach: function (context) { - var $context = $(context); +$(document).on('attach', function (e) { + var $context = $(e.target); $context.find('fieldset.node-form-revision-information').drupalSetSummary(function (context) { var $context = $(context); var revisionCheckbox = $context.find('.form-item-revision input'); @@ -40,7 +39,6 @@ Drupal.behaviors.nodeFieldsetSummaries = { } return vals.join(', '); }); - } -}; +}); })(jQuery); diff --git a/core/modules/openid/openid.js b/core/modules/openid/openid.js index 754efb1..22d526c 100644 --- a/core/modules/openid/openid.js +++ b/core/modules/openid/openid.js @@ -1,15 +1,14 @@ (function ($) { -Drupal.behaviors.openid = { - attach: function (context) { - var $context = $(context); +$(document).on('attach', function (e) { + var $context = $(e.target); var loginElements = $('.form-item-name, .form-item-pass, li.openid-link'); var openidElements = $('.form-item-openid-identifier, li.user-link'); var cookie = $.cookie('Drupal.visitor.openid_identifier'); // This behavior attaches by ID, so is only valid once on a page. if (cookie || location.hash == '#openid-login') { - $edit_openid_identifier = $('#edit-openid-identifier'); + var $edit_openid_identifier = $('#edit-openid-identifier'); if (cookie) { $edit_openid_identifier.val(cookie); } @@ -45,7 +44,6 @@ Drupal.behaviors.openid = { $('#edit-name')[0].focus(); return false; }); - } -}; +}); })(jQuery); diff --git a/core/modules/overlay/overlay-child.js b/core/modules/overlay/overlay-child.js index ff111d8..5fb83ca 100644 --- a/core/modules/overlay/overlay-child.js +++ b/core/modules/overlay/overlay-child.js @@ -3,8 +3,8 @@ /** * Attach the child dialog behavior to new content. */ -Drupal.behaviors.overlayChild = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { + var context = e.target; // Make sure this behavior is not processed more than once. if (this.processed) { return; @@ -16,7 +16,7 @@ Drupal.behaviors.overlayChild = { window.location = window.location.href.replace(/([?&]?)render=overlay&?/g, '$1').replace(/\?$/, ''); } - var settings = settings.overlayChild || {}; + settings = settings.overlayChild || {}; // If the entire parent window should be refreshed when the overlay is // closed, pass that information to the parent window. @@ -68,8 +68,7 @@ Drupal.behaviors.overlayChild = { $(this).removeClass('overlay-disable-message-focused') .find('a.element-focusable').addClass('element-invisible'); }); - } -}; +}); /** * Overlay object for child windows. diff --git a/core/modules/overlay/overlay-parent.js b/core/modules/overlay/overlay-parent.js index 19d2d64..a6868e0 100644 --- a/core/modules/overlay/overlay-parent.js +++ b/core/modules/overlay/overlay-parent.js @@ -3,10 +3,9 @@ /** * Open the overlay, or load content into it, when an admin link is clicked. */ -Drupal.behaviors.overlayParent = { - attach: function (context, settings) { +$(document).on('attach', function (e) { if (Drupal.overlay.isOpen) { - Drupal.overlay.makeDocumentUntabbable(context); + Drupal.overlay.makeDocumentUntabbable(e.target); } if (this.processed) { @@ -27,8 +26,7 @@ Drupal.behaviors.overlayParent = { // scripts to bind their own handlers to links and also to prevent // overlay's handling. .bind('click.drupal-overlay mouseup.drupal-overlay', $.proxy(Drupal.overlay, 'eventhandlerOverrideLink')); - } -}; +}); /** * Overlay object for parent windows. @@ -782,10 +780,10 @@ Drupal.overlay.refreshRegions = function (data) { if (region_info.hasOwnProperty(regionClass)) { (function (regionName, regionSelector) { var $region = $(regionSelector); - Drupal.detachBehaviors($region); + $region.trigger('detach'); $.get(Drupal.settings.basePath + Drupal.settings.overlay.ajaxCallback + '/' + regionName, function (newElement) { $region.replaceWith($(newElement)); - Drupal.attachBehaviors($region, Drupal.settings); + $region.trigger('attach', Drupal.settings); }); }(region_info[regionClass], '.' + regionClass)); } diff --git a/core/modules/path/path.js b/core/modules/path/path.js index b20378e..a685e83 100644 --- a/core/modules/path/path.js +++ b/core/modules/path/path.js @@ -4,16 +4,14 @@ */ (function ($) { -Drupal.behaviors.pathFieldsetSummaries = { - attach: function (context) { - $(context).find('fieldset.path-form').drupalSetSummary(function (context) { +$(document).on('attach', function (e) { + $(e.target).find('fieldset.path-form').drupalSetSummary(function (context) { var path = $('.form-item-path-alias input').val(); return path ? Drupal.t('Alias: @alias', { '@alias': path }) : Drupal.t('No alias'); }); - } -}; +}); })(jQuery); diff --git a/core/modules/shortcut/shortcut.admin.js b/core/modules/shortcut/shortcut.admin.js index 2647f34..f826a18 100644 --- a/core/modules/shortcut/shortcut.admin.js +++ b/core/modules/shortcut/shortcut.admin.js @@ -6,8 +6,7 @@ * This behavior is dependent on the tableDrag behavior, since it uses the * objects initialized in that behavior to update the row. */ -Drupal.behaviors.shortcutDrag = { - attach: function (context, settings) { +$(document).on('attach', function (e) { if (Drupal.tableDrag) { var table = $('table#shortcuts'), visibleLength = 0, @@ -97,20 +96,17 @@ Drupal.behaviors.shortcutDrag = { .removeClass('even').addClass('odd'); }; } - } -}; +}); /** * Make it so when you enter text into the "New set" textfield, the * corresponding radio button gets selected. */ -Drupal.behaviors.newSet = { - attach: function (context, settings) { +$(document).on('attach', function (e) { var selectDefault = function() { $(this).closest('form').find('.form-item-set .form-type-radio:last input').attr('checked', 'checked'); }; $('div.form-item-new input').focus(selectDefault).keyup(selectDefault); - } -}; +}); })(jQuery); diff --git a/core/modules/simpletest/simpletest.js b/core/modules/simpletest/simpletest.js index 2199fed..816b8e6 100644 --- a/core/modules/simpletest/simpletest.js +++ b/core/modules/simpletest/simpletest.js @@ -3,8 +3,7 @@ /** * Add the cool table collapsing on the testing overview page. */ -Drupal.behaviors.simpleTestMenuCollapse = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { var timeout = null; // Adds expand-collapse functionality. $('div.simpletest-image').once('simpletest-image', function () { @@ -52,15 +51,13 @@ Drupal.behaviors.simpleTestMenuCollapse = { }); }); - } -}; +}); /** * Select/deselect all the inner checkboxes when the outer checkboxes are * selected/deselected. */ -Drupal.behaviors.simpleTestSelectAll = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { $('td.simpletest-select-all').once('simpletest-select-all', function () { var testCheckboxes = settings.simpleTest['simpletest-test-group-' + $(this).attr('id')].testNames; var groupCheckbox = $(''); @@ -98,7 +95,6 @@ Drupal.behaviors.simpleTestSelectAll = { updateGroupCheckbox(); $(this).append(groupCheckbox); }); - } -}; +}); })(jQuery); diff --git a/core/modules/system/system.cron.js b/core/modules/system/system.cron.js index af17dab..994301b 100644 --- a/core/modules/system/system.cron.js +++ b/core/modules/system/system.cron.js @@ -3,8 +3,7 @@ /** * Checks to see if the cron should be automatically run. */ -Drupal.behaviors.cronCheck = { - attach: function(context, settings) { +$(document).on('attach', function(e, settings) { if (settings.cronCheck || false) { $('body').once('cron-check', function() { // Only execute the cron check if its the right time. @@ -13,7 +12,6 @@ Drupal.behaviors.cronCheck = { } }); } - } -}; +}); })(jQuery); diff --git a/core/modules/system/system.js b/core/modules/system/system.js index 98da1a6..47230f9 100644 --- a/core/modules/system/system.js +++ b/core/modules/system/system.js @@ -26,8 +26,7 @@ Drupal.hideEmailAdministratorCheckbox = function () { * This function is not used to verify whether or not clean URLs * are currently enabled. */ -Drupal.behaviors.cleanURLsSettingsCheck = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { // 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. @@ -43,8 +42,7 @@ Drupal.behaviors.cleanURLsSettingsCheck = { location = settings.basePath +"admin/config/search/clean-urls"; } }); - } -}; +}); /** * Internal function to check using Ajax if clean URLs can be enabled on the @@ -73,10 +71,9 @@ Drupal.cleanURLsInstallCheck = function () { * use the same value. In the installer this is used to populate the * administrator e-mail address with the same value as the site e-mail address. */ -Drupal.behaviors.copyFieldValue = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { for (var sourceId in settings.copyFieldValue) { - $('#' + sourceId, context).once('copy-field-values').bind('blur', function () { + $('#' + sourceId, e.target).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. @@ -88,14 +85,13 @@ Drupal.behaviors.copyFieldValue = { } }); } - } -}; +}); /** * Show/hide custom format sections on the regional settings page. */ -Drupal.behaviors.dateTime = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { + var context = e.target; for (var fieldName in settings.dateTime) { if (settings.dateTime.hasOwnProperty(fieldName)) { (function (fieldSettings, fieldName) { @@ -113,16 +109,14 @@ Drupal.behaviors.dateTime = { })(settings.dateTime[fieldName], fieldName); } } - } -}; +}); /** * Show/hide settings for page caching depending on whether page caching is * enabled or not. */ -Drupal.behaviors.pageCache = { - attach: function (context, settings) { - var $context = $(context); + $(document).on('attach', function (e) { + var $context = $(e.target); $context.find('#edit-cache-0').change(function () { $('#page-compression-wrapper').hide(); $('#cache-error').hide(); @@ -135,7 +129,6 @@ Drupal.behaviors.pageCache = { $('#page-compression-wrapper').show(); $('#cache-error').show(); }); - } -}; +}); })(jQuery); diff --git a/core/modules/taxonomy/taxonomy.js b/core/modules/taxonomy/taxonomy.js index 035dc29..9d700c4 100644 --- a/core/modules/taxonomy/taxonomy.js +++ b/core/modules/taxonomy/taxonomy.js @@ -6,9 +6,8 @@ * This behavior is dependent on the tableDrag behavior, since it uses the * objects initialized in that behavior to update the row. */ -Drupal.behaviors.termDrag = { - attach: function (context, settings) { - var table = $(context).find('#taxonomy'); +$(document).on('attach', function (e, settings) { + var table = $(e.target).find('#taxonomy'); var tableDrag = Drupal.tableDrag.taxonomy; // Get the blocks tableDrag object. var rows = table.find('tr').length; @@ -34,7 +33,6 @@ Drupal.behaviors.termDrag = { $(table[0].tBodies[0].rows[rows - settings.taxonomy.forwardStep - 1]).addClass('taxonomy-term-divider-bottom'); } }; - } -}; +}); })(jQuery); diff --git a/core/modules/toolbar/toolbar.js b/core/modules/toolbar/toolbar.js index d345284..88bab78 100644 --- a/core/modules/toolbar/toolbar.js +++ b/core/modules/toolbar/toolbar.js @@ -5,9 +5,8 @@ Drupal.toolbar = Drupal.toolbar || {}; /** * Attach toggling behavior and notify the overlay of the toolbar. */ -Drupal.behaviors.toolbar = { - attach: function(context) { - var $context = $(context); +$(document).on('attach', function(e) { + var $context = $(e.target); // Set the initial state of the toolbar. $context.find('#toolbar').once('toolbar', Drupal.toolbar.init); @@ -18,8 +17,7 @@ Drupal.behaviors.toolbar = { $(window).triggerHandler('resize'); return false; }); - } -}; +}); /** * Retrieve last saved cookie settings and set up the initial toolbar state. diff --git a/core/modules/user/user.js b/core/modules/user/user.js index 59a178e..ba57764 100644 --- a/core/modules/user/user.js +++ b/core/modules/user/user.js @@ -4,10 +4,9 @@ * Attach handlers to evaluate the strength of any password fields and to check * that its confirmation is correct. */ -Drupal.behaviors.password = { - attach: function (context, settings) { +$(document).on('attach', function (e, settings) { var translate = settings.password; - $(context).find('input.password-field').once('password', function () { + $(e.target).find('input.password-field').once('password', function () { var passwordInput = $(this); var innerWrapper = $(this).parent(); var outerWrapper = $(this).parent().parent(); @@ -86,8 +85,7 @@ Drupal.behaviors.password = { passwordInput.keyup(passwordCheck).focus(passwordCheck).blur(passwordCheck); confirmInput.keyup(passwordCheckMatch).blur(passwordCheckMatch); }); - } -}; +}); /** * Evaluate the strength of a user's password. @@ -182,12 +180,11 @@ Drupal.evaluatePasswordStrength = function (password, translate) { * Field instance settings screen: force the 'Display on registration form' * checkbox checked whenever 'Required' is checked. */ -Drupal.behaviors.fieldUserRegistration = { - attach: function (context, settings) { +$(document).on('attach', function (e) { var $checkbox = $('form#field-ui-field-edit-form input#edit-instance-settings-user-register-form'); if ($checkbox.length) { - $(context).find('input#edit-instance-required').once('user-register-form-checkbox', function () { + $(e.target).find('input#edit-instance-required').once('user-register-form-checkbox', function () { $(this).bind('change', function (e) { if ($(this).attr('checked')) { $checkbox.attr('checked', true); @@ -196,7 +193,6 @@ Drupal.behaviors.fieldUserRegistration = { }); } - } -}; +}); })(jQuery); diff --git a/core/modules/user/user.permissions.js b/core/modules/user/user.permissions.js index 896c936..24d4d66 100644 --- a/core/modules/user/user.permissions.js +++ b/core/modules/user/user.permissions.js @@ -3,8 +3,8 @@ /** * Shows checked and disabled checkboxes for inherited permissions. */ -Drupal.behaviors.permissions = { - attach: function (context) { +var permissions = { + attach: function (e) { var self = this; $('table#permissions').once('permissions', function () { // On a site with many roles and permissions, this behavior initially has @@ -66,4 +66,7 @@ Drupal.behaviors.permissions = { } }; +$(document).on('attach', permissions.attach); +Drupal.behaviors.permissions = permissions; + })(jQuery);