<commit_before>import {
  arrayJoin,
  logIfUnequal,
  formatFinancialYear,
  ratingColor,
  formatForType
} from '../utils.js';
import ColumnChart from 'municipal-money-charts/src/components/MunicipalCharts/ColumnChart.js';
import ComparisonMenu from './comparison-menu';
import "core-js/stable";
import "regenerator-runtime/runtime";

const indicatorMetricClass = {
  "good": ".indicator-metric--status-green",
  "ave": ".indicator-metric--status-yellow",
  "bad": ".indicator-metric--status-red",
  "": ".indicator-metric--no-status",
};

function comparePeriod( a, b ) {
  if ( a.period < b.period ){
    return -1;
  }
  if ( a.period > b.period ){
    return 1;
  }
  return 0;
}

export class IndicatorSection {
  constructor(selector, key, pageData) {
    this.selector = selector;
    this.key = key;
    this.sectionData = pageData.indicators[key];
    this.medians = pageData.medians[key];
    this.geography = pageData.geography;
    this.amountTypes = pageData["amount_types_v1"];
    this.cubeNames = pageData["cube_names"];
    this.municipalCategoryDescriptions = pageData["municipal_category_descriptions"];
    this.$element = $(selector);
    logIfUnequal(1, this.$element.length);
    this.latestItem = this.sectionData.values[0];
    this.comparisons = [];

    const chartContainerSelector = `${this.selector} .indicator-chart`;
    this.chart = new ColumnChart(chartContainerSelector, [this.chartData()]);
    this.chartContainer = $(chartContainerSelector);
    this.chartContainer.addClass("column-chart");
    this.chartContainerParent = $(chartContainerSelector).parent();

    this._initAverageButtons();
    this._initComparisonButtons();
    this._initCategoryInfo();
    this._initCalculation();
    this._initSectionPeriod();
    this._initMetric();

    this.comparisonMenu = new ComparisonMenu(selector, this.key);
    this.comparisonMenu.$element.on("option-select", ((e) => {
      this.updateChartComparison(e.detail.option);
    }).bind(this));
  }

  formatMetric(value) {
    return formatForType(this.sectionData.result_type, value);
  }

  _initCategoryInfo() {
    const miifCategory = this.geography["miif_category"];
    const categoryName = this.geography["category_name"];
    const $category = this.$element.find(".muni-type__wrapper");
    const $label = $category.find(".label");
    const $description = $category.find(".tooltip__description");
    const $link = $category.find(".tooltip__link");
    $label.text(`${miifCategory} ${categoryName}`);
    $description.text(this.municipalCategoryDescriptions[miifCategory]);
    $link.attr("href", "/help#similar-munis");
  }

  _initCalculation() {
    // Render the calculation reference
    const referenceData = this.sectionData.ref;
    const $referenceEl = this.$element.find(".indicator-calculation__reference");
    if ($referenceEl.length && referenceData) {
      const $el = $('<a></a>');
      $el.attr('href', referenceData.url);
      $el.text(referenceData.title);
      $referenceEl.append($el);
    }
    // Render the formula data
    const formulaDataV2 = this.sectionData.formula_v2;
    const formulaData = this.sectionData.formula;

    if (this.sectionData.values[this.sectionData.values.length - 1].cube_version == "v2"){
      $(".is--pre-2019-20").hide()
      $(".indicator-calculation__heading").hide()
    }

    if (formulaDataV2) {
      const textData = formulaDataV2.text;
      const $textEl = this.$element.find(".indicator-calculation__formula-text");
      if ($textEl.length && textData) {
          $textEl.text(textData);
      }
      this.setFormulaLink(formulaDataV2, ".is--post-2019-20")
    }

    if (formulaData) {
      const textData = formulaData.text;
      const $textEl = this.$element.find(".indicator-calculation__formula-text");
      if ($textEl.length && textData) {
          $textEl.text(textData);
      }
      this.setFormulaLink(formulaData, ".is--pre-2019-20")
    }
  }

  _initSectionPeriod() {
    this.$element.find(".section-header__info-right").text(this.formatPeriod(this.latestItem.date));
  }

