diff --git a/core/misc/drupal.js b/core/misc/drupal.js index ce13994..52fda7f 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -71,29 +71,33 @@ if (window.jQuery) { * 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 + * @param {DOMElement} context * An element to attach behaviors to. If none is given, the document element * is used. - * @param settings + * @param {Object} settings * An object containing settings for the current context. If none is given, * the global drupalSettings object is used. + * @param {Array} behaviorList + * An list of behavior names to run on the current context. */ - Drupal.attachBehaviors = function (context, settings) { + Drupal.attachBehaviors = function (context, settings, behaviorList) { context = context || document; settings = settings || drupalSettings; - var i, errors = [], behaviors = Drupal.behaviors; + behaviorList = behaviorList || Object.keys(Drupal.behaviors); + var errors = []; + var behaviors = Drupal.behaviors; // Execute all of them. - for (i in behaviors) { - if (behaviors.hasOwnProperty(i) && typeof behaviors[i].attach === 'function') { + behaviorList.forEach(function (behaviorName) { + if (behaviors.hasOwnProperty(behaviorName) && typeof behaviors[behaviorName].attach === 'function') { // Don't stop the execution of behaviors in case of an error. try { - behaviors[i].attach(context, settings); + behaviors[behaviorName].attach(context, settings); } catch (e) { - errors.push({ behavior: i, error: e }); + errors.push({ behavior: behaviorName, error: e }); } } - } + }); // Once all behaviors have been processed, inform the user about errors. if (errors.length) { throw new DrupalBehaviorError(errors, 'attach'); @@ -116,13 +120,13 @@ if (window.jQuery) { * behaviorName-processed, to ensure the behavior is detached only from * previously processed elements. * - * @param context + * @param {DOMElement} context * An element to detach behaviors from. If none is given, the document element * is used. - * @param settings + * @param {Object} settings * An object containing settings for the current context. If none given, the * global drupalSettings object is used. - * @param trigger + * @param {String} 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. @@ -140,26 +144,30 @@ if (window.jQuery) { * 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. + * @param {Array} behaviorList + * An list of behavior names to run on the current context. * * @see Drupal.attachBehaviors */ - Drupal.detachBehaviors = function (context, settings, trigger) { + Drupal.detachBehaviors = function (context, settings, trigger, behaviorList) { context = context || document; settings = settings || drupalSettings; trigger = trigger || 'unload'; - var i, errors = [], behaviors = Drupal.behaviors; + behaviorList = behaviorList || Object.keys(Drupal.behaviors); + var errors = []; + var behaviors = Drupal.behaviors; // Execute all of them. - for (i in behaviors) { - if (behaviors.hasOwnProperty(i) && typeof behaviors[i].detach === 'function') { + behaviorList.forEach(function (behaviorName) { + if (behaviors.hasOwnProperty(behaviorName) && typeof behaviors[behaviorName].detach === 'function') { // Don't stop the execution of behaviors in case of an error. try { - behaviors[i].detach(context, settings, trigger); + behaviors[behaviorName].detach(context, settings, trigger); } catch (e) { - errors.push({ behavior: i, error: e }); + errors.push({ behavior: behaviorName, error: e }); } } - } + }); // Once all behaviors have been processed, inform the user about errors. if (errors.length) { throw new DrupalBehaviorError(errors, 'detach:' + trigger);