<commit_before>highlight_time_on_load_and_creation    = 1000
highlight_time_on_in_view = 1500

delay_before_mouseover_detected = 75
delay_between_highlight_and_show_button_open = 325

delay_before_mouseout_detected = 300


class Highlighter
  constructor: (@$elements, @class) ->
    throw 'Higlighter requires class' unless @class

  highlight:   -> @$elements.addClass(@class)
  dehighlight: -> @$elements.removeClass(@class)

class FactInteraction
  constructor: (elements, @id, @options) ->
    @button_attention = new Factlink.AttentionSpan
      onAttentionLost:   => @show_button.hide()
      onAttentionGained: => @show_button.show()
      wait_for_attention:  delay_before_mouseover_detected + delay_between_highlight_and_show_button_open
      wait_for_neglection: delay_before_mouseout_detected

    @highlight_attention = new Factlink.AttentionSpan
      onAttentionLost:   => @highlighter.dehighlight()
      onAttentionGained: => @highlighter.highlight()
      wait_for_attention:  delay_before_mouseover_detected
      wait_for_neglection: delay_before_mouseout_detected

    @highlighter = new Highlighter $(elements), 'fl-active'

    @show_button = new Factlink.ShowButton
      mouseenter: => @onHover()
      mouseleave: => @onUnhover()
      click:      => @onClick()

    $(elements).on
      mouseenter: (e) =>
        @onHover()
        @show_button.setCoordinates($(e.target).offset().top, e.pageX)
      mouseleave: => @onUnhover()
      click: => @onClick()

  onClick: (options={}) =>
    @button_attention.gainAttentionNow()
    @highlight_attention.gainAttentionNow()
    @show_button.startLoading() # must be called after show
    @options.on_click success: =>
      @button_attention.loseAttentionNow()
      @highlight_attention.loseAttentionNow()

  onHover: ->
    @button_attention.gainAttention()
    @highlight_attention.gainAttention()
  onUnhover:  ->
    @button_attention.loseAttention()
    @highlight_attention.loseAttention()

  destroy: ->
    @button_attention.loseAttentionNow()
    @highlight_attention.loseAttentionNow()
    @show_button.destroy()

class FactPromotion
  constructor: (elements) ->
    @highlighter = new Highlighter $(elements), 'fl-highlight'

    @highlight_temporary highlight_time_on_load_and_creation

    $(elements).on 'inview', (event, isInView, visiblePart) =>
      @highlight_temporary(highlight_time_on_in_view) if ( isInView && visiblePart == 'both' )

  highlight_temporary: (duration) ->
    @highlighter.highlight()
    setTimeout (=> @highlighter.dehighlight()), duration

class Factlink.Fact
  constructor: (@id, @elements) ->
    @fact_interaction = new FactInteraction @elements, @id,
      on_click: @openFactlinkModal
    @fact_promotion = new FactPromotion(@elements)


  openFactlinkModal: (options={})=>
    Factlink.showInfo @id, => options.success?()

  destroy: ->
    for el in @elements<commit_msg>defined on view when a fact is visible<commit_after>highlight_time_on_load_and_creation    = 1000
highlight_time_on_in_view = 1500

delay_before_mouseover_detected = 75
delay_between_highlight_and_show_button_open = 325

delay_before_mouseout_detected = 300


class Highlighter
  constructor: (@$elements, @class) ->
    throw 'Higlighter requires class' unless @class

  highlight:   -> @$elements.addClass(@class)
  dehighlight: -> @$elements.removeClass(@class)

class FactInteraction
  constructor: (elements, @id, @options) ->
    @button_attention = new Factlink.AttentionSpan
      onAttentionLost:   => @show_button.hide()
      onAttentionGained: => @show_button.show()
      wait_for_attention:  delay_before_mouseover_detected + delay_between_highlight_and_show_button_open
      wait_for_neglection: delay_before_mouseout_detected

    @highlight_attention = new Factlink.AttentionSpan
      onAttentionLost:   => @highlighter.dehighlight()
      onAttentionGained: => @highlighter.highlight()
      wait_for_attention:  delay_before_mouseover_detected
      wait_for_neglection: delay_before_mouseout_detected

    @highlighter = new Highlighter $(elements), 'fl-active'

    @show_button = new Factlink.ShowButton
      mouseenter: => @onHover()
      mouseleave: => @onUnhover()
      click:      => @onClick()

    $(elements).on
      mouseenter: (e) =>
        @onHover()
        @show_button.setCoordinates($(e.target).offset().top, e.pageX)
      mouseleave: => @onUnhover()
      click: => @onClick()

  onClick: (options={}) =>
    @button_attention.gainAttentionNow()
    @highlight_attention.gainAttentionNow()
    @show_button.startLoading() # must be called after show
    @options.on_click success: =>
      @button_attention.loseAttentionNow()
      @highlight_attention.loseAttentionNow()

  onHover: ->
    @button_attention.gainAttention()
    @highlight_attention.gainAttention()
  onUnhover:  ->
    @button_attention.loseAttention()
    @highlight_attention.loseAttention()

  destroy: ->
    @button_attention.loseAttentionNow()
    @highlight_attention.loseAttentionNow()
    @show_button.destroy()

class FactPromotion
  constructor: (@fact) ->
    @highlighter = new Highlighter $(fact.elements), 'fl-highlight'

    @highlight_temporary highlight_time_on_load_and_creation

    $(fact.elements).on 'inview', @onVisibilityChanged

  onVisibilityChanged: =>
    if @fact.isInView()
      @highlight_temporary(highlight_time_on_in_view)

  highlight_temporary: (duration) ->
    @highlighter.highlight()
    setTimeout (=> @highlighter.dehighlight()), duration

class Factlink.Fact
  constructor: (@id, @elements) ->
    @fact_interaction = new FactInteraction @elements, @id,
      on_click: @openFactlinkModal
    @fact_promotion = new FactPromotion(this)

  isInView: ->
    all = true
    $(@elements).each ->
      all = all & ($(this).data('inview') == 'both')
    all

  openFactlinkModal: (options={})=>
    Factlink.showInfo @id, => options.success?()

  destroy: ->
    for el in @elements
{'fraction_non_alphanumeric': 0.11349072512647555, 'fraction_numerical': 0.005396290050590219, 'mean_word_length': 4.286096256684492, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 59, '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': '4133789', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1837, 'n_words': 320}