<commit_before>$.extend $.expr[":"],
  containsi: (elem, i, match, array) ->
    (elem.textContent or elem.innerText or "").toLowerCase().indexOf((match[3] or "").toLowerCase()) >= 0

class Thorax.Views.ResultsView extends Thorax.View
  template: JST['dashboard/results']
  options:
    fetch: false
  events:
    model:
      change: ->
        if @model.get('sub_id')
          measureid = String(@model.get('measure_id')) + String(@model.get('sub_id'))
        else
          measureid = String(@model.get('measure_id')) 
        loadingDiv = "." + measureid + "-loading-measure"
        if (PopHealth.currentUser.showAggregateResult() and @model.aggregateResult()) or (!PopHealth.currentUser.showAggregateResult() and @model.isPopulated())
          $(loadingDiv).hide()
          clearInterval(@timeout) if @timeout?
          d3.select(@el).select('.pop-chart').datum(_(lower_is_better: @lower_is_better).extend @model.result()).call(@popChart)
        else
          $(loadingDiv).show()
          @authorize()
          if @response == 'false'
            clearInterval(@timeout)
            @view.setView ''
          else
            @timeout ?= setInterval =>
              @model.fetch()
            , 3000
      rescale: ->
        if @model.isPopulated()
          if PopHealth.currentUser.populationChartScaledToIPP() then @popChart.maximumValue(@model.result().IPP) else @popChart.maximumValue(PopHealth.patientCount)
          @popChart.update(_(lower_is_better: @lower_is_better).extend @model.result())
    rendered: ->
      unless PopHealth.currentUser.showAggregateResult() then @$('.aggregate-result').hide()
      @$(".icon-popover").popover()
      @$('.dial').knob()
      if @model.isPopulated()
        if PopHealth.currentUser.populationChartScaledToIPP() then @popChart.maximumValue(@model.result().IPP) else @popChart.maximumValue(PopHealth.patientCount)
        d3.select(@el).select('.pop-chart').datum(_(lower_is_better: @lower_is_better).extend @model.result()).call(@popChart)
        @$('rect').popover()
    destroyed: ->
      clearInterval(@timeout) if @timeout?

  authorize: ->
    @response = $.ajax({ 
      async: false,
      url: "home/check_authorization/", 
      data: {"id": @provider_id}
    }).responseText    
    
  shouldDisplayPercentageVisual: -> !@model.isContinuous() and PopHealth.currentUser.shouldDisplayPercentageVisual()
  context: (attrs) ->
    _(super).extend
      unit: if @model.isContinuous() and @model.parent.get('cms_id') isnt 'CMS179v2' then 'min' else '%'
      resultValue: if @model.isContinuous() then @model.observation() else @model.performanceRate()
      fractionTop: if @model.isContinuous() then @model.measurePopulation() else @model.numerator()
      fractionBottom: if @model.isContinuous() then @model.ipp() else @model.performanceDenominator()
      aggregateResult: @model.aggregateResult()
  initialize: ->
    @popChart = PopHealth.viz.populationChart().width(125).height(25).maximumValue(PopHealth.patientCount)
    @model.set('providers', [@provider_id]) if @provider_id?


class Thorax.Views.DashboardSubmeasureView extends Thorax.View
  template: JST['dashboard/submeasure']
  className: 'measure'
  options:
    fetch: false
  events:
    rendered: ->
      @$('.icon-popover').popover()
      # TODO when we upgrade to Thorax 3, use `getQueryForProvider`
      query = @model.get('query')
      unless query.isPopulated()
        @$el.fadeTo 'fast', 0.5
        @listenTo query, 'change:status', =>
          if query.isPopulated()
            @$el.fadeTo 'fast', 1
            @stopListening query, 'change:status'
  context: ->
    matches = @model.get('cms_id').match(/CMS(\d+)v(\d+)/)
    _(super).extend
      cms_number: matches?[1]
      cms_version: matches?[2]


