<commit_before>(function($){
  'use strict';

  /* Plugin constants. */
  var ENTER_KEY_CODE = 13;
  var SPACE_KEY_CODE = 32;
  var AVAILABLE_ROLE_BUTTON_TAGS = ['a', 'div', 'figure', 'p', 'pre',
                                    'blockquote', 'img', 'ins', 'del',
                                    'output', 'span', 'summary'];

  /* Plugin variables. */
  var pluginName  = 'contentToggle';
  var instances = {};
  var defaultOptions = {};
  var $global = $(document);
  var isIthing = navigator.userAgent.match(/iPad|iPhone/i);
  var sanitize = /[^a-z0-9_-]/gi;
  var uid = 0;
  var gid = 0;

  /* Plugin default options. */
  defaultOptions = {
    defaultState: null,
    globalClose: false,
    independent: false,
    beforeCallback: null,
    triggerSelector: '.js-contentToggle__trigger',
    triggerSelectorContext: true,
    contentSelector: '.js-contentToggle__content',
    contentSelectorContext: true,
    elementClass: 'is-open',
    triggerClass: 'is-active',
    toggleProperties: ['height'],
    toggleOptions: {
      duration: 0
    }
  };

  /**
   * Plugin Constructor.
   *
   * @param {Node|jQuery} element
   *   Main DOM element.
   * @param {string} selector
   *   Element initial selector.
   * @param {object} options
   *   Instance specific options.
   */
  function Plugin(element, selector, options) {
    var data;

    // Merge specific and default options.
    this.options = {
      group: selector
    };
    $.extend(this.options, defaultOptions, options);

    // Initialize data.
    this.$element = (element instanceof $)? element: $(element);
    this.uid = ++uid;

    // Data initialization.
    this.setup();

    // Empty object initialization.
    data = this.$element.data(pluginName);
    if (!instances[this.options.group]) {
      instances[this.options.group] = {};
    }
    if (!data) {
      data = {};
    }

    // Check if instance has not been already initialized.
    if (!data[this.options.group]) {
      // Save the new instance.
      instances[this.options.group][this.uid] = this;
      data[this.options.group] = this;
      this.$element.data(pluginName, data);

      // Plugin initialization.
      this.bind();
      this.init();
    }
  }

  /**
   * Setup plugin.
   * e.g. Get DOM elements, setup data...
   */
  Plugin.prototype.setup = function() {
    this.setupDataOptions();

    // Sanitize group name.
    if (this.options.group) {
      this.options.group = this.options.group.toString().replace(sanitize, '');
    }

    // Parse JSON options.
    if (typeof this.options.toggleProperties == 'string') {
      this.options.toggleProperties = JSON.parse(this.options.toggleProperties);
    }
    if (typeof this.options.toggleOptions == 'string') {
      this.options.toggleOptions = JSON.parse(this.options.toggleOptions);
    }

    // Get trigger elements.
    if (this.options.triggerSelectorContext) {
      this.$triggers = $(this.options.triggerSelector, this.$element);
    } else {
      this.$triggers = $(this.options.triggerSelector);
    }
    if (this.$triggers.length === 0) {
      this.$triggers = this.$element;
    }

    // Get content elements.
    if (this.options.contentSelectorContext) {
      this.$contents = $(this.options.contentSelector, this.$element);
    } else {
      this.$contents = $(this.options.contentSelector);
    }

    // Get callback.
    if (typeof this.options.beforeCallback == 'string' &&
        window[this.options.beforeCallback] &&
        typeof window[this.options.beforeCallback] == 'function') {
      this.options.beforeCallback = window[this.options.beforeCallback].bind(this);
    } else if (typeof this.options.beforeCallback == 'function') {
      this.options.beforeCallback = this.options.beforeCallback.bind(this);
    }
  };

  /**
   * Setup plugin specific data.
   * e.g. Get DOM elements, setup data...
   */
  Plugin.prototype.setupDataOptions = function() {
    $.each(
      this.$element.data(),
      function(index, value){
        if (index in defaultOptions) {
          this.options[index] = value;
        }
      }.bind(this)
    );
  };

  /**
   * Bind events.
   */
  Plugin.prototype.bind = function() {
    var namespaces = '.' + pluginName + '.' + this.options.group;
    var eventName = (isIthing && this.options.globalClose)? 'touchstart': 'click';
    var $all = this.$element.add(this.$triggers).add(this.$contents);

    // Bind custom events on all elements.
    $all.on('destroy' + namespaces, this.destroy.bind(this));
    $all.on('toggle' + namespaces, $.proxy(this.toggle, this, null));
    $all.on('close' + namespaces, $.proxy(this.toggle, this, false));
    $all.on('open' + namespaces, $.proxy(this.toggle, this, true));
    $all.on('isOpen' + namespaces, function(){
      return this.isOpen;
    }.bind(this));

    // Bind native events on triggers.
    this.$triggers.on(eventName + namespaces, function(event){
      event.preventDefault();
      if (!event.originalEvent.mozInputSource ||
          event.originalEvent.mozInputSource !== MouseEvent.MOZ_SOURCE_KEYBOARD) {
        this.toggle(null, event);
      }
    }.bind(this));
    this.$triggers.on('keydown' + namespaces, function(event){
      if (event.keyCode == ENTER_KEY_CODE || event.keyCode == SPACE_KEY_CODE) {
        event.preventDefault();
        this.toggle(null, event);
      }
    }.bind(this));

    // Bind native events on contents (avoid triggers click event).
    this.$contents.on(eventName + namespaces, function(event){
      event.stopPropagation();
    });
  };

  /**<commit_msg>Regroup key codes into an object.<commit_after>(function($){
  'use strict';

  /* Plugin constants. */
  var TRIGGER_KEY_CODES = {
    enter: 13,
    space: 32
  };
  var AVAILABLE_ROLE_BUTTON_TAGS = ['a', 'div', 'figure', 'p', 'pre',
                                    'blockquote', 'img', 'ins', 'del',
                                    'output', 'span', 'summary'];

  /* Plugin variables. */
  var pluginName  = 'contentToggle';
  var instances = {};
  var defaultOptions = {};
  var $global = $(document);
  var isIthing = navigator.userAgent.match(/iPad|iPhone/i);
  var sanitize = /[^a-z0-9_-]/gi;
  var uid = 0;
  var gid = 0;

  /* Plugin default options. */
  defaultOptions = {
    defaultState: null,
    globalClose: false,
    independent: false,
    beforeCallback: null,
    triggerSelector: '.js-contentToggle__trigger',
    triggerSelectorContext: true,
    contentSelector: '.js-contentToggle__content',
    contentSelectorContext: true,
    elementClass: 'is-open',
    triggerClass: 'is-active',
    toggleProperties: ['height'],
    toggleOptions: {
      duration: 0
    }
  };

  /**
   * Plugin Constructor.
   *
   * @param {Node|jQuery} element
   *   Main DOM element.
   * @param {string} selector
   *   Element initial selector.
   * @param {object} options
   *   Instance specific options.
   */
  function Plugin(element, selector, options) {
    var data;

    // Merge specific and default options.
    this.options = {
      group: selector
    };
    $.extend(this.options, defaultOptions, options);

    // Initialize data.
    this.$element = (element instanceof $)? element: $(element);
    this.uid = ++uid;

    // Data initialization.
    this.setup();

    // Empty object initialization.
    data = this.$element.data(pluginName);
    if (!instances[this.options.group]) {
      instances[this.options.group] = {};
    }
    if (!data) {
      data = {};
    }

    // Check if instance has not been already initialized.
    if (!data[this.options.group]) {
      // Save the new instance.
      instances[this.options.group][this.uid] = this;
      data[this.options.group] = this;
      this.$element.data(pluginName, data);

      // Plugin initialization.
      this.bind();
      this.init();
    }
  }

  /**
   * Setup plugin.
   * e.g. Get DOM elements, setup data...
   */
  Plugin.prototype.setup = function() {
    this.setupDataOptions();

    // Sanitize group name.
    if (this.options.group) {
      this.options.group = this.options.group.toString().replace(sanitize, '');
    }

    // Parse JSON options.
    if (typeof this.options.toggleProperties == 'string') {
      this.options.toggleProperties = JSON.parse(this.options.toggleProperties);
    }
    if (typeof this.options.toggleOptions == 'string') {
      this.options.toggleOptions = JSON.parse(this.options.toggleOptions);
    }

    // Get trigger elements.
    if (this.options.triggerSelectorContext) {
      this.$triggers = $(this.options.triggerSelector, this.$element);
    } else {
      this.$triggers = $(this.options.triggerSelector);
    }
    if (this.$triggers.length === 0) {
      this.$triggers = this.$element;
    }

    // Get content elements.
    if (this.options.contentSelectorContext) {
      this.$contents = $(this.options.contentSelector, this.$element);
    } else {
      this.$contents = $(this.options.contentSelector);
    }

    // Get callback.
    if (typeof this.options.beforeCallback == 'string' &&
        window[this.options.beforeCallback] &&
        typeof window[this.options.beforeCallback] == 'function') {
      this.options.beforeCallback = window[this.options.beforeCallback].bind(this);
    } else if (typeof this.options.beforeCallback == 'function') {
      this.options.beforeCallback = this.options.beforeCallback.bind(this);
    }
  };

  /**
   * Setup plugin specific data.
   * e.g. Get DOM elements, setup data...
   */
  Plugin.prototype.setupDataOptions = function() {
    $.each(
      this.$element.data(),
      function(index, value){
        if (index in defaultOptions) {
          this.options[index] = value;
        }
      }.bind(this)
    );
  };

  /**
   * Bind events.
   */
  Plugin.prototype.bind = function() {
    var namespaces = '.' + pluginName + '.' + this.options.group;
    var eventName = (isIthing && this.options.globalClose)? 'touchstart': 'click';
    var $all = this.$element.add(this.$triggers).add(this.$contents);

    // Bind custom events on all elements.
    $all.on('destroy' + namespaces, this.destroy.bind(this));
    $all.on('toggle' + namespaces, $.proxy(this.toggle, this, null));
    $all.on('close' + namespaces, $.proxy(this.toggle, this, false));
    $all.on('open' + namespaces, $.proxy(this.toggle, this, true));
    $all.on('isOpen' + namespaces, function(){
      return this.isOpen;
    }.bind(this));

    // Bind native events on triggers.
    this.$triggers.on(eventName + namespaces, function(event){
      event.preventDefault();
      if (!event.originalEvent.mozInputSource ||
          event.originalEvent.mozInputSource !== MouseEvent.MOZ_SOURCE_KEYBOARD) {
        this.toggle(null, event);
      }
    }.bind(this));
    this.$triggers.on('keydown' + namespaces, function(event){
      if (TRIGGER_KEY_CODES.indexOf(event.keyCode) !== -1) {
        event.preventDefault();
        this.toggle(null, event);
      }
    }.bind(this));

    // Bind native events on contents (avoid triggers click event).
    this.$contents.on(eventName + namespaces, function(event){
      event.stopPropagation();
    });
  };

  /**
{'fraction_non_alphanumeric': 0.15185551262467428, 'fraction_numerical': 0.0018869619911941775, 'mean_word_length': 2.6360666448872916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4787731', 'n_tokens_mistral': 3402, 'n_tokens_neox': 3317, 'n_words': 707}