<commit_before>highlight_time_on_load    = 1500
highlight_time_on_in_view = 1500
delay_between_highlight_and_show_button_open = 400


class ShowButtonManager
  constructor: (@dom_events) ->
    @show_button = new Factlink.ShowButton @dom_events

  set_coordinates: (top, left) =>
    @show_button.setCoordinates(top, left)

  openShowButton:  ->  @show_button.show()
  closeShowButton: ->  @show_button.hide()

  startLoading: -> @show_button.startLoading()

  destroy: -> @show_button.destroy()

class Highlighter
  constructor: (@$elements) ->

  highlight:   -> @$elements.addClass('fl-active')
  dehighlight: -> @$elements.removeClass('fl-active')

class AttentionSpan
  constructor: (@options={})->
    @_has_attention = false

  attend: ->
    clearTimeout @losing_attention_timeout
    @gaining_attention_timeout = setTimeout =>
      @gained_attention()
    , 300

  neglect: ->
    clearTimeout @gaining_attention_timeout
    @losing_attention_timeout = setTimeout =>
      @lost_attention()
    , 300

  has_attention: -> @_has_attention

  lost_attention: ->
    @_has_attention = false
    @options.lost_attention?()

  gained_attention: ->
    @_has_attention = true
    @options.gained_attention?()

class Factlink.Fact
  constructor: (@id, @elements) ->
    @attention_span = new AttentionSpan
      lost_attention: => @stopEmphasis()
      gained_attention: => @startEmphasis()

    @highlighter = new Highlighter $(@elements)

    @show_button_manager = new ShowButtonManager
      mouseenter: => @onFocus()
      mouseleave: => @onBlur()
      click:      => @openFactlinkModal()

    @highlight_temporary highlight_time_on_load

    $(@elements)
      .on('mouseenter', (e)=> @onFocus(e))
      .on('mouseleave', => @onBlur())
      .on('click', => @openFactlinkModal())
      .on 'inview', (event, isInView, visiblePart) =>
        @highlight_temporary(highlight_time_on_in_view) if ( isInView && visiblePart == 'both' )

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

  onBlur: -> @attention_span.neglect()
  onFocus: (e) =>
    if e?
      @show_button_manager.set_coordinates($(e.target).offset().top, e.pageX)
    @attention_span.attend()

  startEmphasis: ->
    @highlighter.highlight()
    @show_button_manager.openShowButton()

  stopEmphasis: =>
    return if @shouldHaveEmphasis()

    @highlighter.dehighlight()
    @show_button_manager.closeShowButton()

  shouldHaveEmphasis: =>
    @attention_span.has_attention() || @_loading

  openFactlinkModal: =>
    @startLoading()
    Factlink.showInfo @id, => @stopLoading()

  startLoading: ->
    @_loading = true
    @show_button_manager.startLoading()

  stopLoading: ->
    @_loading = false
    @stopEmphasis()

  destroy: ->
    for el in @elements
      $el = $(el)
      unless $el.is('.fl-first')
        $el.before $el.html()

      $el.remove()

    @show_button_manager.closeShowButton()<commit_msg>removed show button manager<commit_after>highlight_time_on_load    = 1500
highlight_time_on_in_view = 1500
delay_between_highlight_and_show_button_open = 400

class Highlighter
  constructor: (@$elements) ->

  highlight:   -> @$elements.addClass('fl-active')
  dehighlight: -> @$elements.removeClass('fl-active')

class AttentionSpan
  constructor: (@options={})->
    @_has_attention = false

  attend: ->
    clearTimeout @losing_attention_timeout
    @gaining_attention_timeout = setTimeout =>
      @gained_attention()
    , 300

  neglect: ->
    clearTimeout @gaining_attention_timeout
    @losing_attention_timeout = setTimeout =>
      @lost_attention()
    , 300

  has_attention: -> @_has_attention

  lost_attention: ->
    @_has_attention = false
    @options.lost_attention?()

  gained_attention: ->
    @_has_attention = true
    @options.gained_attention?()

class Factlink.Fact
  constructor: (@id, @elements) ->
    @attention_span = new AttentionSpan
      lost_attention: => @stopEmphasis()
      gained_attention: => @startEmphasis()

    @highlighter = new Highlighter $(@elements)

    @show_button = new Factlink.ShowButton
      mouseenter: => @onFocus()
      mouseleave: => @onBlur()
      click:      => @openFactlinkModal()

    @highlight_temporary highlight_time_on_load

    $(@elements)
      .on('mouseenter', (e)=> @onFocus(e))
      .on('mouseleave', => @onBlur())
      .on('click', => @openFactlinkModal())
      .on 'inview', (event, isInView, visiblePart) =>
        @highlight_temporary(highlight_time_on_in_view) if ( isInView && visiblePart == 'both' )

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

  onBlur: -> @attention_span.neglect()
  onFocus: (e) =>
    if e?
      @show_button.setCoordinates($(e.target).offset().top, e.pageX)
    @attention_span.attend()

  startEmphasis: ->
    @highlighter.highlight()
    @show_button.show()

  stopEmphasis: =>
    return if @shouldHaveEmphasis()

    @highlighter.dehighlight()
    @show_button.hide()

  shouldHaveEmphasis: =>
    @attention_span.has_attention() || @_loading

  openFactlinkModal: =>
    @startLoading()
    Factlink.showInfo @id, => @stopLoading()

  startLoading: ->
    @_loading = true
    @show_button.startLoading()

  stopLoading: ->
    @_loading = false
    @stopEmphasis()

  destroy: ->
    for el in @elements
      $el = $(el)
      unless $el.is('.fl-first')
        $el.before $el.html()

      $el.remove()

    @show_button.destroy()
{'fraction_non_alphanumeric': 0.13644655235315578, 'fraction_numerical': 0.006202116016052536, 'mean_word_length': 3.6036943744752308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 75, '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': '3087688', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1741, 'n_words': 307}