class Thorax.Views.Dashboard extends Thorax.View
  template: JST['dashboard/index']
  events:
    'click .aggregate-btn': 'toggleAggregateShow'
    'click .btn-checkbox.all':           'toggleCategory'
    'click .btn-checkbox.individual':    'toggleMeasure'
    'keyup .category-measure-search': 'search'
    'click .clear-search':            'clearSearch'
    'change .rescale': (event) ->
      @$('.rescale').parent().toggleClass("btn-primary")
      PopHealth.currentUser.setPopulationChartScale(event.target.value=="true")
      this.selectedCategories.each (category) ->
          category.get("measures").each (measure) ->
            measure.get("submeasures").each (submeasure) ->
              submeasure.attributes.query.trigger("rescale")
    rendered: ->
      toggleChevron = (e) -> $(e.target).parent('.panel').find('.panel-chevron').toggleClass 'glyphicon-chevron-right glyphicon-chevron-down'
      @$('.collapse').on 'hidden.bs.collapse', toggleChevron
      @$('.collapse').on 'show.bs.collapse', toggleChevron
  initialize: ->
    @selectedCategories = PopHealth.currentUser.selectedCategories(@collection)
    @populationChartScaledToIPP = PopHealth.currentUser.populationChartScaledToIPP()
    @currentUser = PopHealth.currentUser.get 'username'
    @showAggregateResult = PopHealth.currentUser.showAggregateResult()
    @opml = PopHealth.OPML

  toggleAggregateShow: (e) ->    
    shown = PopHealth.currentUser.showAggregateResult()
    PopHealth.currentUser.setShowAggregateResult(!shown)
    if !shown 
      if confirm "Please wait for the aggregate measure to calculate. The result will appear when the calculation is completed."
        location.reload()
        @$('.aggregate-result').toggle(400)   
        @$('.aggregate-btn').toggleClass('active')<commit_msg>fix opml mode aggregate display bug<commit_after>$.extend $.expr[":"],
  containsi: (elem, i, match, array) ->
    (elem.textContent or elem.innerText or "").toLowerCase().indexOf((match[3] or "").toLowerCase()) >= 0

class Thorax.Views.ResultsView extends Thorax.View
  template: JST['dashboard/results']
  options:
    fetch: false
  events:
    model:
      change: ->
        if @model.get('sub_id')
          measureid = String(@model.get('measure_id')) + String(@model.get('sub_id'))
        else
          measureid = String(@model.get('measure_id')) 
        loadingDiv = "." + measureid + "-loading-measure"
        if (PopHealth.currentUser.showAggregateResult() and @model.aggregateResult()) or (!PopHealth.currentUser.showAggregateResult() and @model.isPopulated())
          $(loadingDiv).hide()
          clearInterval(@timeout) if @timeout?
          d3.select(@el).select('.pop-chart').datum(_(lower_is_better: @lower_is_better).extend @model.result()).call(@popChart)
        else
          $(loadingDiv).show()
          @authorize()
          if @response == 'false'
            clearInterval(@timeout)
            @view.setView ''
          else
            @timeout ?= setInterval =>
              @model.fetch()
            , 3000
      rescale: ->
        if @model.isPopulated()
          if PopHealth.currentUser.populationChartScaledToIPP() then @popChart.maximumValue(@model.result().IPP) else @popChart.maximumValue(PopHealth.patientCount)
          @popChart.update(_(lower_is_better: @lower_is_better).extend @model.result())
    rendered: ->
      unless PopHealth.currentUser.showAggregateResult() then @$('.aggregate-result').hide()
      @$(".icon-popover").popover()
      @$('.dial').knob()
      if @model.isPopulated()
        if PopHealth.currentUser.populationChartScaledToIPP() then @popChart.maximumValue(@model.result().IPP) else @popChart.maximumValue(PopHealth.patientCount)
        d3.select(@el).select('.pop-chart').datum(_(lower_is_better: @lower_is_better).extend @model.result()).call(@popChart)
        @$('rect').popover()
    destroyed: ->
      clearInterval(@timeout) if @timeout?

  authorize: ->
    @response = $.ajax({ 
      async: false,
      url: "home/check_authorization/", 
      data: {"id": @provider_id}
    }).responseText    
    
  shouldDisplayPercentageVisual: -> !@model.isContinuous() and PopHealth.currentUser.shouldDisplayPercentageVisual()
  context: (attrs) ->
    _(super).extend
      unit: if @model.isContinuous() and @model.parent.get('cms_id') isnt 'CMS179v2' then 'min' else '%'
      resultValue: if @model.isContinuous() then @model.observation() else @model.performanceRate()
      fractionTop: if @model.isContinuous() then @model.measurePopulation() else @model.numerator()
      fractionBottom: if @model.isContinuous() then @model.ipp() else @model.performanceDenominator()
      aggregateResult: @model.aggregateResult()
  initialize: ->
    @popChart = PopHealth.viz.populationChart().width(125).height(25).maximumValue(PopHealth.patientCount)
    @model.set('providers', [@provider_id]) if @provider_id?