  _initMetric() {
    const $skeleton = this.$element.find(".indicator-metric");
    logIfUnequal(1, $skeleton.length);
    const templateClass = indicatorMetricClass[this.latestItem.rating];
    const $element = $(`.components ${templateClass}`).clone();
    logIfUnequal(1, $element.length);<commit_msg>Set v1 formula URL to link to 2018-2019 on the Data Explorer<commit_after>import {
  arrayJoin,
  logIfUnequal,
  formatFinancialYear,
  ratingColor,
  formatForType
} from '../utils.js';
import ColumnChart from 'municipal-money-charts/src/components/MunicipalCharts/ColumnChart.js';
import ComparisonMenu from './comparison-menu';
import "core-js/stable";
import "regenerator-runtime/runtime";

const indicatorMetricClass = {
  "good": ".indicator-metric--status-green",
  "ave": ".indicator-metric--status-yellow",
  "bad": ".indicator-metric--status-red",
  "": ".indicator-metric--no-status",
};

function comparePeriod( a, b ) {
  if ( a.period < b.period ){
    return -1;
  }
  if ( a.period > b.period ){
    return 1;
  }
  return 0;
}

export class IndicatorSection {
  constructor(selector, key, pageData) {
    this.selector = selector;
    this.key = key;
    this.sectionData = pageData.indicators[key];
    this.medians = pageData.medians[key];
    this.geography = pageData.geography;
    this.amountTypes = pageData["amount_types_v1"];
    this.cubeNames = pageData["cube_names"];
    this.municipalCategoryDescriptions = pageData["municipal_category_descriptions"];
    this.$element = $(selector);
    logIfUnequal(1, this.$element.length);
    this.latestItem = this.sectionData.values[0];
    this.comparisons = [];

    const chartContainerSelector = `${this.selector} .indicator-chart`;
    this.chart = new ColumnChart(chartContainerSelector, [this.chartData()]);
    this.chartContainer = $(chartContainerSelector);
    this.chartContainer.addClass("column-chart");
    this.chartContainerParent = $(chartContainerSelector).parent();

    this._initAverageButtons();
    this._initComparisonButtons();
    this._initCategoryInfo();
    this._initCalculation();
    this._initSectionPeriod();
    this._initMetric();

    this.comparisonMenu = new ComparisonMenu(selector, this.key);
    this.comparisonMenu.$element.on("option-select", ((e) => {
      this.updateChartComparison(e.detail.option);
    }).bind(this));
  }

  formatMetric(value) {
    return formatForType(this.sectionData.result_type, value);
  }

  _initCategoryInfo() {
    const miifCategory = this.geography["miif_category"];
    const categoryName = this.geography["category_name"];
    const $category = this.$element.find(".muni-type__wrapper");
    const $label = $category.find(".label");
    const $description = $category.find(".tooltip__description");
    const $link = $category.find(".tooltip__link");
    $label.text(`${miifCategory} ${categoryName}`);
    $description.text(this.municipalCategoryDescriptions[miifCategory]);
    $link.attr("href", "/help#similar-munis");
  }

  _initCalculation() {
    // Render the calculation reference
    const referenceData = this.sectionData.ref;
    const $referenceEl = this.$element.find(".indicator-calculation__reference");
    if ($referenceEl.length && referenceData) {
      const $el = $('<a></a>');
      $el.attr('href', referenceData.url);
      $el.text(referenceData.title);
      $referenceEl.append($el);
    }
    // Render the formula data
    const formulaDataV2 = this.sectionData.formula_v2;
    const formulaData = this.sectionData.formula;

    if (this.sectionData.values[this.sectionData.values.length - 1].cube_version == "v2"){
      $(".is--pre-2019-20").hide()
      $(".indicator-calculation__heading").hide()
    }

    if (formulaDataV2) {
      const textData = formulaDataV2.text;
      const $textEl = this.$element.find(".indicator-calculation__formula-text");
      if ($textEl.length && textData) {
          $textEl.text(textData);
      }
      this.setFormulaLink(formulaDataV2, ".is--post-2019-20")
    }

    if (formulaData) {
      const textData = formulaData.text;
      const $textEl = this.$element.find(".indicator-calculation__formula-text");
      if ($textEl.length && textData) {
          $textEl.text(textData);
      }
      this.sectionData.last_year = "2019"
      this.setFormulaLink(formulaData, ".is--pre-2019-20")
    }
  }

  _initSectionPeriod() {
    this.$element.find(".section-header__info-right").text(this.formatPeriod(this.latestItem.date));
  }

  _initMetric() {
    const $skeleton = this.$element.find(".indicator-metric");
    logIfUnequal(1, $skeleton.length);
    const templateClass = indicatorMetricClass[this.latestItem.rating];
    const $element = $(`.components ${templateClass}`).clone();
    logIfUnequal(1, $element.length);
{'fraction_non_alphanumeric': 0.15219114483071, 'fraction_numerical': 0.00894575925716227, 'mean_word_length': 3.8209606986899565, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1988895', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2710, 'n_words': 434}