<commit_before>highlight_time_on_load_and_creation = 2000
highlight_time_on_in_view = 1500

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

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

class HighlightLoadPromotion
  constructor: (elements) ->
    @highlighter = new Highlighter $(elements), 'fl-load-highlight'
    @highlighter.highlight()
    setTimeout =>
      @highlighter.dehighlight()
    , highlight_time_on_load_and_creation

class HighlightScrollPromotion
  # states: invisible, just_visible, visible

  constructor: (@fact) ->
    @highlighter = new Highlighter $(fact.elements), 'fl-scroll-highlight'
    $(fact.elements).inview(@onSomethingChanged)
    FactlinkJailRoot.on 'fast_scrolling_changed', @onSomethingChanged
    @state = 'visible'

  onSomethingChanged: =>
    switch @state
      when 'invisible'
        if @fact.isInView() && ! FactlinkJailRoot.isFastScrolling
          @switchToState 'just_visible'
          @timeout_handler = setTimeout =>
            @switchToState 'visible'
          , highlight_time_on_in_view
      when 'visible', 'just_visible'
        if !@fact.isInView()
          clearTimeout @timeout_handler
          @switchToState 'invisible'

  switchToState: (to_state) =>
    return if to_state == @state

    if to_state == 'just_visible'
      @highlighter.highlight()
    else
      @highlighter.dehighlight()
    @state = to_state

class FactlinkJailRoot.Highlight
  constructor: (@id, @elements) ->
    @show_button = new FactlinkJailRoot.ShowButton @elements, @id
    @fact_promotion = new HighlightScrollPromotion(this)
    @fact_load_promotion = new HighlightLoadPromotion(@elements)
    @core_highlighter = new Highlighter $(@elements), 'fl-core-highlight'

  isInView: ->
    for element in $(@elements)
      return false unless $(element).data('inview') == 'both'
    true

  destroy: ->
    for el in @elements
      $(el).contents().unwrap()

    @show_button.destroy()

previousCoreHighlightId = null
FactlinkJailRoot.showCoreHighlight = (factId) ->
  for highlight in FactlinkJailRoot.highlightsByFactIds[previousCoreHighlightId] || []
    highlight.core_highlighter.dehighlight()

  for highlight in FactlinkJailRoot.highlightsByFactIds[factId] || []
    highlight.core_highlighter.highlight()

  previousCoreHighlightId = factId<commit_msg>Store currently highlighted fact-id in a global variable so that info can be used elsewhere.<commit_after>highlight_time_on_load_and_creation = 2000
highlight_time_on_in_view = 1500

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

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

class HighlightLoadPromotion
  constructor: (elements) ->
    @highlighter = new Highlighter $(elements), 'fl-load-highlight'
    @highlighter.highlight()
    setTimeout =>
      @highlighter.dehighlight()
    , highlight_time_on_load_and_creation

class HighlightScrollPromotion
  # states: invisible, just_visible, visible

  constructor: (@fact) ->
    @highlighter = new Highlighter $(fact.elements), 'fl-scroll-highlight'
    $(fact.elements).inview(@onSomethingChanged)
    FactlinkJailRoot.on 'fast_scrolling_changed', @onSomethingChanged
    @state = 'visible'

  onSomethingChanged: =>
    switch @state
      when 'invisible'
        if @fact.isInView() && ! FactlinkJailRoot.isFastScrolling
          @switchToState 'just_visible'
          @timeout_handler = setTimeout =>
            @switchToState 'visible'
          , highlight_time_on_in_view
      when 'visible', 'just_visible'
        if !@fact.isInView()
          clearTimeout @timeout_handler
          @switchToState 'invisible'

  switchToState: (to_state) =>
    return if to_state == @state

    if to_state == 'just_visible'
      @highlighter.highlight()
    else
      @highlighter.dehighlight()
    @state = to_state

class FactlinkJailRoot.Highlight
  constructor: (@id, @elements) ->
    @show_button = new FactlinkJailRoot.ShowButton @elements, @id
    @fact_promotion = new HighlightScrollPromotion(this)
    @fact_load_promotion = new HighlightLoadPromotion(@elements)
    @core_highlighter = new Highlighter $(@elements), 'fl-core-highlight'

  isInView: ->
    for element in $(@elements)
      return false unless $(element).data('inview') == 'both'
    true

  destroy: ->
    for el in @elements
      $(el).contents().unwrap()

    @show_button.destroy()

FactlinkJailRoot.highlightedFactId = null
FactlinkJailRoot.showCoreHighlight = (factId) ->
  for highlight in FactlinkJailRoot.highlightsByFactIds[FactlinkJailRoot.highlightedFactId] || []
    highlight.core_highlighter.dehighlight()

  for highlight in FactlinkJailRoot.highlightsByFactIds[factId] || []
    highlight.core_highlighter.highlight()

  FactlinkJailRoot.highlightedFactId = factId
{'fraction_non_alphanumeric': 0.10578081091930952, 'fraction_numerical': 0.003211561621838619, 'mean_word_length': 4.043522267206478, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1550056', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1478, 'n_words': 301}