<commit_before>{ buildAnnotatedIncidentSnippet } = require('/imports/ui/annotation')
import { createIncidentReportsFromEnhancements } from '/imports/utils.coffee'

POPUP_DELAY = 200
POPUP_PADDING = 5
POPUP_PADDING_TOP = 20
POPUP_WINDOW_PADDING = 50

Template.newIncidentFromSelection.onCreated ->
  @selection = window.getSelection()
  @data.selecting.set(true)
  range = @selection.getRangeAt(0)
  {top, bottom, left, width} = range.getBoundingClientRect()
  selectionHeight = bottom - top
  topPosition = "#{Math.floor(top + selectionHeight + POPUP_PADDING)}px"
  bottomPosition = 'auto'
  # Handle case when selection is near bottom of window
  if (bottom + POPUP_WINDOW_PADDING) > window.innerHeight
    topPosition = 'auto'
    bottomPosition = "#{window.innerHeight - top + POPUP_PADDING_TOP}px"
  @popupPosition =
    top: topPosition
    bottom: bottomPosition
    left:  "#{Math.floor(left + width / 2)}px"

Template.newIncidentFromSelection.onRendered ->
  Meteor.setTimeout =>
    @$('.new-incident-from-selection').addClass('active')
  , @data.popupDelay or POPUP_DELAY

  @autorun =>
    if not @data.selecting.get()
      @$('.new-incident-from-selection').remove()
      @data.scrolled.set(false)

Template.newIncidentFromSelection.helpers
  topPosition: ->
    Template.instance().popupPosition.top

  leftPosition: ->
    Template.instance().popupPosition.left

  bottomPosition: ->
    Template.instance().popupPosition.bottom

  scrolled: ->
    Template.instance().data.scrolled.get()

  annotatedText: ->
    instance = Template.instance()
    selection = instance.selection
    range = selection.getRangeAt(0)
    textOffsets = [range.startOffset, range.endOffset]
    content = selection.anchorNode.textContent
    content.slice(textOffsets[0], textOffsets[1])

Template.newIncidentFromSelection.events
  'mousedown .add-incident-from-selection': (event, instance) ->
    source = instance.data.source
    range = instance.selection.getRangeAt(0)
    incident = createIncidentReportsFromEnhancements(source.enhancements, {
      countAnnotations: [{
        textOffsets: [[range.startOffset, range.endOffset]]
      }]
    })[0]
    incident.autogenerated = false
    snippetHtml = buildAnnotatedIncidentSnippet(source.content, incident)
    Modal.show 'suggestedIncidentModal',
      add: true
      articles: [source]
      incident: incident
      incidentText: Spacebars.SafeString(snippetHtml)
      offCanvasStartPosition: 'top'

    window.getSelection().removeAllRanges()
<commit_msg>Fix content related bug - give content from enhancements obj<commit_after>{ buildAnnotatedIncidentSnippet } = require('/imports/ui/annotation')
import { createIncidentReportsFromEnhancements } from '/imports/utils.coffee'

POPUP_DELAY = 200
POPUP_PADDING = 5
POPUP_PADDING_TOP = 20
POPUP_WINDOW_PADDING = 50

Template.newIncidentFromSelection.onCreated ->
  @selection = window.getSelection()
  @data.selecting.set(true)
  range = @selection.getRangeAt(0)
  {top, bottom, left, width} = range.getBoundingClientRect()
  selectionHeight = bottom - top
  topPosition = "#{Math.floor(top + selectionHeight + POPUP_PADDING)}px"
  bottomPosition = 'auto'
  # Handle case when selection is near bottom of window
  if (bottom + POPUP_WINDOW_PADDING) > window.innerHeight
    topPosition = 'auto'
    bottomPosition = "#{window.innerHeight - top + POPUP_PADDING_TOP}px"
  @popupPosition =
    top: topPosition
    bottom: bottomPosition
    left:  "#{Math.floor(left + width / 2)}px"

Template.newIncidentFromSelection.onRendered ->
  Meteor.setTimeout =>
    @$('.new-incident-from-selection').addClass('active')
  , @data.popupDelay or POPUP_DELAY

  @autorun =>
    if not @data.selecting.get()
      @$('.new-incident-from-selection').remove()
      @data.scrolled.set(false)

Template.newIncidentFromSelection.helpers
  topPosition: ->
    Template.instance().popupPosition.top

  leftPosition: ->
    Template.instance().popupPosition.left

  bottomPosition: ->
    Template.instance().popupPosition.bottom

  scrolled: ->
    Template.instance().data.scrolled.get()

  annotatedText: ->
    instance = Template.instance()
    selection = instance.selection
    range = selection.getRangeAt(0)
    textOffsets = [range.startOffset, range.endOffset]
    content = selection.anchorNode.textContent
    content.slice(textOffsets[0], textOffsets[1])

Template.newIncidentFromSelection.events
  'mousedown .add-incident-from-selection': (event, instance) ->
    source = instance.data.source
    range = instance.selection.getRangeAt(0)
    incident = createIncidentReportsFromEnhancements(source.enhancements, {
      countAnnotations: [{
        textOffsets: [[range.startOffset, range.endOffset]]
      }]
    })[0]
    incident.autogenerated = false
    snippetHtml = buildAnnotatedIncidentSnippet(
      source.enhancements.source.cleanContent.content, incident
    )
    Modal.show 'suggestedIncidentModal',
      add: true
      articles: [source]
      incident: incident
      incidentText: Spacebars.SafeString(snippetHtml)
      offCanvasStartPosition: 'top'

    window.getSelection().removeAllRanges()

{'fraction_non_alphanumeric': 0.11479890667707926, 'fraction_numerical': 0.005857087075361187, 'mean_word_length': 4.164314516129032, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '7660281', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1582, 'n_words': 273}