<commit_before>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

function MaterialComponents() {
  'use strict';

  this.element_ = document.querySelector('.mdl-js-components');
  this.componentLinks = this.element_.querySelectorAll('.mdl-components__link');

  this.activeLink = null;
  this.activePage = null;

  this.init();
}

/**
 * Store strings for class names defined by this component that are used in
 * JavaScript. This allows us to simply change it in one place should we
 * decide to modify at a later date.
 * @enum {string}
 * @private
 */
MaterialComponents.prototype.CssClasses_ = {
  ACTIVE: 'is-active'
};

/**
 * Initializes the MaterialComponents component.
 */
MaterialComponents.prototype.init = function() {
  'use strict';

  this.activeLink = this.componentLinks[0];
  this.activePage = this.findPage(this.activeLink);

  for (var i = 0; i < this.componentLinks.length; i++) {
    this.componentLinks[i].addEventListener('click',
        this.clickHandler(this.componentLinks[i]));
  }
};

/**
 * Returns a clickHandler for a navigation link.
 * @param  {HTMLElement} link the navigation link
 * @return {function} the click handler
 */
MaterialComponents.prototype.clickHandler = function(link) {
  'use strict';

  var ctx = this;

  return function(e) {
    e.preventDefault();
    var page = ctx.findPage(link);
    ctx.activePage.classList.remove(ctx.CssClasses_.ACTIVE);
    ctx.activeLink.classList.remove(ctx.CssClasses_.ACTIVE);

    ctx.activePage = page;
    ctx.activeLink = link;

    link.classList.add(ctx.CssClasses_.ACTIVE);
    page.classList.add(ctx.CssClasses_.ACTIVE);
  };
};

/**
 * Finds the corresponding page for a navigation link.
 * @param  {HTMLElement} link the navigation link
 * @return {HTMLElement} the corresponding page<commit_msg>Add deep linking and back button support in the Components page.<commit_after>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

function MaterialComponents() {
  'use strict';

  this.element_ = document.querySelector('.mdl-js-components');
  this.componentLinks = this.element_.querySelectorAll('.mdl-components__link');

  this.activeLink = null;
  this.activePage = null;

  this.init();
}

/**
 * Stores a Map of the components links using their corresponding hashFragment
 * as key.
 * @type {Object.<string, HTMLElement>}
 * @private
 */
MaterialComponents.prototype.linksMap_ = {};

/**
 * Store strings for class names defined by this component that are used in
 * JavaScript. This allows us to simply change it in one place should we
 * decide to modify at a later date.
 * @enum {string}
 * @private
 */
MaterialComponents.prototype.CssClasses_ = {
  ACTIVE: 'is-active'
};

/**
 * Initializes the MaterialComponents component.
 */
MaterialComponents.prototype.init = function() {
  'use strict';

  this.activeLink = this.componentLinks[0];
  this.activePage = this.findPage(this.activeLink);

  for (var i = 0; i < this.componentLinks.length; i++) {
    this.componentLinks[i].addEventListener('click',
        this.clickHandler(this.componentLinks[i]));
    // Mapping the list of links using their hash fragment.
    this.linksMap_["#" + this.componentLinks[i].href.split("#")[1]]
        = this.componentLinks[i];
  }

  // If a Hash fragment is available on the page then display the section.
  this.displaySectionForFragment(window.location.hash);

  // If the hash fragment changes we display the corresponding section.
  // We won't support older browser as it's not efficient.
  if ("onhashchange" in window) {
    var this_ = this;
    window.onhashchange = function () {
      this_.displaySectionForFragment(window.location.hash);
    }
  }
};

/**
 * Displays the section for the given hash fragment.
 * @param  {String} fragment The hash fragment used in the link to the section
 */
MaterialComponents.prototype.displaySectionForFragment = function(fragment) {
  if (fragment
      && this.linksMap_[fragment]
      && this.linksMap_[fragment].click) {
    this.linksMap_[fragment].click();
  }
};

/**
 * Returns a clickHandler for a navigation link.
 * @param  {HTMLElement} link the navigation link
 * @return {function} the click handler
 */
MaterialComponents.prototype.clickHandler = function(link) {
  'use strict';

  var ctx = this;

  return function(e) {
    e.preventDefault();
    var page = ctx.findPage(link);
    ctx.activePage.classList.remove(ctx.CssClasses_.ACTIVE);
    ctx.activeLink.classList.remove(ctx.CssClasses_.ACTIVE);

    ctx.activePage = page;
    ctx.activeLink = link;

    link.classList.add(ctx.CssClasses_.ACTIVE);
    page.classList.add(ctx.CssClasses_.ACTIVE);

    // Add an history entry and display the hash fragment in the URL.
    if (window.location.hash != "#"+link.href.split("#")[1]) {
      history.pushState(null, "Material Design Lite", link);
    }
  };
};

/**
 * Finds the corresponding page for a navigation link.
 * @param  {HTMLElement} link the navigation link
 * @return {HTMLElement} the corresponding page
{'fraction_non_alphanumeric': 0.11207319065512171, 'fraction_numerical': 0.0035941839568697925, 'mean_word_length': 3.779078844652615, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2820181', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1719, 'n_words': 564}