class Thorax.Views.DashboardSubmeasureView extends Thorax.View
  template: JST['dashboard/submeasure']
  className: 'measure'
  options:
    fetch: false
  events:
    rendered: ->
      @$('.icon-popover').popover()
      # TODO when we upgrade to Thorax 3, use `getQueryForProvider`
      query = @model.get('query')
      unless query.isPopulated()
        @$el.fadeTo 'fast', 0.5
        @listenTo query, 'change:status', =>
          if query.isPopulated()
            @$el.fadeTo 'fast', 1
            @stopListening query, 'change:status'
  context: ->
    matches = @model.get('cms_id').match(/CMS(\d+)v(\d+)/)
    _(super).extend
      cms_number: matches?[1]
      cms_version: matches?[2]


class Thorax.Views.Dashboard extends Thorax.View
  template: JST['dashboard/index']
  events:
    'click .aggregate-btn': 'toggleAggregateShow'
    'click .btn-checkbox.all':           'toggleCategory'
    'click .btn-checkbox.individual':    'toggleMeasure'
    'keyup .category-measure-search': 'search'
    'click .clear-search':            'clearSearch'
    'change .rescale': (event) ->
      @$('.rescale').parent().toggleClass("btn-primary")
      PopHealth.currentUser.setPopulationChartScale(event.target.value=="true")
      this.selectedCategories.each (category) ->
          category.get("measures").each (measure) ->
            measure.get("submeasures").each (submeasure) ->
              submeasure.attributes.query.trigger("rescale")
    rendered: ->
      toggleChevron = (e) -> $(e.target).parent('.panel').find('.panel-chevron').toggleClass 'glyphicon-chevron-right glyphicon-chevron-down'
      @$('.collapse').on 'hidden.bs.collapse', toggleChevron
      @$('.collapse').on 'show.bs.collapse', toggleChevron
  initialize: ->
    @selectedCategories = PopHealth.currentUser.selectedCategories(@collection)
    @populationChartScaledToIPP = PopHealth.currentUser.populationChartScaledToIPP()
    @currentUser = PopHealth.currentUser.get 'username'
    @showAggregateResult = PopHealth.currentUser.showAggregateResult()
    @opml = Config.OPML

  toggleAggregateShow: (e) ->    
    shown = PopHealth.currentUser.showAggregateResult()
    PopHealth.currentUser.setShowAggregateResult(!shown)
    if !shown 
      if confirm "Please wait for the aggregate measure to calculate. The result will appear when the calculation is completed."
        location.reload()
        @$('.aggregate-result').toggle(400)   
        @$('.aggregate-btn').toggleClass('active')
{'fraction_non_alphanumeric': 0.14958299704062417, 'fraction_numerical': 0.004663258900547036, 'mean_word_length': 3.3460639127045986, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 47, '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': '3130933', 'n_tokens_mistral': 3416, 'n_tokens_neox': 3299, 'n_words': 594}